From f98dd6b3479d70465d87c6d794990d56ebb0e799 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 15 Mar 2013 16:27:24 +0100 Subject: Add score display --- CMakeLists.txt | 1 + snake.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index d31ff5e..ec6a3ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ set_target_properties(snake.elf PROPERTIES LINK_FLAGS "-Wall -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Os -mmcu=${BOARD}" COMPILE_DEFINITIONS "F_CPU=${CLOCK}" ) +target_link_libraries(snake.elf m) add_custom_command(OUTPUT snake.hex COMMAND ${CMAKE_OBJCOPY} -O ihex -R .eeprom snake.elf snake.hex DEPENDS snake.elf) add_custom_target(snake ALL DEPENDS snake.hex) diff --git a/snake.c b/snake.c index c7f3d86..1b958c3 100644 --- a/snake.c +++ b/snake.c @@ -7,6 +7,83 @@ #include +#define NUMBER_WIDTH 3 +#define NUMBER_HEIGHT 7 + +static const uint8_t NUMBERS[10][NUMBER_HEIGHT][NUMBER_WIDTH] = { + { + {1, 1, 1}, + {1, 0, 1}, + {1, 0, 1}, + {1, 0, 1}, + {1, 1, 1}, + }, + { + {0, 0, 1}, + {0, 0, 1}, + {0, 0, 1}, + {0, 0, 1}, + {0, 0, 1}, + }, + { + {1, 1, 1}, + {0, 0, 1}, + {1, 1, 1}, + {1, 0, 0}, + {1, 1, 1}, + }, + { + {1, 1, 1}, + {0, 0, 1}, + {1, 1, 1}, + {0, 0, 1}, + {1, 1, 1}, + }, + { + {1, 0, 1}, + {1, 0, 1}, + {1, 1, 1}, + {0, 0, 1}, + {0, 0, 1}, + }, + { + {1, 1, 1}, + {1, 0, 0}, + {1, 1, 1}, + {0, 0, 1}, + {1, 1, 1}, + }, + { + {1, 1, 1}, + {1, 0, 0}, + {1, 1, 1}, + {1, 0, 1}, + {1, 1, 1}, + }, + { + {1, 1, 1}, + {0, 0, 1}, + {0, 0, 1}, + {0, 0, 1}, + {0, 0, 1}, + }, + { + {1, 1, 1}, + {1, 0, 1}, + {1, 1, 1}, + {1, 0, 1}, + {1, 1, 1}, + }, + { + {1, 1, 1}, + {1, 0, 1}, + {1, 1, 1}, + {0, 0, 1}, + {1, 1, 1}, + }, +}; + + typedef struct _point_t { uint8_t x, y; } point_t; @@ -25,6 +102,7 @@ static volatile bool start; static volatile uint16_t lfsr = 0xace1; static uint8_t dir; +static uint8_t score; static point_t tail = {7, 4}; #define HISTORY_MAX 128 @@ -94,6 +172,7 @@ static void reset(void) { tail = (point_t){7, 4}; history_len = 0; history_pos = 0; + score = 0; unsigned i; for (i = 0; i < 2; i++) @@ -104,6 +183,39 @@ static void set_led(uint8_t x, uint8_t y, uint8_t val) { LedSignSet(13-x, 8-y, val); } +static void set_number(uint8_t x, uint8_t y, uint8_t val) { + uint8_t i, j; + + for (i = 0; i < NUMBER_WIDTH; i++) { + for (j = 0; j < NUMBER_HEIGHT; j++) { + set_led(x+i, y+j, NUMBERS[val][j][i] ? 7 : 0); + } + } +} + +static void show_score(uint8_t score) { + LedSignClear(7); + _delay_ms(50); + LedSignClear(6); + _delay_ms(50); + LedSignClear(5); + _delay_ms(50); + LedSignClear(4); + _delay_ms(50); + LedSignClear(3); + _delay_ms(50); + LedSignClear(2); + _delay_ms(50); + LedSignClear(1); + _delay_ms(50); + LedSignClear(0); + _delay_ms(50); + + set_number(1, 2, (score/100)%10); + set_number(NUMBER_WIDTH+2, 2, (score/10)%10); + set_number(2*NUMBER_WIDTH+3, 2, score%10); +} + void kbd_handle(uint16_t code, bool make) { if (!make) return; @@ -187,6 +299,7 @@ static bool step(void) { return false; if (points_equal(&pt, &q)) { + score++; rand_q(); move(d, true); } @@ -218,6 +331,9 @@ int main() { while (step()) _delay_ms(100); + + _delay_ms(200); + show_score(score); } return 0; -- cgit v1.2.3