Python snake game debug
5 posts
Hi, i am having problems with my first python snake game. The game obviously doesn't work but no error comes up, all that comes up is the black game screen, nothing else. Anyone else having similar problems? It would be great if someone could tell me what's wrong. 
- Posts: 3
- Joined: Wed Jan 16, 2013 6:10 pm
Can you post your code ?
Use the code button or it will be hard to debug.
Use the code button or it will be hard to debug.
/uk/lancashire/heapey/dave
#!usr/bin/env python
import pygame, sys, time, random
from pygame.locals import *
pygame.init()
fpsClock = pygame.time.Clock()
playSurface = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Raspberry Snake')
redColour = pygame.Color(255, 0, 0)
blackColour = pygame.Color(0, 0, 0)
whiteColour = pygame.Color(255, 255, 255)
greyColor = pygame.Color(150, 150, 150)
snakeposition = [100, 100]
snakesegments = [[100, 100], [80, 100], [60, 100]]
raspberryposition = [300, 300]
raspberrySpawned = 1
direction = 'right'
changedirection = direction
def gameOver():
gameOverFont = pygame.font.Font('freesansbold.ttf', 72)
gameOverSurf =gameOverFont.render('Game Over', True, greyColour)
gameOverRect = gameOverSurf.get_rect()
gameOverRect.midtop = (320, 10)
playSurface.blit(gameOverSurf, gameOverRect)
pygame.display.flip()
time.sleep(5)
pygame.quit()
sys.exit()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_RIGHT or event.key == ord('d'):
changedirection = 'right'
if event.key == K_LEFT or event.key == ord('a'):
changedirection = 'left'
if event.key == K_UP or event.key == ord('w'):
changedirection = 'up'
if event.key == K_DOWN or event.key == ord('s'):
changedirection = 'down'
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
if changedirection == 'right' and not direction == 'left':
direction = changedirection
if changedirection == 'left' and not direction == 'right':
direction = changedirection
if changedirection == 'up' and not direction == 'down':
direction = changedirection
if changedirection == down and not direction == 'up':
direction = changedirection
if direction == 'right':
snakeposition[0] += 20
if direction == 'left':
snakeposition[0] -+ 20
if direction == 'up':
snakeposition[1] -= 20
if direction == 'down':
snakeposition[1] += 20
snakesegments.insert(0,list(snakeposition))
if snakeposition[0] == raspberryposition[0] and snakeposition[1] == raspberryposition[1]:
raspberrySpawned = 0
else:
snakesegments.pop()
if raspberrySpawned == 0:
x = random.randrange(1,32)
y = random.randrange(1,24)
raspberryposition = [int(x*20),int(y*20)]
raspberrySpawned = 1
playSurface.fill(blackColour)
for position in snakesegments:
pygame.draw.rect(playSurface,whiteColour,Rect(position[0], position[1], 20, 20))
pygame.draw.rect(playSurface,redColour,Rect(raspberryposition[0], raspberryposition[1], 20, 20))
pygame.display.flip()
if snakeposition[0] > 620 or snakeposition[0] < 0:
gameOver()
if snakePosition[1] > 460 or snakeposition[1] < 0:
gameOver()
for snakeBody in snakesegments[1:]:
if snakeposition[0] == snakeBody[0] and snakeposition[1] == snakeBody[1]:
gameover()
fpsClock.tick(20)
I gave the Pi over to the school technicians, they said their best guess is that this doesn't have proper graphics or something related, as the game screen comes up with nothing in it.
import pygame, sys, time, random
from pygame.locals import *
pygame.init()
fpsClock = pygame.time.Clock()
playSurface = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Raspberry Snake')
redColour = pygame.Color(255, 0, 0)
blackColour = pygame.Color(0, 0, 0)
whiteColour = pygame.Color(255, 255, 255)
greyColor = pygame.Color(150, 150, 150)
snakeposition = [100, 100]
snakesegments = [[100, 100], [80, 100], [60, 100]]
raspberryposition = [300, 300]
raspberrySpawned = 1
direction = 'right'
changedirection = direction
def gameOver():
gameOverFont = pygame.font.Font('freesansbold.ttf', 72)
gameOverSurf =gameOverFont.render('Game Over', True, greyColour)
gameOverRect = gameOverSurf.get_rect()
gameOverRect.midtop = (320, 10)
playSurface.blit(gameOverSurf, gameOverRect)
pygame.display.flip()
time.sleep(5)
pygame.quit()
sys.exit()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_RIGHT or event.key == ord('d'):
changedirection = 'right'
if event.key == K_LEFT or event.key == ord('a'):
changedirection = 'left'
if event.key == K_UP or event.key == ord('w'):
changedirection = 'up'
if event.key == K_DOWN or event.key == ord('s'):
changedirection = 'down'
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
if changedirection == 'right' and not direction == 'left':
direction = changedirection
if changedirection == 'left' and not direction == 'right':
direction = changedirection
if changedirection == 'up' and not direction == 'down':
direction = changedirection
if changedirection == down and not direction == 'up':
direction = changedirection
if direction == 'right':
snakeposition[0] += 20
if direction == 'left':
snakeposition[0] -+ 20
if direction == 'up':
snakeposition[1] -= 20
if direction == 'down':
snakeposition[1] += 20
snakesegments.insert(0,list(snakeposition))
if snakeposition[0] == raspberryposition[0] and snakeposition[1] == raspberryposition[1]:
raspberrySpawned = 0
else:
snakesegments.pop()
if raspberrySpawned == 0:
x = random.randrange(1,32)
y = random.randrange(1,24)
raspberryposition = [int(x*20),int(y*20)]
raspberrySpawned = 1
playSurface.fill(blackColour)
for position in snakesegments:
pygame.draw.rect(playSurface,whiteColour,Rect(position[0], position[1], 20, 20))
pygame.draw.rect(playSurface,redColour,Rect(raspberryposition[0], raspberryposition[1], 20, 20))
pygame.display.flip()
if snakeposition[0] > 620 or snakeposition[0] < 0:
gameOver()
if snakePosition[1] > 460 or snakeposition[1] < 0:
gameOver()
for snakeBody in snakesegments[1:]:
if snakeposition[0] == snakeBody[0] and snakeposition[1] == snakeBody[1]:
gameover()
fpsClock.tick(20)
I gave the Pi over to the school technicians, they said their best guess is that this doesn't have proper graphics or something related, as the game screen comes up with nothing in it.
- Posts: 3
- Joined: Wed Jan 16, 2013 6:10 pm
I have a new error which wasn't there before, which comes up when i run the proper snake game, wormy:
"Traceback (most recent call last):
File "/home/pi/python_games/wormy.py", line 8, in <module>
import random, pygame, sys
ImportError: No module named pygame"
This error has also came up when i run the snake game at school. Anyone have a clue what's going on?
"Traceback (most recent call last):
File "/home/pi/python_games/wormy.py", line 8, in <module>
import random, pygame, sys
ImportError: No module named pygame"
This error has also came up when i run the snake game at school. Anyone have a clue what's going on?
- Posts: 3
- Joined: Wed Jan 16, 2013 6:10 pm
Hi, I've had a play with your code and I think this is working correctly now. I've changed the way the game exits, but other than that, I think the problems were just a few typos.
The pygame thing is a different issue. Could it be to do with which version of Python you are running the game in?
I think for pygame to work, you should stick with Python 2 on the RPi (unless you have got pygame working with Python 3).
The pygame thing is a different issue. Could it be to do with which version of Python you are running the game in?
I think for pygame to work, you should stick with Python 2 on the RPi (unless you have got pygame working with Python 3).
- Code: Select all
# snake.py
import pygame, sys, time, random
from pygame.locals import *
pygame.init()
fpsClock = pygame.time.Clock()
playSurface = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Raspberry Snake')
redColour = pygame.Color(255, 0, 0)
blackColour = pygame.Color(0, 0, 0)
whiteColour = pygame.Color(255, 255, 255)
greyColour = pygame.Color(150, 150, 150)
snakeposition = [100, 100]
snakesegments = [[100, 100], [80, 100], [60, 100]]
raspberryposition = [300, 300]
raspberrySpawned = 1
direction = 'right'
changedirection = direction
def gameover():
gameOverFont = pygame.font.Font('freesansbold.ttf', 72)
gameOverSurf = gameOverFont.render('Game Over', True, greyColour)
gameOverRect = gameOverSurf.get_rect()
gameOverRect.midtop = (320, 10)
playSurface.blit(gameOverSurf, gameOverRect)
pygame.display.flip()
time.sleep(3)
running = True
while running:
playSurface.fill(blackColour)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == KEYDOWN:
if event.key == K_RIGHT or event.key == ord('d'):
changedirection = 'right'
if event.key == K_LEFT or event.key == ord('a'):
changedirection = 'left'
if event.key == K_UP or event.key == ord('w'):
changedirection = 'up'
if event.key == K_DOWN or event.key == ord('s'):
changedirection = 'down'
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
if changedirection == 'up' and not direction == 'down':
direction = changedirection
if changedirection == 'down' and not direction == 'up':
direction = changedirection
if changedirection == 'left' and not direction == 'right':
direction = changedirection
if changedirection == 'right' and not direction == 'left':
direction = changedirection
if direction == 'right':
snakeposition[0] += 20
if direction == 'left':
snakeposition[0] -= 20
if direction == 'up':
snakeposition[1] -= 20
if direction == 'down':
snakeposition[1] += 20
snakesegments.insert(0,list(snakeposition))
if snakeposition[0] == raspberryposition[0] and snakeposition[1] == raspberryposition[1]:
raspberrySpawned = 0
else:
snakesegments.pop()
if not raspberrySpawned:
x = random.randrange(1,32)
y = random.randrange(1,24)
raspberryposition = [int(x*20),int(y*20)]
raspberrySpawned = 1
for position in snakesegments:
pygame.draw.rect(playSurface,whiteColour,Rect(position[0], position[1], 20, 20))
pygame.draw.rect(playSurface,redColour,Rect(raspberryposition[0], raspberryposition[1], 20, 20))
if snakeposition[0] > 620 or snakeposition[0] < 0:
gameover()
running = False
if snakeposition[1] > 460 or snakeposition[1] < 0:
gameover()
running = False
for snakeBody in snakesegments[1:]:
if snakeposition[0] == snakeBody[0] and snakeposition[1] == snakeBody[1]:
gameover()
running = False
pygame.display.flip()
fpsClock.tick(20)
pygame.quit()
- Posts: 398
- Joined: Sun Nov 20, 2011 11:37 am