|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Spot the error for the Newbie
Hello!!
Ok here is a code example i copied from a guide on Pygame. It worked when i used IDLE and typed in line by line But it doesnt work if I try running the code from a .py file --error-- File "bouncyball.py", line 19 ballrect = ballrect.move(speed) ^ SyntaxError: invalid syntax --code-- import pygame ,sys pygame.init() size = width, height = 320, 240 speed = [2, 2] black = 0,0,0 screen = pygame.display.set_mode(size) ball = pygame.image.load("e:\\ball.bmp") ballrect = ball.get_rect() while 1: ..for event in pygame.event.get(): .....if event.type == pygame.QUIT: sys.exit() ..ballrect = ballrect.move(speed) ..if ballrect.left < 0 or ballrect.right > width: ....speed[0] = -speed[0] ..if ballrect.top < 0 or ballrect.bottom >height: ....speed[1] = -speed[1] ..screen.fill(black) ..screen.blit(ball, ballrect) ..pygame.display.flip() pygame.QUIT() any ideas??????? p.s. ".." means tab, |
|
#2
|
||||
|
||||
|
I cant see anything from what the code but maybe it was lost when you posted it? Try posting the code again using Code-tags as discribed the sticky at the top of this forum
.Also, you may want to try changing... Code:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
to.. Code:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
Just to see if that helps, Mark. |
|
#3
|
|||
|
|||
|
Code:
import pygame ,sys
pygame.init()
size = width, height = 320, 240
speed = [2, 2]
black = 0,0,0
screen = pygame.display.set_mode(size)
ball = pygame.image.load("e:\\ball.bmp")
ballrect = ball.get_rect()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom >height:
speed[1] = -speed[1]
screen.fill(black)
screen.blit(ball, ballrect)
pygame.display.flip()
pygame.QUIT()
error: File "bouncyball.py", line 20 ballrect = ballrect.move(speed) ^ SyntaxError: invalid syntax |
|
#4
|
||||
|
||||
|
Quote:
Code:
import sys import pygame Much better. Quote:
Don't do this. Do: Code:
width = 320 height = 240 size = (width, height) if you must. Code:
ballrect = ballrect.move(speed) ballrect.move() returns something? A new ballrect? Weird. |
|
#5
|
||||
|
||||
|
Not sure if this is your problem, but..
I have had similar problems with "syntax errors" which were entirely due to files mixing tab characters and spaces or just because one line has 3 spaces for indentation when 4 spaces are used in other places.. This often happens when pasting code from other sources (e.g. off a web-page). You can also cause problems if you switch editors and the editors are not configured the same way. (the use of tabs character/ number of spaces for tab). Try loading the file into Idle then select the whole text (CTRL-A) the use the Format-"Untabify Region" menu option. Then save the file and try again. If you still get errors then search for odd indentation depths around the line reported. I have also had indentation errors like this that only show up when a module has been included by another module. Try to be consistent with other modules which typically use 4 spaces for each tab. You will save yourself problems in the future. grim ![]()
__________________
*** Experimental Python Markup CGI V2 *** Last edited by Grim Archon : July 22nd, 2004 at 04:38 AM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Spot the error for the Newbie |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|