I am in an introductory cs course, and we have a project due in a couple of weeks. I have asked my instructor if I can ask for help here and he did allow me to post the code and ask.

The project is to write a simple game where four players move down a hallway which has tiles that teleport players forward and backwards in the hallway. The objective is to reach the end of the hall before any other players. Each player is supposed to roll two dice use one on his/her self and choose the other to use on another player.

Those are the basics for the project and are mostly irrelevant to what I need help with.

Right now the code I have will move the players but opens a new window every time and leaves all other windows open. I need the window to just refresh each time.

Additionally I am not developing the game with rules yet. I am just trying to get everything to work.

Here is my code with comments, if you have questions please ask.

from Tkinter import *
import Tkinter
import random
import tkMessageBox

##Player positions
position1=0
position2=0
position3=0
position4=0



def rollDice(num, sides):
   try:                    
      int(num)                ##Attempt to force num to Integer form.
      int(sides)              ##Attempt to force sides to Integer form.
      if num <= 0 or sides <= 0:
         raise Exception()    ##Raise exception flag when num or sides is < 1
   except Exception:          ##Handle Exception flag.
      handleError("The 'num' and 'sides' argument must be positive integers.")
      return [-1]             ##Return list of -1 when an error occurs.
      
   rolls = []                 ##Set rolls to be an empty list.
   currRoll = 1               ##Variable for keeping track of which die roll we are on.
   while currRoll <= num:     
      rolls.append(random.randint(1,sides))  ##Rolls the die and adds the result to the end of the list 'rolls'.
      currRoll += 1           

   return rolls               ##Return the list containing 'num' dice rolls.

##asks how many humans will play
def Num_Humans ():
    print '\nHow many human players will play?'
    Humans = input ('Please enter a number 1-4: ')
    while Humans != 1 and Humans != 2 and Humans != 3 and Humans != 4:
        print '\nincorrect input please enter a correct input'
        print '\nHow many human players will play?'
        Humans = input ('Please enter a number 1-4: ')

##Button command that adds what the dice gives to player position
def orderDieP1():
       DieP1=rollDice(1,2)[0]
       global position1
       position1=position1+DieP1
       tkMessageBox.showinfo( "Order Die for first user",DieP1 )
       print "Player 1's postion is", position1
       Board()

##Button command that adds what the dice gives to player position
def orderDieP2():
       DieP2=rollDice(1,6)[0]
       global position2
       position2=position2+DieP2
       tkMessageBox.showinfo( "Order Die for second user",DieP2 )
       print "Player 2's postion is", position2
       Board()

##Button command that adds what the dice gives to player position
def orderDieP3():
       DieP3=rollDice(1,6)[0]
       global position3
       position3=position3+DieP3
       tkMessageBox.showinfo( "Order Die for thrid user",DieP3 )
       print "Player 3's postion is", position3
       Board()

##Button command that adds what the dice gives to player position
def orderDieP4():
       DieP4=rollDice(1,6)[0]
       global position4
       position4=position4+DieP4
       tkMessageBox.showinfo( "Order Die for forth user",DieP4 )
       print "Player 4's postion is", position4
       Board()


