2015-05-21 29 views
1

對於我的計算機科學課,我正在研究年底項目,我選擇使用Python和Tkinter創建一個Yahtzee遊戲。我已經開始了代碼,但到目前爲止我沒有工作,我不知道爲什麼,有人可以幫我嗎?有人可以幫我製作Yahtzee遊戲嗎?

「#roll1」,「#roll2」等後面的代碼是爲五個骰子上的每個可能性創造點。它們存儲在我不太瞭解的定義中。複選框用於確定玩家想要擲哪個骰子,但是當我測試了可以滾動1的擲骰按鈕時,該複選框並未奏效。此外,我希望複選框水平組織,但由於某種原因,它們是垂直的。有人請幫忙!

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() 
+0

充滿「globals」的代碼很瘋狂。這是Python的缺點,你做了很多糟糕的編程......你也可能會考慮有一個列表,你可以通過代碼獲得很長的變量列表...... – nbro

+1

@Xenomorph我不認爲OP的糟糕設計是Python的錯...... – jonrsharpe

+0

@Xenomorph我真的不知道我在做什麼,請幫忙!和哪個變量的列表? –

回答

3

這裏是我的建議:

關注一件事,只有一兩件事。不要試圖讓所有的骰子同時工作。弄清楚如何獲得一個骰子的工作。沒有其他的。沒有遊戲邏輯,沒有其他骰子。現在就好像你正在學習用10個球開始玩。你需要從最簡單的事情開始。

事實上,不要擔心所有的骰子功能。選擇死亡的一個方面並解決這個問題。找出如何繪製一個。或者,找出如何計算隨機角色。解決其中的一個問題。

一旦你解決了這個問題,移動到下一個。所以,如果你能繪製一個骰子,找出如何計算一個骰子並重新繪製骰子。讓工作100%。然後,添加一個返回骰子當前值的函數。

接下來,弄清楚如何把你所做的所有事情都放到課堂上。你想成爲能夠寫出你的主要邏輯來做到這一點(和此):

<import the Die class, or define it here> 
root = Tk() 
drawpad = Canvas(...) 
die = Die(drawpad, x, y) # draw a die at coordinate (x,y) 
die.roll() # roll the die, and redraw it with the value 
print(die.value()) # print out the value 

一旦你擁有了這一切,那麼你可以開始着眼於一個簡單的循環創造了其他骰子。一旦你有了,那麼你可以專注於遊戲的其他部分 - 菜單,按鈕等。

0

如上所述,從一件事開始。以下是一個骰子的代碼。你會從另一個班級打電話給他,但爲了簡單起見,這是獨立的。我使用乘法器的公式來計算骰子上點的位置,因爲這樣更簡單,並且不需要六個骰子的重複代碼X 5。 6種不同的顏色用於測試,以確定哪個模具正在被執行。還要注意,與模具相關的一切都包含在課堂上。

from Tkinter import * 
import random 


class OneDie(): 
    def __init__(self, drawpad, x1, x2, color): 
     self.drawpad=drawpad 
     self.x1=x1 
     self.x2=x2 
     self.color=color 
     self.dots_location={1:[[0.5, 0.5]], 
          2:[[0.25, 0.25], [0.75, 0.75]], 
          3:[[0.25, 0.25], [0.5, 0.5], [0.75, 0.75]], 
          4:[[0.25, 0.25], [0.25, 0.75], [0.75, 0.25], [0.75, 0.75]], 
          5:[[0.5, 0.5], [0.25, 0.25], [0.25, 0.75], [0.75, 0.25], [0.75, 0.75]], 
          6:[[0.25, 0.25], [0.25, 0.75], [0.75, 0.25], [0.75, 0.75], [0.50, 0.25], [0.50, 0.75]]} 
     self.die = self.drawpad.create_rectangle(x1, 10, x2, 110, fill="red") 
     self.update_this_die(6) ## initialize every die to 6 

    def roll_this_die(self): 
     num=random.randint(1, 6) 
     self.update_this_die(num) 

    def update_this_die(self, num): 
     self.drawpad.delete(self.die) 
     self.die = self.drawpad.create_rectangle(self.x1, 10, self.x2, 110, fill="red") 
     location_list = self.dots_location[num] 
     for ctr in range(len(location_list)): 
      multiplier_x_y = location_list[ctr] 
      x=(self.x2-self.x1)*multiplier_x_y[0]+self.x1-10 ## 10 is 1/2 of circle size 
      y=(110-10)*multiplier_x_y[1] 
      self.drawpad.create_oval(x, y, x+20, y+20, fill=self.color, outline="red") 

root = Tk() 

drawpad = Canvas(root, width=600, height=300) 
drawpad.grid() 
Button(root, text="Exit", command=root.quit, bg="orange").grid(row=100) 

instance_list=[] 
x1=10 
x2=110 
colors=["white", "lightblue", "yellow", "black", "green"] ## identify each die while testing 
for ctr in range(5): 
    OD=OneDie(drawpad, x1, x2, colors[ctr]) 
    x1 += 120 
    x2 += 120 
    instance_list.append(OD) 

## roll each die once 
for instance in instance_list: 
    instance.roll_this_die() 
root.mainloop() 
相關問題