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
. 例如,我想初始化
人的速度
属于
工人
等于
1.4
. 我怎样才能转换
到
速度
以便
pygame
我需要
人的速度
一些后端计算。
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)