相关疑难解决方法(0)

如何知道两点之间的角度?

我正在用pygame制作小游戏,我制作了一支围绕其中心旋转的枪.我的问题是我希望枪自己向敌方方向旋转,但是我不能这样做因为我找不到枪和敌人之间的角度让枪旋转到它我已经搜索过了我发现我必须使用atan2但我没有找到任何正常工作的代码所以我希望有人可以帮助我.

这是我的代码:

import pygame
from pygame.locals import*
pygame.init()
height=650
width=650
screen=pygame.display.set_mode((height,width))
clock=pygame.time.Clock()
gun=pygame.image.load("m2.png").convert_alpha() 
gun=pygame.transform.smoothscale(gun,(200,200)).convert_alpha()
angle=0
angle_change=0
RED=(255,0,0)
x=525
y=155
while True :
    screen.fill((150,150,150))
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            quit()
        if event.type==KEYDOWN:
            if event.key==K_a:
                angle_change=+1
            if event.key==K_d:
                angle_change=-1
        elif event.type==KEYUP:
            angle_change=0
    angle+=angle_change
    if angle>360:
        angle=0
    if angle<0:
        angle=360
    pygame.draw.rect(screen,RED,(x,y,64,64))
    position = (height/2,width/2)
    gun_rotate=pygame.transform.rotate(gun,angle) 
    rotate_rect = gun_rotate.get_rect()
    rotate_rect.center = position
    screen.blit(gun_rotate, rotate_rect)
    pygame.display.update()
    clock.tick(60) 
Run Code Online (Sandbox Code Playgroud)

这是一张试图说清楚的图片:

在此输入图像描述

谢谢.

python math pygame angle

10
推荐指数
3
解决办法
3万
查看次数

标签 统计

angle ×1

math ×1

pygame ×1

python ×1