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

Re: [pygame] Drawing Bullets



Charles Joseph Christie II schrieb:
On Saturday 28 April 2007 11:45:15 am DR0ID wrote:
Charles Joseph Christie II schrieb:
On Saturday 28 April 2007 01:28:38 am Kris Schnee wrote:
Charles Christie wrote:
Well, bullets just seem to be a sticking point for me...
Stick with it!

1. bullet cleanup (self.kill, etc.) which isn't important to me right
now, as the demo won't be running for an extended period, and when it
does it'll be running on a monstrous beast of a computer that can
probably handle the crappy coding.
2. Drawing bullets (pygame.update.dirtyrects, etc) which is VERY
important to me right now.
I don't think you need a kill function. What I did in my shooter game
was to have the bullets stored in two lists, and then every frame:

self.player_bullets = [bullet for bullet in self.player_bullets if
bullet.ttl > 0]
self.enemy_bullets = [bullet for bullet in self.enemy_bullets if
bullet.ttl > 0]

This filtering gets rid of the ones whose time-to-live is expired, due
to Python's automatic garbage-collection.

So, I tried to load an image for the bullets, and that didn't work
either. What happened was that it couldn't load the image... probably
because it doesn't exist. Which it does, though.

It's in the project directory under the folder img/Bullet.png. When I
put img/Bullet.png into the load_image function it says it can't load
the image! I tried it with front and back slashes and it didn't work.
Yeah, Python doesn't automatically handle path names. Try os.path.join,
as in:

import os
filename = os.path.join("img","Bullet.png")
my_image = pygame.image.load(filename).convert_alpha()

As I understand it, os.path.join attaches the directory terms (any
number of them) according to whatever format is appropriate for the OS.

After I get the image loaded, however, I can't add the bulletlist to my
list of dirtyrects to update... The dirtyrect doesn't read from lists!
How do I get pygame.sprite.RenderUpdates to display my bullets on the
screen?
I haven't messed with dirty rectangle functions myself, so this one is
beyond me.

Kris
Woohoo! Thanks! I'll try this ASAP. Still need to figure out how to get
the dirtyrects thing sorted out though...
Hi

if you use Renderupdates then you have to do the following:

make an instance of the RenderUpdateGroup
add as many sprites you want to it

then in each frame of you game:
       renderUGroup.clear()
       # update your sprites in here
       dirty_rects = renderUGroup.draw(screen)
       display.update(dirty_rects)

It should be more or less that thing. Try to look at an example like
"chimp" on the pagame website.

Sorry, about that short and quick instruction, but I have not much time
at the moment.

~DR0ID

No problem, any help is appreciated! Does RenderUpdateGroup read from lists, though?


Hi again

well, the pygame.sprite.RenderUpdates is like your list, it is a container for sprites. I would inherit from Sprite to make the Bullet:

class myBullet(pygame.sprite.Sprite):
   def __init__(self):
         pygame.sprite.Sprite.__init__(self)
         ....

Then in your code you do:

#instanciate Render group
rg = pygame.sprite.RenderUpdates()
# add some Bullets
for i in range(100):
   rg.add( myBullet() )

# in main loop, to draw all bullet in rg
rg.clear()
rg.update()
dirty_rects = rg.draw(screen)
pygame.display.update(dirty_rects)

Naturally any Bullet in the group will be updated and drawen on screen. I would just instanciate a new Bullet when needed and add it to the rg group (so it get visible). If you need to go through the bullet (for collisions detection for example) you can use rg.sprites() which returns a list with the bullet sprites.

I hope that helps a better. I hope I havent told you something incorect, because it is a while I have used the pygame.sprite groups.

~DR0ID