无法显示超过1个精灵 - PyGame

时间:2017-05-19 05:07:44

标签: python pygame

问题

我的问题是,我有一款游戏在比赛开始时会产生8个障碍。问题是当我遍历障碍列表并更新sprite组时,它只生成1个精灵。

我想要发生什么

当游戏加载时,我希望8个方格以随机的速度从窗口顶部向下飞,并从随机位置开始。

目前正在发生什么

目前,当游戏加载时,屏幕上只有一个正方形落下。

PYthon代码

OBSTICLES_AMOUNT = 8

class Obstacle(pygame.sprite.Sprite):
def __init__(self):
    pygame.sprite.Sprite.__init__(self)

    self.image = pygame.Surface((30, 30))
    self.image.fill(BLUE)
    self.rect = self.image.get_rect()
    self.rect.x = random.randrange(0, WIDTH - self.rect.width)
    self.rect.y = random.randrange(-100, -40)
    self.velY = 6

def animate(self):
    self.rect.y += self.velY

class Game(pygame.sprite.Sprite):
def __init__(self):
    pygame.sprite.Sprite.__init__(self)

    pygame.init()
    pygame.mixer.init()

    self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption(TITLE)
    self.running = True
    self.clock = pygame.time.Clock()

    self.obstaclesList = []
    self.allSprites = pygame.sprite.Group()
    self.obstacles = pygame.sprite.Group()

def new(self):
    # create a new game

    # add obstacles to list
    for i in range(OBSTICLES_AMOUNT):
        self.obstacle = Obstacle()
        self.obstaclesList.append(self.obstacle)

    # make new sprite using list
    for i in self.obstaclesList:
        self.allSprites.add(i)
        self.obstacles.add(i)

    self.gameLoop()

def gameLoop(self):
    # main game loop
    while self.running:
        self.draw()

def draw(self):
    self.screen.fill(WHITE)

    self.allSprites.draw(self.screen)
    for sprites in self.obstaclesList:
        sprites.update()

    self.allSprites.update()

1 个答案:

答案 0 :(得分:0)

您的代码由

修复
  • 添加缺少的导入
  • 添加缺失的常量
  • animate
  • 中将update重命名为Obstacle
  • 在绘制后调用pygame.display.update
  • 使用Clock限制帧速率
  • 添加事件处理
  • 添加代码以创建Game实例

进一步改进:

  • 如果您已经obstaclesList
  • ,则无需obstacles
  • 您可以将Groups直接传递给Sprite __init__函数
  • 删除屏幕上不再显示的Sprite

以下是代码:

import pygame
import random

OBSTICLES_AMOUNT = 8
WIDTH, HEIGHT = 800,600
TITLE='some game of falling stuff'
BLUE = pygame.color.THECOLORS['blue']
WHITE = pygame.color.THECOLORS['white']

class Obstacle(pygame.sprite.Sprite):
    def __init__(self, *args):
        pygame.sprite.Sprite.__init__(self, *args)

        self.image = pygame.Surface((30, 30))
        self.image.fill(BLUE)
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(0, WIDTH - self.rect.width)
        self.rect.y = random.randrange(-100, -40)
        self.velY = 6

    def update(self):
        self.rect.y += self.velY
        if self.rect.y > HEIGHT:
            self.kill()

class Game(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        pygame.init()
        pygame.mixer.init()
        self.clock = pygame.time.Clock()
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption(TITLE)
        self.running = True
        self.clock = pygame.time.Clock()

        self.allSprites = pygame.sprite.Group()
        self.obstacles = pygame.sprite.Group()

    def new(self):
        # create a new game

        # add obstacles to list
        for i in range(OBSTICLES_AMOUNT):
            Obstacle(self.allSprites, self.obstacles)

        while self.running:
            self.allSprites.update()
            for e in pygame.event.get():
                if e.type == pygame.QUIT:
                    self.running = False
            self.draw()
            self.clock.tick(60)

    def draw(self):

        self.screen.fill(WHITE)

        self.allSprites.draw(self.screen)
        for sprites in self.obstacles:
            sprites.update()

        pygame.display.update()

if __name__ == '__main__':
    Game().new()