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:

        bulletdistance=math.sqrt(abs((bullet_pos_from_player[0])^2) + abs((bullet_pos_from_player[1])^2))

        print bullet_vel[0], bullet_vel[1]


and here is my class for the bullet:

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

def update(self):
    screen.blit(self.image, self.pos)

Hope someone can help out,

thanks guys.


The error is likely the typo in the line:


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:


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.

| improve this answer | |
  • \$\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 May 19 '13 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.