How do we specify a direction? There are several good choices, but This function takes the direction that we want to snake to move. To get this working, we'll write the moveSnake function. The next step is for the snake to start moving in response to the arrow Arrow Keys (Snake Movement without Error Checking)Ĭomplicated, step in writing this program.That said, you should feelįree to consult the solutions as needed during this tutorial (that's why they are provided, after all). With this description, see if you can start from snake0.py and write snake1.py. The grid and the snake body based on the values in the snakeBoard. LoadSnakeBoard, loads the 2d list representing the board intoĬanvas.data. The firstįunction just prints the instructions to the console window (not ideal,īut good enough for our purposes). Here is the init function we used: def init(canvas):Ĭourse, you have to write these three functions. Values highlighted for clarity): snakeBoard =, ,, ,, ,, , ]Īnd then draws the initial board like this: That the program creates the snakeBoard like this (with positive Write an init function (and several helper functions) so Snake Task #1: Create and Display the Initial Snake Start:.However, it turns out to be helpful while writing the game to start with a Ordinarily, at the start of the game the snake's body is just one cell. Why is the head of the snake important? Because the snake moves That we could have made this the tail, instead, but we chose to do it this In the example, the head of the snake is the 9. Rules regarding the values in the snakeBoard:ġ) 0 means the cell is empty (and so the corresponding board cell isĢ) -1 means the cell holds food (and so the corresponding board cellģ) Positive values mean the cell holds part of the snake (and so theĤ) The head of the snake is the largest value in the entire list. The numbers are only included here to help explain how the (though for debugging purposes, you can turn them on or off by pressing OfĬourse, while we're playing the game, we do not display these numbers In this picture, the snake is blue and the food is green. Values corresponding to snake body parts and the food. While the board we display shows colorsįor the snake's body and for food, the snakeBoard will hold integer In our game, we will use a 2d list of integersĬalled the snakeBoard. (knowing that there are other approaches that are, for example, moreĮfficient). So how are we going toĭo this? We'll choose a way that is fairly easy to understand Let's get to it!!!!Īre many ways to go about writing Snake. Make the snake as large as possible before that happens.ĭescription of the game, or just curious where we are headed with our design, you can run the final version ( snake8.py). The game ends when the snakeĮither moves off the screen or moves into itself. In the game of Snake, the player uses the arrow keys to Using Python and Tkinter (it is assumed you are already familiar withĮach). This self-paced tutorial, you will learn how to write the game of Snake
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |