|Start:||events-example0.py and snake0.py|
|Goal:||Write an init function (and several helper functions) so
that the program creates the snakeBoard like this (with positive
values highlighted for clarity):
snakeBoard = [ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 4, 5, 6, 0, 0, 0 ], [ 0, 0, 0, 0, 3, 0, 7, 0, 0, 0 ], [ 0, 0, 0, 1, 2, 0, 8, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 9, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ]
And then draws the initial board like this:
Printing these instructions:
course, you have to write these three functions. The first
function just prints the instructions to the console window (not ideal,
but good enough for our purposes). The second function,
loadSnakeBoard, loads the 2d list representing the board into
canvas.data["snakeBoard"]. The third function, redrawAll, draws
the grid and the snake body based on the values in the snakeBoard.
It's a good idea to start from our solution to the previous step (snake1.py, in this case),
even if you solved it yourself! This way, you will start each section consistently with the tutorial.
Continuing from the last step, add snake movement in response to arrow keys. This will be accomplished in several steps:
When you are done: Test your program by moving the snake
with the arrow keys. It moves! Wow!!!
|Goal:||Continuing from the last step, change the moveSnake function so that it deals with the two error cases that can occur
during snake movement.
To do this, you'll also have to move the code that already was in
the moveSnake function into an "else" clause. This should become
clear as you do the task.
|Goal:||Continuing from the last step, we'll add random food
placement as follows:
When you're done, you should see green food randomly placed on the board at the start of the game. Now, we haven't implemented eating food, so if you try to eat it, it simply disappears and that's that. No worries (for now). But if you want to test your code, you can repeatedly press "r" to reset the game, and you should see the food appearing in random locations on the board (and not on the snake). Voila!
|Goal:||Continuing from the last step, we'll add eating food
simply by changing the moveSnake function. and nothing else! In
that function, right after the case where you check if the snake will
run into itself (that is, if the snakeBoard contains a positive
number in the new location), add a case (an "elif" clause) that
checks if the snake is about to eat food (-1 in that location).
Then, if the snake is eating food, do the following:
That's it! To test this, move the snake up to food then slowly make sure that the snake grows properly when it eats the food, and that food appears at some other random location at that time. Sweet!
|Goal:||Continuing from the last step, we'll add animation
(timer-based movement) as follows:
That's it! And when you get this working, suddenly the game springs to life!!!! While we'll make a couple tiny improvements, now you have a really solid Snake game!!!!! Excellent!!!
|Goal:||Continuing from the last step, change the
loadSnakeBoard function in two ways:
When you're done, you program should look like this:
|Goal:||Fix the funny timing problem after key presses, as
Also, as a small aside, I changed the timer to 150 milliseconds at this point, since I think that leads to slightly more fun game play.