2009-01-02 02:18:20 +00:00
|
|
|
#include "../../config.h"
|
|
|
|
#include "../../compat/pgmspace.h"
|
|
|
|
#include "../../menu/menu.h"
|
|
|
|
#include "../../pixel.h"
|
|
|
|
#include "../../random/prng.h"
|
|
|
|
#include "../../util.h"
|
|
|
|
#include "../../joystick/joystick.h"
|
2009-01-01 17:06:38 +00:00
|
|
|
|
2010-01-12 21:32:25 +00:00
|
|
|
// if defined, joystick controls are NOT as "seen"
|
|
|
|
// by the snake but absolute, that is, if pressing
|
|
|
|
// up, snake goes up, etc.
|
|
|
|
#define GAME_SNAKE_NEWCONTROL
|
|
|
|
|
2009-01-01 17:06:38 +00:00
|
|
|
void snake_game();
|
|
|
|
|
2010-01-14 01:38:15 +00:00
|
|
|
#ifdef MENU_SUPPORT
|
2010-01-15 12:52:15 +00:00
|
|
|
// MSB is leftmost pixel
|
|
|
|
static uint8_t icon[8] PROGMEM =
|
2011-01-30 22:47:44 +00:00
|
|
|
{0xff, 0x81, 0xbd, 0xa5, 0xa5, 0xad, 0xa1, 0xbf}; // Snake icon
|
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"))) =
|
|
|
|
{&snake_game, icon};
|
2010-01-14 01:38:15 +00:00
|
|
|
#endif
|
2009-01-01 17:06:38 +00:00
|
|
|
|
2011-01-30 22:47:44 +00:00
|
|
|
void snake_game()
|
|
|
|
{
|
|
|
|
pixel pixels[64] = {{4, NUM_ROWS - 2}, {4, NUM_ROWS - 3}};
|
|
|
|
pixel *head = &pixels[1];
|
|
|
|
pixel *tail = &pixels[0];
|
2009-01-01 17:06:38 +00:00
|
|
|
pixel old_head;
|
|
|
|
|
2011-01-30 22:47:44 +00:00
|
|
|
pixel apples[10];
|
2009-01-01 17:06:38 +00:00
|
|
|
unsigned char apple_num = 0;
|
|
|
|
direction dir = up;
|
2011-01-30 22:47:44 +00:00
|
|
|
|
|
|
|
clear_screen(0);
|
|
|
|
|
2009-01-01 17:06:38 +00:00
|
|
|
unsigned char apple_found = 0;
|
|
|
|
unsigned char j;
|
|
|
|
|
2011-01-30 22:47:44 +00:00
|
|
|
unsigned char x, y, dead = 0;
|
|
|
|
uint8_t joy, joy_old = 0xff, joy_cmd = 0xff;
|
|
|
|
|
2009-01-01 17:06:38 +00:00
|
|
|
// zeichne Rahmen
|
2011-01-30 22:47:44 +00:00
|
|
|
for (x = 0; x < NUM_COLS; x++)
|
|
|
|
{
|
|
|
|
for (y = 0; y < NUM_ROWS; y++)
|
|
|
|
{
|
|
|
|
if (((x == 0) || (x == NUM_COLS - 1)) || ((y == 0) || (y
|
|
|
|
== NUM_ROWS - 1)))
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
setpixel((pixel) {x, y}, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-30 22:47:44 +00:00
|
|
|
x = 0;
|
|
|
|
while (1)
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
x++;
|
|
|
|
old_head = *head;
|
2011-01-30 22:47:44 +00:00
|
|
|
if (++head == pixels + 64)
|
|
|
|
head = pixels;
|
2010-01-12 21:32:25 +00:00
|
|
|
|
|
|
|
#ifdef GAME_SNAKE_NEWCONTROL
|
|
|
|
if (joy_cmd != 0xff)
|
|
|
|
{
|
2011-01-30 22:47:44 +00:00
|
|
|
if ((dir == left && joy_cmd != right) || (dir == right && joy_cmd
|
|
|
|
!= left) || (dir == up && joy_cmd != down) || (dir == down
|
|
|
|
&& joy_cmd != up))
|
2010-01-12 21:32:25 +00:00
|
|
|
dir = joy_cmd;
|
|
|
|
}
|
|
|
|
#else
|
2011-01-30 22:47:44 +00:00
|
|
|
if (joy_cmd == right)
|
|
|
|
{
|
|
|
|
dir = direction_r(dir);
|
|
|
|
joy_cmd = 0xff;
|
|
|
|
}
|
|
|
|
else if (joy_cmd == left)
|
|
|
|
{
|
|
|
|
dir = direction_r(dir);
|
|
|
|
dir = direction_r(dir);
|
|
|
|
dir = direction_r(dir);
|
|
|
|
joy_cmd = 0xff;
|
|
|
|
}
|
2010-01-12 21:32:25 +00:00
|
|
|
#endif
|
2011-01-30 22:47:44 +00:00
|
|
|
|
2009-01-01 17:06:38 +00:00
|
|
|
// kopf einen weiter bewegen
|
|
|
|
*head = next_pixel(old_head, dir);
|
2011-01-30 22:47:44 +00:00
|
|
|
|
|
|
|
apple_found = 0;
|
|
|
|
|
2009-01-01 17:06:38 +00:00
|
|
|
// pr?fen ob man auf nen Apfel drauf ist
|
2011-01-30 22:47:44 +00:00
|
|
|
for (j = 0; j < apple_num; j++)
|
|
|
|
{
|
|
|
|
if ((head->x == apples[j].x) && (head->y == apples[j].y))
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
apple_found = 1;
|
2011-01-30 22:47:44 +00:00
|
|
|
for (; j < apple_num - 1; j++)
|
|
|
|
{
|
|
|
|
apples[j] = apples[j + 1];
|
2009-01-01 17:06:38 +00:00
|
|
|
}
|
|
|
|
apple_num--;
|
|
|
|
goto apple_se;
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 22:47:44 +00:00
|
|
|
if (get_pixel(*head))
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
dead = 1;
|
|
|
|
}
|
|
|
|
apple_se:
|
|
|
|
|
2011-01-30 22:47:44 +00:00
|
|
|
if (!dead)
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
setpixel(*head, 3);
|
2011-01-30 22:47:44 +00:00
|
|
|
|
2009-01-01 17:06:38 +00:00
|
|
|
// setze neue ?pfel
|
2011-01-30 22:47:44 +00:00
|
|
|
if ((apple_num < 9) && (random8() < 10))
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
pixel new_apple = (pixel) {(random8() % (NUM_COLS-2))+1,
|
|
|
|
(random8() % (NUM_ROWS-2))+1};
|
2011-01-30 22:47:44 +00:00
|
|
|
if (!get_pixel(new_apple))
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
apples[apple_num++] = new_apple;
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 22:47:44 +00:00
|
|
|
// l?sche Ende
|
|
|
|
if (!apple_found && !dead)
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
clearpixel(*tail);
|
2011-01-30 22:47:44 +00:00
|
|
|
if (++tail == pixels + 64)
|
2009-01-01 17:06:38 +00:00
|
|
|
tail = pixels;
|
|
|
|
}
|
2011-01-30 22:47:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (tail != head)
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
clearpixel(*tail);
|
2011-01-30 22:47:44 +00:00
|
|
|
if ((++tail) > pixels + 64)
|
2009-01-01 17:06:38 +00:00
|
|
|
tail = pixels;
|
2011-01-30 22:47:44 +00:00
|
|
|
wait(60);
|
2009-01-01 17:06:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-01-30 22:47:44 +00:00
|
|
|
|
|
|
|
for (j = 0; j < apple_num; j++)
|
|
|
|
{
|
|
|
|
if (x % 2)
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
setpixel(apples[j], 3);
|
2011-01-30 22:47:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
clearpixel(apples[j]);
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 22:47:44 +00:00
|
|
|
for (j = 0; j < 20; j++)
|
|
|
|
{
|
|
|
|
if (JOYISLEFT)
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
joy = left;
|
2011-01-30 22:47:44 +00:00
|
|
|
}
|
|
|
|
else if (JOYISRIGHT)
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
joy = right;
|
2010-01-12 21:32:25 +00:00
|
|
|
#ifdef GAME_SNAKE_NEWCONTROL
|
2011-01-30 22:47:44 +00:00
|
|
|
}
|
|
|
|
else if (JOYISUP)
|
|
|
|
{
|
2010-01-12 21:32:25 +00:00
|
|
|
joy = up;
|
2011-01-30 22:47:44 +00:00
|
|
|
}
|
|
|
|
else if (JOYISDOWN)
|
|
|
|
{
|
2010-01-12 21:32:25 +00:00
|
|
|
joy = down;
|
|
|
|
#endif
|
2011-01-30 22:47:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
joy = 0xff;
|
|
|
|
}
|
2011-01-30 22:47:44 +00:00
|
|
|
if (joy != joy_old)
|
|
|
|
{
|
2009-01-01 17:06:38 +00:00
|
|
|
joy_cmd = joy;
|
|
|
|
}
|
|
|
|
joy_old = joy;
|
2011-01-30 22:47:44 +00:00
|
|
|
wait(5);
|
2009-01-01 17:06:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|