2013-03-27 160 views
1

我想弄清楚如何使用randint功能在Python GUI中滾動一對骰子。骰子滾動在Python GUI

我想用兩次鼠標點擊生成randint的骰子卷的兩個數字,並使用兩次點擊更改骰子以顯示這些數字。

我有骰子顯示和一切,但我不確定如何使用randintmouseclick函數與該項目的窗口中的骰子。此項目必須使用randintmouseclick。我一直試圖使用list來隨機刪除骰子上的點,但問題在於使用randint似乎不是一種有效的方法。我將不勝感激任何建議。

from graphics import * 

def main(): 


    #Set interface 
    w = GraphWin() 
    s1 = Rectangle(Point(70,70), Point(20,20)) 
    s1.setFill("Green") 
    s1.setOutline("Black") 
    s1.draw(w) 


    s2 = Rectangle(Point(140, 140), Point (90,90)) 
    s2.setFill("Green") 
    s2.setOutline("Black") 
    s2.draw(w) 

    center = Point(60,60) 
    ds1 = Circle(center, 5) 
    ds1.setFill("Red") 
    ds1.draw(w) 

    center2 = Point(45,60) 
    ds2 = Circle(center2, 5) 
    ds2.setFill("Red") 
    ds2.draw(w) 

    center3 = Point(30,60) 
    ds3 = Circle(center3, 5) 
    ds3.setFill("Red") 
    ds3.draw(w) 

    center4 = Point(30,30) 
    ds4 = Circle(center4, 5) 
    ds4.setFill("Red") 
    ds4.draw(w) 

    center5 = Point(45,30) 
    ds5 = Circle(center5, 5) 
    ds5.setFill("Red") 
    ds5.draw(w) 

    center6 = Point(60,30) 
    ds6 = Circle(center6, 5) 
    ds6.setFill("Red") 
    ds6.draw(w) 

    center7 = Point(130,130) 
    ds7 = Circle(center7, 5) 
    ds7.setFill("Red") 
    ds7.draw(w) 

    center8 = Point(130,115) 
    ds8 = Circle(center8, 5) 
    ds8.setFill("Red") 
    ds8.draw(w) 

    center9 = Point(130,100) 
    ds9 = Circle(center9, 5) 
    ds9.setFill("Red") 
    ds9.draw(w) 

    center10 = Point(100,100) 
    ds10 = Circle(center10, 5) 
    ds10.setFill("Red") 
    ds10.draw(w) 

    center11 = Point(100,115) 
    ds11 = Circle(center11, 5) 
    ds11.setFill("Red") 
    ds11.draw(w) 

    center12 = Point(100,130) 
    ds12 = Circle(center12, 5) 
    ds12.setFill("Red") 
    ds12.draw(w) 

    #Display message 
    m = Text(Point(1,3), ("Click to roll dice: ")) 
    m.draw(w) 

Lab4.py

