Jump to content

Pygame Bullet Bug

- - - - -

  • Please log in to reply
15 replies to this topic

#1
Gh0st

Gh0st

    Newbie

  • Members
  • PipPip
  • 17 posts
I have this bug in my game where every time I press the space key it fires a beam of light like it is suposed to but when you remove your hand from the space key before it hit one of its collision points it disappears then if you hit it again it reappears where it left off. What I need it to do is when you press the space bar that it fires the bullet once and keeps going until it hits a collision point. Any help will be appreciated. Here is my code.

Edited by Gh0st, 18 February 2011 - 04:33 PM.


#2
xetama

xetama

    Newbie

  • Members
  • Pip
  • 9 posts
It looks like the draw and update for the weapon sprites only happen if the space bar is held down. You need to make the space bar be a trigger event for instantiating a new 'weaponSprite' into your list, and the draw and update for the list happens every iteration for each 'weaponSprite' in the list.

#3
Gh0st

Gh0st

    Newbie

  • Members
  • PipPip
  • 17 posts
So like
for event in pygame.event.get():

            if event.type == pygame.QUIT:

                keepGoing = False

            if event.type == pygame.KEYDOWN:

                if pygame.key.get_pressed()[pygame.K_SPACE]:

                    weaponSprites.draw(screen)

                    weaponSprites.update()

Would that work.

#4
xetama

xetama

    Newbie

  • Members
  • Pip
  • 9 posts
OK, 'weaponSprites' actually refers to only 1 sprite image. Rather than an actual group of them. Each game-loop iteration you need to always draw, and update, the sprites in the group if there are any to be drawn/updated. The space bar should be the event you're looking for to add another sprite to the 'weaponSprites' group. Assuming each individual addition can update itself independently from any other addition.

This means initially there will be no sprites in the 'weaponSprites' group. Then the space bar is hit and one is added, which will be drawn and updated continuously until it leaves the screen (at which time it should be removed). If the space bar is hit again, another will be added, which will update and draw itself independently. This is handled in the pygame.sprite.Group by simply calling 'weaponSprites.update()' and 'weaponSprites.draw(screen)'.

This code is still checking if the space bar is being pressed. If it is, it's going to update and draw, if it's not no update or draw action will occur.


if pygame.key.get_pressed()[pygame.K_SPACE]:

        weaponSprites.draw(screen)

        weaponSprites.update()


Another point to make is that holding down the space bar, should not infinitely create more sprites, there should probably be a firing-rate associated with the add operation as well.

#5
Gh0st

Gh0st

    Newbie

  • Members
  • PipPip
  • 17 posts
OK here is what I have so far based on my understanding of what you are saying

if event.type == pygame.KEYDOWN:

                if pygame.key.get_pressed()[pygame.K_SPACE]:

                    weaponSprites.add(hv)

        if hv.rect.top <= 0:

            weaponSprite.remove(hv)


#6
xetama

xetama

    Newbie

  • Members
  • Pip
  • 9 posts
That's essentially right, except I believe the add() function only accepts items that don't already exist in the list. Which would mean you need a new instance of HeatVision() each time you call add. You can also handle the remove() event inside the update function of the HeatVision class if you make weaponSprite accessible in a scope that HeatVision can access. In this case either the global scope, or passed to the HeatVision class when it's added.

#7
Gh0st

Gh0st

    Newbie

  • Members
  • PipPip
  • 17 posts
ok So how do I handle the add() then cause I'm not sure what else to use.

#8
Gh0st

Gh0st

    Newbie

  • Members
  • PipPip
  • 17 posts
k now I have implemented that. there is now a different problem I press the space key once and it does not stop firing it just keeps going.

#9
xetama

xetama

    Newbie

  • Members
  • Pip
  • 9 posts
This is where you have to throttle the actual firing rate, only allow a certain amount to be added in a given time-frame (depending on what you're aiming for i.e. a beam, a single bullet, etc.). For a bullet 1 every 1.5 seconds is usually a good rate.

Edited by xetama, 14 February 2011 - 08:53 PM.
grammar, it's getting late.


#10
Gh0st

Gh0st

    Newbie

  • Members
  • PipPip
  • 17 posts
a beam would be nice but I'm not exactly sure how to deal with timing as you can see from the way I set up powerups.

#11
xetama

xetama

    Newbie

  • Members
  • Pip
  • 9 posts
You can use the pygame.time.get_ticks() to get the current tick rate, set a variable when a sprite is added to the current ticks and fiddle with 'pygame.time.get_ticks() - last_add > firerate' where last_add is the time.get_ticks() value of the last time you added a sprite and firing rate is a value you fiddle with until it looks right.

As a note this is where we get into territory I am unfamiliar with, I don't know that much about pygame, I just looked up that function in the docs.
time - Pygame Documentation

#12
xetama

xetama

    Newbie

  • Members
  • Pip
  • 9 posts
So I can't post links without a moderator's approval =\. You can use 'pygame.time.get_ticks()' to get the current tick count when you add a sprite. If you fiddle with a firing rate value in something like

""" firing rate """

firingrate = 100

last_add = 0


""" ... """


if pygame.key.get_pressed()[pygame.K_SPACE]:

    if pygame.time.get_ticks() - last_add > firingrate:

        """ add logic """

        last_add = pygame.time.get_ticks()


That should let you adjust the firing rate till it looks right.

Edited by xetama, 14 February 2011 - 09:24 PM.
Going to bed, can't tell the difference between 10 and 100





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users