Snake game.
If you have never played the old-school snake game, try it here.
I strongly suggest trying to plan out the basic structure of your program before starting to code.
|
Directions:
- You MUST use classes as the basic components of your program to receive full credit.
- Your code must be commented for full credit!
- You must use the mainloop function to animate the snake, not a naked while loop.
- A snake, made out of some repeated shape, which moves across the screen
- The initial 'snake' will have 3 segments (it may be easiest to start with 1 segment and try to scale up.)
- The snake's direction is controlled by the keyboard arrows
- If the snake hits a wall the game ends.
- If the snake bites its own tail the game ends.
- Place food on the screen in random locations (see picture above).
- If the snake eats the food it grows by one segment.
- The user scores points by eating food.
- Display the score on the screen.
- The longer the snake, the harder the game gets (i.e. the speed increases).
- Once the game ends, ask the user if they would like to play again.
My suggestion:
1) Snake class (for the head and body)
2) Food class
3) animation loop
1) Snake class (for the head and body)
2) Food class
3) animation loop
Some milestones:
- Make a 1 segment snake move around the screen using the arrow keys on the keyboard.
- If the snake hits the edge of the window, stop the game.
- Make the food appear on the screen at a random location.
- When the snake "eats" the food, move the food to a new location.
(this works much better than deleting the food and making a new one--trust me, I've tried) - Make your snake 3 segments long. Each segment should follow the path of the head.
I have seen this successfully done 2 different ways:
i) move the head and have the follwing body segments go to the position of the previous body segment
ii) move the last segment to where the snake is going to go and leave the rest of the body segements where they were - When the snake "eats" the food, add a body segment to the snake.
- When the snake bites itself, stop the game.
- When the snake "eats" the food, increase the player's score.
- Display the player's score.
- When the game ends, ask the player if they would like to play again and restart the game.
- When the snake "eats" the food, increase the speed of the snake.
Here's my thoughts on what the basic outline of the game might look like:
from tkinter import *
import random import time class Snake(object): def __init__(self, ...): # initialize snake: # - create head # - create body # - set starting x, y position # - put score on the screen def eat_food(self, ...): # call a function from the Food class to move the food # stretch body # add score # make the snake move faster def add_body(self, ...) # add a body segment every time the snake eats food # the body segment must be added behind the snake def update(self): # update snake's location to figure out which direction it is headed. # update the snake's location with a new body segment def draw_snake(self): # draw the snake in the window/canvas def controls(self): # this will be several functions, not just one: # move up, down, left, right class Food(object): def __init__(self, ...): # set random x, y position # draw food to canvas/window def if_eaten(self): # if eaten, move the food to a new location #This is our animation loop which pulls everyting together. def mainloop() : # if the snake is alive: # draw the snake # display the score # listen to keypress event # respond to keypress event # if the snake is dead: #stop the animation #display game over text #set up the snake #set up the food #run the mainloop |