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

Re: [pygame] Sprite Classes



did you see this?

http://www.sacredchao.net/~piman/writing/sprite-tutorial.shtml

You could create a Player that has an attribute bullets... something like

class Actor:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Bullet(Actor):
    def __init__(self, x, y):
        Actor.__init__(x,y)

class Player:
    def __init__(self, x, y):
        Actor.__init__(x,y)
        self.bullets = []
    def shoot(self):
        bul = Bullet(self.x, self.y)
        self.bullets.append(bul)


something along these lines?|

On Wed, Dec 14, 2011 at 9:08 AM, Joe Ranalli <jranalli@xxxxxxxxx> wrote:
> I'm far from an expert programmer, so maybe someone has better advice than
> this, but I don't think you should make the player and the things it shoots
> parts of the same sprite. A 'bullet' should be a new sprite that tracks its
> own movement, independent of the player.
>
> So what you'd do is to make a new sprite class that's image is the bullet.
> When you call your playerSprite's Shoot() function, what you would do is
> create a new bullet and start it moving.  You could use parameters already
> contained in the player like Direction, Velocity, etc. to help determine the
> bullet's initial conditions depending on how you want the bullets to
> behave.  The point though is that as soon as it's created, the player can
> forget about it, the bullet will take care of updating its own movement and
> figuring out where it should be drawn.
>
> Hope this helps.
>
>
>
> On Tue, Dec 13, 2011 at 11:59 PM, Andrew Godfroy <killerrin@xxxxxxxxxxx>
> wrote:
>>
>> Hi,
>>
>> I was wondering some things about sprites to help me understand them
>> better. I currently have a sprite class which loads up the player and can
>> move it around using the joystick/keyboard as well as rotate the character
>> based on what direction they are moving. What I want to be able to do is add
>> into the class a shoot function that will take the parameters I pass to it
>> (I already have the control system for joysticks ready for it)
>>
>> What I want to know is how sprite track what they draw, and if it is
>> possible to have it track two different images and locations per instance?
>>
>>
>> With Thanks,
>>
>>
>> Andrew Godfroy
>
>



-- 
A musician must make music, an artist must paint, a poet must write,
if he is to be ultimately at peace with himself.
- Abraham Maslow