【发布时间】:2021-05-06 19:51:24
【问题描述】:
我一直在尝试创建一个精灵创建器,但我注意到 pygame 窗口在所有这些精灵都被创建之前不会加载,我想知道两件事:
- 为什么它只在所有这些 sprite 创建后才加载屏幕
- 如何在不进行太多更改的情况下解决此问题
代码:
#!/usr/bin/env python3
import random
import pygame
import time
display_width = 1280
display_height = 720
rotation = random.randint(0, 359)
size = random.random()
pic = pygame.image.load('assets/meteor.png')
pygame.init()
clock = pygame.time.Clock()
running = True
class Meteor(pygame.sprite.Sprite):
def __init__(self, x=0, y=0):
pygame.sprite.Sprite.__init__(self)
self.rotation = random.randint(0, 359)
self.size = random.randint(1, 2)
self.image = pic
self.image = pygame.transform.rotozoom(self.image, self.rotation, self.size)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
all_meteors = pygame.sprite.Group()
# completely random spawn
for i in range(5):
new_x = random.randrange(0, display_width)
new_y = random.randrange(0, display_height)
all_meteors.add(Meteor(new_x, new_y))
time.sleep(2) # this*5 = time for screen to show up
主要:
import pygame
import meteors
pygame.init()
while True:
meteors.all_meteors.update()
meteors.all_meteors.draw(screen)
pygame.display.update()
我不知道为什么它会在创建 pygame 窗口之前优先创建精灵,并且它阻止我创建无穷无尽的流星精灵。
【问题讨论】:
-
问题不清楚。应用程序循环仅在执行循环之前的代码时开始。为什么在创建流星的循环中有
time.sleep(2)? -
复制随机流星生成。我现在只选择了 2
标签: python python-3.x pygame pygame-surface pygame2