yahtzee-py/views/board_view.py

254 lines
12 KiB
Python
Raw Normal View History

2026-11-02 18:32:36 +00:00
import tkinter as tk
import random
from PIL import Image, ImageTk
from models.constants import CATEGORIES
from models.i18n import t
from views.widgets import ResizingBG, ImageDie, create_img_button, BG_COLOR, FG_COLOR, ACCENT_ORANGE
class BoardView:
def __init__(self, root_view):
self.root = root_view
self.ctrl = root_view.ctrl
self.assets = root_view.assets
# Bild- & Widget-Referenzen
self.bg_l = None
self.bg_r = None
self.game_title_img = None
self.game_logo_img = None
self.turn_lbl = None
self.dice_wrapper = None
self.dice_widgets = []
self.btn_roll = None
self.score_buttons = {}
self.score_labels = {}
self.bonus_lbl = None
self.total_lbl = None
self.bot_total_labels = {}
self.anim_count = 0
def init_ui(self):
self.root.clear_window(apply_global_bg=False)
self.root.bind("<space>", lambda e: self.ctrl.roll_dice())
for i in range(1, 6):
self.root.bind(str(i), lambda e, idx=i-1: self.ctrl.toggle_die(idx))
content_frame = tk.Frame(self.root, bg=BG_COLOR)
content_frame.pack(fill="both", expand=True, padx=20, pady=10)
# Top Bar
top_frame = tk.Frame(content_frame, bg=BG_COLOR)
top_frame.pack(fill="x", pady=5)
self.turn_lbl = tk.Label(top_frame, text="", font=("Helvetica", 16, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE)
self.turn_lbl.pack(side="left")
right_frame = tk.Frame(top_frame, bg=BG_COLOR)
right_frame.pack(side="right")
create_img_button(right_frame, "Speichern & Beenden", self.assets.sel_inactive, self.assets.sel_active, self.ctrl.save_and_quit).pack(side="right", padx=5)
create_img_button(right_frame, "Menü", self.assets.sel_inactive, self.assets.sel_active, lambda: self.root.dialogs.show_menu_confirm_dialog()).pack(side="right", padx=5)
create_img_button(right_frame, "Hilfe", self.assets.sel_inactive, self.assets.sel_active, lambda: self.root.dialogs.show_help_dialog()).pack(side="right", padx=5)
main_split = tk.Frame(content_frame, bg=BG_COLOR)
main_split.pack(fill="both", expand=True, pady=10)
self._build_left_panel(main_split)
self._build_right_panel(main_split)
def _build_left_panel(self, parent):
self.dice_wrapper = tk.Frame(parent, bg=BG_COLOR)
self.dice_wrapper.pack(side="left", fill="both", expand=True, padx=5)
if self.assets.raw_bg:
self.bg_l = ResizingBG(self.dice_wrapper, self.assets.raw_bg, bg=BG_COLOR)
self.bg_l.place(relx=0, rely=0, relwidth=1, relheight=1)
dice_inner = tk.Frame(self.dice_wrapper, bg=BG_COLOR)
dice_inner.place(relx=0.5, rely=0.5, anchor="center")
if self.assets.raw_title:
self.game_title_img = ImageTk.PhotoImage(self.assets.raw_title.resize((300, 70), Image.Resampling.LANCZOS))
tk.Label(dice_inner, text="Deine Würfel", image=self.game_title_img, compound="center", font=("Helvetica", 14, "bold"), bg=BG_COLOR, fg=FG_COLOR, bd=0).pack(pady=10)
else:
tk.Label(dice_inner, text="Deine Würfel", font=("Helvetica", 16, "bold"), bg=BG_COLOR, fg=FG_COLOR).pack(pady=10)
dice_row = tk.Frame(dice_inner, bg=BG_COLOR)
dice_row.pack(pady=15)
self.dice_widgets = []
for i in range(5):
die = ImageDie(dice_row, self.assets, command=lambda idx=i: self.ctrl.toggle_die(idx))
die.grid(row=0, column=i, padx=3)
self.dice_widgets.append(die)
if self.assets.raw_logo:
self.game_logo_img = ImageTk.PhotoImage(self.assets.raw_logo.resize((180, 180), Image.Resampling.LANCZOS))
tk.Label(dice_inner, image=self.game_logo_img, bg=BG_COLOR, bd=0).pack(pady=10)
self.btn_roll = create_img_button(dice_inner, "WÜRFELN", self.assets.btn_orange, self.assets.btn_orange_active, self.ctrl.roll_dice)
self.btn_roll.pack(pady=10)
self.dice_wrapper.bind("<Configure>", self._on_dice_wrapper_resize)
def _build_right_panel(self, parent):
score_wrapper = tk.Frame(parent, bg=BG_COLOR)
score_wrapper.pack(side="right", fill="both", expand=True, padx=5)
if self.assets.raw_bg:
self.bg_r = ResizingBG(score_wrapper, self.assets.raw_bg, bg=BG_COLOR)
self.bg_r.place(relx=0, rely=0, relwidth=1, relheight=1)
score_inner = tk.Frame(score_wrapper, bg=BG_COLOR)
score_inner.place(relx=0.5, rely=0.5, anchor="center")
tk.Label(score_inner, text="Deine Wertungskarte", font=("Helvetica", 14, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=5)
grid_frame = tk.Frame(score_inner, bg=BG_COLOR)
grid_frame.pack(fill="x", pady=5)
upper_frame = tk.Frame(grid_frame, bg=BG_COLOR)
upper_frame.grid(row=0, column=0, sticky="n", padx=5)
lower_frame = tk.Frame(grid_frame, bg=BG_COLOR)
lower_frame.grid(row=0, column=1, sticky="n", padx=5)
self.score_buttons = {}
self.score_labels = {}
for row, i in enumerate(range(6)):
cat_key, cat_name_key = CATEGORIES[i]
btn = create_img_button(upper_frame, t(cat_name_key), self.assets.sel_inactive, self.assets.sel_active, command=lambda k=cat_key: self.ctrl.score_category(k))
btn.grid(row=row, column=0, pady=2, sticky="w")
self.score_buttons[cat_key] = btn
lbl = tk.Label(upper_frame, text="-", font=("Courier", 13, "bold"), bg=BG_COLOR, fg="#555", width=4)
lbl.grid(row=row, column=1, padx=5)
self.score_labels[cat_key] = lbl
tk.Frame(upper_frame, bg="#444", height=1).grid(row=6, column=0, columnspan=2, sticky="ew", pady=5)
self.bonus_lbl = tk.Label(upper_frame, text="Bonus: 0 / 63", font=("Helvetica", 10, "bold"), bg=BG_COLOR, fg="#888")
self.bonus_lbl.grid(row=7, column=0, columnspan=2)
for row, i in enumerate(range(6, 13)):
cat_key, cat_name_key = CATEGORIES[i]
btn = create_img_button(lower_frame, t(cat_name_key), self.assets.sel_inactive, self.assets.sel_active, command=lambda k=cat_key: self.ctrl.score_category(k))
btn.grid(row=row, column=0, pady=2, sticky="w")
self.score_buttons[cat_key] = btn
lbl = tk.Label(lower_frame, text="-", font=("Courier", 13, "bold"), bg=BG_COLOR, fg="#555", width=4)
lbl.grid(row=row, column=1, padx=5)
self.score_labels[cat_key] = lbl
tk.Frame(score_inner, bg="#444", height=1).pack(fill="x", pady=10)
self.total_lbl = tk.Label(score_inner, text="GESAMT: 0", font=("Courier", 16, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE)
self.total_lbl.pack()
if len(self.ctrl.model.players) > 1:
bots_frame = tk.Frame(score_inner, bg="#111018", highlightbackground="#333", highlightthickness=1)
bots_frame.pack(fill="x", pady=15)
tk.Label(bots_frame, text="Gegner Übersicht", font=("Helvetica", 11, "bold"), bg="#111018", fg=FG_COLOR).pack(pady=5)
self.bot_total_labels = {}
for p in self.ctrl.model.players[1:]:
row_f = tk.Frame(bots_frame, bg="#111018")
row_f.pack(fill="x", padx=10, pady=2)
lbl = tk.Label(row_f, text=f"{p.name}: 0", font=("Helvetica", 11), bg="#111018", fg=FG_COLOR)
lbl.pack(side="left")
self.bot_total_labels[p.name] = lbl
btn = create_img_button(row_f, t("Details"), self.assets.sel_inactive, self.assets.sel_active, command=lambda p=p: self.root.dialogs.show_bot_details(p))
btn.pack(side="right")
def _on_dice_wrapper_resize(self, event):
if event.width < 400:
new_size = max(40, (event.width - 100) // 5)
for die in self.dice_widgets:
if die.current_size != new_size:
die.resize_die(new_size)
else:
for die in self.dice_widgets:
if die.current_size != 70:
die.resize_die(70)
def set_turn_label(self, text):
self.turn_lbl.config(text=text)
def get_held_dice(self):
return [die.held for die in self.dice_widgets]
def toggle_die_visual(self, idx):
self.dice_widgets[idx].toggle_hold()
def reset_dice_visuals(self):
for die in self.dice_widgets:
die.held = False
die.update_image()
die.config(bg=BG_COLOR)
def disable_input(self):
self.btn_roll.config(state="disabled")
for btn in self.score_buttons.values():
btn.config(state="disabled")
def enable_input(self):
self.btn_roll.config(state="normal")
def update_dice_ui(self):
for i, val in enumerate(self.ctrl.model.dice):
self.dice_widgets[i].set_value(val)
if self.ctrl.model.rolls_left > 0:
self.btn_roll.config(text=f"WÜRFELN ({self.ctrl.model.rolls_left})", state="normal")
else:
self.btn_roll.config(text="KEINE WÜRFE MEHR", state="disabled")
def update_scoreboard(self):
human = self.ctrl.model.players[0]
active_p = self.ctrl.model.players[self.ctrl.current_player_idx]
is_human_turn = not active_p.is_bot
is_yz = len(set(self.ctrl.model.dice)) == 1
has_joker = is_yz and human.scores["Y"] == 50
upper_sum = 0
for cat_key, _ in CATEGORIES:
val = human.scores[cat_key]
lbl = self.score_labels[cat_key]
btn = self.score_buttons[cat_key]
if val is not None and cat_key in ["1", "2", "3", "4", "5", "6"]:
upper_sum += val
if val is not None:
lbl.config(text=str(val), fg="#00FF00")
btn.config(state="disabled")
else:
if is_human_turn:
pot = self.ctrl.model.calculate_score(cat_key, self.ctrl.model.dice, has_joker)
lbl.config(text=f"({pot})", fg=ACCENT_ORANGE)
btn.config(state="normal")
else:
lbl.config(text="-", fg="#555")
btn.config(state="disabled")
if upper_sum >= 63:
self.bonus_lbl.config(text="Bonus: +35 (Erreicht!)", fg="#00FF00")
else:
self.bonus_lbl.config(text=f"Bonus: {upper_sum} / 63", fg="#888")
self.total_lbl.config(text=f"GESAMT: {human.total_score}")
for p in self.ctrl.model.players[1:]:
if p.name in self.bot_total_labels:
self.bot_total_labels[p.name].config(text=f"{p.name}: {p.total_score}")
def trigger_roll_animation(self, is_bot=False, callback=None):
self.btn_roll.config(state="disabled")
self.anim_count = 0
def anim_step():
self.anim_count += 1
for die in self.dice_widgets:
if not die.held:
if self.assets.raw_anim:
die.show_anim_frame(self.anim_count)
else:
die.set_value(random.randint(1, 6))
if self.anim_count < 15:
self.root.after(50, anim_step)
else:
if callback:
callback()
anim_step()