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

Re: [pygame] Drawing Bullets



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?