[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Firing bullets
- To: pygame-users@xxxxxxxx
- Subject: Re: [pygame] Firing bullets
- From: "Brendan Speer" <brendan.speer@xxxxxxxxx>
- Date: Mon, 26 Mar 2007 09:10:58 -0500
- Delivered-to: email@example.com
- Delivered-to: firstname.lastname@example.org
- Delivered-to: email@example.com
- Delivery-date: Mon, 26 Mar 2007 10:11:14 -0400
- Dkim-signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:references; b=D8YM3sDiGcXYkXTcr/GYrp4JhRZTbZ07/MP1edFDAv/3V+rUSDU4VlHwdsS9hT0WYEdKE4NXqgBA2tcWp/tmkj9Jbmq3xBw0z8I00NQI6LMPRLl+7FYBl4mEPppjpPkJU3IHe931BDPL0lsZy3nfmPcrWkbC3rEHHhYKrxL2G5o=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:references; b=n+KeTTxIT36E86BVZvS24NBhYQ0pWjvC+JykqEPRzvK1OuT/alg1sCLZ/GH0/pjiSrb9Zvzb2fcf54mnY9kLcJ+YgCIKNshC9d/I80ydRVmpQdhaU1FQhKRULRC91PkM6e7IudPmiG4k2R7U46ddj7z80vXYpEV7c+ij4UlrNSM=
- In-reply-to: <firstname.lastname@example.org>
- References: <email@example.com>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
OK, A few points:
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.
- A __init__ function is not required for a class. Almost all of them end up actually having one, but...
- Look carefully at your fire() function. It doesn't do anything.
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.)
- 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.
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:
def __init__(self, position=(0,0), velocity=(0,0)):
self.position = position
self.velocity = velocity
self.ttd = 5
self.velocity = self.velocity
position += velocity
class Big_Bullet(Bullet, pygame.sprite.Sprite):
Also, it's been kinda quiet lately, hasn't it?