[Tutor] program hangs in while loop using wx.yield
Alan Gauld
alan.gauld at btinternet.com
Sun Nov 14 23:21:33 CET 2010
"Alex Hall" <mehgcap at gmail.com> wrote
> The problem is that I want to go until there is a winner. You are
> right about just letting the mainloop of the gui handle input (I
> forgot the gui is already looping and waiting for input) but I would
> like to make the user pause while the computer goes, this way the
> user
> cannot just keep hammering out moves as fast as possible
Thats fine so you simple stop accepting input while the computer goes.
I'm sure your computer can keep up with aeven the fastest typist/mouse
clicker. If you really want to add a delay simply add a short pause in
your event handler - 1 second say. Or even better set a semaphore
which blocks any input and then a timer to reset it after a given
delay.
This is all fairly satandard GUI practice none of which requires
complex
loops. You need to start thinking about a world controlled by the GUI
firing events( or messages if you like) telling you what is happening
which you catch and process.
> be given to the user properly. Therefore, I have gotten rid of the
> loop I sent originally, but I still have a loop which switches
> between
> the two players
Is it two players or is it a player and a computer.
If its the former you need a semaphore to block input from one
player until the other has gone. In chess terms the values might
be black and white and the semaphore called player. You then
either need two input handlers, one for black and one for white
which call a common utility fuction after checking the semaphor
or a single function that somehow can identify the player.
If its the latter then the computer should be faster than a hiuman
so there should be little problem...
> the other. If I fire (it is Battleship) and hit your ship,and I am
> the
> computer, then this loop will give you the information about where I
> fired. You will check to see if it was a hit and, since it was, pass
> back information about what was hit. You will also update the gui to
> reflect the new status of the damaged ship.
I'm confusing you and I, its safer to use explicit trerms when
describing
interactions. But the displaing of information and updating the GUI
can
all happen in a blink of an eye if its the computer playing.
> this, I do not want you firing as fast as possible, and I want you
> to
> take in the results before you are allowed to fire.
Why do you care if the human "takes in the results" - if they want
to trust in randomness surely thats their problem. You as a computer
can match them blast for blast...
> while not player1.isWinner or not player2.isWinner:
> if player1.grid.turnOver:
> print("player2 is going")
> player1.grid.speak("player 2 is going.")
> player2.enemyMove=player1.grid.cmd #tell p2 what p1 just did
> player1.lock() #so p1 cannot interact with the gui
> player2.takeTurn() #decides what to do and updates its Grid object
> accordingly
> player1.grid.speak(helpers.interpret(player2.grid.cmd)) #just
> output what p2 did after takeTurn() is over
> elif player2.grid.turnOver:
> print("player1 is going")
> player1.enemyMove=player2.grid.cmd
> player1.unlock() #p1 can now interact with the gui
> player1.grid.speak("It is now your turn.")
> player1.takeTurn()
> #end if
> #wx.Yield()
> time.sleep(.1)
> #end while
Still looks way too complicated to me!
> I am honestly not sure how all the control flows, but this loop is
> gone so hopefully it will not be a problem.
You need to think about the control flow vfery carefully - after all
you the programmer are supposed to be in charge of your program!
And if you ever have to debug it you will need to understand
where the control is going next!
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list