【问题标题】:Could not draw the pillars of flappy bird using sprite in pygame无法在pygame中使用精灵绘制飞扬的小鸟的柱子
【发布时间】:2018-02-11 09:52:01
【问题描述】:

我正在使用 pygame 制作一个飞扬的小鸟克隆游戏。我想用 Sprite.draw 画柱子。我创建了一个Pillar 类,并在屏幕左侧用两个矩形p_upperp_lower 对其进行了初始化,借助精灵的update 函数朝向右侧。但屏幕只显示 p_lower 柱子。有人可以帮忙吗?

class Pillar(pygame.sprite.Sprite):
    # the "h" parameter is height of upper pillar upto gap
    # "w" is the width of the pillar
    # pillar is coming from left to right
    def __init__(self, w, h, gap):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface((w, h))
        self.image.fill(green)
        self.p_upper = self.rect = self.image.get_rect()
        self.p_upper.topleft = (-w, 0)

        self.image = pygame.Surface((w, HEIGHT - (h + gap)))
        self.image.fill(green)
        self.p_lower = self.rect = self.image.get_rect()
        self.p_lower.topleft = (-w, h + gap)

    def update(self):
        self.p_upper.x += 1
        self.p_lower.x += 1

【问题讨论】:

    标签: python pygame sprite


    【解决方案1】:

    因为以下两行:

    self.p_upper = self.rect = self.image.get_rect()
    

    还有……

    self.p_lower = self.rect = self.image.get_rect()
    

    它们都获取了对 self.rect 的相同引用。第一行运行并将矩形引用分配给p_upper。然后将 same 引用分配给p_lower。因为它是同一个参考,所以当您更新下部矩形的位置时,您实际上是在更新两者。

    【讨论】:

    • 但是在这两行中 self.image 是不同的,所以 self.rect 也应该是不同的。
    • 嗯,刚刚注意到了。我可以看看你在哪里画这些的代码吗?您创建两个图像然后立即丢弃其中一个(以及覆盖self.rect)仍然令人怀疑,我的直觉是它最终是相关的。
    【解决方案2】:

    使用由两个矩形和图像组成的精灵并不是解决此问题的好方法。我建议用自己的图像和矩形创建两个单独的精灵。要同时创建两个精灵实例并将它们添加到精灵组中,您可以编写一个简短的函数,如下例所示:

    import pygame as pg
    from pygame.math import Vector2
    
    
    green = pg.Color('green')
    HEIGHT = 480
    
    class Pillar(pg.sprite.Sprite):
    
        def __init__(self, x, y, w, h):
            pg.sprite.Sprite.__init__(self)
            self.image = pg.Surface((w, h))
            self.image.fill(green)
            self.rect = self.image.get_rect(topleft=(x, y))
    
        def update(self):
            self.rect.x += 1
    
    
    def create_pillars(w, h, gap, sprite_group):
        sprite_group.add(Pillar(0, 0, w, h-gap))
        sprite_group.add(Pillar(0, HEIGHT-(h+gap), w, h+gap))
    
    
    def main():
        screen = pg.display.set_mode((640, 480))
        clock = pg.time.Clock()
        all_sprites = pg.sprite.Group()
        create_pillars(50, 170, 0, all_sprites)
    
        done = False
    
        while not done:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    done = True
                elif event.type == pg.KEYDOWN:
                    if event.key == pg.K_a:
                        create_pillars(50, 170, 15, all_sprites)
                    elif event.key == pg.K_s:
                        create_pillars(50, 170, 30, all_sprites)
                    elif event.key == pg.K_d:
                        create_pillars(50, 100, -60, all_sprites)
    
            all_sprites.update()
            screen.fill((30, 30, 30))
            all_sprites.draw(screen)
    
            pg.display.flip()
            clock.tick(30)
    
    
    if __name__ == '__main__':
        pg.init()
        main()
        pg.quit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多