finlay-rg1
Posts: 17
Joined: Tue Nov 27, 2012 9:02 am

Can't run snake pygame

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?

MattCook
Posts: 16
Joined: Tue Feb 21, 2012 1:46 pm
Location: S. Manchester

Re: Can't run snake pygame

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

finlay-rg1
Posts: 17
Joined: Tue Nov 27, 2012 9:02 am

Re: Can't run snake pygame

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)

gordon77
Posts: 2939
Joined: Sun Aug 05, 2012 3:12 pm

Re: Can't run snake pygame

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)

Pnb116
Posts: 2
Joined: Sun Dec 30, 2012 11:50 am

Re: Can't run snake pygame

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?

Pnb116
Posts: 2
Joined: Sun Dec 30, 2012 11:50 am

Re: Can't run snake pygame

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.

techpaul
Posts: 1514
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Can't run snake pygame

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/

Dancerbear
Posts: 1
Joined: Wed Aug 30, 2017 7:02 pm

Re: Can't run snake pygame

Fri Sep 01, 2017 2:39 pm

Hi i am new to the pi and was trying the raspberry snake so i copied the code out of the user guide but i cant understand why it isn't working. Here is the code:

Code: Select all

#!/usr/bin/env python
#python raspberry snake
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)
I would find it really helpful if you can point me in the right direction.

Thanks again
Leanne
(p.s. i'm running it in idle 2.7.9)

Moderator added code tags

gordon77
Posts: 2939
Joined: Sun Aug 05, 2012 3:12 pm

Re: Can't run snake pygame

Fri Sep 01, 2017 5:43 pm

Does it report any errors when run?

gordon77
Posts: 2939
Joined: Sun Aug 05, 2012 3:12 pm

Re: Can't run snake pygame

Sun Sep 03, 2017 9:03 am

I tried this and it gives no errors and appears to work OK.

Just press the keyboard arrow keys to move. When you hit the red dot it will move to a new random position, if you hit the edge of the window it will be game over.

Leannehendry
Posts: 1
Joined: Tue Sep 12, 2017 6:01 pm

Re: Can't run snake pygame

Tue Sep 12, 2017 6:18 pm

hi i am new to the pi and just wanted to do a basic bit of programming to get used to it so i decided to use the raspberry snake but for some reason it isn't running. when we enter the game it doesnt move untill i press a jey bu tit will work with any key and doesnt move all the time like it should. here is the code, would really apprciate some help:

Code: Select all

#!/usr/bin/env python
#python raspberry snake
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) 
thanks again for any help.

p.s. i running it in python 2.7.9

Return to “General programming discussion”

Who is online

Users browsing this forum: No registered users and 5 guests