1. Introduction
This is not bad overall, considering that this is your first program written with PyGame. I've made many comments below, but don't take the length of this answer to heart: there are always many things to say about a piece of code of this length.
2. Game design issues
The game could do with some instructions. I had to look at the source code to see that I need to use WASD for movement. Alternatively, you might allow the player to use the arrow keys too (these are natural keys the player might try).
You use FPS (the number of frames per second) to control the speed of the snake. This design decision commits you to processing everything in the game at the same frequency as the snake moves. The concepts frames per second and speed of the snake in moves per second are distinct, so it's good practice to separate them.
At the moment there's nothing in the game other than the snake, so you get away with this. But as soon as you add other game elements that need to animate at different speeds, you'll run up against this difficulty. Better to get this right while things are still simple.
See section 5 for one way to solve this problem.
New pieces of food can be created in positions occupied by the snake!
The food position is not reset when a new game starts. (This can cause the snake to be overlap the food at the start of the game.)
The score doesn't get drawn during the game.
3. Major comments
The docstring for collidesWithSelf
reads like this:
"""
# Because of the way new pieces are added when the snake grows, eating a
# new food block could cause the snake to die if it's in a certain position.
# So instead of checking if any of the spots have two pieces at once, the new
# algorithm only checks if the position of the head piece contains more than one block.
for p in self.pieces:
if len(self.pieces) - len([c for c in self.pieces if c != p]) > 1: return True
return False
"""
This is not appropriate content for a docstring. The purpose of a docstring is to explain the interface of a method to a programmer who is trying to use it. But here you have some notes to yourself about the history of this function and why it is implemented like it is. These notes properly belong in a comment.
The reason you have been having problems in this function is that the growth of the snake is not right. In the grow()
method you grow a new tail segment in the opposite direction to the snake's current movement. But this can cause the snake to self-intersect.
The usual way that "snake" games work is that when the snake eats some food, it does not grow a new tail segment immediately. Instead, it waits until the next time it moves and grows a new tail segment in the position where its old tail used to be. This is easily implemented by incrementing a counter each time the snake eats food:
def grow(self):
self.growth_pending += 1
and then decrementing the counter instead of deleting the tail segment:
if self.growth_pending > 0:
self.growth_pending -= 1
else:
# Remove tail
self.pieces.pop()
This avoids self-intersection, and so this would allow you to implement the collision operation using your original approach. But you might consider this simpler approach:
it = iter(self.pieces)
head = next(it)
return head in it
You represent directions by numbers between 1 and 4. It is hard to remember which direction is which, so it would be easy to make a mistake and treat 1 as "up" in one part of the code but "down" in another. You'd be much less likely to make this mistake if you used the names DIRECTION_UP
and so on. You went to all the trouble to create these names: why not use them?
(But see 3.4 below for a better suggestion.)
The code below looks dodgy because there is no else:
on the end of the series of tests.
head = ()
if self.direction == 1: head = (headX, headY - 1)
elif self.direction == 2: head = (headX, headY + 1)
elif self.direction == 3: head = (headX - 1, headY)
elif self.direction == 4: head = (headX + 1, headY)
A programmer reading this would want to know what happens if self.direction
is not in the range 1 to 4. Of course, you hope that you have designed the program so that this can't happen. So you might make this crystal clear by rewriting this code like this:
if self.direction == DIRECTION_UP: head = (headX, headY - 1)
elif self.direction == DIRECTION_DOWN: head = (headX, headY + 1)
elif self.direction == DIRECTION_LEFT: head = (headX - 1, headY)
elif self.direction == DIRECTION_RIGHT: head = (headX + 1, headY)
else: raise RuntimeError("Bad direction: {}".format(self.direction))
(But see 3.4 below for a better suggestion.)
Instead of representing a direction with a number from 1 to 4 (which it's hard to remember which is which), why not use a pair (δx, δy)? For example, you could write:
DIRECTION_UP = 0, -1
DIRECTION_DOWN = 0, 1
DIRECTION_LEFT = -1, 0
DIRECTION_RIGHT = 1, 0
This would save you a bunch of if ... elif ...
tests. For example you could rewrite the code I gave above like this:
old_head = self.getHead()
new_head = old_head[0] + self.direction[0], old_head[1] + self.direction[1]
Similarly, instead of having a bunch of if .. elif ...
tests for the input:
if e.key == K_w: self.nextDirection = 1
elif e.key == K_s: self.nextDirection = 2
elif e.key == K_a: self.nextDirection = 3
elif e.key == K_d: self.nextDirection = 4
you could have a lookup table that maps key to direction:
# Input mapping
KEY_DIRECTION = {
K_w: DIRECTION_UP, K_UP: DIRECTION_UP,
K_s: DIRECTION_DOWN, K_DOWN: DIRECTION_DOWN,
K_a: DIRECTION_LEFT, K_LEFT: DIRECTION_LEFT,
K_d: DIRECTION_RIGHT, K_RIGHT: DIRECTION_RIGHT,
}
and then the test becomes:
if e.key in KEY_DIRECTION:
self.next_direction = KEY_DIRECTION[e.key]
You check for collision with the edges of the playing area like this:
(hx, hy) = self.snake.getHead()
if hx < 1 or hy < 1 or hx > self.sizeX or hy > self.sizeY:
but PyGame defines a Rect
class with a collidepoint
method, so you could set up the rectangle like this in SnakeGame.__init__
:
self.world = Rect(1, 1, 21, 21)
and then test for collision like this:
if not self.world.collidepoint(self.snake.getHead()):
You number your coordinates starting at 1. This causes you some difficulty. For example, you have to subtract 1 from each of your coordinates before passing them to pygame.draw.rect
. If your coordinates started at 0 you wouldn't have to do this.
A lot of your code deals with positions or vectors represented by a pair (x, y). This means that each time you process a position, you have to break it down into its x and y coordinates, process the coordinates, and then reassemble the results into a new pair. For example:
(headX, headY) = self.getHead()
if self.direction == 1: head = (headX, headY - 1)
You could simplify this code by making a class to represent positions and vectors. Sadly, PyGame does not come with such a class, but you can easily find many implementations on the web, or just write one yourself:
class Vector(tuple):
def __add__(self, other): return Vector(v + w for v, w in zip(self, other))
now you can write:
new_head = self.getHead() + self.direction
and make many other simplifications. (See section 5 for more methods on the Vector
class.)
4. Minor comments
The Python style guide (PEP8) says that method names should "Use the function naming rules: lowercase with words separated by underscores as necessary to improve readability". So you should consider renaming collidesWithSelf
as collides_with_self
and so on. (You're not obliged to follow PEP8 but it makes it easier for other Python programmers to read your code.)
The organization of the code into the files game.py
and classes.py
doesn't seem to be motivated by any principle, and the vague name classes.py
confirms this. For a small game like this, I don't think there's anything to be lost by putting all the code in one file. (And if it grows to the point where you want to split it, the obvious thing to do would be to put the Snake
class in its own module.)
It's not clear to me what you gain from separating your game initialization code into main
and SnakeGame.__init__
. Why not put all the initialization into the latter?
You don't need sys.exit()
at the end of main
: Python quits automatically when it finishes running your program. Adding this line just makes it hard to test your program from the interactive interpreter, because when you quit from the game, it exits the interactive interpreter too.
You have unnecessary parentheses in many places. A line like:
(headX, headY) = self.getHead()
can be written:
headX, headY = self.getHead()
since comma binds more tighly than assignment in Python. "Program as if you know the language"!
DIRECTON_DOWN
is misspelled. (Is this why you don't use it?)
You represent the snake using a queue of positions, which is a good approach. However, you implement your queue using a Python list. The trouble here is that Python lists are efficient at adding and removing elements at the end, but not at the beginning. In particular the operation
self.pieces.insert(0, head)
takes time proportional to the length of self.pieces
. (You can consult the TimeComplexity page on the Python wiki to see the time complexity of operations on built-in Python data structures.) This isn't a big deal here, since the snake never gets very long, but it's worth getting into practice at thinking about the complexity of your algorithms.
For an efficient queue implementation, use collections.deque
.
The line:
return self.pieces[len(self.pieces) - 1]
can be rewritten:
return self.pieces[-1]
Since negative list indexes count backwards from the end of the list.
Instead of doing division and coercing the result to an integer:
int(width / self.sizeX)
use Python's floor division operation:
width // self.sizeX
5. Revised code
This code addresses the comments above and includes some more improvements for you to discover.
from collections import deque
import pygame
from random import randrange
import sys
from pygame.locals import *
class Vector(tuple):
"""A tuple that supports some vector operations.
>>> v, w = Vector((1, 2)), Vector((3, 4))
>>> v + w, w - v, v * 10, 100 * v, -v
((4, 6), (2, 2), (10, 20), (100, 200), (-1, -2))
"""
def __add__(self, other): return Vector(v + w for v, w in zip(self, other))
def __radd__(self, other): return Vector(w + v for v, w in zip(self, other))
def __sub__(self, other): return Vector(v - w for v, w in zip(self, other))
def __rsub__(self, other): return Vector(w - v for v, w in zip(self, other))
def __mul__(self, s): return Vector(v * s for v in self)
def __rmul__(self, s): return Vector(v * s for v in self)
def __neg__(self): return -1 * self
FPS = 60 # Game frames per second
SEGMENT_SCORE = 50 # Score per segment
SNAKE_SPEED_INITIAL = 4.0 # Initial snake speed (squares per second)
SNAKE_SPEED_INCREMENT = 0.25 # Snake speeds up this much each time it grows
SNAKE_START_LENGTH = 4 # Initial snake length in segments
WORLD_SIZE = Vector((20, 20)) # World size, in blocks
BLOCK_SIZE = 24 # Block size, in pixels
BACKGROUND_COLOR = 45, 45, 45
SNAKE_COLOR = 0, 255, 0
FOOD_COLOR = 255, 0, 0
DEATH_COLOR = 255, 0, 0
TEXT_COLOR = 255, 255, 255
DIRECTION_UP = Vector(( 0, -1))
DIRECTION_DOWN = Vector(( 0, 1))
DIRECTION_LEFT = Vector((-1, 0))
DIRECTION_RIGHT = Vector(( 1, 0))
DIRECTION_DR = DIRECTION_DOWN + DIRECTION_RIGHT
# Map from PyGame key event to the corresponding direction.
KEY_DIRECTION = {
K_w: DIRECTION_UP, K_UP: DIRECTION_UP,
K_s: DIRECTION_DOWN, K_DOWN: DIRECTION_DOWN,
K_a: DIRECTION_LEFT, K_LEFT: DIRECTION_LEFT,
K_d: DIRECTION_RIGHT, K_RIGHT: DIRECTION_RIGHT,
}
class Snake(object):
def __init__(self, start, start_length):
self.speed = SNAKE_SPEED_INITIAL # Speed in squares per second.
self.timer = 1.0 / self.speed # Time remaining to next movement.
self.growth_pending = 0 # Number of segments still to grow.
self.direction = DIRECTION_UP # Current movement direction.
self.segments = deque([start - self.direction * i
for i in xrange(start_length)])
def __iter__(self):
return iter(self.segments)
def __len__(self):
return len(self.segments)
def change_direction(self, direction):
"""Update the direction of the snake."""
# Moving in the opposite direction of current movement is not allowed.
if self.direction != -direction:
self.direction = direction
def head(self):
"""Return the position of the snake's head."""
return self.segments[0]
def update(self, dt, direction):
"""Update the snake by dt seconds and possibly set direction."""
self.timer -= dt
if self.timer > 0:
# Nothing to do yet.
return
# Moving in the opposite direction of current movement is not allowed.
if self.direction != -direction:
self.direction = direction
self.timer += 1 / self.speed
# Add a new head.
self.segments.appendleft(self.head() + self.direction)
if self.growth_pending > 0:
self.growth_pending -= 1
else:
# Remove tail.
self.segments.pop()
def grow(self):
"""Grow snake by one segment and speed up."""
self.growth_pending += 1
self.speed += SNAKE_SPEED_INCREMENT
def self_intersecting(self):
"""Is the snake currently self-intersecting?"""
it = iter(self)
head = next(it)
return head in it
class SnakeGame(object):
def __init__(self):
pygame.display.set_caption('PyGame Snake')
self.block_size = BLOCK_SIZE
self.window = pygame.display.set_mode(WORLD_SIZE * self.block_size)
self.screen = pygame.display.get_surface()
self.clock = pygame.time.Clock()
self.font = pygame.font.Font('freesansbold.ttf', 20)
self.world = Rect((0, 0), WORLD_SIZE)
self.reset()
def reset(self):
"""Start a new game."""
self.playing = True
self.next_direction = DIRECTION_UP
self.score = 0
self.snake = Snake(self.world.center, SNAKE_START_LENGTH)
self.food = set()
self.add_food()
def add_food(self):
"""Ensure that there is at least one piece of food.
(And, with small probability, more than one.)
"""
while not (self.food and randrange(4)):
food = Vector(map(randrange, self.world.bottomright))
if food not in self.food and food not in self.snake:
self.food.add(food)
def input(self, e):
"""Process keyboard event e."""
if e.key in KEY_DIRECTION:
self.next_direction = KEY_DIRECTION[e.key]
elif e.key == K_SPACE and not self.playing:
self.reset()
def update(self, dt):
"""Update the game by dt seconds."""
self.snake.update(dt, self.next_direction)
# If snake hits a food block, then consume the food, add new
# food and grow the snake.
head = self.snake.head()
if head in self.food:
self.food.remove(head)
self.add_food()
self.snake.grow()
self.score += len(self.snake) * SEGMENT_SCORE
# If snake collides with self or the screen boundaries, then
# it's game over.
if self.snake.self_intersecting() or not self.world.collidepoint(self.snake.head()):
self.playing = False
def block(self, p):
"""Return the screen rectangle corresponding to the position p."""
return Rect(p * self.block_size, DIRECTION_DR * self.block_size)
def draw_text(self, text, p):
"""Draw text at position p."""
self.screen.blit(self.font.render(text, 1, TEXT_COLOR), p)
def draw(self):
"""Draw game (while playing)."""
self.screen.fill(BACKGROUND_COLOR)
for p in self.snake:
pygame.draw.rect(self.screen, SNAKE_COLOR, self.block(p))
for f in self.food:
pygame.draw.rect(self.screen, FOOD_COLOR, self.block(f))
self.draw_text("Score: {}".format(self.score), (20, 20))
def draw_death(self):
"""Draw game (after game over)."""
self.screen.fill(DEATH_COLOR)
self.draw_text("Game over! Press Space to start a new game", (20, 150))
self.draw_text("Your score is: {}".format(self.score), (140, 180))
def play(self):
"""Play game until the QUIT event is received."""
while True:
dt = self.clock.tick(FPS) / 1000.0 # convert to seconds
for e in pygame.event.get():
if e.type == QUIT:
return
elif e.type == KEYUP:
self.input(e)
if self.playing:
self.update(dt)
self.draw()
else:
self.draw_death()
pygame.display.flip()
def main():
pygame.init()
SnakeGame().play()
pygame.quit()