我从第一个点(玩家位置)射击子弹,它需要以给定的速度向第二个点(鼠标点击)移动。
现在我拥有所有这些代码 cx和cy标记玩家的中心或第一点 而MouseX和MouseY代表mous点击的坐标
if MouseX < cx: #determines direction of bullet
direction = 'left'
else:
direction = 'right'
if (cx-MouseX) != 0:
new_slope = (cy - MouseY) / (cx - MouseX)
else:
new_slope = 'undefined' #determines slope and b value to get the equation of the line
#that the bullet travels on
if new_slope != 'undefined':
b_value = ((-1)*(new_slope)*(cx))+(cy)
else:
b_value = None
和
if self.direction == 'right':
if self.slope != 'undefined':
if self.slope > 0 or self.slope < 0 and self.slope != 0:
if self.slope < 1 or self.slope > -1:
float(self.bx)
float(bullet_speed)
self.by = (self.slope*(self.bx+bullet_speed)+self.b_value)
self.bx = ((self.by - self.b_value)/self.slope)
else:
float(self.bx)
float(bullet_speed)
self.bx = (((self.by+bullet_speed) - self.b_value)/self.slope)
self.by = (self.slope*(self.bx)+self.b_value)
else:
self.bx = self.bx + bullet_speed
else:
if self.slope != 'undefined':
if self.slope > 0 or self.slope < 0:
if self.slope < 1 or self.slope > -1:
self.by = (self.slope*(self.bx-bullet_speed)+self.b_value)
self.bx = ((self.by - self.b_value)/self.slope)
elif self.slope == 0:
self.bx -= bullet_speed
else:
self.bx = (((self.by-bullet_speed) - self.b_value)/self.slope)
self.by = (self.slope*(self.bx)+self.b_value)
else:
self.bx = self.bx - bullet_speed
它非常凌乱,导致我的子弹在向上或向下射击时速度增加,并且在向左或向右射击时变慢。
然而,我不知道应该做些什么来改变代码以允许我的子弹以相同的速度行进而不管角度拍摄。如果有擅长数学和tkinter的人可以提供帮助,那将非常感激。
(作为旁注,我的所有对象都是Tkinter画布项,所以它们必须按整数移动)
答案 0 :(得分:0)
我希望我没有犯错 - 我不会记得太清楚。
编辑: atan2(dy, dx)
中出现错误 - 必须为atan2(dx, dy)
如果您有子弹的位置和速度以及目标,则计算新位置(在一帧/移动之后) 位置。
你必须重复它
import math
speed = 10
# bullet current position
x1 = 0
y1 = 0
# taget possition
x2 = 100
y2 = 100
dx = x2 - x1
dy = y2 - y1
angle = math.atan2(dx, dy)
#print(math.degrees(angle))
cx = speed * math.sin(angle)
cy = speed * math.cos(angle)
#print(cx, cy)
# bullet new current position
x1 += cx
y1 += cy
print(x1, y1)
编辑:循环示例
编辑:abs()
需要if abs(cx) < abs(dx) or abs(cy) < abs(dy):
BTW:如果target
没有更改位置或bullet
没有更改angle
,那么您只能计算一次cx,cy
。
import math
def move(x1, y1, x2, y2, speed):
# distance
dx = x2 - x1
dy = y2 - y1
# angle
angle = math.atan2(dx, dy)
#print(math.degrees(angle))
#
cx = speed * math.sin(angle)
cy = speed * math.cos(angle)
#print(cx, cy)
# if distance is smaller then `cx/cy`
# then you have to stop in target.
if abs(cx) < abs(dx) or abs(cy) < abs(dy):
# move bullet to new position
x1 += cx
y1 += cy
in_target = False
else:
# move bullet to target
x1 = x2
y1 = y2
in_target = True
return x1, y1, in_target
#---
speed = 10
# bullet position
x1 = 10
y1 = 0
# taget possition
x2 = 120
y2 = 10
print('x: {:6.02f} | y: {:6.02f}'.format(x1, y1))
in_target = False
while not in_target:
x1, y1, in_target = move(x1, y1, x2, y2, speed)
print('x: {:6.02f} | y: {:6.02f}'.format(x1, y1))
结果
x: 10.00 | y: 0.00
x: 19.96 | y: 0.91
x: 29.92 | y: 1.81
x: 39.88 | y: 2.72
x: 49.84 | y: 3.62
x: 59.79 | y: 4.53
x: 69.75 | y: 5.43
x: 79.71 | y: 6.34
x: 89.67 | y: 7.24
x: 99.63 | y: 8.15
x: 109.59 | y: 9.05
x: 119.55 | y: 9.96
x: 120.00 | y: 10.00
BTW:如果target
没有更改位置或bullet
没有更改angle
,那么您只能计算一次cx,cy
。