我和我的一个朋友刚刚开始学习在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))
答案 0 :(得分:2)
您有一个游戏循环,请使用它。只需在每个帧中将角色移动一定位置即可。例如,每帧将字符移动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_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
并移动到精灵的跟随者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()
答案 1 :(得分: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)之前,您都不会更新显示器