python – Why are my bullets travelling at a 90 degree angle from the mouse?

python – Why are my bullets travelling at a 90 degree angle from the mouse?

Because the calculation of the angle is wrong:

while True:
    # [...]
    
    if key[pygame.K_SPACE]:
       # [...]

        #bullet.angle=math.atan2(mouse[0]-bullet.rect.x,mouse[1]-bullet.rect.y)
        bullet.angle = math.atan2(soldier.rect.y - mouse[1], mouse[0] - soldier.rect.x)
        
    #angle = math.atan2(mouse[0]-soldier.rect.x,mouse[1]-soldier.rect.y)/6.28*360
    angle = math.atan2(soldier.rect.y - mouse[1], mouse[0] - soldier.rect.x) * 180/math.pi

See How to know the angle between two points?
and How to rotate an image(player) to the mouse direction?

python – Why are my bullets travelling at a 90 degree angle from the mouse?

Leave a Reply

Your email address will not be published.