from graphics import * 
from random import* 
def main(): 
    print ("This program rolls dice using randint") 

    w = GraphWin() 
    s1 = Rectangle(Point(70,70), Point(20,20)) 
    s1.setFill("Green") 
    s1.setOutline("Black") 
    s1.draw(w) 

    s2 = Rectangle(Point(140, 140), Point (90,90)) 
    s2.setFill("Green") 
    s2.setOutline("Black") 
    s2.draw(w) 

    #Display message 
    m = Text(Point(1,3), ("Click to roll dice: ")) 
    m.draw(w) 

    center = Point(60,60) 
    sd1 = Circle(center, 5) 
    sd1.setFill("Red") 

    center = Point(45,60) 
    sd2 = Circle(center, 5) 
    sd2.setFill("Red") 

    center3 = Point(30,60) 
    sd3 = Circle(center3, 5) 
    sd3.setFill("Red") 

    center4 = Point(30,30) 
    sd4 = Circle(center4, 5) 
    sd4.setFill("Red") 

    center5 = Point(45,30) 
    sd5 = Circle(center5, 5) 
    sd5.setFill("Red") 

    center6 = Point(60,30) 
    sd6 = Circle(center6, 5) 
    sd6.setFill("Red") 

    center7 = Point(130,130) 
    sd7 = Circle(center7, 5) 
    sd7.setFill("Red") 

    center8 = Point(130,115) 
    sd8 = Circle(center8, 5) 
    sd8.setFill("Red") 

    center9 = Point(130,100) 
    sd9 = Circle(center9, 5) 
    sd9.setFill("Red") 

    center10 = Point(100,100) 
    sd10 = Circle(center10, 5) 
    sd10.setFill("Red") 

    center11 = Point(100,115) 
    sd11 = Circle(center11, 5) 
    sd11.setFill("Red") 

    center12 = Point(100,130) 
    sd12 = Circle(center12, 5) 
    sd12.setFill("Red") 

    w.getMouse() 
    RN = randint(0,5) 
    if RN == 0: 
     w.getMouse() 
     sd1.draw 
    elif RN == 1: 
     w.getMouse() 
     sd1.draw 
     sd2.draw 
    elif RN == 2: 
     w.getMouse() 
     sd1.draw 
     sd2.draw 
     sd3.draw 
    elif RN == 3: 
     w.getMouse() 
     sd1.draw 
     sd2.draw 
     sd3.draw 
     sd4.draw 
    elif RN == 4: 
     w.getMouse() 
     sd1.draw 
     sd2.draw 
     sd3.draw 
     sd4.draw 
     sd5.draw 
    elif RN == 5: 
     w.getMouse() 
     sd1.draw 
     sd2.draw 
     sd3.draw 
     sd4.draw 
     sd5.draw 
     sd6.draw 

     w.getMouse() 
     RN = randint(0,5) 
     if RN == 0: 
       w.getMouse() 
       sd7.draw 
     elif RN == 1: 
       w.getMouse() 
       sd7.draw 
       sd8.draw 
     elif RN == 2: 
       w.getMouse() 
       sd7.draw 
       sd8.draw 
       sd9.draw 
     elif RN == 3: 
       w.getMouse() 
       sd7.draw 
       sd8.draw 
       sd9.draw 
       sd10.draw 
     elif RN == 4: 
       w.getMouse() 
       sd7.draw 
       sd8.draw 
       sd9.draw 
       sd10.draw 
       sd11.draw 
     elif RN == 5: 
       w.getMouse() 
       sd7.draw 
       sd8.draw 
       sd9.draw 
       sd10.draw 
       sd11.draw 
       sd12.draw 

       #Change message 
       m = Text(Point(1,3), ("Click to close program: ")) 
       m.draw(w) 
       w.getMouse() 
       w.close() 

這是怎麼了,我終於解決了它。

