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

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Syntax Error in constructor?


    So these past days I've been trying to get back into Python and pygame.

    I've settled with Python 3.2 as that's the newest version that pygame supports.

    Currently I'm trying to work my way through this tutorial:

    Peter Collinridge - Pygame Physics Simulation

    [Edit: I'm new so I can't post a hyperlink, but if you google that, it should be the first result]

    I've gotten to part 4 without too much trouble - but cannot proceed any further as there is an issue that I don't know how to solve.

    I don't think you'll need the whole code, but here it is anyway.

    Code:
    import pygame
    import random
    import math
    
    background_colour = (255,255,255)
    (width, height) = (300, 200)
    
    class Particle():
        def __init__(self, (x, y), size):
            self.x = x
            self.y = y
            self.size = size
            self.colour = (0, 0, 255)
            self.thickness = 1
            self.speed = 0
            self.angle = 0
    
        def display(self):
            pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)
    
        def move(self):
            self.x += math.sin(self.angle) * self.speed
            self.y -= math.cos(self.angle) * self.speed
    
    screen = pygame.display.set_mode((width, height))
    pygame.display.set_caption('Tutorial 4')
    
    number_of_particles = 10
    my_particles = []
    
    for n in range(number_of_particles):
        size = random.randint(10, 20)
        x = random.randint(size, width-size)
        y = random.randint(size, height-size)
    
        particle = Particle((x, y), size)
        particle.speed = random.random()
        particle.angle = random.uniform(0, math.pi*2)
    
        my_particles.append(particle)
    
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        screen.fill(background_colour)
    
        for particle in my_particles:
            particle.move()
            particle.display()
        pygame.display.flip()
    [Edit: The code should be properly formatted now.]

    My problem so far is on line 9:
    Code:
    def __init__(self, (x, y), size):
    I get a syntax error on the inner parenthesis.

    Now, I should say that, when last I was familiar with Python, I used v2.x - back then the above code ran fine, so there are a few changes I'll have to adjust to, I know that.

    Also I usually don't ask for help, but this thing's got me stumped and all my searches have been in vain.
    So, any and all help appreciated - Thanks in Advance.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    434
    Rep Power
    67
    Originally Posted by Ironbard
    My problem so far is on line 9:

    Code:
    def __init__(self, (x, y), size):
    I get a syntax error on the inner parenthesis.
    Easy enough: change the parameters so that the location is represented by a single tuple, and unpack the tuple:

    Code:
    def __init__(self, loc, size):
        x, y = loc
    If you want to ensure the correctness of parameters, either enclose the unpacking in a try...except block or first check that “loc” is an instance of tuple.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    : D Works like a charm! ^^ So simple, but I have to wonder - why did that change? I mean, when did that change in Python? Why is it necessary to do it like that nowadays?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    As I recall, that's a change from python 2 to python 3. You can no longer group tuples of arguments in python3.

    They made the change just when I was starting to enjoy the feature. However, you can make assignments like
    Code:
    $ python3
    Python 3.2.3 (default, Oct 19 2012, 19:53:16) 
    [GCC 4.7.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def f(*args):
    ...  (a,*b,c,) = args  # b gets everything else
    ...  print('a',a)
    ...  print('b',b)
    ...  print('c',c)
    ... 
    >>> f(1,2,3,4,5,6,7)
    a 1
    b [2, 3, 4, 5, 6]
    c 7
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo