Fixed errors and documented everything
This commit is contained in:
+30
-15
@@ -22,6 +22,8 @@ void spawnApple();
|
||||
void tick();
|
||||
void draw();
|
||||
|
||||
// start_game
|
||||
// ==========
|
||||
void start_game(int len, int n_apples)
|
||||
{
|
||||
if (startLength == 0)
|
||||
@@ -36,8 +38,6 @@ void start_game(int len, int n_apples)
|
||||
|
||||
nib_init();
|
||||
|
||||
//printf("Started with worm length %i and %i apples!\n", snakeLength, numApples);
|
||||
|
||||
// Initialize the snake array
|
||||
// Set every cell to a time value greater than the snake length
|
||||
for (unsigned int i = 0; i < WIDTH*HEIGHT; ++i) {
|
||||
@@ -69,6 +69,8 @@ void start_game(int len, int n_apples)
|
||||
}
|
||||
}
|
||||
|
||||
// spawnApple
|
||||
// ==========
|
||||
void spawnApple()
|
||||
{
|
||||
unsigned int x = rand() % WIDTH;
|
||||
@@ -77,26 +79,30 @@ void spawnApple()
|
||||
x = rand() % WIDTH;
|
||||
y = rand() % HEIGHT;
|
||||
}
|
||||
//printf("Placing apple at (%i,%i)\n", x, y);
|
||||
snake[x + y*WIDTH] = -1;
|
||||
}
|
||||
|
||||
// tick
|
||||
// ====
|
||||
void tick()
|
||||
{
|
||||
currentTick++;
|
||||
// Increment cell times
|
||||
// --------------------
|
||||
for (unsigned int i = 0; i < WIDTH*HEIGHT; ++i) {
|
||||
int val = snake[i];
|
||||
|
||||
if (val < 0) {
|
||||
// Apple lifetime
|
||||
//snake[i]--;
|
||||
/* snake[i]--; */
|
||||
} else if (val >= 0 && val <= snakeLength) {
|
||||
// Other cells
|
||||
snake[i]++;
|
||||
}
|
||||
}
|
||||
|
||||
// Input
|
||||
// -----
|
||||
int key = nib_poll_kbd();
|
||||
if (key == 'w' && snakeDirY != 1) {
|
||||
snakeDirX = 0;
|
||||
@@ -116,8 +122,12 @@ void tick()
|
||||
}
|
||||
|
||||
// Move snake
|
||||
// ----------
|
||||
snakeX = (snakeX + snakeDirX + WIDTH) % WIDTH;
|
||||
snakeY = (snakeY + snakeDirY + HEIGHT) % HEIGHT;
|
||||
|
||||
// Collision
|
||||
// ---------
|
||||
// On collision with self
|
||||
int val = snake[snakeX + snakeY*WIDTH];
|
||||
if (val < snakeLength && val >= 0) {
|
||||
@@ -131,45 +141,50 @@ void tick()
|
||||
// Increase our length!
|
||||
snakeLength++;
|
||||
}
|
||||
|
||||
// Update world
|
||||
// ------------
|
||||
snake[snakeX + snakeY*WIDTH] = 0;
|
||||
}
|
||||
|
||||
// draw
|
||||
// ====
|
||||
void draw()
|
||||
{
|
||||
char body[] = "NIBBLES";
|
||||
|
||||
for (int y = -1; y <= HEIGHT; ++y) {
|
||||
for (int x = -1; x <= WIDTH; ++x) {
|
||||
|
||||
// Draw a border outside the playing field
|
||||
if (x == -1 || y == -1 || x == WIDTH || y == HEIGHT) {
|
||||
if (x == -1 || y == -1) {
|
||||
unsigned int screenX = x + 1;
|
||||
unsigned int screenY = y + 1;
|
||||
nib_put_scr(screenX, screenY, '#');
|
||||
} else {
|
||||
unsigned int screenX = x + 1;
|
||||
unsigned int screenY = y + 1;
|
||||
nib_put_scr(screenX, screenY, '#');
|
||||
}
|
||||
unsigned int screenX = x + 1;
|
||||
unsigned int screenY = y + 1;
|
||||
nib_put_scr(screenX, screenY, '#');
|
||||
} else {
|
||||
int snakeVal = snake[x + y*WIDTH];
|
||||
unsigned int screenX = x + 1;
|
||||
unsigned int screenY = y + 1;
|
||||
|
||||
|
||||
// If the `DEBUG` flag is set, draw the raw ASCII-value for debugging purposes
|
||||
if (DEBUG) {
|
||||
nib_put_scr(screenX, screenY, '0' + snakeVal);
|
||||
continue;
|
||||
}
|
||||
|
||||
// If the value is greater than `0` and less than `snakeLength`, it's part of the snake
|
||||
if (snakeVal >= 0 && snakeVal < snakeLength) {
|
||||
if (x == snakeX && y == snakeY) {
|
||||
// Draw the head
|
||||
nib_put_scr(screenX, screenY, '@');
|
||||
} else {
|
||||
// Draw the body
|
||||
nib_put_scr(screenX, screenY, body[(snakeVal - 1) % 7]);
|
||||
}
|
||||
} else if (snakeVal < 0) {
|
||||
// If the value is less than `0`, it's an apple
|
||||
nib_put_scr(screenX, screenY, 'o');
|
||||
} else {
|
||||
// Otherwise it's empty.
|
||||
nib_put_scr(screenX, screenY, ' ');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user