我如何才能使一个块跟随pygame中的另一个块 [英] How can i make a block follow another block in pygame
问题描述
我有两个模块,一个由用户控制.当我移动积木时,我希望另一个积木跟随我.我试图做这样的事情
def follow():距离= math.hypot(abs(m.x-p.x),abs(m.y-p.y))angle_radians = math.atan2(abs(m.y-p.y),abs(m.x-p.x))如果距离!= 0:p.y + = math.sin(angle_radians)p.x + = math.cos(angle_radians)
但是,块最终以与我完全相反的方向移动.任何帮助将不胜感激.
要使算法起作用,您必须对浮点数进行运算.如果 m
和 p
为code> pygame.Rect 对象,则该算法将不起作用, pygame.Rect
使用整数进行运算,小数部分会丢失.
注意 math.sin(angle_radians)
和 math.cos(angle_radians)
是< = 1.
这意味着您必须将对象的位置存储在单独的变量中.假设您有浮点坐标( mx
, my
)和( py
, py
)
您必须从( mx
, my
)到( px
, py
).
可以通过将向量从( mx
, my
)除以( px
, py
)来找到单位向量按其长度.
向量的长度可以通过欧几里德距离来计算.
最后,将向量乘以不大于点之间距离的比例( step
),并将其添加到位置.例如:
stepDist = 1#向量从(`mx`,`my`)到(`px`,`py`)dx,dy = p.y-mx,py-px#[欧几里得距离](https://en.wikipedia.org/wiki/Euclidean_distance)len = math.sqrt(dx * dx + dy * dy)如果len>0:#[单位向量](https://en.wikipedia.org/wiki/Unit_vector)ndx,ndy = dx/len,dy/len#最小步长和到目标的距离步长=分钟(len,stepDist)# 向前一步px + = ndx *步骤py + = ndy *步骤
如果需要 pygame.Rect
对象,则可以设置矩形的位置.例如:
m.topleft = round(mx),round(my)p.topleft =圆(px),圆(py)
但是您不必分别将位置存储在( mx
, my
)( px
, py
)中).如果分别执行 mx,my = m.topleft
和 px,py = p.topleft
,则算法将崩溃,因为小数部分丢失了.>
I have two blocks, one is controlled by the user. When i move my block, i want the other block to follow me. I tried doing something like this
def follow():
distance = math.hypot(abs(m.x - p.x), abs(m.y - p.y))
angle_radians = math.atan2(abs(m.y - p.y), abs(m.x - p.x))
if distance != 0:
p.y += math.sin(angle_radians)
p.x += math.cos(angle_radians)
However, the block ends up moving in the complete opposite direction to me . Any help would be appreciated.
To make the algorithm work, you have to operate with floating point numbers. If m
and p
are pygame.Rect
objects, then the algorithm won't work, pygame.Rect
operates with integral numbers and the fraction part gets lost.
Note math.sin(angle_radians)
and math.cos(angle_radians)
is <= 1.
That means you have to store the positions of the objects in separate variables. Let's assume you have the floating point coordinates (mx
, my
) and (py
, py
)
You have to find the Unit vector from (mx
, my
) to (px
, py
).
The unit vector can be found by dividing the vector from (mx
, m.y
) to (px
, py
) by its length.
The length of a vector can be computed by the Euclidean distance.
Finally multiply the vector by a scale (step
) that is not greater than the distance between the points and add it to the position. e.g:
stepDist = 1
# vector from (`mx`, `my`) to (`px`, `py`)
dx, dy = p.y - mx, py - px
# [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
len = math.sqrt(dx*dx + dy*dy)
if len > 0:
# [Unit vector](https://en.wikipedia.org/wiki/Unit_vector)
ndx, ndy = dx/len, dy/len
# minimum of step size and distance to target
step = min(len, stepDist)
# step forward
px += ndx * step
py += ndy * step
If a pygame.Rect
object is of need, then the position of the rectangle can be set. e.g:
m.topleft = round(mx), round(my)
p.topleft = round(px), round(py)
But not you have to store the positions in (mx
, my
) respectively (px
, py
). If you would do mx, my = m.topleft
respectively px, py = p.topleft
, then the algorithm will break down, because the fraction component gets lost.
这篇关于我如何才能使一个块跟随pygame中的另一个块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!