Can't run snake pygame

General programming chat and advice for beginners

7 posts
by finlay-rg1 » Fri Dec 21, 2012 10:16 am
I have written the snake pygame code into IDLE exactly as shown in the Users Guide (pages 162 to 171) but when I try to run it I get the following message.

Python 2.7.3rc2 (default, May 6 2012, 20:02:25)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>

Traceback (most recent call last):
File "/home/pi/pygame.py", line 2, in <module>
import pygame, sys, time, random
File "/home/pi/pygame.py", line 3, in <module>
from pygame.locals import *
ImportError: No module named locals
>>>

Does anyone out there know what this means and what I should do to run the game?
Posts: 16
Joined: Tue Nov 27, 2012 9:02 am
by MattCook » Fri Dec 21, 2012 11:44 am
It looks like you have a program called pygame.py, which is trying to import the pygame module. If this is the case, I would try saving the program with a different name - probably snake.py or something similar, so you know what it is.

Let us know how that goes, and if it doesn't work, then can you include the first few lines of your source ?

Matt
Posts: 16
Joined: Tue Feb 21, 2012 1:46 pm
Location: S. Manchester
by finlay-rg1 » Fri Dec 21, 2012 12:04 pm
Thanks Matt Cook.
I saved using snake.py as suggested but it did not solve the problem. I can open pygames in my pi directory so it seems to have downloaded successfully. Here is the code.

#!/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(1)
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 rasperrySpawned == 0:
x = randon.randrange(1,32)
y = randon.randrange(1,24)
rasperryPosition = [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)
Posts: 16
Joined: Tue Nov 27, 2012 9:02 am
by gordon77 » Fri Dec 21, 2012 2:54 pm
Hopefully it now works as expected.

A few typos, eg Rasperry, Randon, color...and identation missing.

It starts with a blank screen and you have to press an arrow key to start

Gordon77

Code: Select all
#!/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)
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(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)
Posts: 688
Joined: Sun Aug 05, 2012 3:12 pm
by Pnb116 » Sun Dec 30, 2012 11:53 am
I have the same problem. I have copied the program from two sources now and it still will not run. This suggests that there is something wrong with the pygame module or where it is stored. Any suggestions anyone?
Posts: 2
Joined: Sun Dec 30, 2012 11:50 am
by Pnb116 » Sun Dec 30, 2012 1:08 pm
Ok, I have narrowed it down. I can run the program from the LX terminal using python raspberrysnake.py and it runs, but it won't work from Idle3 when I use f5 or run module, it says no module named pygame.
Posts: 2
Joined: Sun Dec 30, 2012 11:50 am
by techpaul » Sun Dec 30, 2012 1:34 pm
Because it is python 2 code use IDLE instead of idle3.

pygame I dont believe yet has python3 support
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1479
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK