1

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:

  1. Why it only loads the screen when all of these sprites have been created
  2. 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.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 4
    The question is unclear. The application loop only starts when the code that is before the loop has been executed. Why do you have a `time.sleep(2)` in the loop that creates the meteors? – Rabbid76 May 06 '21 at 20:02
  • To replicate random meteor spawning. I just chose 2 for now – DarkSlayer3202 May 06 '21 at 22:22

1 Answers1

1

Don't create the meteors before the application loop, create them with a time delay in the loop.

Use pygame.time.get_ticks() to get the current time in milliseconds and set the start time, before the main loop. Define a time interval after which a new meteorite should appear. When a meteorite spawns, calculate the time when the next meteorite must spawn:

next_meteor_time = 0
meteor_interval = 2000 # 2000 milliseconds == 2 sceonds

while ready:
    clock.tick(60)  # FPS, Everything happens per frame
    for event in pygame.event.get():
        # [...]

    # [...]

    current_time = pygame.time.get_ticks()
    if current_time >= next_meteor_time:
         next_meteor_time += meteor_interval
         new_x = random.randrange(0, display_width)
         new_y = random.randrange(0, display_height)
         all_meteors.add(Meteor(new_x, new_y))

    # [...]


    meteors.all_meteors.update()
    meteors.all_meteors.draw(screen)
    pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • The pygame window still does not show up until all meteors have been created, and since this is a loop it will just keep making meteors without creating the window itself. – DarkSlayer3202 May 06 '21 at 22:25
  • Im not blaming you at all, Im just telling you what is happening on my end. – DarkSlayer3202 May 09 '21 at 14:34
  • Ok look, I might have sounded rude, so I apologize. I have read your answer before, but at that time it loaded everything except the meteors, so I was genuinly confused and wondered if I did something wrong. I modified my code into one file so I could tell what isn't working, but I clearly cannot. (https://pastebin.com/AeLxv6TZ) – DarkSlayer3202 May 10 '21 at 20:43
  • 1
    @DarkSlayer3202 You have to put the code in your application loop instead of adding a 2nd application loop at the end of the code – Rabbid76 May 10 '21 at 20:47
  • Ok I just realised that my IDE was trying to run an older version of my main script. o_o Sorry for wasting your time. – DarkSlayer3202 May 10 '21 at 22:05