2012-05-07 06:56:00 +00:00
|
|
|
/**
|
|
|
|
* \defgroup Snake Snake, a casual game including a demo mode.
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file snake_game.c
|
|
|
|
* @brief Implementation of the snake game.
|
|
|
|
* @author Peter Fuhrmann, Martin Ongsiek, Daniel Otte, Christian Kroll
|
|
|
|
*/
|
|
|
|
|
2011-02-23 10:33:27 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdint.h>
|
2009-01-02 02:18:20 +00:00
|
|
|
#include "../../config.h"
|
|
|
|
#include "../../compat/pgmspace.h"
|
|
|
|
#include "../../pixel.h"
|
|
|
|
#include "../../random/prng.h"
|
|
|
|
#include "../../util.h"
|
|
|
|
#include "../../joystick/joystick.h"
|
2011-02-23 10:33:27 +00:00
|
|
|
#include "../../menu/menu.h"
|
|
|
|
#include "snake_game.h"
|
2009-01-01 17:06:38 +00:00
|
|
|
|
2012-05-07 06:56:00 +00:00
|
|
|
|
2011-02-23 10:33:27 +00:00
|
|
|
#if defined MENU_SUPPORT && defined GAME_SNAKE
|
|
|
|
// snake icon (MSB is leftmost pixel)
|
2011-09-04 01:08:13 +00:00
|
|
|
static const uint8_t icon[8] PROGMEM =
|
2011-02-23 10:33:27 +00:00
|
|
|
{0xff, 0x81, 0xbd, 0xa5, 0xa5, 0xad, 0xa1, 0xbf};
|
2010-01-15 12:52:15 +00:00
|
|
|
|
2011-01-30 22:47:44 +00:00
|
|
|
game_descriptor_t snake_game_descriptor __attribute__((section(".game_descriptors"))) =
|
2011-02-23 10:33:27 +00:00
|
|
|
{
|
|
|
|
&snake_game,
|
|
|
|
icon,
|
|
|
|
};
|
2010-01-14 01:38:15 +00:00
|
|
|
#endif
|
2009-01-01 17:06:38 +00:00
|
|
|
|
2011-02-23 10:33:27 +00:00
|
|
|
|
2012-05-07 06:56:00 +00:00
|
|
|
/**
|
|
|
|
* If defined, joystick controls are NOT as "seen" by the snake but absolute,
|
|
|
|
* that is, if pressing up, snake goes up, etc.
|
|
|
|
*/
|
2011-02-23 10:33:27 +00:00
|
|
|
#define SNAKE_NEWCONTROL
|
|
|
|
|
2012-05-07 06:56:00 +00:00
|
|
|
#if !defined USNAKE_MAX_LENGTH || defined DOXYGEN
|
|
|
|
/** The maximum length of the snake. */
|
2011-10-29 23:44:51 +00:00
|
|
|
#define USNAKE_MAX_LENGTH 64u
|
|
|
|
#endif
|
|
|
|
|
2012-05-07 06:56:00 +00:00
|
|
|
#if !defined SNAKE_MAX_APPLES || defined DOXYGEN
|
|
|
|
/** The maximum number of apples lying on the playing field. */
|
2011-10-29 23:44:51 +00:00
|
|
|
#define SNAKE_MAX_APPLES 10
|
2011-10-29 05:59:46 +00:00
|
|
|
#endif
|
2011-02-23 10:33:27 +00:00
|
|
|
|
2012-05-07 06:56:00 +00:00
|
|
|
#if !defined SNAKE_CYCLE_DELAY || defined DOXYGEN
|
|
|
|
/** Delay (in ms) between every state change. */
|
2011-10-29 05:59:46 +00:00
|
|
|
#define SNAKE_CYCLE_DELAY 100
|
|
|
|
#endif
|
2012-05-07 06:56:00 +00:00
|
|
|
|
|
|
|
#if !defined SNAKE_TERMINATION_DELAY || defined DOXYGEN
|
|
|
|
/** Delay (in ms) between every disappearing pixel of a dying snake. */
|
2011-10-29 05:59:46 +00:00
|
|
|
#define SNAKE_TERMINATION_DELAY 60
|
|
|
|
#endif
|
2011-02-23 10:33:27 +00:00
|
|
|
|
2012-05-07 06:56:00 +00:00
|
|
|
/** The color of the surrounding border. */
|
2011-02-23 10:33:27 +00:00
|
|
|
#define SNAKE_COLOR_BORDER 3
|
2012-05-07 06:56:00 +00:00
|
|
|
/** The color of the snake. */
|
2011-02-23 10:33:27 +00:00
|
|
|
#define SNAKE_COLOR_PROTAGONIST 3
|
2012-05-07 06:56:00 +00:00
|
|
|
/** The color of the apples. */
|
2011-02-23 10:33:27 +00:00
|
|
|
#define SNAKE_COLOR_APPLE 3
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Directions of the snake.
|
|
|
|
*/
|
2012-05-07 06:56:00 +00:00
|
|
|
enum snake_dir_e
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2012-05-07 06:56:00 +00:00
|
|
|
SNAKE_DIR_UP, /**< Snake is heading up. */
|
|
|
|
SNAKE_DIR_RIGHT,/**< Snake is heading right. */
|
|
|
|
SNAKE_DIR_DOWN, /**< Snake is heading down. */
|
|
|
|
SNAKE_DIR_LEFT, /**< Snake is heading left. */
|
|
|
|
SNAKE_DIR_NONE /**< Helper value for a "resting" joystick. */
|
2011-02-25 04:31:34 +00:00
|
|
|
};
|
|
|
|
#ifdef NDEBUG
|
|
|
|
typedef uint8_t snake_dir_t;
|
|
|
|
#else
|
2012-05-07 06:56:00 +00:00
|
|
|
typedef enum snake_dir_e snake_dir_t;
|
2011-02-25 04:31:34 +00:00
|
|
|
#endif
|
2011-01-30 22:47:44 +00:00
|
|
|
|
2011-02-23 10:33:27 +00:00
|
|
|
/**
|
|
|
|
* This structure represents the snake character itself. It keeps track of the
|
|
|
|
* snake's segments, its head and tail and the direction it is heading.
|
|
|
|
*/
|
2012-05-07 06:56:00 +00:00
|
|
|
typedef struct snake_protagonist_s
|
2011-02-23 10:33:27 +00:00
|
|
|
{
|
2011-10-29 23:44:51 +00:00
|
|
|
pixel aSegments[USNAKE_MAX_LENGTH]; /**< All segments of the snake. */
|
|
|
|
uint8_t nHeadIndex; /**< Index of the head segment. */
|
|
|
|
uint8_t nTailIndex; /**< Index of the tail segment. */
|
|
|
|
snake_dir_t dir; /**< Direction of the snake. */
|
2011-02-23 10:33:27 +00:00
|
|
|
} snake_protagonist_t;
|
2011-01-30 22:47:44 +00:00
|
|
|
|
2009-01-01 17:06:38 +00:00
|
|
|
|
2011-02-23 10:33:27 +00:00
|
|
|
/**
|
|
|
|
* This structure keeps track of all apples which are on the playing field.
|
|
|
|
*/
|
2012-05-07 06:56:00 +00:00
|
|
|
typedef struct snake_apples_s
|
2011-02-23 10:33:27 +00:00
|
|
|
{
|
2012-05-07 06:56:00 +00:00
|
|
|
pixel aApples[SNAKE_MAX_APPLES]; /**< Positions of all existing apples. */
|
|
|
|
uint8_t nAppleCount; /**< Count of currently existing apples. */
|
2011-02-23 10:33:27 +00:00
|
|
|
} snake_apples_t;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-05-07 06:56:00 +00:00
|
|
|
* This function returns the next position which is calculated from a given
|
|
|
|
* (current) position and a direction.
|
|
|
|
* @param pxNext The position we're going to leave.
|
|
|
|
* @param dir The direction that we are heading.
|
|
|
|
* @return The next position according the given direction.
|
2011-02-23 10:33:27 +00:00
|
|
|
*/
|
2012-05-07 06:56:00 +00:00
|
|
|
static pixel snake_nextDirection(pixel const pxNext,
|
|
|
|
snake_dir_t const dir)
|
2011-02-23 10:33:27 +00:00
|
|
|
{
|
|
|
|
assert(dir < 4);
|
|
|
|
static int8_t const nDelta[] = {0, -1, 0, 1, 0};
|
|
|
|
return (pixel){pxNext.x + nDelta[dir], pxNext.y + nDelta[dir + 1]};
|
|
|
|
}
|
|
|
|
|
2011-01-30 22:47:44 +00:00
|
|
|
|
2011-02-23 10:33:27 +00:00
|
|
|
/**
|
2012-05-07 06:56:00 +00:00
|
|
|
* This function draws a border around the playing field.
|
2011-02-23 10:33:27 +00:00
|
|
|
*/
|
|
|
|
static void snake_drawBorder(void)
|
|
|
|
{
|
|
|
|
#if NUM_COLS == NUM_ROWS
|
|
|
|
for (uint8_t i = NUM_COLS; i--;)
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2011-02-23 10:33:27 +00:00
|
|
|
setpixel((pixel){i, 0}, SNAKE_COLOR_BORDER);
|
|
|
|
setpixel((pixel){i, NUM_ROWS - 1}, SNAKE_COLOR_BORDER);
|
|
|
|
setpixel((pixel){0, i}, SNAKE_COLOR_BORDER);
|
|
|
|
setpixel((pixel){NUM_COLS -1, i}, SNAKE_COLOR_BORDER);
|
|
|
|
}
|
|
|
|
#else
|
2011-08-17 03:35:36 +00:00
|
|
|
for (uint8_t x = NUM_COLS; x--;)
|
2011-02-23 10:33:27 +00:00
|
|
|
{
|
|
|
|
setpixel((pixel){x, 0}, SNAKE_COLOR_BORDER);
|
|
|
|
setpixel((pixel){x, NUM_ROWS - 1}, SNAKE_COLOR_BORDER);
|
|
|
|
}
|
2011-08-17 03:35:36 +00:00
|
|
|
for (uint8_t y = NUM_ROWS; y--;)
|
2011-02-23 10:33:27 +00:00
|
|
|
{
|
|
|
|
setpixel((pixel){0, y}, SNAKE_COLOR_BORDER);
|
2011-08-17 03:35:36 +00:00
|
|
|
setpixel((pixel){NUM_COLS - 1, y}, SNAKE_COLOR_BORDER);
|
2009-01-01 17:06:38 +00:00
|
|
|
}
|
2011-02-23 10:33:27 +00:00
|
|
|
#endif
|
|
|
|
}
|
2009-01-01 17:06:38 +00:00
|
|
|
|
2011-10-28 20:50:23 +00:00
|
|
|
#ifdef GAME_SNAKE
|
2011-02-23 10:33:27 +00:00
|
|
|
/**
|
2012-05-07 06:56:00 +00:00
|
|
|
* This function translates hardware port information into joystick directions.
|
|
|
|
* @return The current direction of the joystick.
|
|
|
|
* @see snake_dir_e
|
2011-02-23 10:33:27 +00:00
|
|
|
*/
|
|
|
|
static snake_dir_t snake_queryJoystick(void)
|
|
|
|
{
|
|
|
|
snake_dir_t dirJoystick;
|
|
|
|
if (JOYISUP)
|
|
|
|
{
|
|
|
|
dirJoystick = SNAKE_DIR_UP;
|
|
|
|
}
|
|
|
|
else if (JOYISRIGHT)
|
|
|
|
{
|
|
|
|
dirJoystick = SNAKE_DIR_RIGHT;
|
|
|
|
}
|
|
|
|
else if (JOYISDOWN)
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2011-02-23 10:33:27 +00:00
|
|
|
dirJoystick = SNAKE_DIR_DOWN;
|
|
|
|
}
|
|
|
|
else if (JOYISLEFT)
|
|
|
|
{
|
|
|
|
dirJoystick = SNAKE_DIR_LEFT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dirJoystick = SNAKE_DIR_NONE;
|
|
|
|
}
|
2010-01-12 21:32:25 +00:00
|
|
|
|
2011-02-23 10:33:27 +00:00
|
|
|
return dirJoystick;
|
|
|
|
}
|
2011-10-28 20:50:23 +00:00
|
|
|
#endif
|
2011-02-23 10:33:27 +00:00
|
|
|
|
|
|
|
/**
|
2012-05-07 06:56:00 +00:00
|
|
|
* This function initializes the structure which represents the snake itself.
|
|
|
|
* @param pprotSnake The pointer the protagonist structure to be initialized.
|
2011-02-23 10:33:27 +00:00
|
|
|
*/
|
|
|
|
static void snake_initGameProtagonist(snake_protagonist_t *pprotSnake)
|
|
|
|
{
|
2011-10-29 23:44:51 +00:00
|
|
|
pprotSnake->aSegments[0] = (pixel){NUM_COLS / 2, NUM_ROWS / 2};
|
|
|
|
pprotSnake->aSegments[1] = (pixel){NUM_COLS / 2, NUM_ROWS / 2 - 1};
|
2011-02-23 10:33:27 +00:00
|
|
|
pprotSnake->nTailIndex = 0;
|
|
|
|
pprotSnake->nHeadIndex = 1;
|
|
|
|
pprotSnake->dir = SNAKE_DIR_UP;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef GAME_SNAKE
|
|
|
|
/**
|
2012-05-07 06:56:00 +00:00
|
|
|
* Determines the next direction of the snake depending on the joystick's input.
|
|
|
|
* @param pprotSnake A pointer to the structure of the protagonist.
|
|
|
|
* @param pdirLast Last joystick direction to recognize prolonged key presses.
|
2011-02-23 10:33:27 +00:00
|
|
|
*/
|
|
|
|
static void snake_userControl(snake_protagonist_t *pprotSnake,
|
2012-05-07 06:56:00 +00:00
|
|
|
snake_dir_t *pdirLast)
|
2011-02-23 10:33:27 +00:00
|
|
|
{
|
|
|
|
snake_dir_t dirJoystick = snake_queryJoystick();
|
|
|
|
#ifdef SNAKE_NEWCONTROL
|
|
|
|
if (dirJoystick != SNAKE_DIR_NONE)
|
|
|
|
{
|
|
|
|
// valid transitions can only be uneven
|
|
|
|
if ((pprotSnake->dir + dirJoystick) & 0x01)
|
2010-01-12 21:32:25 +00:00
|
|
|
{
|
2011-02-23 10:33:27 +00:00
|
|
|
pprotSnake->dir = dirJoystick;
|
2010-01-12 21:32:25 +00:00
|
|
|
}
|
2011-02-23 10:33:27 +00:00
|
|
|
}
|
2010-01-12 21:32:25 +00:00
|
|
|
#else
|
2011-02-23 10:33:27 +00:00
|
|
|
if ((dirJoystick ^ *pdirLast) && (dirJoystick != SNAKE_DIR_NONE))
|
2012-05-07 06:56:00 +00:00
|
|
|
{
|
|
|
|
// only left or right movements are valid
|
2011-02-23 10:33:27 +00:00
|
|
|
if (dirJoystick & 0x01)
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2011-02-23 10:33:27 +00:00
|
|
|
// rotate through directions (either clockwise or counterclockwise)
|
|
|
|
pprotSnake->dir = (pprotSnake->dir +
|
2011-03-07 01:13:35 +00:00
|
|
|
(dirJoystick == SNAKE_DIR_LEFT ? 3 : 1)) % 4u;
|
2011-01-30 22:47:44 +00:00
|
|
|
}
|
2011-02-23 10:33:27 +00:00
|
|
|
}
|
|
|
|
*pdirLast = dirJoystick;
|
|
|
|
#endif
|
|
|
|
}
|
2010-01-12 21:32:25 +00:00
|
|
|
#endif
|
2011-01-30 22:47:44 +00:00
|
|
|
|
|
|
|
|
2011-02-23 10:33:27 +00:00
|
|
|
#ifdef ANIMATION_SNAKE
|
|
|
|
/**
|
2012-05-07 06:56:00 +00:00
|
|
|
* This function approximates the next direction which may lead to an apple
|
|
|
|
* (with a particular probability).
|
|
|
|
* @param pprotSnake A pointer to the hungry protagonist.
|
|
|
|
* @param pApples A pointer to a bunch of apples.
|
2011-02-23 10:33:27 +00:00
|
|
|
*/
|
|
|
|
static void snake_autoRoute(snake_protagonist_t *pprotSnake,
|
|
|
|
snake_apples_t *pApples)
|
|
|
|
{
|
|
|
|
pixel pxHead = pprotSnake->aSegments[pprotSnake->nHeadIndex];
|
|
|
|
if (random8() < 80)
|
|
|
|
{
|
|
|
|
uint8_t nNextApple = 0;
|
|
|
|
if (pApples->nAppleCount)
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2011-02-23 10:33:27 +00:00
|
|
|
uint8_t nMinDist = UINT8_MAX;
|
|
|
|
for (uint8_t i = 0; i < pApples->nAppleCount; ++i)
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2011-02-23 10:33:27 +00:00
|
|
|
uint8_t nDistX;
|
|
|
|
if (pxHead.x > pApples->aApples[i].x)
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2011-02-23 10:33:27 +00:00
|
|
|
nDistX = pxHead.x - pApples->aApples[i].x;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nDistX = pApples->aApples[i].x - pxHead.x;
|
2009-01-01 17:06:38 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 10:33:27 +00:00
|
|
|
uint8_t nDistY;
|
|
|
|
if (pxHead.y > pApples->aApples[i].y)
|
|
|
|
{
|
|
|
|
nDistY = pxHead.y - pApples->aApples[i].y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nDistY = pApples->aApples[i].y - pxHead.y;
|
|
|
|
}
|
2011-01-30 22:47:44 +00:00
|
|
|
|
2011-02-23 10:33:27 +00:00
|
|
|
if ((nDistX + nDistY) < nMinDist)
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2011-02-23 10:33:27 +00:00
|
|
|
nMinDist = nDistX + nDistY;
|
|
|
|
nNextApple = i;
|
2009-01-01 17:06:38 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-23 10:33:27 +00:00
|
|
|
if (pprotSnake->dir ^ 0x01) // vertical direction?
|
|
|
|
{
|
|
|
|
pprotSnake->dir = pApples->aApples[nNextApple].x > pxHead.x ?
|
|
|
|
SNAKE_DIR_LEFT : SNAKE_DIR_RIGHT;
|
|
|
|
}
|
|
|
|
else
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2011-02-23 10:33:27 +00:00
|
|
|
pprotSnake->dir = pApples->aApples[nNextApple].y > pxHead.y ?
|
|
|
|
SNAKE_DIR_DOWN : SNAKE_DIR_UP;
|
2009-01-01 17:06:38 +00:00
|
|
|
}
|
2011-01-30 22:47:44 +00:00
|
|
|
}
|
2011-02-23 10:33:27 +00:00
|
|
|
}
|
|
|
|
|
2011-11-01 14:56:24 +00:00
|
|
|
for (uint8_t i = 0; i < 4; ++i)
|
2011-02-23 10:33:27 +00:00
|
|
|
{
|
2012-05-07 06:56:00 +00:00
|
|
|
pixel pxTest = snake_nextDirection(pxHead, pprotSnake->dir);
|
2011-02-23 10:33:27 +00:00
|
|
|
if (get_pixel(pxTest))
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2011-11-01 14:56:24 +00:00
|
|
|
for (uint8_t j = 0; j < pApples->nAppleCount; ++j)
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2011-02-23 10:33:27 +00:00
|
|
|
if ((pxTest.x == pApples->aApples[j].x) &&
|
|
|
|
(pxTest.y == pApples->aApples[j].y))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-01-01 17:06:38 +00:00
|
|
|
}
|
2012-09-08 01:35:32 +00:00
|
|
|
pprotSnake->dir = (pprotSnake->dir + 1u) % 4u;
|
2011-02-23 10:33:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-02-23 10:33:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-05-07 06:56:00 +00:00
|
|
|
* Small animation that lets the dying snake disappear piece by piece.
|
|
|
|
* @param pprotSnake A pointer to the dying snake.
|
2011-02-23 10:33:27 +00:00
|
|
|
*/
|
|
|
|
static void snake_eliminateProtagonist(snake_protagonist_t *pprotSnake)
|
|
|
|
{
|
|
|
|
while (pprotSnake->nTailIndex != pprotSnake->nHeadIndex)
|
|
|
|
{
|
|
|
|
clearpixel(pprotSnake->aSegments[pprotSnake->nTailIndex++]);
|
2011-10-29 23:44:51 +00:00
|
|
|
pprotSnake->nTailIndex %= USNAKE_MAX_LENGTH;
|
2011-02-23 10:33:27 +00:00
|
|
|
wait(SNAKE_TERMINATION_DELAY);
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 22:47:44 +00:00
|
|
|
|
2011-02-23 10:33:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the structure that keeps track of all currently existing apples.
|
2012-05-07 06:56:00 +00:00
|
|
|
* @param pApples Pointer to the set of apples in question.
|
2011-02-23 10:33:27 +00:00
|
|
|
*/
|
|
|
|
static void snake_initApples(snake_apples_t *pApples)
|
|
|
|
{
|
|
|
|
pApples->nAppleCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks for an apple at a given position and removes it if there is one.
|
2012-05-07 06:56:00 +00:00
|
|
|
* @param pApples The set of apples which are lying on the playing field.
|
2011-02-23 10:33:27 +00:00
|
|
|
* @param pxHead The position to be tested.
|
|
|
|
* @return 0 if no apples were found, 1 otherwise
|
|
|
|
*/
|
|
|
|
static uint8_t snake_checkForApple(snake_apples_t *pApples, pixel pxHead)
|
|
|
|
{
|
|
|
|
for (uint8_t i = pApples->nAppleCount; i--;)
|
|
|
|
{
|
|
|
|
if ((pxHead.x == pApples->aApples[i].x) &&
|
|
|
|
(pxHead.y == pApples->aApples[i].y))
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2011-02-23 10:33:27 +00:00
|
|
|
for (; i < pApples->nAppleCount; ++i)
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2011-02-23 10:33:27 +00:00
|
|
|
pApples->aApples[i] = pApples->aApples[i + 1];
|
2009-01-01 17:06:38 +00:00
|
|
|
}
|
2011-02-23 10:33:27 +00:00
|
|
|
--pApples->nAppleCount;
|
|
|
|
return 1;
|
2009-01-01 17:06:38 +00:00
|
|
|
}
|
2011-02-23 10:33:27 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates some new apples from time to time.
|
2012-05-07 06:56:00 +00:00
|
|
|
* @param pApples Pointer to a set of apples.
|
2011-02-23 10:33:27 +00:00
|
|
|
*/
|
|
|
|
static void snake_spawnApples(snake_apples_t *pApples)
|
|
|
|
{
|
|
|
|
if ((pApples->nAppleCount < SNAKE_MAX_APPLES) && (random8() < 10))
|
|
|
|
{
|
|
|
|
pixel pxApple = (pixel){(random8() % (NUM_COLS-2)) + 1,
|
|
|
|
(random8() % (NUM_ROWS - 2)) + 1};
|
|
|
|
if (!get_pixel(pxApple))
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2011-02-23 10:33:27 +00:00
|
|
|
pApples->aApples[pApples->nAppleCount++] = pxApple;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-05-07 06:56:00 +00:00
|
|
|
* The main loop (plus initialization) that both drives the game and the
|
|
|
|
* demo mode.
|
|
|
|
* @param bDemoMode 0 indicates game mode, 1 indicates demo mode
|
2011-02-23 10:33:27 +00:00
|
|
|
*/
|
|
|
|
void snake_engine(uint8_t bDemoMode)
|
|
|
|
{
|
|
|
|
// init
|
|
|
|
snake_protagonist_t protSnake;
|
|
|
|
snake_initGameProtagonist(&protSnake);
|
|
|
|
snake_apples_t apples;
|
|
|
|
snake_initApples(&apples);
|
|
|
|
snake_dir_t dirLast = SNAKE_DIR_NONE;
|
|
|
|
|
|
|
|
// init screen
|
|
|
|
clear_screen(0);
|
|
|
|
snake_drawBorder();
|
|
|
|
|
|
|
|
for (uint8_t nAppleColor = 0; 1; nAppleColor ^= SNAKE_COLOR_APPLE)
|
|
|
|
{
|
|
|
|
// determine new direction
|
|
|
|
#if defined ANIMATION_SNAKE && defined GAME_SNAKE
|
|
|
|
if (bDemoMode)
|
|
|
|
{
|
|
|
|
snake_autoRoute(&protSnake, &apples);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snake_userControl(&protSnake, &dirLast);
|
|
|
|
}
|
|
|
|
#elif defined ANIMATION_SNAKE
|
|
|
|
snake_autoRoute(&protSnake, &apples);
|
|
|
|
#else
|
|
|
|
snake_userControl(&protSnake, &dirLast);
|
2010-01-12 21:32:25 +00:00
|
|
|
#endif
|
2011-02-23 10:33:27 +00:00
|
|
|
|
|
|
|
// actually move head
|
|
|
|
pixel pxOldHead = protSnake.aSegments[protSnake.nHeadIndex];
|
2012-09-08 01:35:32 +00:00
|
|
|
protSnake.nHeadIndex = (protSnake.nHeadIndex + 1u) % USNAKE_MAX_LENGTH;
|
2011-02-23 10:33:27 +00:00
|
|
|
protSnake.aSegments[protSnake.nHeadIndex] =
|
2012-05-07 06:56:00 +00:00
|
|
|
snake_nextDirection(pxOldHead, protSnake.dir);
|
2011-02-23 10:33:27 +00:00
|
|
|
|
|
|
|
// look if we have found an apple
|
|
|
|
if (!snake_checkForApple(&apples,
|
|
|
|
protSnake.aSegments[protSnake.nHeadIndex]))
|
|
|
|
{
|
|
|
|
// quit game if we hit something which is not an apple
|
|
|
|
if (get_pixel(protSnake.aSegments[protSnake.nHeadIndex]))
|
2011-01-30 22:47:44 +00:00
|
|
|
{
|
2011-02-23 10:33:27 +00:00
|
|
|
snake_eliminateProtagonist(&protSnake);
|
|
|
|
return;
|
2009-01-01 17:06:38 +00:00
|
|
|
}
|
2011-02-23 10:33:27 +00:00
|
|
|
|
|
|
|
// remove last segment
|
|
|
|
clearpixel(protSnake.aSegments[protSnake.nTailIndex])
|
2012-09-08 01:35:32 +00:00
|
|
|
protSnake.nTailIndex =
|
|
|
|
(protSnake.nTailIndex + 1u) % USNAKE_MAX_LENGTH;
|
2011-02-23 10:33:27 +00:00
|
|
|
|
|
|
|
// new apples
|
|
|
|
snake_spawnApples(&apples);
|
|
|
|
}
|
|
|
|
// draw new head
|
|
|
|
setpixel(protSnake.aSegments[protSnake.nHeadIndex],
|
|
|
|
SNAKE_COLOR_PROTAGONIST);
|
|
|
|
|
|
|
|
// draw apples
|
|
|
|
for (uint8_t i = apples.nAppleCount; i--;)
|
|
|
|
{
|
|
|
|
setpixel(apples.aApples[i], nAppleColor);
|
2009-01-01 17:06:38 +00:00
|
|
|
}
|
2011-02-23 10:33:27 +00:00
|
|
|
|
|
|
|
wait(SNAKE_CYCLE_DELAY);
|
2009-01-01 17:06:38 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-23 10:33:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Snake in game mode.
|
|
|
|
*/
|
|
|
|
void snake_game(void)
|
|
|
|
{
|
|
|
|
snake_engine(0);
|
|
|
|
}
|
2012-05-07 06:56:00 +00:00
|
|
|
|
|
|
|
/*@}*/
|