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

Re: [pygame] circles



Thanks guys!  I think I've got a better handle on it now. 

On Sat, Oct 2, 2010 at 10:05 PM, B W <stabbingfinger@xxxxxxxxx> wrote:
I like Ian's earlier suggestion, Kevin, which he only hinted at. But I sense you're really struggling with how to extend your Circle class, and could use an example to go with the explanations.

Note that every time you create a circle object with the Circle class, you're executing the constructor __init__. So if you add some parameters to __init__, then you can manipulate the creation and will get objects that behave in the same manner but with different details. Details such as position, direction, color, and anything else you need.

In the constructor do something like this:


class Circle(pygame.sprite.Sprite):
    def __init__(self, position, direction, color, *groups):
        pygame.sprite.Sprite.__init__(self, *groups)
        #[use your same image and rect code]
        self.rect.center = position
        self.dx,self.dy = direction

Your update method is fine the way it is. As Ian points out, get rid of update_circle2; it doesn't serve your purposes.

In main create some circles at different start positions, directions, and colors, and might as well supply their sprite group since the Sprite superclass's constructor can automatically add the sprite to the group for you:

Circle([25,25], [+5,+5], Color("yellow"), allSprites)  # top-left, going down-right
Circle([640-25,25], [-5,+5], Color("red"), allSprites)  # top-right, going down-left

You may be wondering where the circles go, since you're obviously not assigning them to a variable. They're in your sprite group, by virtue of the pygame.sprite.Sprite.__init__(self, *groups) call.

I'm sure you can figure out the other two circles.

After making these changes, you can remove the few lines of main's sprite initialization that no longer serve a purpose.

Hope that helps to get you over the hump.

Gumm



On Sat, Oct 2, 2010 at 7:38 PM, kevin hayes <kevinosky@xxxxxxxxx> wrote:
I just want to thank you for taking the time to help me out.  I'm going to take a break for a while and when I come back I'm going to look over your suggestions.  I hope to be able to answer some questions at some point rather than asking them. 

On Sat, Oct 2, 2010 at 7:24 PM, Ian Mallett <geometrian@xxxxxxxxx> wrote:
On Sat, Oct 2, 2010 at 8:09 PM, kevin hayes <kevinosky@xxxxxxxxx> wrote:
At this point I don't really care if the sprites write over eachother...
You should.  Because otherwise, you can't see anything.  I don't mean proper handling of occlusion; I mean not being able to see what's going on.  The problem can be found by first figuring out why they are being drawn on top of each other.  In graphics programming the problem is almost always self-evident once you see what's actually happening.
as long as that doesn't cause the program to crash.  I'm just trying to get four circles going diagonally from each corner.  The problem I'm having is with the update function...Do I write two update functions?  How do I get the update function to apply separate code for the two (or more) circles?
Only one update function can exist.  What you CAN (and should) do, though, is have the update function do a different thing for each sprite.  Your second update function is never getting called, so delete it. 
Here is my code as it stands:
Add this:
circle2.dx = -5
Then, you'll see that the sprites were being drawn over each other (and moreover, they were starting in the same position, so the yellow one darts off the screen to the left).  This latter is due to the fact that startPosx is never used.  Change your update function to change startPosx and startPosy instead of rect.center.
 
Ian