from graphics import * 
from random import* 
def main(): 
    print ("This program rolls dice using randint") 

    w = GraphWin() 
    s1 = Rectangle(Point(70,70), Point(20,20)) 
    s1.setFill("Green") 
    s1.draw(w) 

    s2 = Rectangle(Point(140, 140), Point (90,90)) 
    s2.setFill("Green") 
    s2.draw(w) 

    #Display message 
    m = Text(Point(60,10), ("Click to roll dice: ")) 
    m.draw(w) 
    #Define die faces 
    center = Point(60,60) 
    sd1 = Circle(center, 5) 
    sd1.setFill("Red") 

    center = Point(45,60) 
    sd2 = Circle(center, 5) 
    sd2.setFill("Red") 

    center3 = Point(30,60) 
    sd3 = Circle(center3, 5) 
    sd3.setFill("Red") 

    center4 = Point(30,30) 
    sd4 = Circle(center4, 5) 
    sd4.setFill("Red") 

    center5 = Point(45,30) 
    sd5 = Circle(center5, 5) 
    sd5.setFill("Red") 

    center6 = Point(60,30) 
    sd6 = Circle(center6, 5) 
    sd6.setFill("Red") 

    center7 = Point(130,130) 
    sd7 = Circle(center7, 5) 
    sd7.setFill("Red") 

    center8 = Point(130,115) 
    sd8 = Circle(center8, 5) 
    sd8.setFill("Red") 

    center9 = Point(130,100) 
    sd9 = Circle(center9, 5) 
    sd9.setFill("Red") 

    center10 = Point(100,100) 
    sd10 = Circle(center10, 5) 
    sd10.setFill("Red") 

    center11 = Point(100,115) 
    sd11 = Circle(center11, 5) 
    sd11.setFill("Red") 

    center12 = Point(100,130) 
    sd12 = Circle(center12, 5) 
    sd12.setFill("Red") 


    #Set loops for each die 
    w.getMouse() 
    RN = randint(1,6) 
    if RN == 1: 
     w.getMouse() 
     sd1.draw(w) 
    elif RN == 2: 
     w.getMouse() 
     sd1.draw(w) 
     sd2.draw(w) 
    elif RN == 3: 
     w.getMouse() 
     sd1.draw(w) 
     sd2.draw(w) 
     sd3.draw(w) 
    elif RN == 4: 
     w.getMouse() 
     sd1.draw(w) 
     sd2.draw(w) 
     sd3.draw(w) 
     sd4.draw(w) 
    elif RN == 5: 
     w.getMouse() 
     sd1.draw(w) 
     sd2.draw(w) 
     sd3.draw(w) 
     sd4.draw(w) 
     sd5.draw(w) 
    elif RN == 6: 
     w.getMouse() 
     sd1.draw(w) 
     sd2.draw(w) 
     sd3.draw(w) 
     sd4.draw(w) 
     sd5.draw(w) 
     sd6.draw(w) 

    w.getMouse() 
    RN2 = randint(1,6) 
    if RN2 == 1: 
     w.getMouse() 
     sd7.draw(w) 
    elif RN2 == 2: 
     w.getMouse() 
     sd7.draw(w) 
     sd8.draw(w) 
    elif RN2 == 3: 
     w.getMouse() 
     sd7.draw(w) 
     sd8.draw(w) 
     sd9.draw(w) 
    elif RN2 == 4: 
     w.getMouse() 
     sd7.draw(w) 
     sd8.draw(w) 
     sd9.draw(w) 
     sd10.draw(w) 
    elif RN2 == 5: 
     w.getMouse() 
     sd7.draw(w) 
     sd8.draw(w) 
     sd9.draw(w) 
     sd10.draw(w) 
     sd11.draw(w) 
    elif RN2 == 6: 
     w.getMouse() 
     sd7.draw(w) 
     sd8.draw(w) 
     sd9.draw(w) 
     sd10.draw(w) 
     sd11.draw(w) 
     sd12.draw(w) 

    #Change message 
    m.undraw() 
    m2 = Text(Point(80,10), ("Click to close program: ")) 
    m2.draw(w) 

    w.getMouse() 
    w.close() 

main() 
+0

您可以顯示您迄今嘗試過的代碼嗎? (這聽起來像你已經完成了一部分工作,它會幫助任何人試圖回答) – 2013-03-27 04:02:40

+0

定義骰子面對1,2,3,4,5和6的樣子,然後創建一個從1到6並渲染那個骰子。 – Patashu 2013-03-27 04:12:50

+0

已添加代碼。我覺得我太過於複雜了,但正如我所說,我是一個初學者。 – 2013-03-27 04:13:08

回答

1

試試這個辦法:

  1. 創建你的骰子中的所有面的列表。
  2. 當有人點擊鼠標時,選擇一個0到骰子面上的數字之間的隨機整數。
  3. 然後取這個數字並用它作爲你的骰子列表的下標。
  4. 顯示骰子的臉。

既然你問randint

>>> from random import randint 
>>> randint(0,5) 
2 
>>> randint(0,5) 
5 
>>> randint(0,5) 
1 

所以,你現在需要的是這樣的邏輯:

if mouse_click: 
    random_number = randint(0,5) 
    display_dice_face(dice_faces[random_number]) 

這是比較容易,因爲getMouse()方法將等待鼠標點擊。

我認爲應該讓你開始。

+0

我嘗試了現在顯示在主帖子中的第二個代碼。理論上它似乎有效,但點擊鼠標沒有做任何事情。我遺漏了什麼? – 2013-03-27 14:04:36