##The board layout using Tkinter organized in a grid
def Board ():
    global root
    root = Tkinter.Tk()

    root.attributes('-topmost', True)

    OrderDicep1 = Tkinter.Button(root, text ="DieP1", command = orderDieP1)
    OrderDicep2 = Tkinter.Button(root, text ="DieP2", command = orderDieP2)
    OrderDicep3 = Tkinter.Button(root, text ="DieP3", command = orderDieP3)
    OrderDicep4 = Tkinter.Button(root, text ="DieP4", command = orderDieP4)


    p1 = Tkinter.Label(root, text ="Player1", relief=RIDGE,pady=10 )
    p2 = Tkinter.Label(root, text ="Player2", relief=RIDGE,pady=10 )
    p3 = Tkinter.Label(root, text ="Player3", relief=RIDGE,pady=10 )
    p4 = Tkinter.Label(root, text ="Player4", relief=RIDGE,pady=10 )

    portal2 = Tkinter.Label(root, text ="P-8", relief=RIDGE,pady=10 )
    portal7 = Tkinter.Label(root, text ="P-15", relief=RIDGE,pady=10 )
    portal13 = Tkinter.Label(root, text ="P-6", relief=RIDGE,pady=10 )
    portal16 = Tkinter.Label(root, text ="P-4", relief=RIDGE,pady=10 )
    portal19 = Tkinter.Label(root, text ="P-1", relief=RIDGE,pady=10 )
    portal22 = Tkinter.Label(root, text ="P-26", relief=RIDGE,pady=10 )
    portal24 = Tkinter.Label(root, text ="P-35", relief=RIDGE,pady=10 )
    portal25 = Tkinter.Label(root, text ="P-21", relief=RIDGE,pady=10 )
    portal28 = Tkinter.Label(root, text ="P-23", relief=RIDGE,pady=10 )
    portal33 = Tkinter.Label(root, text ="P-29", relief=RIDGE,pady=10 )
    portal37 = Tkinter.Label(root, text ="P-43", relief=RIDGE,pady=10 )
    portal39 = Tkinter.Label(root, text ="P-1", relief=RIDGE,pady=10 )
    portal44 = Tkinter.Label(root, text ="P-47", relief=RIDGE,pady=10 )
    portal50 = Tkinter.Label(root, text ="P-41", relief=RIDGE,pady=10 )
    
    Start = Tkinter.Label(root, text ="Start", relief=RIDGE )
    T1 = Tkinter.Label(root, text ="1", relief=RIDGE,padx=3,pady=10 )
    T2 = Tkinter.Label(root, text ="2", relief=RIDGE,padx=3,pady=10 )
    T3 = Tkinter.Label(root, text ="3", relief=RIDGE,padx=3,pady=10 )
    T4 = Tkinter.Label(root, text ="4", relief=RIDGE,padx=3,pady=10 )
    T5 = Tkinter.Label(root, text ="5", relief=RIDGE,padx=3,pady=10 )
    T6 = Tkinter.Label(root, text ="6", relief=RIDGE,padx=3,pady=10 )
    T7 = Tkinter.Label(root, text ="7", relief=RIDGE,padx=3,pady=10 )
    T8 = Tkinter.Label(root, text ="8", relief=RIDGE,padx=3,pady=10 )
    T9 = Tkinter.Label(root, text ="9", relief=RIDGE,padx=3,pady=10 )
    T10 = Tkinter.Label(root, text ="10", relief=RIDGE,padx=3,pady=10 )
    T11 = Tkinter.Label(root, text ="11", relief=RIDGE,padx=3,pady=10 )
    T12 = Tkinter.Label(root, text ="12", relief=RIDGE,padx=3,pady=10 )
    T13 = Tkinter.Label(root, text ="13", relief=RIDGE,padx=3,pady=10 )
    T14 = Tkinter.Label(root, text ="14", relief=RIDGE,padx=3,pady=10 )
    T15 = Tkinter.Label(root, text ="15", relief=RIDGE,padx=3,pady=10 )
    T16 = Tkinter.Label(root, text ="16", relief=RIDGE,padx=3,pady=10 )
    T17 = Tkinter.Label(root, text ="17", relief=RIDGE,padx=3,pady=10 )
    T18 = Tkinter.Label(root, text ="18", relief=RIDGE,padx=3,pady=10 )
    T19 = Tkinter.Label(root, text ="19", relief=RIDGE,padx=3,pady=10 )
    T20 = Tkinter.Label(root, text ="20", relief=RIDGE,padx=3,pady=10 )
    T21 = Tkinter.Label(root, text ="21", relief=RIDGE,padx=3,pady=10 )
    T22 = Tkinter.Label(root, text ="22", relief=RIDGE,padx=3,pady=10 )
    T23 = Tkinter.Label(root, text ="23", relief=RIDGE,padx=3,pady=10 )
    T24 = Tkinter.Label(root, text ="24", relief=RIDGE,padx=3,pady=10 )
    T25 = Tkinter.Label(root, text ="25", relief=RIDGE,padx=3,pady=10 )
    T26 = Tkinter.Label(root, text ="26", relief=RIDGE,padx=3,pady=10 )
    T27 = Tkinter.Label(root, text ="27", relief=RIDGE,padx=3,pady=10 )
    T28 = Tkinter.Label(root, text ="28", relief=RIDGE,padx=3,pady=10 )
    T29 = Tkinter.Label(root, text ="29", relief=RIDGE,padx=3,pady=10 )
    T30 = Tkinter.Label(root, text ="30", relief=RIDGE,padx=3,pady=10 )
    T31 = Tkinter.Label(root, text ="31", relief=RIDGE,padx=3,pady=10 )
    T32 = Tkinter.Label(root, text ="32", relief=RIDGE,padx=3,pady=10 )
    T33 = Tkinter.Label(root, text ="33", relief=RIDGE,padx=3,pady=10 )
    T34 = Tkinter.Label(root, text ="34", relief=RIDGE,padx=3,pady=10 )
    T35 = Tkinter.Label(root, text ="35", relief=RIDGE,padx=3,pady=10 )
    T36 = Tkinter.Label(root, text ="36", relief=RIDGE,padx=3,pady=10 )
    T37 = Tkinter.Label(root, text ="37", relief=RIDGE,padx=3,pady=10 )
    T38 = Tkinter.Label(root, text ="38", relief=RIDGE,padx=3,pady=10 )
    T39 = Tkinter.Label(root, text ="39", relief=RIDGE,padx=3,pady=10 )
    T40 = Tkinter.Label(root, text ="40", relief=RIDGE,padx=3,pady=10 )
    T41 = Tkinter.Label(root, text ="41", relief=RIDGE,padx=3,pady=10 )
    T42 = Tkinter.Label(root, text ="42", relief=RIDGE,padx=3,pady=10 )
    T43 = Tkinter.Label(root, text ="43", relief=RIDGE,padx=3,pady=10 )
    T44 = Tkinter.Label(root, text ="44", relief=RIDGE,padx=3,pady=10 )
    T45 = Tkinter.Label(root, text ="45", relief=RIDGE,padx=3,pady=10 )
    T46 = Tkinter.Label(root, text ="46", relief=RIDGE,padx=3,pady=10 )
    T47 = Tkinter.Label(root, text ="47", relief=RIDGE,padx=3,pady=10 )
    T48 = Tkinter.Label(root, text ="48", relief=RIDGE,padx=3,pady=10 )
    T49 = Tkinter.Label(root, text ="49", relief=RIDGE,padx=3,pady=10 )
    T50 = Tkinter.Label(root, text ="50", relief=RIDGE,padx=3,pady=10 )
    Finish = Tkinter.Label(root, text ="Finish", relief=RIDGE )
    
    OrderDicep1.grid(row=6, column=1, columnspan=3)
    OrderDicep2.grid(row=6, column=4, columnspan=3)
    OrderDicep3.grid(row=6, column=7, columnspan=3)
    OrderDicep4.grid(row=6, column=10, columnspan=3)

    
    p1.grid(row=2,column=position1)
    p2.grid(row=3,column=position2)
    p3.grid(row=4,column=position3)
    p4.grid(row=5,column=position4)

    portal2.grid(row=1,column=2)
    portal7.grid(row=1,column=7)
    portal13.grid(row=1,column=13)
    portal16.grid(row=1,column=16)
    portal19.grid(row=1,column=19)
    portal22.grid(row=1,column=22)
    portal24.grid(row=1,column=24)
    portal25.grid(row=1,column=25)
    portal28.grid(row=1,column=28)
    portal33.grid(row=1,column=33)
    portal37.grid(row=1,column=37)
    portal39.grid(row=1,column=39)
    portal44.grid(row=1,column=44)
    portal50.grid(row=1,column=50)
    
    Start.grid(row=0,column=0)
    T1.grid(row=0,column=1)
    T2.grid(row=0,column=2)
    T3.grid(row=0,column=3)
    T4.grid(row=0,column=4)
    T5.grid(row=0,column=5)
    T6.grid(row=0,column=6)
    T7.grid(row=0,column=7)
    T8.grid(row=0,column=8)
    T9.grid(row=0,column=9)
    T10.grid(row=0,column=10)
    T11.grid(row=0,column=11)
    T12.grid(row=0,column=12)
    T13.grid(row=0,column=13)
    T14.grid(row=0,column=14)
    T15.grid(row=0,column=15)
    T16.grid(row=0,column=16)
    T17.grid(row=0,column=17)
    T18.grid(row=0,column=18)
    T19.grid(row=0,column=19)
    T20.grid(row=0,column=20)
    T21.grid(row=0,column=21)
    T22.grid(row=0,column=22)
    T23.grid(row=0,column=23)
    T24.grid(row=0,column=24)
    T25.grid(row=0,column=25)
    T26.grid(row=0,column=26)
    T27.grid(row=0,column=27)
    T28.grid(row=0,column=28)
    T29.grid(row=0,column=29)
    T30.grid(row=0,column=30)
    T31.grid(row=0,column=31)
    T32.grid(row=0,column=32)
    T33.grid(row=0,column=33)
    T34.grid(row=0,column=34)
    T35.grid(row=0,column=35)
    T36.grid(row=0,column=36)
    T37.grid(row=0,column=37)
    T38.grid(row=0,column=38)
    T39.grid(row=0,column=39)
    T40.grid(row=0,column=40)
    T41.grid(row=0,column=41)
    T42.grid(row=0,column=42)
    T43.grid(row=0,column=43)
    T44.grid(row=0,column=44)
    T45.grid(row=0,column=45)
    T46.grid(row=0,column=46)
    T47.grid(row=0,column=47)
    T48.grid(row=0,column=48)
    T49.grid(row=0,column=49)
    T50.grid(row=0,column=50)
    Finish.grid(row=0, column=51)


    
    root.mainloop()
