[Tutor] while loops / listeners

Alex Hall mehgcap at gmail.com
Mon Jun 7 05:09:37 CEST 2010


Hi all,
First off, I apologize to the list for my previous thread; somehow,
despite my having written the post, it ended up blank (????)

I have a main loop which will continue for as long as neither player1
nor player2 has won. Inside that loop I have a call to a function
which should basically wait until the user completes a turn, then
return, exiting this loop and returning to the main loop, the one
looping until there is a winner. Once back there, the second player's
idling function is called; once that user completes a turn, the main
loop switches back to the first user, and so on. Here is the main
loop:
 player1.takeTurn() #someone has to start off the game

 #now loop until someone wins...
 while not player1.isWinner and not player2.isWinner:
  if player1.grid.turnOver: #player1 is done for this turn
   player1.grid.speak("player 2 is going.")
   #lock p1 out of the window, unlock p2, and then loop p2 until s/he
does something
   player1.lock(); player2.unlock(); player2.takeTurn()
  else: #player2 has completed a turn
   player2.grid.speak("player 1 is going.")
   #opposite of above - lock p2, unlock p1, loop until p1 is done
   player2.lock(); player1.unlock(); player1.takeTurn()
  #end if
  continue #is this doing anything?
  time.sleep(.1) #again, is this important?
 #end while

The lock and unlock functions are simply there to disable and enable
keyboard/mouse commands, respectively, so lock causes the player's
screen to stop responding to input while unlock unfreezes the screen.
This way, if you and I are playing a game, I can't keep shooting even
if my turn is over.
TakeTurn() is that smaller loop I was talking about:
 def takeTurn(self):
  while not self.grid.turnOver: #turnOver is true once a turn-ending
function is called in grid
   continue
   time.sleep(.1)
  #end while
 #end def

That is inside the Player class. Grid is just another object that is
really the most important part of all the game logic; Grid holds the
wx frame on which everything is drawn, the boolean to tell if the turn
is over, the ships, and more. That is why the function checks
self.grid.turnOver, instead of just self.turnOver; turnOver tells if
the player has performed a move that ends a turn or not. Firing ends a
turn, while just moving around does not.

The question, then, is this: when I run the code, Windows says
"python.exe has stopped responding". I know that this is because it is
getting stuck, probably in the takeTurn() loop. Ordinarily, a
situation like this would probably call for:
while ok:
 ok=#program logic

The hard part with my program, though, is that all input is set up
with a wx.AcceleratorTable object, so I cannot just dump everything
into a huge while loop and have it process that way. What I am looking
for is some kind of listener object, so that I can just monitor
self.grid.turnOver and call a function, or perform a few lines of
code, when the listener detects a change in turnOver. On the other
hand, there may be something simple that I am missing in my while loop
that would cause the problem of python.exe crashing to not happen
(well, not crashing, but rather not responding). I hope it is the
latter problem, but I am not sure what else I could add to the loop(s)
to stop this problem. Thanks, and sorry again for the blank message;
still not sure how that happened. Hopefully this one works!

-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap


More information about the Tutor mailing list