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

Re: [pygame] Firing bullets

OK, A few points:
  1. A __init__ function is not required for a class.  Almost all of them end up actually having one, but...
  2. Look carefully at your fire() function.   It doesn't do anything.
  3. Simiarly, this:
        def __init__(self):
    Does the exact same thing as if you have just chosen not to define an __init__ function (Because your class would inherit from the Bullet.__init__ already.)
  4. This one is a bit more esoteric, but, your bullet fires at a rate related to the speed at which the game is running.   Usually any sort of timing things like that should be done based off of how much time has elapsed from the last time the object was updated.
I've included the Bullet class from one of my games.  It's pretty basic, so I've commented it up to explain.   The class is intended to be used by Creating a new PlayerBullet object each time the player fires their gun.  This is places in a list of other bullets that might be on the screen at the same time.  There is also an cleanup routine in my main Update() that removes bullets after they have been alive a certain length of time.

class PlayerBullet(object):
    def __init__(self, x, y, angle=0):
        self.speed = 50  ## This is speed in Pixels per 1/10th of a second.  
        self.angle = angle  ## Angle at which the bullet is travelling
        self.born = pygame.time.get_ticks()  ## When was I born? (Used in cleanup function)
        self.strength = 10 ## How much damage do I do?
        self.x = x ## Absolute Map X coord
        self.y = y ## Absolute Map Y coord
        self.image = pygame.transform.rotate(pygame.image.load("data/bullet.png").convert_alpha(), -1 * math.degrees(self.angle))
           ## The image is loaded, converted, then rotated to be 'pointing' the direction that the shot is fired.
    def update(self, interval):
        ## Interval is the number of ticks since the last frame
        ## The formula here gives you an X / Y offset using SINE / COSINE functions.   Then you also have speed relative to
        ##  the last update interval.
        self.x += math.cos(self.angle) * self.speed * interval / 100
        self.y += math.sin(self.angle) * self.speed * interval / 100
    def render(self, te, suf):
         ## Just a simple render call.   te.screenx and te.screeny are the offset that the current viewport is at.
        suf.blit(self.image, (self.x-te.screenx, self.y-te.screeny))

On 3/26/07, Charles Christie <sonicbhoc@xxxxxxxxx> wrote:
Well, I'm trying to get my program to fire bullets now. Isn't that great?

I think I'm doing it all wrong though...

My problem has been that I don't know how to make the program fire bullets. But then I thought, "shouldn't the character be firing the bullets, not the program?" and it's then that I realized why I couldn't do anything. My logic has been completely wrong. Again. Go figure <_<

Well, I still don't have images for characters yet and I still have no movement code (I should probably work on that, huh?) but everything else aside from the characters and bullets is done.

Once I finish these, I'll be able to probably try to work on a title screen and put a crappy proof-of-concept idea out there (which is pretty much all I need to finish my senior project - a proof-of-concept with a demo that is playable).

My question is: Is it possible to use bulletml to make bullet patterns for this game? I've seen a C++ wrapper for it called libbulletml but nothing much else.

So. I made a thread asking for help at the Game Programmer's Wiki Forums. Could you also look at that too?

I tried to fix his syntax (a class without an __init__ function? Don't you need those?) and this is what I have so far:

class Bullet:
    def __init__(self, position=(0,0), velocity=(0,0)):
        self.position = position
        self.velocity = velocity
        self.ttd = 5
    def fire(self):
        self.position = self.position
        self.velocity = self.velocity
    def update(self):
        position += velocity

class Big_Bullet(Bullet, pygame.sprite.Sprite):
    def __init__(self):
    def update(self):

class Small_Bullet(Bullet):
    def __init__(self):
    def update(self):

Also, it's been kinda quiet lately, hasn't it?