##Initial game rules before loading the game board
print 'Teleportation Hallway'
print '\nGame Overview: Teleportation Hallway is a race to the finish line. Four players must roll 2 dice and choose one for their player to move and one for the other player. But watch out!!! There are randomly spaced teleportation tiles throughout the hallway, so choose wisely!'
print '\nRules'
print '1. All players start on the start tile.'
print '2. All players will roll one die to find the order, Highest-Lowest'
print '3. At the begging of each players turn they will roll 2 6-sided dice'
print '    a) The player will then choose which dice to use on the other player'
print '    b) Then the player will decide which other player they would like to move'
print '    c) Finally the current player will move'
print '    d) If any player landed on a teleportation tile they will then be moved to the output of that tile'
print '4. Players may only use teleportation tiles if that is the tile the player stops on'
print '5. Once one player has reached the end the game will end'
print '6. There will always be 4 players at least one human'
print '7. Computers will always choose the best move for their player'
print '8. To land on the final tile and win, the player must roll the exact number to land on that tile'
print '\nGame Board Layout'
print '1. The top row shows the spaces of the board'
print '2. The second row shows all portals'
print '3. The number after the p is what space the portal takes the player to'
print '4. DieP1-P4 will be the first buttons available'
print '    a)The first user will click P1'
print '    b)The second user will click P2'
print '    c)The third user will click P3'
print '    d)The forth user will click P4'
print '    e)This will determine the order of players!'
print '5. These buttons will dissappear after use.'



