Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
initial gameboard layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Hopkins committed Apr 21, 2022
1 parent 5e62a2c commit bdfd30c
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 19 deletions.
35 changes: 32 additions & 3 deletions source/gui/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from cgitb import enable
import tkinter as tk
import os
from functools import partial
Expand Down Expand Up @@ -65,7 +66,14 @@ def __init__(self) -> None:
partial(
self.__switch_context,
screen=self.leaderboard_screen,
funcs=(self.leaderboard_screen.refresh_scores,),
# When switching to leaderboard from welcome screen: refresh scores, disable back btn, enable main menu btn
funcs=(
self.leaderboard_screen.refresh_scores,
partial(self.leaderboard_screen.set_back_btn_state, enabled=False),
partial(
self.leaderboard_screen.set_mainmenu_btn_state, enabled=True
),
),
)
)

Expand All @@ -77,10 +85,31 @@ def __init__(self) -> None:
partial(self.__switch_context, previous=True)
)

self.gameboard_screen.set_back_cmd(
partial(self.__switch_context, previous=True)
self.gameboard_screen.set_mainmenu_cmd(
partial(self.__switch_context, screen=self.welcome_screen)
)

self.gameboard_screen.set_highscores_cmd(
partial(
self.__switch_context,
screen=self.leaderboard_screen,
# When switching to leaderboard from welcome screen: refresh scores, enable back btn, disable main menu btn
funcs=(
self.leaderboard_screen.refresh_scores,
partial(self.leaderboard_screen.set_back_btn_state, enabled=True),
partial(
self.leaderboard_screen.set_mainmenu_btn_state, enabled=False
),
),
)
)

self.gameboard_screen.set_help_cmd(
partial(self.__switch_context, screen=self.help_screen)
)

self.help_screen.set_back_cmd(partial(self.__switch_context, previous=True))

# Set welcome screen as current context and try to import/use theme
self.__switch_context(self.welcome_screen)
self.__import_theme()
Expand Down
75 changes: 63 additions & 12 deletions source/gui/gameboard.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from tkinter import ttk, Tk
from tkinter import ttk, Tk, messagebox
from functools import partial
from gui.base import AppGuiBase
from localization import localizer


class Gameboard(AppGuiBase):
Expand All @@ -11,27 +12,77 @@ class Gameboard(AppGuiBase):
def __init__(self, master, *args, **kwargs) -> None:
AppGuiBase.__init__(self, master, *args, **kwargs)

self.label = ttk.Label(
self.rowconfigure(index=0, weight=5, minsize=100)
self.rowconfigure(index=1, weight=90, minsize=100)
self.rowconfigure(index=2, weight=5, minsize=100)
# Title
self.title = ttk.Label(
self,
padding=(10, 10),
justify="center",
font=("-size", 45, "-weight", "bold"),
text="TODO: GAMEBOARD SCREEN",
font=("-size", 30, "-weight", "bold"),
text=localizer.get("GAME_TITLE"),
)
self.label.pack()
self.title.grid(row=0, column=1, padx=(10, 10), pady=(20, 10), sticky="n")

self.back_btn = ttk.Button(
self, text="BACK", command=partial(print, "TODO: GOTO WELCOME")
self.timer = ttk.Label(
self,
justify="center",
font=("-size", 18, "-weight", "bold"),
text="Timer: 00:00",
)
self.timer.grid(row=0, column=0, padx=(10, 10), pady=(20, 10), sticky="sw")

self.help_btn = ttk.Button(
self, text="HELP", command=partial(print, "TODO: GOTO HELP")
)
self.help_btn.grid(row=0, column=2, padx=(10, 10), sticky="se")

# Middle panel
self.scores_panel = ttk.Labelframe(
self,
padding=(5, 5),
)
self.scores_panel.grid(
row=1, column=0, columnspan=self.num_columns, sticky="nsew"
)

# Bottom Buttons
self.mainmenu_btn = ttk.Button(
self,
text=localizer.get("ARROW_LEFT") + localizer.get("MAIN_MENU_BUTTON"),
command=partial(
self.__confirm_quit, partial(print, "TODO: RETURN MAIN MENU")
),
)
self.mainmenu_btn.grid(row=2, column=0, padx=(10, 10), pady=(10, 10))

self.highscores_btn = ttk.Button(
self,
text=localizer.get("HIGHSCORES_BUTTON") + localizer.get("ARROW_RIGHT"),
command=partial(print, "TODO: GOTO LAST CONTEXT"),
)
self.back_btn.pack()
self.highscores_btn.grid(row=2, column=2, padx=(10, 10), pady=(10, 10))

def __confirm_quit(self, func) -> None:
ret = messagebox.askyesno(
title="Are you sure?", message="Are you sure you want to quit?"
)
if ret:
func()

def set_mainmenu_cmd(self, command) -> None:
self.mainmenu_btn.configure(command=partial(self.__confirm_quit, command))

def set_highscores_cmd(self, command) -> None:
self.highscores_btn.configure(command=command)

def set_back_cmd(self, command) -> None:
self.back_btn.configure(command=command)
def set_help_cmd(self, command) -> None:
self.help_btn.configure(command=command)


if __name__ == "__main__":
root = Tk()
app = Gameboard(master=root, rows=3, columns=3)
app = Gameboard(master=root, rows=3, columns=3, theme=None, name="")
app.pack(fill="both", expand=True)

root.update()
Expand Down
18 changes: 18 additions & 0 deletions source/gui/help.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from tkinter import ttk
from gui.base import AppGuiBase
from functools import partial


class Help(AppGuiBase):
Expand All @@ -10,6 +11,23 @@ class Help(AppGuiBase):
def __init__(self, master, *args, **kwargs) -> None:
AppGuiBase.__init__(self, master, *args, **kwargs)

self.label = ttk.Label(
self,
padding=(10, 10),
justify="center",
font=("-size", 45, "-weight", "bold"),
text="TODO: HELP SCREEN",
)
self.label.pack()

self.back_btn = ttk.Button(
self, text="BACK", command=partial(print, "TODO: GOTO WELCOME")
)
self.back_btn.pack()

def set_back_cmd(self, command) -> None:
self.back_btn.configure(command=command)


if __name__ == "__main__":
app = Help(master=None)
Expand Down
12 changes: 10 additions & 2 deletions source/gui/leaderboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ def __init__(self, master, *args, **kwargs) -> None:
# Bottom Buttons
self.mainmenu_btn = ttk.Button(
self,
text=localizer.get("MAIN_MENU_BUTTON"),
text=localizer.get("ARROW_LEFT") + localizer.get("MAIN_MENU_BUTTON"),
command=partial(print, "TODO: GOTO MAIN MENU"),
)
self.mainmenu_btn.grid(row=2, column=0, padx=(10, 10), pady=(10, 10))

self.back_btn = ttk.Button(
self,
text=localizer.get("BACK_BUTTON"),
text=localizer.get("BACK_BUTTON") + localizer.get("ARROW_RIGHT"),
command=partial(print, "TODO: GOTO LAST CONTEXT"),
)
self.back_btn.grid(row=2, column=2, padx=(10, 10), pady=(10, 10))
Expand Down Expand Up @@ -96,7 +96,15 @@ def set_back_cmd(self, command) -> None:
def set_mainmenu_cmd(self, command) -> None:
self.mainmenu_btn.configure(command=command)

def set_back_btn_state(self, enabled: bool) -> None:
self.back_btn.configure(state="enabled" if enabled else "disabled")

def set_mainmenu_btn_state(self, enabled: bool) -> None:
self.mainmenu_btn.configure(state="enabled" if enabled else "disabled")

def refresh_scores(self) -> None:
self.scores_table.delete(*self.scores_table.get_children())

scores = self.__read_scores()
for i, score in enumerate(scores):
self.scores_table.insert(
Expand Down
7 changes: 5 additions & 2 deletions source/localization/localizer_en.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
THEME_COPYRIGHT = "© 2021 rdbende"
THEME_NAME = "Sun-Valley-ttk-theme"
LEADERBOARD_TITLE = "High Scores"
MAIN_MENU_BUTTON = "<< Return to Main Menu"
BACK_BUTTON = "Go Back >>"
MAIN_MENU_BUTTON = "Return to Main Menu"
BACK_BUTTON = "Go Back"

ARROW_LEFT = "<< "
ARROW_RIGHT = " >>"

WELCOME_SCREEN = "Main Menu"
GAMEBOARD_SCREEN = "Game"
Expand Down

0 comments on commit bdfd30c

Please sign in to comment.