jeudi 21 mai 2015

Can someone help me with making a Yahtzee game?

For my computer science class, I'm working on an end of year project and I chose to create a Yahtzee game using Python and Tkinter. I've started the code but what I have so far is not working and I'm not sure why, can someone help me?

The code after the "#roll1","#roll2",etc. is to create the dots for each roll possibility on the five dice. They are stored in definitions which I do not know much about. The checkboxes are used to determine which dice the player wants to roll, but when I tested out the roll button with the possibility of rolling a 1, it hasn't been working. Also I wanted the checkboxes to be organized horizontally but for some reason they are vertically. Someone please help!

from Tkinter import *
import random
root = Tk()
drawpad = Canvas(root, width=600,height=600, background='white')
dice1 = drawpad.create_rectangle(10,10,110,110, fill = "white")
dice2 = drawpad.create_rectangle(130,10,230,110, fill = "white")
dice3 = drawpad.create_rectangle(250,10,350,110, fill = "white")
dice4 = drawpad.create_rectangle(370,10,470,110, fill = "white")
dice5 = drawpad.create_rectangle(490,10,590,110, fill = "white")
check1 = False
check2 = False
check3 = False
check4 = False
check5 = False

#roll 1
roll1 = []
for i in range(1,6,1):
    x = (120 * i) - 65
    y = x + 10
    roll1.append(drawpad.create_oval(x,55,y,65, fill = "red", state = HIDDEN))
#roll 2
roll2 = {}
for i in range(1,6,1):
    x = (120 * i) - 98
    y = x + 10
    x2 = (120 * i) - 33
    y2 = x2 + 10
    roll2[i] = [drawpad.create_oval(x,23,y,33, fill = "red",state=HIDDEN), drawpad.create_oval(x2,87,y2,97, fill = "red",state = HIDDEN)]
#roll3
roll3 = {}
for i in range(1,6,1):
    x = (120 * i) - 65
    y = x + 10
    x2 = (120 * i) - 98
    y2 = x2 + 10
    x3 = (120 * i) - 33
    y3 = x3 + 10
    roll3[i] = [drawpad.create_oval(x,55,y,65, fill = "red", state = HIDDEN),drawpad.create_oval(x2,23,y2,33, fill = "red",state=HIDDEN), drawpad.create_oval(x3,87,y3,97, fill = "red",state = HIDDEN)]
#roll4
roll4 = {}
for i in range(1,6,1):
    x = (120 * i) - 98
    y = x + 10
    x2 = (120 * i) - 33
    y2 = x2 + 10
    roll4[i] = [drawpad.create_oval(x,23,y,33,fill="red",state=HIDDEN),drawpad.create_oval(x2,23,y2,33,fill="red",state=HIDDEN),drawpad.create_oval(x2,87,y2,97,fill="red",state=HIDDEN),drawpad.create_oval(x,87,y,97,fill="red",state=HIDDEN)]
#roll5
roll5 = {}
for i in range(1,6,1):
    x = (120 * i) - 98
    y = x + 10
    x2 = (120 * i) - 33
    y2 = x2 + 10
    x3 = (120 * i) - 65
    y3 = x3 + 10
    roll5[i] = [drawpad.create_oval(x,23,y,33,fill="red",state=HIDDEN),drawpad.create_oval(x2,23,y2,33,fill="red",state=HIDDEN),drawpad.create_oval(x2,87,y2,97,fill="red",state=HIDDEN),drawpad.create_oval(x,87,y,97,fill="red",state=HIDDEN),drawpad.create_oval(x3,55,y3,65,fill="red",state=HIDDEN)]
#roll6
roll6 = {}
for i in range(1,6,1):
    x = (120 * i) - 98
    y = x + 10
    x2 = (120 * i) - 33
    y2 = x2 + 10
    roll6[i] = [drawpad.create_oval(x,23,y,33,fill="red",state=HIDDEN),drawpad.create_oval(x2,23,y2,33,fill="red",state=HIDDEN),drawpad.create_oval(x2,87,y2,97,fill="red",state=HIDDEN),drawpad.create_oval(x,87,y,97,fill="red",state=HIDDEN),drawpad.create_oval(x,55,y,65,fill="red",state=HIDDEN),drawpad.create_oval(x2,55,y2,65,fill="red",state=HIDDEN)]


class MyApp(object):
    def __init__(self, parent):
            global drawpad
            self.myParent = parent  
            self.myContainer1 = Frame(parent)
            self.myContainer1.pack()

        #Roll Button
        self.rollButton = Button(self.myContainer1)
        self.rollButton.configure(text="Roll", background= "green")
        self.rollButton.grid(row=0,column=0)

        #Stop Button
        self.stop = Button(self.myContainer1)
        self.stop.configure(text="End Turn", background= "green")
        self.stop.grid(row=0,column=1)

        #Dice Checkboxes
        self.var1 = IntVar()
        c = Checkbutton(root, text="Dice 1",variable = self.var1,command=self.cb)
        c.grid(row=1,column=0)
        self.var2 = IntVar()
        c2 = Checkbutton(root, text="Dice 2",variable = self.var2,command=self.cb2)
        c2.grid(row=1,column=1)
        self.var3 = IntVar()
        c3 = Checkbutton(root, text="Dice 3",variable = self.var3,command=self.cb3)
        c3.grid(row=1,column=2)
        self.var4 = IntVar()
        c4 = Checkbutton(root, text="Dice 4",variable = self.var4,command=self.cb4)
        c4.grid(row=1,column=3)
        self.var5 = IntVar()
        c5 = Checkbutton(root, text="Dice 5",variable = self.var5,command=self.cb5)
        c5.grid(row=1,column=4)


        self.rollButton.bind("<Button-1>", self.rollButtonClick)

        c.pack()
        c2.pack()
        c3.pack()
        c4.pack()
        c5.pack()
        drawpad.pack()

    def cb(self):
        global check1
        if(self.var1.get() == 1):
            check1 = True
        else:
            check1 = False

    def cb2(self):
        global check2
        if(self.var2.get() == 1):
            check2 = True
        else:
            check2 = False

    def cb3(self):
        global check3
        if(self.var3.get() == 1):
            check3 = True
        else:
            check3 = False

    def cb4(self):
        global check4
        if(self.var4.get() == 1):
            check4 = True
        else:
            check4 = False

    def cb5(self):
        global check5
        if(self.var5.get() == 1):
            check5 = True
        else:
            check5 = False

    def rollButtonClick(self, event):   
        global drawpad
        global roll1
        global check
        global check2
        global check3
        global check4
        global check5
        dice1=0
        dice2=0
        dice3=0
        dice4=0
        dice5=0
        diceValues = [dice1,dice2,dice3,dice4,dice5]
        if(check1==True):
            dice1 = random.randint(1,6)
        if(check2==True):
            dice2 = random.randint(1,6)
        if(check3==True):
            dice3 = random.randint(1,6)
        if(check4==True):
            dice4 = random.randint(1,6)
        if(check5==True):
            dice5 = random.randint(1,6)
        for i in (0,4,1):
            if(diceValues[i]==1):
                drawpad.itemconfig(roll1[i],state=NORMAL)



app = MyApp(root)
root.mainloop()




Aucun commentaire:

Enregistrer un commentaire