【发布时间】:2015-11-18 15:49:14
【问题描述】:
我正在使用 PyGame 创建自动移动的单位。
可以为这些单元指定一个包含两个元素 (x, y) 的元组类型的目标,并且这些单元具有静态速度。
我正在调用units.update(dt),其中dt 是自上次更新以来的时间(以毫秒为单位)。我需要计算自上次更新以来单位移动了多少。这是我的Unit 课程:
class Unit(pygame.sprite.Sprite):
def __init__(self, image, speed):
self.image = pygame.image.load(image)
self.rect = self.image.get_rect() # This rect contains x and y for the Unit
self.speed = speed
self.destination = None
def update(self, dt):
if self.destination is not None and self.speed > 0:
dist = self.speed * dt
这样我可以得到斜边 (dist),但我需要 self.rect.x 和 self.rect.y 的单独指示。如何从dist 获取dx 和dy?
另外,这是我的main.py:
import unit
import pygame
pygame.init()
display = pygame.display.set_mode((960, 720))
units = pygame.sprite.Group()
my_unit = unit.Unit('my_image.png', 3)
units.add(my_unit)
my_unit.destination = (150, 150)
clock = pygame.time.Clock()
running = True
while running:
dt = clock.tick(30)
units.update(dt)
display.fill((0, 0, 0))
units.draw(display)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
【问题讨论】:
标签: python pygame game-physics