|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import messagebox |
| 3 | +import random |
| 4 | + |
| 5 | +# ========================= |
| 6 | +# THEME |
| 7 | +# ========================= |
| 8 | +APP_BG = "#121212" |
| 9 | +PANEL_BG = "#1F1F1F" |
| 10 | +BTN_BG = "#2C2C2C" |
| 11 | +BTN_HOVER = "#3A3A3A" |
| 12 | +BTN_ACTIVE = "#FF6F61" |
| 13 | +ACCENT = "#FF6F61" |
| 14 | +TEXT_CLR = "#E0E0E0" |
| 15 | +SUBTEXT_CLR = "#AAAAAA" |
| 16 | +INPUT_BG = "#333333" |
| 17 | +INPUT_FG = "#FFFFFF" |
| 18 | + |
| 19 | +# ========================= |
| 20 | +# APP |
| 21 | +# ========================= |
| 22 | +class DiceGameApp: |
| 23 | + def __init__(self, root): |
| 24 | + self.root = root |
| 25 | + root.title("MateTools – Dice Game") |
| 26 | + root.geometry("1000x500") |
| 27 | + root.configure(bg=APP_BG) |
| 28 | + root.resizable(False, False) |
| 29 | + |
| 30 | + # ========================= |
| 31 | + # LEFT PANEL (Actions) |
| 32 | + # ========================= |
| 33 | + left = tk.Frame(root, bg=PANEL_BG, width=420) |
| 34 | + left.pack(side="left", fill="y") |
| 35 | + |
| 36 | + header = tk.Frame(left, bg=PANEL_BG) |
| 37 | + header.pack(fill="x", padx=16, pady=(18, 10)) |
| 38 | + |
| 39 | + tk.Label( |
| 40 | + header, |
| 41 | + text="MateTools", |
| 42 | + bg=PANEL_BG, |
| 43 | + fg=ACCENT, |
| 44 | + font=("Segoe UI", 20, "bold") |
| 45 | + ).pack(side="left") |
| 46 | + |
| 47 | + tk.Frame(left, bg=ACCENT, height=2).pack(fill="x", padx=16, pady=(0, 14)) |
| 48 | + |
| 49 | + tk.Label( |
| 50 | + left, |
| 51 | + text="Dice Game", |
| 52 | + bg=PANEL_BG, |
| 53 | + fg=TEXT_CLR, |
| 54 | + font=("Segoe UI", 14, "bold") |
| 55 | + ).pack(anchor="w", padx=16, pady=(0, 2)) |
| 56 | + |
| 57 | + tk.Label( |
| 58 | + left, |
| 59 | + text="Play against the computer", |
| 60 | + bg=PANEL_BG, |
| 61 | + fg=SUBTEXT_CLR, |
| 62 | + font=("Segoe UI", 10) |
| 63 | + ).pack(anchor="w", padx=16, pady=(0, 16)) |
| 64 | + |
| 65 | + tk.Frame(left, bg=BTN_BG, height=1).pack(fill="x", padx=16, pady=(0, 16)) |
| 66 | + |
| 67 | + btn_frame = tk.Frame(left, bg=PANEL_BG) |
| 68 | + btn_frame.pack(fill="x", padx=16, pady=16) |
| 69 | + |
| 70 | + def make_btn(text, cmd, color=BTN_BG): |
| 71 | + btn = tk.Button( |
| 72 | + btn_frame, |
| 73 | + text=text, |
| 74 | + command=cmd, |
| 75 | + bg=color, |
| 76 | + fg="white", |
| 77 | + font=("Segoe UI", 11, "bold"), |
| 78 | + relief="flat", |
| 79 | + height=2, |
| 80 | + width=20 |
| 81 | + ) |
| 82 | + btn.bind("<Enter>", lambda e, b=btn: b.config(bg=BTN_HOVER)) |
| 83 | + btn.bind("<Leave>", lambda e, b=btn, c=color: b.config(bg=c)) |
| 84 | + btn.bind("<ButtonPress-1>", lambda e, b=btn: b.config(bg=BTN_ACTIVE)) |
| 85 | + btn.bind("<ButtonRelease-1>", lambda e, b=btn, c=color: b.config(bg=BTN_HOVER)) |
| 86 | + return btn |
| 87 | + |
| 88 | + make_btn("Roll Dice", self.roll_dice, ACCENT).pack(side="top", pady=8) |
| 89 | + make_btn("Clear History", self.clear_history, BTN_BG).pack(side="top", pady=8) |
| 90 | + make_btn("About", self.show_about, BTN_BG).pack(side="top", pady=20) |
| 91 | + |
| 92 | + # ========================= |
| 93 | + # RIGHT PANEL (Game UI) |
| 94 | + # ========================= |
| 95 | + right = tk.Frame(root, bg=APP_BG) |
| 96 | + right.pack(side="right", fill="both", expand=True) |
| 97 | + |
| 98 | + self.result_card = tk.Frame(right, bg=PANEL_BG, bd=2, relief="ridge") |
| 99 | + self.result_card.pack(padx=30, pady=20, fill="both", expand=True) |
| 100 | + |
| 101 | + # ---------- TOP: Game Label ---------- |
| 102 | + tk.Label( |
| 103 | + self.result_card, |
| 104 | + text="Dice Game vs Computer", |
| 105 | + bg=PANEL_BG, |
| 106 | + fg=TEXT_CLR, |
| 107 | + font=("Segoe UI", 18, "bold") |
| 108 | + ).pack(pady=(20, 10)) |
| 109 | + |
| 110 | + # ---------- MAIN FRAME ---------- |
| 111 | + main_frame = tk.Frame(self.result_card, bg=PANEL_BG) |
| 112 | + main_frame.pack(fill="both", expand=True, padx=20, pady=10) |
| 113 | + |
| 114 | + # ===== LEFT COLUMN: Dice Results ===== |
| 115 | + left_frame = tk.Frame(main_frame, bg=PANEL_BG) |
| 116 | + left_frame.pack(side="left", fill="y", padx=(0, 15)) |
| 117 | + |
| 118 | + # Player result |
| 119 | + tk.Label( |
| 120 | + left_frame, |
| 121 | + text="Your Roll:", |
| 122 | + bg=PANEL_BG, |
| 123 | + fg=SUBTEXT_CLR, |
| 124 | + font=("Segoe UI", 12, "bold") |
| 125 | + ).pack(anchor="w", pady=(0, 2)) |
| 126 | + self.player_label = tk.Label( |
| 127 | + left_frame, |
| 128 | + text="--", |
| 129 | + bg=PANEL_BG, |
| 130 | + fg=ACCENT, |
| 131 | + font=("Segoe UI", 18, "bold") |
| 132 | + ) |
| 133 | + self.player_label.pack(anchor="w", pady=(0, 10)) |
| 134 | + |
| 135 | + # Computer result |
| 136 | + tk.Label( |
| 137 | + left_frame, |
| 138 | + text="Computer Roll:", |
| 139 | + bg=PANEL_BG, |
| 140 | + fg=SUBTEXT_CLR, |
| 141 | + font=("Segoe UI", 12, "bold") |
| 142 | + ).pack(anchor="w", pady=(0, 2)) |
| 143 | + self.computer_label = tk.Label( |
| 144 | + left_frame, |
| 145 | + text="--", |
| 146 | + bg=PANEL_BG, |
| 147 | + fg=ACCENT, |
| 148 | + font=("Segoe UI", 18, "bold") |
| 149 | + ) |
| 150 | + self.computer_label.pack(anchor="w", pady=(0, 10)) |
| 151 | + |
| 152 | + # Winner |
| 153 | + tk.Label( |
| 154 | + left_frame, |
| 155 | + text="Winner:", |
| 156 | + bg=PANEL_BG, |
| 157 | + fg=SUBTEXT_CLR, |
| 158 | + font=("Segoe UI", 12, "bold") |
| 159 | + ).pack(anchor="w", pady=(0, 2)) |
| 160 | + self.winner_label = tk.Label( |
| 161 | + left_frame, |
| 162 | + text="--", |
| 163 | + bg=PANEL_BG, |
| 164 | + fg=ACCENT, |
| 165 | + width=13, |
| 166 | + font=("Segoe UI", 18, "bold") |
| 167 | + ) |
| 168 | + self.winner_label.pack(anchor="w", pady=(0, 10)) |
| 169 | + |
| 170 | + # ===== RIGHT COLUMN: History ===== |
| 171 | + right_frame = tk.Frame(main_frame, bg=PANEL_BG) |
| 172 | + right_frame.pack(side="right", fill="both", expand=True) |
| 173 | + |
| 174 | + history_card = tk.Frame(right_frame, bg="#1C1C1C", bd=1, relief="ridge") |
| 175 | + history_card.pack(fill="both", expand=True) |
| 176 | + |
| 177 | + tk.Label( |
| 178 | + history_card, |
| 179 | + text="History", |
| 180 | + bg="#1C1C1C", |
| 181 | + fg=SUBTEXT_CLR, |
| 182 | + font=("Segoe UI", 12, "bold") |
| 183 | + ).pack(anchor="w", padx=10, pady=(5, 0)) |
| 184 | + |
| 185 | + self.history_box = tk.Listbox( |
| 186 | + history_card, |
| 187 | + bg=INPUT_BG, |
| 188 | + fg=INPUT_FG, |
| 189 | + font=("Segoe UI", 12), |
| 190 | + height=20, |
| 191 | + selectbackground=ACCENT, |
| 192 | + relief="flat" |
| 193 | + ) |
| 194 | + self.history_box.pack(fill="both", expand=True, padx=10, pady=(0, 10)) |
| 195 | + |
| 196 | + self.history = [] |
| 197 | + |
| 198 | + # ========================= |
| 199 | + # METHODS |
| 200 | + # ========================= |
| 201 | + def roll_dice(self): |
| 202 | + player_roll = random.randint(1, 6) |
| 203 | + computer_roll = random.randint(1, 6) |
| 204 | + self.player_label.config(text=str(player_roll)) |
| 205 | + self.computer_label.config(text=str(computer_roll)) |
| 206 | + |
| 207 | + if player_roll > computer_roll: |
| 208 | + winner = "You Win!" |
| 209 | + elif player_roll < computer_roll: |
| 210 | + winner = "Computer Wins!" |
| 211 | + else: |
| 212 | + winner = "Draw!" |
| 213 | + self.winner_label.config(text=winner) |
| 214 | + |
| 215 | + entry = f"Player: {player_roll} | Computer: {computer_roll} -> {winner}" |
| 216 | + self.history.append(entry) |
| 217 | + self.history = self.history[-10:] # last 10 rolls |
| 218 | + self.update_history() |
| 219 | + |
| 220 | + def update_history(self): |
| 221 | + self.history_box.delete(0, tk.END) |
| 222 | + for item in self.history: |
| 223 | + self.history_box.insert(tk.END, item) |
| 224 | + |
| 225 | + def clear_history(self): |
| 226 | + self.history = [] |
| 227 | + self.update_history() |
| 228 | + self.player_label.config(text="--") |
| 229 | + self.computer_label.config(text="--") |
| 230 | + self.winner_label.config(text="--") |
| 231 | + |
| 232 | + def show_about(self): |
| 233 | + messagebox.showinfo( |
| 234 | + "About", |
| 235 | + "MateTools – Dice Game\n\n" |
| 236 | + "• Roll dice against the computer\n" |
| 237 | + "• Keep track of last 10 rolls\n" |
| 238 | + "• Winner is displayed each round\n\n" |
| 239 | + "Built by MateTools" |
| 240 | + ) |
| 241 | + |
| 242 | +# ========================= |
| 243 | +# RUN |
| 244 | +# ========================= |
| 245 | +if __name__ == "__main__": |
| 246 | + root = tk.Tk() |
| 247 | + DiceGameApp(root) |
| 248 | + root.mainloop() |
0 commit comments