A.h
A.h

Reputation: 11

Pausing game and user input in pygame

I am currently making a game in Pygame, I have the basic game made as I followed a tutorial however know I want to improve it and make it so that after a certain amount of time (e.g. every 30 seconds or so) the game pauses and the user is asked a question and they must enter the right answer to continue playing the game. I have been looking on-line and experimenting a bit however struggling quite a lot! Here is the relevant code:

class question():
    pressKeySurf, pressKeyRect = makeTextObjs('9+1.', BASICFONT, WHITE)
    pressKeySurf, pressKeyRect = makeTextObjs('Press A for 10.', BASICFONT, WHITE)
    pressKeySurf, pressKeyRect = makeTextObjs('Press B for 15.', BASICFONT, WHITE)
    for event in pygame.event.get():
        if (event.key==K_a):
            showTextScreen('Correct answer') # pause until a key press
            lastFallTime = time.time()
            lastMoveDownTime = time.time()
            lastMoveSidewaysTime = time.time()
        else:
            showTextScreen("Wrong")

pygame.time.set_timer(question,1000)

I have no idea if this is even going in the right direction at the moment as I can't get it to work because I think I understand the error and that I shouldn't be using a class in the timer, however not sure?

Upvotes: 1

Views: 1104

Answers (1)

mooglinux
mooglinux

Reputation: 845

You want this to be a function, not a class. Replace class with def

The bigger problem is that your use of set_timer is incorrect. It is:

set_timer(eventid, timems)

where eventid and timems are both integers. set_timer puts an event into the event queue at the specified interval, it doesn't call any function. You have to put code in the main loop to check for that eventid and then execute the appropriate code.

Example code from this SO answer::

pygame.init()
pygame.time.set_timer(USEREVENT + 1, 100)
while True:
    for event in pygame.event.get():
        if event.type == USEREVENT + 1:
           functionName()
        if event.type == QUIT:
            pygame.quite()
            sys.exit()

Upvotes: 2

Related Questions