gpt4 book ai didi

python - 如何在pygame中平滑移动

转载 作者:行者123 更新时间:2023-12-05 04:58:16 29 4
gpt4 key购买 nike

我和我的一个 friend 刚刚开始学习在 repl.it 上使用 pygame 进行编程,对于我们的第一个“真实”项目,我们想制作一个像点击冒险这样的老派游戏。

但是,我们在角色移动方面遇到了问题,如果我们单击屏幕上的某处,角色只是“传送”到那里,但我们希望它看起来尽可能平滑。

所以基本上,我们想要摆脱角色的“瞬移”,而是从角色当前位置到鼠标位置进行平滑的逐帧过渡。

我们已经尝试减慢 while 循环,以便我们可以在每次执行 while 循环时投影角色,但这只会导致整个站点崩溃,我们还尝试在 repl.it 之外进行,以防万一是网站的问题,但它在那里也不起作用。

#PMC = Character
#mpos = the mouse position
#mstate= the state of the mouse buttons (0 if nothing is pressed, 1 if a mouse
#button is pressed)
#charspeed = the speed at which the character moves (=1px)
```
#---PMC movement when mouse click-----------------------
#---x,y = mpos x2,y2 = characterpos
if mstate == (1,0,0):
#print('x: ', x, ' y: ', y, ' x2: ', x2, ' y2: ', y2) #debugging_positions


while x2 != x:
if x2>x:
x2-=charspeed
screen.blit(pmc, (x2-46, y2-184))
if x2<x:
x2+=charspeed
screen.blit(pmc, (x2-46, y2-184))

while y2 != y:
if y2>y:
y2 -= charspeed
screen.blit(pmc, (x2-46, y2-184))
if y2<y:
y2 += charspeed
screen.blit(pmc, (x2-46, y2-184))

最佳答案

你有一个游戏循环,所以使用它。只需在每一帧中将角色移动到特定位置即可。例如,每帧按 step 移动角色:

step = 1

if x2 + step <= x:
x2 += step
elif x2 - step >= x:
x2 -= step
else:
x2 = x

if y2 + step <= y:
y2 += step
elif y2 - step >= y:
y2 -= step
else:
y2 = y

对于更复杂的解决方案,您必须计算 Euclidean distance形成指向目标的点。使用 pygame.math.Vector2用于计算。

计算follower和sprite之间的距离和单位方向向量从(follower_x, follower_y)到(mainsprite_x, mainsprite_y)。 Unit Vector可以通过将方向向量除以距离或通过归一化 (normalize()) 方向向量来计算:

target_vector = Vector2(mainsprite_x, mainsprite_y)
follower_vector = Vector2(follower_x, follower_y)

distance = follower_vector.distance_to(target_vector)
direction_vector = target_vector - follower_vector
if distance > 0:
direction_vector /= distance

现在你可以定义一个精确的step_distance并移动到 Sprite 的follower int方向:

if distance > 0:
new_follower_vector = follower_vector + direction_vector * step_distance.

定义一个maximum_distance 和一个minimum_distance。最小步距为:

min_step = max(0, distance - maximum_distance)

最大步距为

max_step = distance - minimum_distance

放在一起:

minimum_distance    = 0
maximum_distance = 10000
target_vector = Vector2(mainsprite_x, mainsprite_y)
follower_vector = Vector2(follower_x, follower_y)
new_follower_vector = Vector2(follower_x, follower_y)

distance = follower_vector.distance_to(target_vector)
if distance > minimum_distance:
direction_vector = (target_vector - follower_vector) / distance
min_step = max(0, distance - maximum_distance)
max_step = distance - minimum_distance
step_distance = min_step + (max_step - min_step) * LERP_FACTOR
new_follower_vector = follower_vector + direction_vector * step_distance

最小示例: repl.it/@Rabbid76/PyGame-FollowMouseSmoothly

import pygame

LERP_FACTOR = 0.05
minimum_distance = 25
maximum_distance = 100

def FollowMe(pops, fpos):
target_vector = pygame.math.Vector2(*pops)
follower_vector = pygame.math.Vector2(*fpos)
new_follower_vector = pygame.math.Vector2(*fpos)

distance = follower_vector.distance_to(target_vector)
if distance > minimum_distance:
direction_vector = (target_vector - follower_vector) / distance
min_step = max(0, distance - maximum_distance)
max_step = distance - minimum_distance
step_distance = min_step + (max_step - min_step) * LERP_FACTOR
new_follower_vector = follower_vector + direction_vector * step_distance

return (new_follower_vector.x, new_follower_vector.y)

pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

follower = (100, 100)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

player = pygame.mouse.get_pos()
follower = FollowMe(player, follower)

window.fill(0)
pygame.draw.circle(window, (0, 0, 255), player, 10)
pygame.draw.circle(window, (255, 0, 0), (round(follower[0]), round(follower[1])), 10)
pygame.display.flip()

关于python - 如何在pygame中平滑移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64087982/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com