问题描述
我正在用pygame制作小型游戏,并且制作了绕其中心旋转的枪支.我的问题是我想让枪自行旋转到敌人的方向,但是我不能这样做,因为我找不到枪和敌人之间的角度来使枪旋转经过搜索,我发现必须使用 atan2
,但是我找不到任何有效的代码,因此希望有人可以帮助我.
I am making small game with pygame and I have made a gun that rotates around its center.My problem is that I want the gun to rotate by itself to the enemy direction, but I couldn't do that because I can't find the angle between the gun and the enemy to make the gun rotate to itI have searched and I found that I have to use the atan2
but I didn't find any working code so I hope someone could help me.
这是我的代码:
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)
这是一张图片,试图弄清楚:
And here is a picture trying to make it clear:
我该如何解决问题?
推荐答案
两点之间的角度的切线定义为Δy/Δx即(y2-y1)/(x2-x1).这意味着 math.atan2(dy,dx)
给出两个点之间的角度,假设,您知道定义坐标的基轴.
The tangent of the angle between two points is defined as delta y / delta xThat is (y2 - y1)/(x2-x1). This means that math.atan2(dy, dx)
give the angle between the two points assuming that you know the base axis that defines the co-ordinates.
为了计算以弧度为单位的角度,您的枪被假定为轴的(0,0)点.有了该角度后,就可以将角度用于其余的计算中.
Your gun is assumed to be the (0, 0) point of the axes in order to calculate the angle in radians. Once you have that angle, then you can use the angle for the remainder of your calculations.
请注意,由于角度为弧度,因此您需要在代码中使用math.pi而不是180度.此外,也不需要进行超过360度(2 * math.pi)的测试.负数(< 0)的测试是不正确的,因为您随后将其强制为0,这将迫使目标在正方向上位于x轴上.
Note that since the angle is in radians, you need to use the math.pi instead of 180 degrees within your code. Also your test for more than 360 degrees (2*math.pi) is not needed. The test for negative (< 0) is incorrect as you then force it to 0, which forces the target to be on the x axis in the positive direction.
您的用于计算枪支与目标之间角度的代码就是
Your code to calculate the angle between the gun and the target is thus
myradians = math.atan2(targetY-gunY, targetX-gunX)
如果要将弧度转换为度数
If you want to convert radians to degrees
mydegrees = math.degrees(myradians)
将度数转换为弧度
myradians = math.radians(mydegrees)
语法
math.atan2(y,x)
Syntax
math.atan2(y,x)
参数
y,x =数字
Parameters
y,x=numbers
示例
返回值为:
Examples
The return is:
>>> import math
>>> math.atan2(88,34)
1.202100424136847
>>>
这篇关于如何知道两点之间的夹角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!