0

i was wondering if anyone had an example of code that shows basically the game asking a question and the user has to input 1,2,3,or 4. I am not sure how to accept user input on pygame and proceed to the next question. I have code that asks for the user to press any key to proceed, but i am specifically gearing towards a correct input. i have this function but am not sure how to change it select a number/string 1,2,3, or 4. I am also trying to actually have that input number random against the computer somehow using random.randit to see if the player will match the number and win. I am also doing this through pygame and using graphics and such. thanks in advance.

import pygame, random, sys
from pygame.locals import *

mainClock = pygame.time.Clock()


def waitForPlayerToSelect():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                leave()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE: # pressing escape quits
                    leave()
                return
furas
  • 134,197
  • 12
  • 106
  • 148

1 Answers1

1

Welcome to Stackoverflow! This is a great site for asking questions. The idea of stackoverflow is to come here after you have done as much looking as you can. A quick search on stack overflow of 'pygame keyboard input' led me to this answer which answers your question: https://stackoverflow.com/a/29068383/2605424

For the future please refer to www.pygame.org it's docs include lots of information about this exact thing.

However this time I will give you further insight of how you may accomplish what you are wanting.

import pygame, random, sys
from pygame.locals import *

mainClock = pygame.time.Clock()

def waitForPlayerToSelect():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                leave()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE: # pressing escape quits
                    leave()
                elif event.key == K_1:
                    return 1
                elif event.key == K_2:
                    return 2
                return

For this to work, from the place where you call it just use something similar to:

playerSelection = waitForPlayerToSelect()
Community
  • 1
  • 1
KodyVanRy
  • 1,110
  • 1
  • 14
  • 25