如何在pygame中进行流畅的移动

问题描述 投票:0回答:3

我和我的一个朋友刚刚开始在 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))
python pygame
3个回答
3
投票

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

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

对于更复杂的解决方案,您必须计算点到目标的欧几里得距离。 使用

pygame.math.Vector2
进行计算。

计算跟随者与精灵之间的距离以及从 (

follower_x
,
follower_y
) 到 (
mainsprite_x
,
mainsprite_y
) 的单位方向向量。 单位向量可以通过将方向向量除以距离或通过标准化(
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
并移动到精灵的 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()

0
投票

我不建议使用 repl.it,因为它运行速度非常慢。

你的代码应该看起来更像这样:

    while True:
        screen.fill((0,0,0))

        stuff happens

        if x2>x:
            x2-=charspeed
        elif x2<x:
            x2+=charspeed
        elif y2>y:
            y2 -= charspeed
        elif y2<y:
            y2 += charspeed
        screen.blit(pmc, (x2-46, y2-184))
        pygame.display.flip()

直到显示一直移动到 (x,y) 时才更新显示


0
投票

我的名字是詹姆斯·皮克森。我今年 7 岁,住在英国。我住在伦敦。这个问题的答案是: 我不知道,因为我才7岁!!!

© www.soinside.com 2019 - 2024. All rights reserved.