Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

python - Is it possible to implement gradual movement of an object to given coordinates in Pygame?

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))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...