I've been trying to create a sprite creator, but I notice that the pygame window doesn't load until all of those sprites have been created, and I wanted to know 2 things:
- Why it only loads the screen when all of these sprites have been created
- How could I fix this without changing too much
code:
#!/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
main:
import pygame
import meteors
pygame.init()
while True:
meteors.all_meteors.update()
meteors.all_meteors.draw(screen)
pygame.display.update()
I do not have a clue on why it prioritizes creating the sprites before creating the pygame window, and it prevents me from creating endless amounts of meteor sprites.