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.