Python 入門指南 5.0
exercise3409
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | # 從標準程式庫引入 Tk from tkinter import * # 從 tkinter 引入 font from tkinter import font # 從 tkinter 引入 ttk from tkinter import ttk # 從 random 引入 randint from random import randint # 從 exercise2503 引入 DiceUser from exercise2503 import DiceUser # 練習用的視窗類別 class DiceGUI(ttk.Frame): def __init__(self, master): ttk.Frame.__init__(self, master) self.winfo_toplevel().title("擲骰子遊戲") self.root = master self.grid() self.label_font = font.Font(master, font="SF 48") self.createWidgets() self.style = ttk.Style() self.style.theme_use("alt") self.banker = DiceUser("莊家", 500) self.user = DiceUser("玩家", 100) def createWidgets(self): self.dice1 = Label(self) self.dice1["text"] = "" self.dice1["width"] = 10 self.dice1["height"] = 4 self.dice1["bg"] = "white" self.dice1["fg"] = "black" self.dice1["font"] = self.label_font self.dice1.grid(row=0, column=0, sticky=N) self.dice2 = Label(self) self.dice2["text"] = "" self.dice2["width"] = 10 self.dice2["height"] = 4 self.dice2["bg"] = "white" self.dice2["fg"] = "black" self.dice2["font"] = self.label_font self.dice2.grid(row=0, column=1, sticky=N) self.result = Label(self) self.result["text"] = "" self.result["width"] = 20 self.result["height"] = 4 self.result["bg"] = "white" self.result["fg"] = "black" self.result["font"] = self.label_font self.result.grid(row=1, column=0, columnspan=2, sticky=N) self.button = Button(self) self.button["text"] = "進行遊戲" self.button["width"] = 20 self.button["height"] = 3 self.button["command"] = self.run_game self.button.grid(row=2, column=0, columnspan=2, sticky=N) def update_text(self, current_frame): if current_frame < 10: self.dice1.config(text=str(randint(1, 6))) self.dice1["bg"] = "silver" self.dice2.config(text=str(randint(1, 6))) self.dice2["bg"] = "silver" self.button.config(state=DISABLED) self.result.config(text="擲骰子中⋯⋯") self.root.after(200, self.update_text, current_frame + 1) else: self.button.config(state=NORMAL) self.dice1["bg"] = "white" self.dice1.config(text=str(self.user.dice1.point)) self.dice2["bg"] = "white" self.dice2.config(text=str(self.user.dice2.point)) self.result.config(text=self.result_text) def run_game(self): self.banker.asset -= 30 self.user.asset -= 30 self.banker.roll2Dice() self.user.roll2Dice() self.result_text = f"莊家擲出 {self.banker.total_points} 點" if self.banker.total_points >= self.user.total_points: self.result_text += f"莊家贏\n" self.banker.asset += 60 else: self.result_text += f"玩家贏\n" self.user.asset += 60 self.result_text += f"玩家資產尚有 {self.user.asset}" self.update_text(0) # 執行部分 if __name__ == '__main__': # 建立 Tk 應用物件 root = Tk() # 建立視窗物件 app = DiceGUI(master=root) # 呼叫維持視窗運作的 mainloop() root.mainloop() # 檔名: exercise3409.py # 說明:《Python入門指南》的練習 # 網站: http://kaiching.org # 作者: 張凱慶 # 時間: 2023 年 11 月 |