首页 > 解决方案 > 如何在 PyGame 中模拟真实的速度?

问题描述

worker我想为所有对象定义一个真实的步行速度(米每秒) 。我该怎么做?

目前我speed在代码中有参数。此参数初始化为randint(2, 4)- 2 到 4 之间的随机整数。但此值未映射到实际比例。

就我而言,众所周知 1 像素是指0.038米。此外,我们知道人类步行和跑步的速度:

METERS_PER_PIXEL = 0.038370147
AVG_WALK_SPEED = 1.4 # meters per second
MIN_RUN_SPEED = 2.0  # meters per second
MAX_RUN_SPEED = 5.0  # meters per second

有了这些常量,我想再介绍一个名为human_speed. 例如,我想初始化human_speed等于。如何转换为可以实现正确的动画?我需要一些后端计算。worker1.41.4speedpygamehuman_speed

import pygame, random
import sys

WHITE = (255, 255, 255)
GREEN = (20, 255, 140)
GREY = (210, 210 ,210)

SCREENWIDTH=1000
SCREENHEIGHT=578  


class Background(pygame.sprite.Sprite):
    def __init__(self, image_file, location, *groups):
        self._layer = -1
        pygame.sprite.Sprite.__init__(self, groups)

        self.image = pygame.transform.scale(pygame.image.load(image_file).convert(), (SCREENWIDTH, SCREENHEIGHT))
        self.rect = self.image.get_rect(topleft=location)


class Worker(pygame.sprite.Sprite):
    def __init__(self, image_file, location, *groups):

        self._layer = 1
        pygame.sprite.Sprite.__init__(self, groups)
        self.image = pygame.transform.scale(pygame.image.load(image_file).convert_alpha(), (40, 40))
        self.rect = self.image.get_rect(topleft=location)
        self.change_direction()
        self.speed = random.randint(2, 4)


    def change_direction(self):

        self.direction = pygame.math.Vector2(random.randint(-100, 100), random.randint(-100, 100))

        while self.direction.length() == 0:
            self.direction = pygame.math.Vector2(random.randint(-100, 100), random.randint(-100, 100)) 

        self.direction = self.direction.normalize()


    def update(self, screen):

        if random.uniform(0,1)<0.005:
            self.change_direction()

        vec = [int(v) for v in self.direction * self.speed]
        self.rect.move_ip(*vec)

        if not screen.get_rect().contains(self.rect):
            self.change_direction()
        self.rect.clamp_ip(screen.get_rect())

pygame.init()

all_sprites = pygame.sprite.LayeredUpdates()
workers = pygame.sprite.Group()

screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
pygame.display.set_caption("TEST")

# create multiple workers
for pos in ((0,0), (100, 100), (200, 100)):
    Worker("worker.png", pos, all_sprites, workers)

Background("background.jpg", [0,0], all_sprites)

carryOn = True
clock = pygame.time.Clock()

while carryOn:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            carryOn = False

    all_sprites.update(screen)
    all_sprites.draw(screen)

    pygame.display.flip()

    clock.tick(20)

标签: pythonpython-3.xpygame

解决方案


如果你1.4每秒计算米,而0.038米是1像素,那么你1.4 / 0.038 = 36.8每秒计算像素。

如果您的帧速率为 20,这意味着您的普通工人应该移动1/20这些36.8像素,从而产生1.84每帧的像素。

这是容易的部分,因为存在一些问题,例如:

  • 如果将您的位置存储Sprite在 aRect中,则会出现舍入错误,因为 aRect只能处理整数,不能处理浮点数。但是您可以创建一个Vector2来存储精确位置,并在每次移动时将坐标四舍五入并将它们存储在Rect.

  • 如果仅依靠固定目标帧率来计算要移动的距离,则可能会出现不一致的移动。通常您使用称为 delta time 的东西,这基本上意味着您跟踪每帧运行所花费的时间,并将该值传递给您的update函数并使用它来计算距离。


推荐阅读