python - 如何在 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
等于。如何转换为可以实现正确的动画?我需要一些后端计算。worker
1.4
1.4
speed
pygame
human_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)
解决方案
如果你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
函数并使用它来计算距离。
推荐阅读
- go - 从golang调用时无法从调用GetConfiguration获取itemCategory信息
- python - Predict_proba() 的随机森林分类器结果与 predict() 不匹配?
- gcc - RISC-V 压缩指令:可以强制编译器在 32 位边界上对齐 32 位指令吗?
- r - 在 R 自动化中将数据附加到具有当前日期的现有数据
- vb.net - 如何在 VB.NET 中循环遍历列表
- pointers - 没有返回关键字的对象返回
- powershell - 如何将 prnmngr 的输出转换为自定义对象?
- google-cloud-platform - 在 Google Cloud Composer 中运行气流 cli
- vb.net - 无法在 VB 中为 ______ 创建清单资源名称
- javascript - IBM API Connect:编码和解码 URL