0

I am a beginner at coding and python and I am trying to create a simple game, where one moves a square in order to prevent small other squares that are coming from the edge of the display from hitting the larger square ("the character"). Here a part of the code:

def figure(x,y):
pygame.draw.rect(gameDisplay, black, (x,y,50,50))


notExit = True

def game_loop(x,x_change,y,y_change):
    while(notExit):
         for event in pygame.event.get():
             if event.type == pygame.QUIT:
                quit()
             if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                     x_change = -5
                if event.key == pygame.K_RIGHT:
                     x_change = 5
                if event.key == pygame.K_UP:
                     y_change = -5
                if event.key == pygame.K_DOWN:
                     y_change = 5

            if event.type == pygame. KEYUP:
               x_change = 0
               y_change = 0

       gameDisplay.fill(white)

    x = x + x_change
    y = y + y_change

    figure(x,y)

    if x < 0 or y < 0 or x > 750 or y > 550:
        crash()

    pygame.display.update()
    clock.tick(60)
    game_loop(x,x_change,y,y_change)

The basic problem is: How can I let other squares come into the display after a certain amount of time without pausing the entire game for that time? If I use the time.sleep() function, the entire game pauses and the player cannot move the character. Is there another function I could use? Thank You!

foobar_98
  • 97
  • 1
  • 2
  • 7
  • Take a look at [this answer](https://stackoverflow.com/questions/43587734/how-to-spawn-a-sprite-after-a-time-limit-and-how-to-display-a-timer-pygame/43596862#43596862). If you don't know yet how classes, sprites and sprite groups work, you can store the positions of your squares in a list of lists or [`pygame.Rect`](http://www.pygame.org/docs/ref/rect.html)s and then use a for loop to iterate over this list and draw the squares. You could also just increment a variable each frame `timer += 1`, but then your game would be frame rate bound. – skrx Jun 11 '17 at 12:26
  • [Here's another answer](https://stackoverflow.com/a/43692652/6220679) with a counter variable that is decremented every frame and that demonstrates how to use a list of rects. I'll flag your question as a duplicate (but maybe the title of the other question should be edited to be more general). – skrx Jun 11 '17 at 12:42

0 Answers0