[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Event Stack Weirdness
I encountered a strange bug. I'm using my "ringtale" game framework,
which uses a stack of game states. It looks something like this:
class Framework:
def __init__(self):
self.states = []
def Push(self,state):
self.states.append(state)
def Pop(self):
if self.states:
self.states.pop()
else:
raise "No states to pop!"
def Loop(self):
## Based on current state, run logic and drawing functions.
## If certain stuff happens, pop the current state.
Now, if I load my game in IDLE, it runs. If I then run it _again_
without closing IDLE, it crashes, giving the no-states-to-pop message!
Closing and restarting IDLE lets it work again.
At the bottom of my code I create my Game object based on the Framework
class: "g = Game()". So it _should_ be making a new instance every time
I run the program, and the id() function suggests it is. One run should
have no effect on subsequent runs.
I thought maybe this problem had to do with the logic loop I was using:
for event in pygame.event.get(): ## If stuff, then end loop
So I tried making a minimal Pygame program that creates a screen and
runs a loop till a key is pressed -- no Game object, no stack. I ran
that, then loaded my game and ran that -- and the game crashed the
_first_ time, with the no-states error again!
I narrowed down the error a bit. If I load and run the following program
in IDLE:
import pygame
screen = pygame.display.set_mode((100,100))
...And then run my game, it crashes on the first run. If I comment out
the screen-creating line above, it does not.
It seems that running a program with a Pygame screen, at all, somehow
interferes with my program, causing it to decide that an object's
internal event stack is empty despite it being told to put something
there. What's going on?