1

I'll state upfront that this is almost exactly the same question as: PyGame: Economic way of handling game loops

I want to make a turn-based dungeon crawl with pygame and pygcurse. I've made a basic template that works if I just let the main while loop go at a certain fps. This seems ridiculous to me to actually implement it this way.

It would be far less complicated to just have the while loop iterate upon a keypress, because literally nothing is happening between inputs. Why make the main loop iterate a million times if someone wants to think about their next move for 15 minutes?

I've tried all sorts of variants on pygcurse.waitforkeypress() and pygame.event.wait(). I can get the loop to wait for a keypress, but the keypress doesn't seem to enter the queue, so the character doesn't move anymore (I know it is looping exactly once on each keypress despite the lack of movement because I put a counter in for debugging purposes).

Is there an obvious intrinsic way to do this or is this just a horribly outdated way to think about game design?

Community
  • 1
  • 1
Matt
  • 113
  • 3

1 Answers1

2

The question you noted really contains most of what you need. Just filter the events based on which you'll need and then use event.wait to block execution until a new event enters the queue. Make sure the window has focus or it won't catch the key presses.

import pygame

pygame.init()
screen = pygame.display.set_mode((400,400))

pygame.event.set_allowed(None)
pygame.event.set_allowed([pygame.QUIT, pygame.KEYDOWN])

while True:
    e = pygame.event.wait()
    print e

This is extremely limiting to your game design though. Continuous execution is required for animation, blocking until input will only allow for totally static graphics while the user is planing their move. Even if there is no "reaction" from the game until the user makes a move, you'll eventually want some kind of "resting/waiting" animations to keep the game visually interesting.

Additionally, if you have some kind of animation as part of the game reacting to an input you'll need to include loops to control the animation and additional input checking during those loops if you want the user to not have to wait through the animation before being able to do anything (including closing the game). In general you'll usually be better off with a continuous game loop and polling the input each time unless you have an extremely limited special case.

Matthew R
  • 232
  • 1
  • 10