python - 如何对同一类的多个对象进行动画处理?

标签 python pygame

我想要 3 个工作人员(Worker 类的对象)以不同的随机方向和不同的速度在屏幕上移动。换句话说,我只想运行这样的东西: worker1.makeRandomStep(x,y,1)worker2.makeRandomStep(x,y,2)worker1.makeRandomStep(x,y,3).

这是我当前的代码,其中只有一个 worker:

import pygame, random
import sys

WHITE = (255, 255, 255)
GREEN = (20, 255, 140)
GREY = (210, 210 ,210)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)

SCREENWIDTH=1000
SCREENHEIGHT=578  


class Background(pygame.sprite.Sprite):
    def __init__(self, image_file, location):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location



class Worker(pygame.sprite.Sprite):
    def __init__(self, image_file, location):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.direction = 2
        self.rect.left, self.rect.top = location


    def makeRandomStep(self,x,y,step):
        # there is a less than 1% chance every time that direction is changed
        if random.uniform(0,1)<0.005:
            self.direction = random.randint(1,4)

        if self.direction == 1:
            return x, y+step # up
        elif self.direction == 2:
            return x+step, y # right
        elif self.direction == 3:
            return x, y-step # down
        elif self.direction == 4:
            return x-step, y # left
        else:
            return x, y # stop


pygame.init()

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

worker = Worker("worker.png", [0,0])
w_x = worker.rect.left
w_y = worker.rect.top

bg = Background("background.jpg", [0,0])

#sprite_group = pygame.sprite.Group()
#sprite_group.add(worker)

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

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

        # Draw floor layout 
        screen.blit(pygame.transform.scale(bg.image, (SCREENWIDTH, SCREENHEIGHT)), bg.rect)

        # Draw geo-fences
        geofence1 = pygame.draw.rect(screen, GREEN, [510,150,75,52])
        geofence2 = pygame.draw.rect(screen, GREEN, [450,250,68,40])

        w_x,w_y = worker.makeRandomStep(w_x,w_y,1)

        # Worker should not go outside the screen area
        if (w_x + worker.rect.width > SCREENWIDTH): w_x = SCREENWIDTH - worker.rect.width
        if (w_x < 0): w_x = 0
        if (w_y + worker.rect.height > SCREENHEIGHT): w_y = SCREENHEIGHT - worker.rect.height
        if (w_y < 0): w_y = 0      
        worker.rect.clamp_ip(screen_rect)

        screen.blit(worker.image, (w_x,w_y))

        # Refresh Screen
        pygame.display.flip()

        #sprite_group.update()
        #sprite_group.draw(screen)

        #Number of frames per secong e.g. 60
        clock.tick(20)

pygame.display.quit()
pygame.quit()
quit()

我不太清楚应该如何实现我的目标。我正在考虑使用 sprite_group,但我误解了如何正确更新 while 循环内的所有 Sprite (工作人员)。 非常感谢任何帮助。谢谢。

最佳答案

您必须将所有工作逻辑移至 Worker 类中(设置随机值、更新位置等)。然后创建多个实例。

这是一个正在运行的示例。请注意注释的解释:

import pygame, random
import sys

WHITE = (255, 255, 255)
GREEN = (20, 255, 140)
GREY = (210, 210 ,210)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)

SCREENWIDTH=1000
SCREENHEIGHT=578  


class Background(pygame.sprite.Sprite):
    def __init__(self, image_file, location, *groups):
        # we set a _layer attribute before adding this sprite to the sprite groups
        # we want the background to be actually in the back
        self._layer = -1
        pygame.sprite.Sprite.__init__(self, groups)
        # let's resize the background image now and only once
        # always call convert() (or convert_alpha) after loading an image
        # so the surface will have to correct pixel format
        self.image = pygame.transform.scale(pygame.image.load(image_file).convert(), (SCREENWIDTH, SCREENHEIGHT))
        self.rect = self.image.get_rect(topleft=location)

# we do everthing with sprites now, because that will make our live easier
class GeoFence(pygame.sprite.Sprite):
    def __init__(self, rect, *groups):
        # we set a _layer attribute before adding this sprite to the sprite groups
        self._layer = 0
        pygame.sprite.Sprite.__init__(self, groups)
        self.image = pygame.surface.Surface((rect.width, rect.height))
        self.image.fill(GREEN)
        self.rect = rect

class Worker(pygame.sprite.Sprite):
    def __init__(self, image_file, location, *groups):
        # we set a _layer attribute before adding this sprite to the sprite groups
        # we want the workers on top
        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)
        # let's call this handy function to set a random direction for the worker
        self.change_direction()
        # speed is also random
        self.speed = random.randint(2, 4)

    def change_direction(self):
        # let's create a random vector as direction, so we can move in every direction
        self.direction = pygame.math.Vector2(random.randint(-100, 100), random.randint(-100, 100))

        # we don't want a vector of length 0, because we want to actually move
        # it's not enough to account for rounding errors, but let's ignore that for now
        while self.direction.length() == 0:
            self.direction = pygame.math.Vector2(random.randint(-100, 100), random.randint(-100, 100)) 

        # always normalize the vector, so we always move at a constant speed at all directions
        self.direction = self.direction.normalize()

    def update(self, screen):
        # there is a less than 1% chance every time that direction is changed
        if random.uniform(0,1)<0.005:
            self.change_direction()

        # now let's multiply our direction with our speed and move the rect
        vec = [int(v) for v in self.direction * self.speed]
        self.rect.move_ip(*vec)

        # if we're going outside the screen, move back and change direction
        if not screen.get_rect().contains(self.rect):
            self.change_direction()
        self.rect.clamp_ip(screen.get_rect())

pygame.init()

# currently, one group would be enough
# but if you want to use some collision handling in the future
# it's best to group all sprites into special groups (no pun intended)
all_sprites = pygame.sprite.LayeredUpdates()
workers = pygame.sprite.Group()
fences = 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)

# create multiple of these green thingies
for rect in (pygame.Rect(510,150,75,52), pygame.Rect(450,250,68,40)):
    GeoFence(rect, all_sprites, fences)

# and the background
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

    # see how clean our main loop is
    # just calling update and draw on the all_sprites group
    all_sprites.update(screen)
    all_sprites.draw(screen)

    pygame.display.flip()

    clock.tick(20)

enter image description here

关于python - 如何对同一类的多个对象进行动画处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51722266/

相关文章:

python - 十六进制颜色代码究竟是如何工作的?/如何实现像热图这样的东西

python - 如何使用 python 从网站获取 XML 文件?

python - 从嵌套字典构建类

python - "variable"可能未定义或从 star 导入 : tkinter 定义

python - 如何使用着色器、pygame 和 pyopengl 在多于 1 个轴上执行 OpenGL 旋转

python - 在 Python 中运行游戏循环时多线程显示游戏分数?

python - 寻找一种将矩形拟合到轮廓点的方法

python - 当您移动/拖动窗口时,为什么 pygame 在 pygame.event.get() 卡住?

python - 如何在 Pygame 中每次重置游戏时初始化新对象

python - 在矩阵中生成数独谜题而不是绘制 GUI