0

I'm making a rock paper scissors game, and I'm using pygame to display some images, and depending on where I put my loop to have pygame open, pygame will either not respond, or my code won't continue

    import random
    import pygame

    pygame.init()
    width=400
    height=400
    dis=pygame.display.set_mode((width,height))
    pygame.display.update

    running=True
    while running:
         for event in pygame.event.get():
            if event.type==pygame.QUIT:
                running=False
    pygame.quit()
    readyToPlay=input("Are you ready? y or n ")
  • You cannot mix a pygame event loop with `input`. `input` waits for an input. Hence the application loop will not response when you call `inpu` in the loop. When you call `input` after the loop, then you have to terminate the loop, before the input. – Rabbid76 Mar 31 '21 at 13:01
  • what error message are you getting? – MEdwin Mar 31 '21 at 13:01
  • @MEdwin I don't get an error message, my code wouldn't run after that point, now I got a picture on pygame and my code runs, but pygame won't respond – Lucas_Not_Smart Mar 31 '21 at 13:07
  • @Rabbid76 If I broke the loop would pygame keep responding? – Lucas_Not_Smart Mar 31 '21 at 13:09
  • @Lucas_Not_Smart Pygame will wait and do nothing as long the input is not done. Some systems may even crash (it depends on the OS). – Rabbid76 Mar 31 '21 at 13:11
  • @Rabbid76 that does make sense, how am I able to keep the input and have pygame keep running? – Lucas_Not_Smart Mar 31 '21 at 13:19
  • See [Pygame Window not Responding after few seconds](https://stackoverflow.com/questions/64830453/pygame-window-not-responding-after-few-seconds/64832291#64832291) or [How to create a text input box with pygame?](https://stackoverflow.com/questions/46390231/how-to-create-a-text-input-box-with-pygame/64613666#64613666) – Rabbid76 Mar 31 '21 at 13:27
  • 1
    @Rabbid76 thank you so much, I will look into these – Lucas_Not_Smart Mar 31 '21 at 13:35

1 Answers1

0

Your game window runs during this loop:

running=True
while running:
     for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False
pygame.quit()

Once pygame.quit() has run, you are back in the console and then

readyToPlay=input("Are you ready? y or n ")

runs.

What you want to do is handle the events from pygame during that loop. You currently only handle pygame.QUIT but you want to handle keyboard events as well, show text on the window using the graphics functions of pygame, etc.

Alternatively you can write a console-only game where you use the console input/output functions and live without the images (or make ASCII art which is what I did for my very first game).

gonutz
  • 5,087
  • 3
  • 22
  • 40