[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Re: Newbie wants to create pong



Chris Ashurst wrote:
Others can help you with your coding issues, but in the meantime, I have a
well-commented example of Pong on my server that could possibly provide a
reference for you: http://telestatic.net/python/pong.py

-----Original Message-----
From: owner-pygame-users@xxxxxxxx [mailto:owner-pygame-users@xxxxxxxx]On
Behalf Of Ville Sokk
Sent: Monday, September 11, 2006 10:21
To: pygame-users@xxxxxxxx
Subject: [pygame] Re: Newbie wants to create pong


I'm new to programming but I've learned the basics and have done some
programs but now I've decided to learn pygame. I thought I'll make a
simple pong game. I have a wierd bug I can't solve at execution. I don't
want you to solve other bugs, because I want to learn through solving
myself ;). Sorry for the .zip and Windows newlines. I currently have no
choice but use Windows.

Help? I uploaded it here: http://commodore.gran.ee/asjad/pong.zip too.


Notes:

#begin code excerpt class bg(pygame.sprite.Sprite):
......
[snip]
.......
ball=ball()
batter=batter()
enemyBatter=enemyBatter()
allThings=pygame.sprite.RenderPlain((bg, ball, batter, enemyBatter))
#end excerpt


On the last line you're creating a pygame.sprite.RenderPlain, but look what you're passing it.
what is bg?
Well, we start at the line where this call is made, and we search back up through the code
until we find the last place 'bg' was defined.
Oops, it turns out 'bg' is a class, not an instance of a class!
That's the first problem you're having.
You can see this error in the traceback:


Traceback (most recent call last):
File "C:\Documents and Settings\Rabidpoobear\Desktop\Pong\pong.py", line 79, in -toplevel-
allThings=pygame.sprite.RenderPlain((bg, ball, batter, enemyBatter))
File "C:\Python24\Lib\site-packages\pygame\sprite.py", line 368, in __init__
self.add(*sprites)
File "C:\Python24\Lib\site-packages\pygame\sprite.py", line 241, in add
sprite.add_internal(self)
AttributeError: 'tuple' object has no attribute 'add_internal'


what it's basically saying is 'in the call to pygame.sprite.RenderPlain you passed us a 'tuple' object,
and tuples don't have an 'add_internal' function in them.
The tuple that you're passing to it is the class 'bg.' I'm not really sure why it considers this a tuple,
but that's not the point.
So when you change bg to an instance of a class whose parent is pygame.sprite.Sprite, it will
magically work :)



Secondly: Your ball, batter, and enemyBatter classes have instances that are named
ball, batter and enemyBatter, respectively.
Effectively, this overwrites the class definitions, so you might as well have not made a class in the first
place and just made one instance (assuming this is possible in Python, I've never tried.).
Make your class instances different names so you can reuse your code.
That's the main goal of the OOP paradigm.



Another note:
Your functions, moveBoth, ballReset, etc.
You're leaving it up to the person to define what a ball, batter and other objects are, globally,
before they call your function.
A function should be a self-contained thing if possible. Either your function is only side-effects
or your function is only a return value, I think, is what you're going for. However, I believe
this mostly applies to functional programming languages and not to Object Oriented langauges.
I don't think you should ever need side-effects or global variables. That's what classes are for :D
I'd suggest making your moveBoth function into a 'reorienty' function
that accepts any number of parameters and sets all of their y-values equal.
The point being here that it doesn't have to know anything about the variables outside
its local scope, because you're handing it all the information it needs.


Anytime you find yourself typing 'global' there's probably a better way to do it.

Especially in your ball class, in the 'fly' function, you check to see if the ball and the batter collided.
However, you didn't get these values passed into the class, you're depending on them already being defined
in the scope from which you call the ball.fly function.
This is bad news.


It seems like what you need is an overarching 'Game' class that holds instances of
all your other classes, the pygame display surface, etc, etc. all as instance-level variables
so you can safely access them from within each individual function.
:D


Good luck!
-Luke

CONFIDENTIAL NOTICE: This email including any attachments, contains confidential information belonging to the sender. It may also be privileged or otherwise protected by work product immunity or other legal rules. This information is intended only for the use of the individual or entity named above. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on the contents of this emailed information is strictly prohibited. If you have received this email in error, please immediately notify us by reply email of the error and then delete this email immediately.