1
votes

I am Python 3 noob creating a checkers game, I have a bunch of functions... and my game's loop is as follows:

while EndGame==0:
    PrintBoard()
    PrintBoardGui()
    print("################","\n","Player 1","\n","################","\n")
    Player=1
    PlayerSelectPiece()
    MovePiece()
    PrintBoard()
    PrintBoardGui()
    print("################","\n","Player 2","\n","################","\n")
    Player=2
    if NbrPlayer==2:
        PlayerSelectPiece()
    else:
        AiSelectPiece()
    MovePiece()

PrintBoardGui() that I run at the beggining of each turn and creates a Tkinter window and draws the board on a new Canvas in a Tkinter Frame.
After that, I have to close the window in order for the program to continue. I know this is a sub-optimal solution.

I have looked around trying to understand Tkinter's loops and read a bit about the after() function but i really don't know how I could implement it in my code.
Ultimatly I would like my Tkinter window to stay open (mabye disabled or something) while I input stuff in the console to move the pieces. Can you help me?

2

2 Answers

0
votes

At first, how do you want to interact with your game ?

  • With text in the console ?
  • With buttons ?
  • With keyboard/mouse event ?

The "after" method is used to refresh your screen after an amount of time. It will call the method that you pass through the parameters. You shouldn't have to put an infinite loop in it. But you will have to check with a simple condition the game ending, to display another screen.

If you have to use the console entry, it can be a bit hard for a beginner to manage the GUI update and the console.

0
votes

I am not sure what ur question was but, If you would want to run the code forever (until you stop it). you would have to use the while loop like this:

while "thing" == True:
    PrintBoard()
    PrintBoardGui()
    print("################","\n","Player 1","\n","################","\n")
    Player=1
    PlayerSelectPiece()
    MovePiece()
    PrintBoard()
    PrintBoardGui()
    print("################","\n","Player 2","\n","################","\n")
    Player=2
    if NbrPlayer==2:
        PlayerSelectPiece()
    else:
        AiSelectPiece()
    MovePiece()
    thing = False

at the end you change thing to False otherwise it would be infinite and it would bug.