Pygame Rotated Shooting

前端 未结 3 1773
Happy的楠姐
Happy的楠姐 2020-12-01 22:42

Me and a few friends have been coding an intresting new shooting mechanic. For It to work, we need to shoot in the direction the player is facing. The Sprite is being rotate

3条回答
  •  既然无缘
    2020-12-01 23:38

    You have angle in degrees so try this:

    import math
    
    # angle examples 
    for angle in [0, 30, 45, 60, 0+90, 30+90, 45+90, 60+90, 0+180, 30+180, 45+180, 60+180, 0+270, 30+270, 45+270, 60+270]: 
    
        bullet_speed = 10
    
        bullet_move_x = bullet_speed * math.cos( math.radians(angle) )
        bullet_move_y = -bullet_speed * math.sin( math.radians(angle) )
    
        print angle, bullet_move_x, bullet_move_y
    

    Now you can add bullet_move_x, bullet_move_y to bullet positon.

    For better results keep bullet position as float and convert to int when you blit bullet

提交回复
热议问题