1

I'm just learning python/pygame and I'm doing a physics tutorial. Unfortunately, I think it was made in an older version of python. I copied the code in the video exactly, but when I run it it returns "Failed to run script - syntax error - invalid syntax(physics.py, line 7)". I'm sure it's just something stupid and obvious that I'm missing, but any answer would go a long way for me!

import os, sys, math, pygame, pygame.mixer
from pygame.locals import *

screen_size = screen_width, screen_height = 600, 400

class MyCircle:
    def __init__(self, (x, y), size, color = (255,255,255), width = 1):
        self.x = x
        self.y = y
        self.size = size
        self.color = color
        self.width = width

    def display(self):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.size, self.width)

screen = pygame.display.set_mode(screen_size)

my_circle = MyCircle((100,100), 10, red)
my_circle_2 = MyCircle((200,200), 30, blue)
my_circle_3 = MyCircle((300,150), 40, green, 4)
my_circle_4 = MyCircle((450,250), 120, black, 0)

fps_limit = 60
run_me = True
while run_me:
    clock.tick(fps_limit)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run_me = False

    my_circle.display()
    my_circle_2.display()
    my_circle_3.display()
    my_circle_4.display()

    pygame.display.flip()

pygame.quit()
sys.exit()
unodeej
  • 25
  • 5
  • If you think the problem is related to your python version, what version of python are you using? – Brionius Aug 13 '13 at 03:38
  • As a side note, what tutorial is this? For example, the code is clearly for something in the 2.3-2.7 range, which means it's intentionally defining `MyCircle` as a classic class. Also, there is no good reason for the tuple parameter unpacking that's causing your problems. And the `while run_me` is a silly attempt to avoid `break` because someone heard it was bad back when he was programming C. This is fine as a quick sample to slap on a wiki, but as a tutorial, it's not something I'd trust. – abarnert Aug 13 '13 at 03:48

2 Answers2

1

You're probably using Python 3. Tuple parameter unpacking was removed in 3.X, so you have to change this:

def __init__(self, (x, y), size, color = (255,255,255), width = 1):
    self.x = x
    self.y = y
    self.size = size
    self.color = color
    self.width = width

To:

def __init__(self, position, size, color = (255,255,255), width = 1):
    self.x, self.y = position
    self.size = size
    self.color = color
    self.width = width
Blender
  • 289,723
  • 53
  • 439
  • 496
1

Well, line 7 is this line:

def __init__(self, (x, y), size, color = (255,255,255), width = 1):

See that (x, y) in the middle of the parameter list? This is a feature called "tuple parameter unpacking", and it was removed in Python 3.0. (It's also discouraged in Python 2.6-2.7, but it works, so some people still use it.)

PEP 3113 has the full details.

If you run the 2to3 tool on your script, it'll tell you how to fix it.

But the simple fix is to replace that tuple (x, y) with a single parameter xy, and split it inside the function:

def __init__(self, xy, size, color = (255,255,255), width = 1):
    self.x, self.y = xy
    self.size = size
    self.color = color
    self.width = width
abarnert
  • 354,177
  • 51
  • 601
  • 671