patternpythonMinor
Simple Snake with pygame
Viewed 0 times
withpygamesimplesnake
Problem
I've been reading Dive Into Python and I needed to actually program something before I forget everything I learned. So, this is my first Python project (apart from trying the basics and playing with console).
I first wrote this using lots of globals but that looked stupid so I rewrote it as a class. It was more-or-less main loop that called
Still, this being my first "real" Python program, I'm sure there's room for improvement.
```
'''
Simple snake game using pygame.
'''
import pygame
import random
# R G B
WHITE = (255, 255, 255)
BLACK = ( 0, 0, 0)
RED = (255, 0, 0)
GREEN = ( 0, 210, 0)
DARK_GREEN = ( 0, 155, 0)
DARK_GRAY = ( 40, 40, 40)
BG_COLOR = BLACK
HEAD_COLOR = GREEN
SNAKE_COLOR = DARK_GREEN
FONT_SIZE = 36
# (dx, dy)
LEFT = (-1, 0)
RIGHT = (1, 0)
UP = (0, -1)
DOWN = (0, 1)
key_mapping = { pygame.K_LEFT : LEFT, pygame.K_a : LEFT,
pygame.K_RIGHT : RIGHT, pygame.K_d : RIGHT,
pygame.K_UP : UP, pygame.K_w : UP,
pygame.K_DOWN : DOWN, pygame.K_s : DOWN }
class Snake():
"""Simple snake game.
title - game title
width - window width
height - window height
cell_size - size of one tile
height and width need to be multiples of cell_size
game_speed - must be >1
"""
def __init__(self, title="Snake", width=640, height=480, cell_size=20, game_speed=8):
self.width = width
self.height = height
self.cell_size = cell_size
if height % cell_size != 0 or width % cell_size != 0:
raise ValueError("height and width need to be multiples of cell_size")
self.game_speed = game_speed
if game_speed = col_count \
or y
I first wrote this using lots of globals but that looked stupid so I rewrote it as a class. It was more-or-less main loop that called
restart_game() which reset variables. But, I had self everywhere (eg. before every x, is this normal?) and that seemed weird so I rewrote it. Now I have run_game() which runs single game and is called from infinite loop.Still, this being my first "real" Python program, I'm sure there's room for improvement.
snake.py```
'''
Simple snake game using pygame.
'''
import pygame
import random
# R G B
WHITE = (255, 255, 255)
BLACK = ( 0, 0, 0)
RED = (255, 0, 0)
GREEN = ( 0, 210, 0)
DARK_GREEN = ( 0, 155, 0)
DARK_GRAY = ( 40, 40, 40)
BG_COLOR = BLACK
HEAD_COLOR = GREEN
SNAKE_COLOR = DARK_GREEN
FONT_SIZE = 36
# (dx, dy)
LEFT = (-1, 0)
RIGHT = (1, 0)
UP = (0, -1)
DOWN = (0, 1)
key_mapping = { pygame.K_LEFT : LEFT, pygame.K_a : LEFT,
pygame.K_RIGHT : RIGHT, pygame.K_d : RIGHT,
pygame.K_UP : UP, pygame.K_w : UP,
pygame.K_DOWN : DOWN, pygame.K_s : DOWN }
class Snake():
"""Simple snake game.
title - game title
width - window width
height - window height
cell_size - size of one tile
height and width need to be multiples of cell_size
game_speed - must be >1
"""
def __init__(self, title="Snake", width=640, height=480, cell_size=20, game_speed=8):
self.width = width
self.height = height
self.cell_size = cell_size
if height % cell_size != 0 or width % cell_size != 0:
raise ValueError("height and width need to be multiples of cell_size")
self.game_speed = game_speed
if game_speed = col_count \
or y
Solution
Three minor remarks:
-
-
The error reporting should all be grouped at the start of init, so I can understand which input are valid at a first glance.
-
-
key_mapping should be uppercase as all the oher constants are for readibility.-
The error reporting should all be grouped at the start of init, so I can understand which input are valid at a first glance.
-
run_game handles both logic and drawing, separating the snake_logic and the snake_drawing would seriously increase readibilityContext
StackExchange Code Review Q#100399, answer score: 2
Revisions (0)
No revisions yet.