1

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
A.h
  • 11
  • 3

1 Answers1

2

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()
Community
  • 1
  • 1
mooglinux
  • 815
  • 1
  • 11
  • 27
  • Hi, thank you very much for your reply. I understand why this should be however then I get an error message for the timer. The message is: `line 524, in pygame.time.set_timer(question,1000) TypeError: an integer is required` Do know how to fix this so it will work as a timer, and so that I can make this like a quiz? Thanks again! – A.h Sep 29 '14 at 19:06
  • Updated my answer for ya. – mooglinux Sep 30 '14 at 00:38