• entries
222
606
• views
592233

# ATmega168 Snake

1415 views

In addition to the Tetris game from the previous post, I've added an implementation of snake to the ATmega168 project.

">
Click to watch a gameplay video on YouTube.

Either game can be selected from a menu that appears when the circuit is powered on. To exit menus I've added a second fire button; this allows you to step back to the main menu to pick a different game if need be. The source code and binary can be downloaded as before.

I've written a number of different Snake implementations in the past. The early versions used a single array to represent every cell that the snake's body lay in (head as the first element, tail as the last element) that I would manually shift every frame and resize when the snake ate some food. This gets slower and slower as the snake gets longer, which isn't very good. When I wrote a version for the TI-83+ calculator in BBC BASIC, I switched to using a ring buffer with a pointer to the head element and another to the tail element that would be shunted along every frame, unless the snake ate some food in which case the tail pointer would stay where it was.

As I have even less memory on the ATmega168, I went for a different tactic again; by using "pretty" graphics for the various parts of the snake in the tilemap, I didn't need to store the snake's path anywhere other than this tilemap. That is, if I wanted to advance the tail one unit, I merely need to look at the current tile graphic being used to represent the tail (which will be pointing up, down, left or right) and follow it along to the tile in front of it. By inspecting this tile, I can see if the snake turned a corner at that point or went straight ahead and so adjust the tail position and graphic accordingly.

void advance_tail(void) {    // Find the current snake tail graphic.    char tail = tvtext_buffer[tail_y * TVTEXT_BUFFER_WIDTH + tail_x];    // Where is the body in relation to the tail?    int8_t body_x = tail_x, body_y = tail_y;    switch (tail) {        case FONT_SNAKE_TAIL_UP:            --body_y;            break;        case FONT_SNAKE_TAIL_DOWN:            ++body_y;            break;        case FONT_SNAKE_TAIL_LEFT:            --body_x;            break;        case FONT_SNAKE_TAIL_RIGHT:            ++body_x;            break;    }    // Ensure the body is on the buffer.    if (body_x < WORLD_LEFT) body_x = WORLD_RIGHT;    if (body_x > WORLD_RIGHT) body_x = WORLD_LEFT;    if (body_y < WORLD_TOP) body_y = WORLD_BOTTOM;    if (body_y > WORLD_BOTTOM) body_y = WORLD_TOP;    // Find the current body graphic.    char body = tvtext_buffer[body_y * TVTEXT_BUFFER_WIDTH + body_x];    // Is it a bend? If so, we'll need to rotate the tail graphic.    switch (body) {        case FONT_SNAKE_BODY_DOWN_RIGHT:            tail = (tail == FONT_SNAKE_TAIL_UP) ? FONT_SNAKE_TAIL_RIGHT : FONT_SNAKE_TAIL_DOWN;            break;        case FONT_SNAKE_BODY_DOWN_LEFT:            tail = (tail == FONT_SNAKE_TAIL_UP) ? FONT_SNAKE_TAIL_LEFT : FONT_SNAKE_TAIL_DOWN;            break;        case FONT_SNAKE_BODY_UP_RIGHT:            tail = (tail == FONT_SNAKE_TAIL_DOWN) ? FONT_SNAKE_TAIL_RIGHT : FONT_SNAKE_TAIL_UP;            break;        case FONT_SNAKE_BODY_UP_LEFT:            tail = (tail == FONT_SNAKE_TAIL_DOWN) ? FONT_SNAKE_TAIL_LEFT : FONT_SNAKE_TAIL_UP;            break;    }    // Erase the old tail.    tvtext_buffer[tail_y * TVTEXT_BUFFER_WIDTH + tail_x] = tvtext_cleared;    // Draw the new tail.    tail_x = body_x;    tail_y = body_y;    tvtext_buffer[tail_y * TVTEXT_BUFFER_WIDTH + tail_x] = tail;}

Similar code is used to advance the head and draw the correct tile behind it.

On an unrelated note, I've released a version of BBC BASIC that should run on the Nspire. The Nspire has an emulator on it to run applications for other calculators, but this emulator doesn't implement undocumented instructions. The TI-83+/TI-84+ BBC BASIC host interface makes use of the sl1 instruction, which shifts a register left one bit and sets the least significant bit to 1. Unfortunately, when this code is run on an Nspire it triggers a crash. Apparently the quick fix I've implemented seems to have done the trick, so unless I hear any further bug reports I'll release the latest version formally soon!

There are no comments to display.

## Create an account

Register a new account