Num_Humans()
Board()

A few final notes.
If anyone has any other suggestions please let me know

And if anyone who is in Eric Rees's class tries taking this code he already has a copy and will be checking if anyone steals my code!

Try removing the following two lines from your board() function and placing them between num_players and board() at the bottom of your script like so:

Num_Humans()
root = Tkinter.Tk()
root.attributes('-topmost', True)
Board()

I hope that's what you were looking for

EDIT:
As far as tips here's my first:
The 50 lines of code used to generate the T1 through T50 labels can be simplified to the following two lines:

for i in xrange( 1, 51 ):
        exec 'T%s = Tkinter.Label(root, text ="%s", relief=RIDGE,padx=3,pady=10 )' % (i,i)

This can be used for the other long sections of code, but this is the most apparent need for it...

Here I was able to cut out over 150 lines of code by using the above method and using a list instead of individual position variables:

from Tkinter import *
import Tkinter
import random
import tkMessageBox

##Player positions
positions = [0,0,0,0]

def rollDice(num, sides):
    try:                    
        int(num)                ##Attempt to force num to Integer form.
        int(sides)              ##Attempt to force sides to Integer form.
        if num <= 0 or sides <= 0:
            raise Exception()    ##Raise exception flag when num or sides is < 1
    except Exception:          ##Handle Exception flag.
        handleError("The 'num' and 'sides' argument must be positive integers.")
        return [-1]             ##Return list of -1 when an error occurs.
    rolls = []                 ##Set rolls to be an empty list.
    currRoll = 1               ##Variable for keeping track of which die roll we are on.
    while currRoll <= num:     
        rolls.append(random.randint(1,sides))  ##Rolls the die and adds the result to the end of the list 'rolls'.
        currRoll += 1           
    return rolls               ##Return the list containing 'num' dice rolls.

