From 24cd3f5d1d5d37269107f35982b579ecf4638e96 Mon Sep 17 00:00:00 2001 From: Christian Kroll Date: Tue, 27 May 2014 08:27:27 +0200 Subject: [PATCH] snake: better playability --- profiles/FFM-Jochen | 3 +- profiles/FFM-LedBrett | 3 +- profiles/LoL-Shield_Leonardo | 3 +- profiles/LoL-Shield_Mega2560 | 3 +- profiles/LoL-Shield_Uno-Duemilanove-Diavolino | 3 +- profiles/borg-16 | 3 +- profiles/borg-ancient | 3 +- profiles/borg-andre | 3 +- profiles/borg-ls | 3 +- profiles/borgjacke | 3 +- src/animations/config.in | 3 +- src/games/snake/snake_game.c | 100 ++++++++++++------ 12 files changed, 90 insertions(+), 43 deletions(-) diff --git a/profiles/FFM-Jochen b/profiles/FFM-Jochen index 5da3e44..f7a0072 100644 --- a/profiles/FFM-Jochen +++ b/profiles/FFM-Jochen @@ -65,7 +65,8 @@ ANIMATION_SPIRAL=y SPIRAL_DELAY=5 # ANIMATION_JOERN1 is not set ANIMATION_SNAKE=y -SNAKE_CYCLE_DELAY=40 +SNAKE_GAME_DELAY=200 +SNAKE_ANIM_DELAY=40 SNAKE_TERMINATION_DELAY=30 SNAKE_MAX_LENGTH=64 SNAKE_MAX_APPLES=10 diff --git a/profiles/FFM-LedBrett b/profiles/FFM-LedBrett index ce52f87..72fabea 100644 --- a/profiles/FFM-LedBrett +++ b/profiles/FFM-LedBrett @@ -68,7 +68,8 @@ ANIMATION_SPIRAL=y SPIRAL_DELAY=5 # ANIMATION_JOERN1 is not set # ANIMATION_SNAKE is not set -SNAKE_CYCLE_DELAY=100 +SNAKE_GAME_DELAY=200 +SNAKE_ANIM_DELAY=100 SNAKE_TERMINATION_DELAY=60 SNAKE_MAX_LENGTH=64 SNAKE_MAX_APPLES=10 diff --git a/profiles/LoL-Shield_Leonardo b/profiles/LoL-Shield_Leonardo index a9ac512..050d78a 100644 --- a/profiles/LoL-Shield_Leonardo +++ b/profiles/LoL-Shield_Leonardo @@ -63,7 +63,8 @@ ANIMATION_SPIRAL=y SPIRAL_DELAY=5 ANIMATION_JOERN1=y ANIMATION_SNAKE=y -SNAKE_CYCLE_DELAY=100 +SNAKE_GAME_DELAY=200 +SNAKE_ANIM_DELAY=100 SNAKE_TERMINATION_DELAY=60 SNAKE_MAX_LENGTH=64 SNAKE_MAX_APPLES=10 diff --git a/profiles/LoL-Shield_Mega2560 b/profiles/LoL-Shield_Mega2560 index f6e9d40..0bd22c2 100644 --- a/profiles/LoL-Shield_Mega2560 +++ b/profiles/LoL-Shield_Mega2560 @@ -63,7 +63,8 @@ ANIMATION_SPIRAL=y SPIRAL_DELAY=5 ANIMATION_JOERN1=y ANIMATION_SNAKE=y -SNAKE_CYCLE_DELAY=100 +SNAKE_GAME_DELAY=200 +SNAKE_ANIM_DELAY=100 SNAKE_TERMINATION_DELAY=60 SNAKE_MAX_LENGTH=64 SNAKE_MAX_APPLES=10 diff --git a/profiles/LoL-Shield_Uno-Duemilanove-Diavolino b/profiles/LoL-Shield_Uno-Duemilanove-Diavolino index f8836f9..1782dcd 100644 --- a/profiles/LoL-Shield_Uno-Duemilanove-Diavolino +++ b/profiles/LoL-Shield_Uno-Duemilanove-Diavolino @@ -63,7 +63,8 @@ ANIMATION_SPIRAL=y SPIRAL_DELAY=5 ANIMATION_JOERN1=y ANIMATION_SNAKE=y -SNAKE_CYCLE_DELAY=100 +SNAKE_GAME_DELAY=200 +SNAKE_ANIM_DELAY=100 SNAKE_TERMINATION_DELAY=60 SNAKE_MAX_LENGTH=64 SNAKE_MAX_APPLES=10 diff --git a/profiles/borg-16 b/profiles/borg-16 index c8d11dd..c75a157 100644 --- a/profiles/borg-16 +++ b/profiles/borg-16 @@ -85,7 +85,8 @@ ANIMATION_SPIRAL=y SPIRAL_DELAY=5 ANIMATION_JOERN1=y ANIMATION_SNAKE=y -SNAKE_CYCLE_DELAY=100 +SNAKE_GAME_DELAY=200 +SNAKE_ANIM_DELAY=100 SNAKE_TERMINATION_DELAY=60 SNAKE_MAX_LENGTH=64 SNAKE_MAX_APPLES=10 diff --git a/profiles/borg-ancient b/profiles/borg-ancient index e62d820..414e68e 100644 --- a/profiles/borg-ancient +++ b/profiles/borg-ancient @@ -61,7 +61,8 @@ ANIMATION_SPIRAL=y SPIRAL_DELAY=20 ANIMATION_JOERN1=y ANIMATION_SNAKE=y -SNAKE_CYCLE_DELAY=100 +SNAKE_GAME_DELAY=200 +SNAKE_ANIM_DELAY=100 SNAKE_TERMINATION_DELAY=60 SNAKE_MAX_LENGTH=64 SNAKE_MAX_APPLES=10 diff --git a/profiles/borg-andre b/profiles/borg-andre index 9e4cba1..9da057d 100644 --- a/profiles/borg-andre +++ b/profiles/borg-andre @@ -82,7 +82,8 @@ ANIMATION_SPIRAL=y SPIRAL_DELAY=5 ANIMATION_JOERN1=y ANIMATION_SNAKE=y -SNAKE_CYCLE_DELAY=100 +SNAKE_GAME_DELAY=200 +SNAKE_ANIM_DELAY=100 SNAKE_TERMINATION_DELAY=60 SNAKE_MAX_LENGTH=64 SNAKE_MAX_APPLES=10 diff --git a/profiles/borg-ls b/profiles/borg-ls index 11dfe30..ee9390a 100644 --- a/profiles/borg-ls +++ b/profiles/borg-ls @@ -74,7 +74,8 @@ ANIMATION_SPIRAL=y SPIRAL_DELAY=5 ANIMATION_JOERN1=y ANIMATION_SNAKE=y -SNAKE_CYCLE_DELAY=50 +SNAKE_GAME_DELAY=200 +SNAKE_ANIM_DELAY=50 SNAKE_TERMINATION_DELAY=60 SNAKE_MAX_LENGTH=64 SNAKE_MAX_APPLES=10 diff --git a/profiles/borgjacke b/profiles/borgjacke index 3468712..40057cd 100644 --- a/profiles/borgjacke +++ b/profiles/borgjacke @@ -78,7 +78,8 @@ ANIMATION_SPIRAL=y SPIRAL_DELAY=5 ANIMATION_JOERN1=y ANIMATION_SNAKE=y -SNAKE_CYCLE_DELAY=100 +SNAKE_GAME_DELAY=200 +SNAKE_ANIM_DELAY=100 SNAKE_TERMINATION_DELAY=60 SNAKE_MAX_LENGTH=64 SNAKE_MAX_APPLES=10 diff --git a/src/animations/config.in b/src/animations/config.in index 2d21fdd..7f9d88c 100644 --- a/src/animations/config.in +++ b/src/animations/config.in @@ -9,7 +9,8 @@ comment "Animations" bool "Joern1" ANIMATION_JOERN1 dep_bool_menu "Snake" ANIMATION_SNAKE $RANDOM_SUPPORT - int "Snake Round Delay" SNAKE_CYCLE_DELAY 100 + int "Snake Game Round Delay" SNAKE_GAME_DELAY 200 + int "Snake Anim Round Delay" SNAKE_ANIM_DELAY 100 int "Snake Termination Delay" SNAKE_TERMINATION_DELAY 60 uint "Snake Max Length" SNAKE_MAX_LENGTH 64 int "Snake Max Apples" SNAKE_MAX_APPLES 10 diff --git a/src/games/snake/snake_game.c b/src/games/snake/snake_game.c index f89a445..9ba1a8d 100644 --- a/src/games/snake/snake_game.c +++ b/src/games/snake/snake_game.c @@ -51,9 +51,14 @@ game_descriptor_t snake_game_descriptor __attribute__((section(".game_descriptor #define SNAKE_MAX_APPLES 10 #endif -#if !defined SNAKE_CYCLE_DELAY || defined DOXYGEN +#if !defined SNAKE_GAME_DELAY || defined DOXYGEN /** Delay (in ms) between every state change. */ - #define SNAKE_CYCLE_DELAY 100 + #define SNAKE_GAME_DELAY 200 +#endif + +#if !defined SNAKE_ANIM_DELAY || defined DOXYGEN + /** Delay (in ms) between every state change. */ + #define SNAKE_ANIM_DELAY 100 #endif #if !defined SNAKE_TERMINATION_DELAY || defined DOXYGEN @@ -192,11 +197,21 @@ static snake_dir_t snake_queryJoystick(void) */ static void snake_initGameProtagonist(snake_protagonist_t *pprotSnake) { - pprotSnake->aSegments[0] = (pixel){NUM_COLS / 2, NUM_ROWS / 2}; - pprotSnake->aSegments[1] = (pixel){NUM_COLS / 2, NUM_ROWS / 2 - 1}; +#if NUM_ROWS > NUM_COLS + pprotSnake->aSegments[0] = + (pixel){(NUM_COLS - 2) / 2 + 1, (NUM_ROWS - 2) / 2 + 1}; + pprotSnake->aSegments[1] = + (pixel){(NUM_COLS - 2) / 2 + 1, (NUM_ROWS - 2) / 2}; + pprotSnake->dir = SNAKE_DIR_DOWN; +#else + pprotSnake->aSegments[0] = + (pixel){(NUM_COLS - 2) / 2 + 1, (NUM_ROWS - 2) / 2 + 1}; + pprotSnake->aSegments[1] = + (pixel){(NUM_COLS - 2) / 2 + 2, (NUM_ROWS - 2) / 2 + 1}; + pprotSnake->dir = SNAKE_DIR_RIGHT; +#endif pprotSnake->nTailIndex = 0; pprotSnake->nHeadIndex = 1; - pprotSnake->dir = SNAKE_DIR_UP; } #ifdef GAME_SNAKE @@ -403,7 +418,7 @@ void snake_engine(uint8_t bDemoMode) clear_screen(0); snake_drawBorder(); - for (uint8_t nAppleColor = 0; 1; nAppleColor ^= SNAKE_COLOR_APPLE) + for (uint8_t nTick = 0; 1; nTick ^= SNAKE_COLOR_APPLE) { // determine new direction #if defined ANIMATION_SNAKE && defined GAME_SNAKE @@ -415,48 +430,66 @@ void snake_engine(uint8_t bDemoMode) { snake_userControl(&protSnake, &dirLast); } + if (bDemoMode || nTick) { #elif defined ANIMATION_SNAKE snake_autoRoute(&protSnake, &apples); + { #else snake_userControl(&protSnake, &dirLast); + if (nTick) { #endif - // actually move head - pixel pxOldHead = protSnake.aSegments[protSnake.nHeadIndex]; - protSnake.nHeadIndex = (protSnake.nHeadIndex + 1u) % USNAKE_MAX_LENGTH; - protSnake.aSegments[protSnake.nHeadIndex] = - snake_nextDirection(pxOldHead, protSnake.dir); + // actually move head + pixel pxOldHead = protSnake.aSegments[protSnake.nHeadIndex]; + protSnake.nHeadIndex = (protSnake.nHeadIndex + 1u) % USNAKE_MAX_LENGTH; + protSnake.aSegments[protSnake.nHeadIndex] = + snake_nextDirection(pxOldHead, protSnake.dir); - // 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])) + // look if we have found an apple + if (!snake_checkForApple(&apples, + protSnake.aSegments[protSnake.nHeadIndex])) { - snake_eliminateProtagonist(&protSnake); - return; + // quit game if we hit something which is not an apple + if (get_pixel(protSnake.aSegments[protSnake.nHeadIndex])) + { + snake_eliminateProtagonist(&protSnake); + return; + } + + // remove last segment + clearpixel(protSnake.aSegments[protSnake.nTailIndex]) + protSnake.nTailIndex = + (protSnake.nTailIndex + 1u) % USNAKE_MAX_LENGTH; + + // new apples + snake_spawnApples(&apples); } - - // remove last segment - clearpixel(protSnake.aSegments[protSnake.nTailIndex]) - protSnake.nTailIndex = - (protSnake.nTailIndex + 1u) % USNAKE_MAX_LENGTH; - - // new apples - snake_spawnApples(&apples); + // draw new head + setpixel(protSnake.aSegments[protSnake.nHeadIndex], + SNAKE_COLOR_PROTAGONIST); } - // 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); + // nTick also serves as blinking color + setpixel(apples.aApples[i], nTick); } - wait(SNAKE_CYCLE_DELAY); +#if defined ANIMATION_SNAKE && defined GAME_SNAKE + if (bDemoMode) + { + wait(SNAKE_ANIM_DELAY); + } + else + { + wait(SNAKE_GAME_DELAY / 2); + } +#elif defined ANIMATION_SNAKE + wait(SNAKE_ANIM_DELAY); +#else + wait(SNAKE_GAME_DELAY / 2); +#endif } } @@ -466,6 +499,9 @@ void snake_engine(uint8_t bDemoMode) void snake_game(void) { snake_engine(0); +#if defined GAME_SNAKE && defined SCROLLTEXT_SUPPORT + scrolltext("