#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    2
    Rep Power
    0

    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. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    2
    Rep Power
    0
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Originally Posted by Mike234
    Code:
    import pygame ,sys
    Code:
    import sys
    
    import pygame
    Much better.

    Code:
    size = width, height = 320, 240
    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.
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    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
    Last edited by Grim Archon; July 22nd, 2004 at 04:38 AM.

IMN logo majestic logo threadwatch logo seochat tools logo