##asks how many humans will play
def Num_Humans ():
    print '\nHow many human players will play?'
    Humans = input ('Please enter a number 1-4: ')
    while Humans != 1 and Humans != 2 and Humans != 3 and Humans != 4:
        print '\nincorrect input please enter a correct input'
        print '\nHow many human players will play?'
        Humans = input ('Please enter a number 1-4: ')

def orderDie(player):
    Die=rollDice(1,6)[0]
    global positions
    positions[player]+=Die
    tkMessageBox.showinfo( "Order Die for user %s" % player, Die )
    print "Player %s's postion is" % player, positions[player]
    Board()

##Button command that adds what the dice gives to player position
def orderDieP1():
    orderDie(0)
##Button command that adds what the dice gives to player position
def orderDieP2():
    orderDie(1)
##Button command that adds what the dice gives to player position
def orderDieP3():
    orderDie(2)
##Button command that adds what the dice gives to player position
def orderDieP4():
    orderDie(3)

##The board layout using Tkinter organized in a grid
def Board ():
    global root
    portals = { 2:'P-8',7:'P-15',13:'P-6',16:'P-4',19:'P-1',22:'P-26',24:'P-35',
                25:'P-21',28:'P-23',33:'P-29',37:'P-43',39:'P-1',44:'P-47',50:'P-41' }

    for i in xrange( 1, 5 ):
        exec 'OrderDicep%s = Tkinter.Button(root, text ="DieP%s", command = orderDieP%s)' % (i,i,i)
        exec 'p%s = Tkinter.Label(root, text ="Player%s", relief=RIDGE,pady=10 )' % (i,i)

    for i in portals:
        exec 'portal%s = Tkinter.Label(root, text ="%s", relief=RIDGE,pady=10 )' % (i, portals[i])
    
    Start = Tkinter.Label(root, text ="Start", relief=RIDGE )
    for i in xrange( 1, 51 ):
        exec 'T%s = Tkinter.Label(root, text ="%s", relief=RIDGE,padx=3,pady=10 )' % (i,i)
    Finish = Tkinter.Label(root, text ="Finish", relief=RIDGE )
    
    OrderDicep1.grid(row=6, column=1, columnspan=3)
    OrderDicep2.grid(row=6, column=4, columnspan=3)
    OrderDicep3.grid(row=6, column=7, columnspan=3)
    OrderDicep4.grid(row=6, column=10, columnspan=3)
    
    p1.grid(row=2,column=positions[0])
    p2.grid(row=3,column=positions[1])
    p3.grid(row=4,column=positions[2])
    p4.grid(row=5,column=positions[3])

    for portal_num in portals:
        exec 'portal%s.grid(row=1,column=%s)' % (portal_num, portal_num)
    
    Start.grid(row=0,column=0)
    for i in xrange( 1, 51 ):
        exec 'T%s.grid(row=0,column=%s)' % (i,i)
    Finish.grid(row=0, column=51)
    
    root.mainloop()
##Initial game rules before loading the game board
print 'Teleportation Hallway'
print '\nGame Overview: Teleportation Hallway is a race to the finish line. Four players must roll 2 dice and choose one for their player to move and one for the other player. But watch out!!! There are randomly spaced teleportation tiles throughout the hallway, so choose wisely!'
print '\nRules'
print '1. All players start on the start tile.'
print '2. All players will roll one die to find the order, Highest-Lowest'
print '3. At the begging of each players turn they will roll 2 6-sided dice'
print '    a) The player will then choose which dice to use on the other player'
print '    b) Then the player will decide which other player they would like to move'
print '    c) Finally the current player will move'
print '    d) If any player landed on a teleportation tile they will then be moved to the output of that tile'
print '4. Players may only use teleportation tiles if that is the tile the player stops on'
print '5. Once one player has reached the end the game will end'
print '6. There will always be 4 players at least one human'
print '7. Computers will always choose the best move for their player'
print '8. To land on the final tile and win, the player must roll the exact number to land on that tile'
print '\nGame Board Layout'
print '1. The top row shows the spaces of the board'
print '2. The second row shows all portals'
print '3. The number after the p is what space the portal takes the player to'
print '4. DieP1-P4 will be the first buttons available'
print '    a)The first user will click P1'
print '    b)The second user will click P2'
print '    c)The third user will click P3'
print '    d)The forth user will click P4'
print '    e)This will determine the order of players!'
print '5. These buttons will dissappear after use.'

Num_Humans()
root = Tkinter.Tk()
root.attributes('-topmost', True)
Board()

If there's anything you don't understand, please ask... I really didn't change much, just reduced the redundant code

Any of your variable names that repeat should be in a container. So instead of T1...T50, you should have a list, T, with 50 elements.

While you might consider jlm699's solution to the repetition, it really doesn't address the fact that you have 50 variables that could easily be reduced to 1.

T = []
for i in range(50):
#create a Tkinter label
#do things with it
#append it to T

This applies to your other variables, like OrderDicep1...OrderDicep4, etc.

Also, you have inconsistency in your function and variable naming. Pick one style and stick with it. My recommendation would be not to start any function or variable with a capital letter.

I need the window to just refresh each time.

I think you want root.update(). This is a simple example that I had lying around (click the start button to test).

from Tkinter import *
import time

## ---------------------------------------------------------
##   Demonstration of window update from FAQTs
## ---------------------------------------------------------
class TestUpdate:
   def __init__(self, top):
      self.top=top
      self.test_list = Listbox(top)
      self.test_list.pack(fill='both', expand=1)

      self.test_b = Button(top, text='Start', command=self.loop)
      self.test_b.pack(fill='x', expand=1)

      self.exit = Button(top, text='EXIT',
          command=top.quit, bg='red', fg='white' )
      self.exit.pack(fill=X, expand=1)

   def loop(self):
      for x in range(5):
         print x+1
         self.test_list.insert('end', 'Foo %d' % x)
         self.top.update()
         time.sleep(1)

root = Tk()
T=TestUpdate(root)
root.mainloop()

Also, consider using a class structure for this. The local and global variables are going to kill you trying to figure which is what, where.

when I ran your updated code it did automatically move the player but the players previous positions would still be displayed. Is there a way to use destroy() for a label? I tried but I kept getting errors. thank you for the help btw! :)

I am sorry that last post was to jlm699

This post is to woooee. I am having to teach myself the tkinter syntax and methods associated with it. I am having some trouble reading you code I tried adding p1.update() and root.update() and I just kept getting errors. I apologize for my small knowledge of tkinter. thank you for the help everyone

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.