1
Fork 0
This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
attack-on-blocks/GUI.py

151 lines
6.2 KiB
Python
Raw Normal View History

2015-04-21 11:00:31 +01:00
from tkinter import *
from tkinter.ttk import *
import logging
from os import system
2015-04-22 09:52:09 +01:00
import game
from assets import Textures
2015-05-07 19:38:43 +01:00
2015-04-22 09:52:09 +01:00
PADDING_TITLE = 15
PADDING_BUTTON = 9
2015-04-21 11:00:31 +01:00
2015-05-09 22:44:32 +01:00
2015-04-22 09:52:09 +01:00
class Main_Window:
2015-04-21 11:00:31 +01:00
def __init__(self, master):
self.master = master
2015-05-14 13:20:33 +01:00
self.master.title("ATTACK ON BLOCKS")
2015-04-21 11:00:31 +01:00
2015-04-22 09:52:09 +01:00
self.title = Label(self.master)
2015-05-14 13:20:33 +01:00
self.title.config(text="ATTACK ON BLOCKS!",font=("Courier New", 37))
2015-04-22 09:52:09 +01:00
self.title.pack(side="top", padx=PADDING_BUTTON, pady=PADDING_TITLE/2)
self.start_button = Button(self.master, style="Menu.TButton")
self.start_button.config(text="Play Game")
self.start_button.pack(fill=BOTH, ipadx=PADDING_BUTTON/2, ipady=PADDING_BUTTON/2, padx=PADDING_BUTTON, pady=PADDING_BUTTON)
self.start_button.bind('<Button-1>', self.play_game)
2015-04-21 11:00:31 +01:00
2015-05-11 22:09:16 +01:00
self.options_button = Button(self.master, style="Menu.TButton")
self.options_button.config(text="Show Options")
self.options_button.pack(fill=BOTH, ipadx=PADDING_BUTTON/2, ipady=PADDING_BUTTON/2, padx=PADDING_BUTTON, pady=PADDING_BUTTON)
self.options_button.bind('<Button-1>', self.show_options)
2015-05-07 19:38:43 +01:00
2015-05-12 18:44:45 +01:00
self.help_button = Button(self.master, style="About.TButton")
self.help_button.config(text="About / Help")
2015-05-12 18:44:45 +01:00
self.help_button.pack(fill=BOTH, ipadx=PADDING_BUTTON/4, ipady=PADDING_BUTTON/4, padx=PADDING_BUTTON, pady=PADDING_BUTTON)
self.help_button.bind('<Button-1>', self.show_info)
2015-04-22 09:52:09 +01:00
self.exit_button = Button(self.master, style="Quit.TButton")
self.exit_button.config(text="Quit Game")
self.exit_button.pack(ipadx=PADDING_BUTTON/3, ipady=PADDING_BUTTON, padx=PADDING_BUTTON, pady=PADDING_BUTTON)
self.exit_button.bind('<Button-1>', self.close)
2015-04-21 11:00:31 +01:00
2015-05-12 18:42:05 +01:00
Style().configure("Menu.TButton", font=("Lucida", 21))
2015-05-12 22:40:22 +01:00
Style().configure("About.TButton", font=("Lucida", 17))
2015-05-12 18:42:05 +01:00
Style().configure("Quit.TButton", font=("Lucida", 13))
2015-04-21 11:00:31 +01:00
2015-05-09 23:53:59 +01:00
self.options_window = Options_Window()
2015-05-09 22:48:18 +01:00
logging.debug("GUI Generated.")
2015-04-21 11:00:31 +01:00
2015-04-22 09:52:09 +01:00
def play_game(self, event):
self.master.withdraw()
2015-05-09 22:44:32 +01:00
exit_code = game.initialise(self.master, self.options_window.options)
2015-05-09 23:53:59 +01:00
if exit_code != "QUIT": self.title["text"] = exit_messages[exit_code]
2015-05-07 19:38:43 +01:00
def show_options(self, event):
self.new_window = Toplevel(self.master)
2015-05-09 23:53:59 +01:00
self.options_window.display(self.new_window)
2015-04-22 09:52:09 +01:00
def show_info(self, event):
logging.info("Loading About Page...")
2015-05-14 13:20:33 +01:00
system("start https://bitbucket.org/theorangeone/attack-on-blocks/wiki/Home")
2015-04-22 09:52:09 +01:00
def close(self, event):
logging.critical("Closing Main Window.")
self.master.destroy()
2015-05-07 19:38:43 +01:00
class Options_Window:
2015-05-09 23:53:59 +01:00
def __init__(self):
self.textures_object = Textures()
2015-05-09 23:53:59 +01:00
self.options = {
2015-05-11 13:01:56 +01:00
"Difficulty": 120,
2015-05-12 22:40:02 +01:00
"Textures": self.textures_object,
"Sounds": True
2015-05-09 23:53:59 +01:00
}
def display(self, master):
2015-05-07 19:38:43 +01:00
self.master = master
2015-05-14 13:20:33 +01:00
self.master.title("ATTACK ON BLOCKS - Options")
2015-05-07 19:38:43 +01:00
self.title = Label(self.master)
self.title.config(text="OPTIONS",font=("Courier New", 37))
2015-05-10 13:43:48 +01:00
self.title.pack(side="top", padx=PADDING_TITLE, pady=PADDING_TITLE/2)
self.difficulty_title = Label(self.master)
self.difficulty_title.config(text="Current Difficulty: {}".format(self.options["Difficulty"]), font=("Courier New", 13))
2015-05-10 13:43:48 +01:00
self.difficulty_title.pack(ipadx=PADDING_BUTTON/3, padx=PADDING_BUTTON)
2015-05-12 18:46:30 +01:00
self.difficulty_scale = Scale(self.master, from_=5, to=500, orient="horizontal", command=self.update_difficulty, length=350)
2015-05-10 13:43:48 +01:00
self.difficulty_scale.pack(ipadx=PADDING_BUTTON/3, padx=PADDING_BUTTON/3)
self.difficulty_scale["value"] = self.options["Difficulty"]
2015-05-12 22:39:40 +01:00
self.difficulty_reset_button = Button(self.master, style="Minor.TButton")
self.difficulty_reset_button.config(text="Reset Difficulty")
self.difficulty_reset_button.pack(ipadx=PADDING_BUTTON/3, padx=PADDING_BUTTON/3)
self.difficulty_reset_button.bind('<Button-1>', self.reset_difficulty)
Frame(self.master,height=23).pack()
2015-05-07 19:38:43 +01:00
self.texture_title = Label(self.master)
self.texture_title.config(text="Select Texture Pack", font=("Courier New", 13))
self.texture_title.pack(ipadx=PADDING_BUTTON/3, padx=PADDING_BUTTON)
self.texture_select = Listbox(master)
self.texture_select.delete(0, END)
for directory in self.options["Textures"].list_packs():
if directory == "": continue
self.texture_select.insert(END, directory)
self.texture_select.pack(ipadx=PADDING_BUTTON/3, padx=PADDING_BUTTON/3)
2015-05-12 22:40:02 +01:00
Frame(self.master,height=23).pack()
self.using_sounds = IntVar()
self.using_sounds.set(self.options["Sounds"])
2015-05-13 09:30:30 +01:00
self.sounds_check = Checkbutton(self.master, text="Use Sound Effects", command=self.update_sounds, variable=self.using_sounds)
2015-05-12 22:40:02 +01:00
self.sounds_check.pack(ipadx=PADDING_BUTTON/3, padx=PADDING_BUTTON)
2015-05-10 13:43:48 +01:00
Style().configure("Minor.TButton", font=("Lucida", 10))
self.master.protocol('WM_DELETE_WINDOW', self.close)
2015-05-10 13:43:48 +01:00
logging.info("Options menu created.")
2015-05-12 22:40:02 +01:00
2015-05-07 19:38:43 +01:00
def close(self):
self.options["Textures"].pack = self.texture_select.get(ACTIVE) if self.texture_select.get(ACTIVE) != "" else "default" # Because it has no changed event
logging.info("Selected Texture Pack: {}".format(self.options["Textures"].pack))
2015-05-07 19:38:43 +01:00
self.master.destroy()
2015-05-12 22:40:02 +01:00
def update_sounds(self):
self.options["Sounds"] = self.using_sounds.get()
2015-05-10 13:43:48 +01:00
def update_difficulty(self, event):
self.options["Difficulty"] = int(self.difficulty_scale.get())
self.difficulty_title["text"] = "Current Difficulty: {}".format(self.options["Difficulty"])
def reset_difficulty(self, event):
self.options["Difficulty"] = 120
self.difficulty_title["text"] = "Current Difficulty: {}".format(self.options["Difficulty"])
self.difficulty_scale["value"] = 120
2015-04-22 09:52:09 +01:00
def display():
2015-04-21 11:00:31 +01:00
root=Tk()
root.resizable(width=False, height=False)
2015-04-22 09:52:09 +01:00
front_end=Main_Window(root)
2015-04-21 11:00:31 +01:00
root.mainloop()
2015-04-22 09:52:09 +01:00
2015-05-07 19:38:43 +01:00
exit_messages = {
"LIVES":"You ran out of lives!",
"PLAYER COLLISION":"The enemies got to you!"
}
2015-04-22 09:52:09 +01:00
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.DEBUG)
2015-04-21 11:00:31 +01:00
if __name__ == "__main__":
2015-04-22 09:52:09 +01:00
display()