Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 6th, 2013, 07:16 PM
Ironbard Ironbard is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 Ironbard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 m 50 sec
Reputation 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.

Reply With Quote
  #2  
Old January 7th, 2013, 01:54 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 15 m 15 sec
Reputation Power: 65
Quote:
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 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Reply With Quote
  #3  
Old January 7th, 2013, 06:57 AM
Ironbard Ironbard is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 Ironbard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 m 50 sec
Reputation 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?

Reply With Quote
  #4  
Old January 7th, 2013, 09:33 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,376 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 12 h 21 m 42 sec
Reputation Power: 383
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Syntax Error in constructor?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap