How to properly apply OOP in the bouncing ball code

Mel Wilson mwilson at the-wire.com
Fri May 8 14:44:28 EDT 2015


On Fri, 08 May 2015 08:40:34 -0700, Tommy C wrote:

> I'm trying to apply OOP in this bouncing ball code in order to have
> multiple balls bouncing around the screen. The objective of this code is
> to create a method called settings, which controls all the settings for
> the screen and the bouncing behaviour of multiple balls, under the class
> Ball. However, I keep on getting errors related to attributes (e.g.,
> speed). I'm new to OOP in Python so your help will be much appreciated.
> Thanks in advance.

I did something similar, with little characters running around the screen 
under joystick control.  What I think:

1. Each instance of the Ball class should control the behaviour of one 
ball.
2. The screen settings and global environment generally should be global, 
not stuffed inside the behaviour of a Ball.
3. Keeping the list of Ball instances should be something the main 
section of your program does, not stuffed inside the behaviour of a Ball.

Each of my little GamePlayer objects is subclassed from 
pygame.sprite.Sprite .  I forget what I got by doing this (it's old code) 
but looking up the docs for that class might give you some insights.  
Each GamePlayer object has an "update" method which accepts info from the 
joystick and updates the player's movements, and a "draw" method which 
the main loop calls on displaying every frame, to show the player's new 
position and attitude.

You might be able to do something along these lines.

	Mel.



More information about the Python-list mailing list