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

Can't resolve syntax error in pygame snake.

Thu Dec 20, 2012 11:22 am

I believe I have entered the snake pygame code into IDLE as set out on pages 162 to 171 in the Users Guide tutorial but when I go to RUN it comes back with a syntax error. The word 'pygame' following 'position [1], 20,20))' is highlighted in the graphics section of the code (bottom of page 169). The line starting 'pygame.draw' to 'raspberryPosition[1], 20, 20))' is on one line. Can anyone help me resolve this problem? I have attached a copy of 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)

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Can't resolve syntax error in pygame snake.

Thu Dec 20, 2012 12:06 pm

Extremely hard to follow the code when not formatted - you might want to put it inside a 'Code' tag (use the Code button above this text box you write into here and put your code between the two square bracket enclosed tags...).

Does your code in your editor look exactly like in the book? Would it work if you break that long line into two (not sure about the code flow, so this indentation might be wrong but...) something like this:

Code: Select all

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()
Btw, this question might have been better placed in the Programmin/Python section....
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

KenT
Posts: 758
Joined: Tue Jan 24, 2012 9:30 am
Location: Hertfordshire, UK
Contact: Website

Re: Can't resolve syntax error in pygame snake.

Thu Dec 20, 2012 12:54 pm

two simple statements on one line should be separated with a semi-colon
Pi Presents - A toolkit to produce multi-media interactive display applications for museums, visitor centres, and more
Download from http://pipresents.wordpress.com

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

Re: Can't resolve syntax error in pygame snake.

Thu Dec 20, 2012 4:54 pm

Thanks for your advice.
The code is written and indented exactly as in the Users Guide.
I have added ; in the long line of code as suggested and as a separate exercise split it into two lines. Both actions got rid of the syntax error message but brought up the following.

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
>>>

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Can't resolve syntax error in pygame snake.

Fri Dec 21, 2012 11:28 am

Unfortunately I don't have the book (yet) - of course there may be typos or printing errors ...then again, these might teach you more than just copying the text ;)

Noticed you continued with yout next issue in the Python section - I hope someone more experienced can help you there!
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

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

Re: Can't resolve syntax error in pygame snake.

Fri Dec 21, 2012 11:48 am

Replied to duplicate post in Programming/General section.

Matt

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

Re: Can't resolve syntax error in pygame snake.

Sat Dec 22, 2012 7:24 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)

Return to “Beginners”