是否可以在 Pygame 中实现对象到给定坐标的逐渐移动? [英] Is it possible to implement gradual movement of an object to given coordinates in Pygame?

查看:58
本文介绍了是否可以在 Pygame 中实现对象到给定坐标的逐渐移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试过的示例代码:

x[0] = 10y[0]=10x[1] = 40y[1]=40宽度=10高度=10pygame.draw.rect(win,(0,0,255),(x[1],y[1],width,height))pygame.draw.rect(win,(0,0,255),(x[0],y[0],width,height))

解决方案

你必须稍微改变应用程序循环中的位置.

定义开始位置(start)、结束位置(end)和速度(speed):

start = 10, 10结束 = 40, 40速度 = 1

初始化当前位置.在应用程序循环中计算从当前位置到结束位置(dx, dy)的向量,并以目标方向的速度改变当前位置:

pos = start[:]运行时:# [...]dx = 结束 [0] - 位置 [0]dy = 结束[1] - 位置[1]dist = math.sqrt(dx*dx + dy*dy)如果距离 >速度:pos = pos[0] + dx*speed/dist, pos[1] + dy*speed/dist

注意,

导入pygame导入数学pygame.init()赢 = pygame.display.set_mode((500, 500))时钟 = pygame.time.Clock()x = [60, 140, 140, 60]y = [60, 140, 60, 140]宽度,高度 = 10, 10速度 = 1当前_i = 0pos = x[current_i], y[current_i]运行 = 真运行时:时钟滴答(60)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误开始 = x[current_i], y[current_i]结束 = x[current_i % len(x)], y[current_i % len(y)],dx = 结束 [0] - 位置 [0]dy = 结束[1] - 位置[1]dist = math.hypot(dx, dy)如果距离 >速度:pos = pos[0] + dx*speed/dist, pos[1] + dy*speed/dist别的:位置 = 结束 [:]current_i = current_i + 1 如果 current_i 

here is the sample code that i have tried:

x[0] = 10
y[0]=10
x[1] = 40
y[1]=40
width=10
height=10
pygame.draw.rect(win,(0,0,255),(x[1],y[1],width,height))
pygame.draw.rect(win,(0,0,255),(x[0],y[0],width,height))

解决方案

You have to slight change the position in the application loop.

Define a start position (start), end position (end) and velocity (speed):

start = 10, 10
end = 40, 40
speed = 1

Init the current position. Compute the vector form the current position to the end position (dx, dy) in the application loop and change the current position by the speed in the direction to the target:

pos = start[:]
while run:
    # [...]

    dx = end[0] - pos[0]
    dy = end[1] - pos[1]
    dist = math.sqrt(dx*dx + dy*dy)
    if dist > speed:
        pos = pos[0] + dx*speed/dist, pos[1] + dy*speed/dist

Note, math.hypot computes the Euclidean distance and (dx/dist, dy/dist) is a Unit vector (a unit vector has length 1).

Draw the object at the current position (pos) in every frame:

pygame.draw.rect(win,(0,0,255),(round(pos[0]), round(pos[1]), width, height))

If you have a list of positions:

x = [60, 140, 140, 60]
y = [60, 140, 60,  140]

Then start and end have to be set from the positions in the list. Use a list index current_i to track the current start position. Increment the index, if the object has reached the current target position (end).
See the example:

import pygame
import math

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

x = [60, 140, 140, 60]
y = [60, 140, 60,  140]

width, height = 10, 10
speed = 1
current_i = 0
pos = x[current_i], y[current_i]

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

    start = x[current_i], y[current_i]
    end = x[current_i % len(x)], y[current_i % len(y)], 
    dx = end[0] - pos[0]
    dy = end[1] - pos[1]
    dist = math.hypot(dx, dy)
    if dist > speed:
        pos = pos[0] + dx*speed/dist, pos[1] + dy*speed/dist
    else:
        pos = end[:]
        current_i = current_i + 1 if current_i < len(x)-1 else 0

    win.fill(0)
    pygame.draw.rect(win,(0,0,255),(round(pos[0]), round(pos[1]), width, height))
    pygame.display.flip()

这篇关于是否可以在 Pygame 中实现对象到给定坐标的逐渐移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