【问题标题】:How to animate multiple objects of the same class?如何为同一类的多个对象设置动画?
【发布时间】:2018-08-07 18:52:21
【问题描述】:

我想让 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 循环内的所有精灵(工人)。 非常感谢任何帮助。谢谢。

【问题讨论】:

    标签: python pygame


    【解决方案1】:

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

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

    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)
    

    【讨论】:

    • 酷!唯一的问题是我看到黑色矩形在屏幕上移动,而不是我的图像worker.png。背景图像已正确加载,但移动对象图像看起来像黑盒子。你知道怎么解决吗?
    • @ScalaBoy 我猜图像是一个带有 alpha 通道的 png,所以将 convert() 更改为 convert_alpha(),其中图像在 Worker 类中加载
    • 你检查对象是否已经停止使用while self.direction.length() == 0:
    • @ScalaBoy 是的,这意味着随机向量是(0, 0),这意味着根本没有运动。
    • @好的,我明白了。谢谢。我在这里发布了另一个相关问题:stackoverflow.com/questions/51724335/… 如果您有兴趣提供帮助,我会感谢您的专业眼光。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多