0
\$\begingroup\$

I'm working on a top down shooter in pygame and am having trouble getting bullets to fire. I'm using a class for the bullets, I'll show what I think is the relevant code.

This is what I do after the mouse button is clicked:

if event.type == pygame.MOUSEBUTTONDOWN:
        bullet_vel=[0,0]
        fired_pos=pygame.mouse.get_pos()
        player.fired=True
        bullet_pos_from_player=[0,0]


        bullet_pos_from_player[0]=fired_pos[0]-player.rect[0]
        bullet_pos_from_player[1]=fired_pos[1]-player.rect[1]
        bulletdistance=math.sqrt(abs((bullet_pos_from_player[0])^2) + abs((bullet_pos_from_player[1])^2))
        bullet_vel[0]=bullet_pos_from_player[0]/bulletdistance
        bullet_vel[1]=bullet_pos_from_player[1]/bulletdistance

        print bullet_vel[0], bullet_vel[1]

        bulletgroup.add(bullet((bullet_vel)))

and here is my class for the bullet:

class bullet(pygame.sprite.Sprite):
def __init__(self, bullet_vel):
    super(bullet,self).__init__()
    self.pos=player.draw_pos
    self.image=pygame.transform.rotate(bullet_image, player.angle)
    self.vel=bullet_vel


def update(self):
    self.pos[0]=self.pos[0]+self.vel[0]
    self.pos[1]=self.pos[1]+self.vel[1]
    screen.blit(self.image, self.pos)

Hope someone can help out,

thanks guys.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

The error is likely the typo in the line:

pygame.sprite.Sprite.__init(self)

The __init__ function in python is special, and should be called as such. That being said, your code would be cleaner and you could avoid these issues if you used new-style class inheritance:

class bullet(pygame.sprite.Sprite):
    def __init__(self, bullet_vel):
        super(bullet, self).__init__()
....

Another fairly egregious error is in these lines:

    bullet_vel[0]=math.ceil(math.sinh(bullet_angle)*bulletspeed)
    bullet_vel[1]=math.ceil(math.cosh(bullet_angle)*bulletspeed)

The problems are:

  1. sinh and cosh are hyperbolic functions, and not what you want. You're probably looking for the regular sin and cos functions.
  2. cos corresponds to the x-axis, and sin corresponds to the y-axis. You have them reversed.
  3. There is no need to take the cieling of them... python does a good job of handling floating point. If you need them to be integers for the sake of pygame's libraries, then it's better to call int() on them once you need to.
  4. The sin and cos functions take arguments in radians

Finally, you're not saving the value of your bullet_vel variable in your bullet's __init__ function. Hence, if you ever do reach the bullet's update call, then it will raise an error. You will need to add something like

self.vel = bullet_vel

to the __init__ function and call self.pos.move(self.vel) in the bullet's update function.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you, you're right in that the first one was a simple typo, I don't know how I managed to miss that. I'll definitely have a good look at everything and try to fix up the code with what you've said when I have time. thank you, \$\endgroup\$
    – Alex
    Commented May 19, 2013 at 6:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .