my code stock.com

GUI For a Board Game

Snippet options

Download: Download snippet as mini-board-game.py.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

from tkinter import *
import random
import sys
from time import sleep
root=Tk()
root.geometry("500x500+200+200")

##***Creating a TextBox to get the names of the players***

my_list=list()
en_t=Entry(root,width=30,font=('Verdana',15))

my_label=Label(text="Enter the name of Player 1",font=("Times New Roman",18),fg="white",bg="black")
my_label.pack(pady=10)

en_t.pack(pady=5,ipady=2)
en_t.focus_set()




def call_back():
    my_list.append(en_t.get())
    en_t.delete(0,END)
    
    if len(my_list)==4:
        en_t.config(state="disabled",disabledbackground="Red")
        en_t.update()
        sleep(0.5)
        my_label.destroy()

        butt.destroy()
 
        en_t.destroy()
        running()
    else:
        my_label.config(text="Enter the name of Player {}".format(len(my_list)+1))

##Keyboard Buttons Enter assigning
        
def keyb(event):
    call_back()

root.bind("<Return>",keyb)


butt=Button(root,text="get",width=10,command=call_back)
butt.pack(pady=10)

## Making the main function

def running():
    class Player():
        def __init__(self,name,color):
            self.name=name
            self.color=color
            self.message="Welcome to the game {}, your color is {}".format(name,color)
            self.val=45
        def roll_dice(self,text,status):
            text.set("Rolling {}".format(self.name))
            status.update()
            sleep(0.5)
            x=6
            a=0
            while x==6:
                x=random.randint(1,6)
                text.set("You get {} this time".format(x))
                status.update()
      #          sleep(0.5)
                if x==6:
                    text.set("Rolling again")
                    status.update()
                #input("Press enter to roll again")
                a+=x
     #           sleep(0.5)
            text.set("Your total move is {}".format(a))
            status.update()
            if a>=self.val:
                text.set("WINNER WINNER CHICKEN DINNER for {}".format(self.name))
                status.update()
                sleep(3)
                sys.exit()
            else:
                self.val-=a
                text.set("{}!! You\'re Next".format(my_list[(my_list.index(self.name)+1)%4]))
                status.update()
       #         sleep(0.75)
                text.set("Total moves remaining are {}".format(self.val))

    ##Creating Objects of Classes
                
    player1=Player(my_list[0],"Red")
    player2=Player(my_list[1],"Blue")
    player3=Player(my_list[2],"Green")
    player4=Player(my_list[3],"Yellow")

    ##Defining Functions for Buttons

    def rolling_dice1():
        player1.roll_dice(texty1,stat_us1)
        
    def rolling_dice2():
        player2.roll_dice(texty2,stat_us2)
        
    def rolling_dice3():
        player3.roll_dice(texty3,stat_us3)
        
    def rolling_dice4():
        player4.roll_dice(texty4,stat_us4)

    ##Creating Wigets and Labels and Frames for the Game window

    frame=Frame(root)
    wel_come=Label(frame,text="WELCOME TO\nLUDO KING",bg="blue",fg="yellow",font=("Arial Bold",20))
    wel_come.pack(pady=5)
    frame.grid(row=1,column=1)

    texty1=StringVar()

    frame2=Frame(root,bg="black")
    stat_us1=Label(frame2,textvariable=texty1,bg="Red",fg="White",font=("Arial Bold",15))
    texty1.set(player1.message)
    stat_us1.pack(pady=5,fill=X)

    texty2=StringVar()

    stat_us2=Label(frame2,textvariable=texty2,bg="Blue",fg="White",font=("Arial Bold",15))
    texty2.set(player2.message)
    stat_us2.pack(pady=5,fill=X)


    texty3=StringVar()

    stat_us3=Label(frame2,textvariable=texty3,bg="Green",fg="White",font=("Arial Bold",15))
    texty3.set(player3.message)
    stat_us3.pack(pady=5,fill=X)

    texty4=StringVar()

    stat_us4=Label(frame2,textvariable=texty4,bg="yellow",fg="black",font=("Arial Bold",15))
    texty4.set(player4.message)
    stat_us4.pack(pady=5,fill=X)

    frame2.grid(row=2,column=1)

    ##Creating Buttons for players

    frame3=Frame(root)

    p1=Label(frame3,text="Player 1\n starts here",bg="grey",fg="pink")
    p1.grid(row=0,padx=40,pady=20)
    button_1=Button(frame3,text="PLAY",command=rolling_dice1)
    button_1.grid(row=1)

    p2=Label(frame3,text="Player 2\n starts here",bg="grey",fg="pink")
    p2.grid(row=2,padx=40,pady=40)
    button_2=Button(frame3,text="PLAY",command=rolling_dice2)
    button_2.grid(row=3)

    p3=Label(frame3,text="Player 3\n starts here",bg="grey",fg="pink")
    p3.grid(row=0,column=1,padx=40,pady=20)
    button_3=Button(frame3,text="PLAY",command=rolling_dice3)
    button_3.grid(row=1,column=1)

    p4=Label(frame3,text="Player 4\n starts here",bg="grey",fg="pink")
    p4.grid(row=2,column=1,padx=40,pady=40)
    button_4=Button(frame3,text="PLAY",command=rolling_dice4)
    button_4.grid(row=3,column=1)

    frame3.grid(row=3,column=1)


root.mainloop()

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.