You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
6.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. from tkinter import *
  2. from tkinter.ttk import *
  3. import logging
  4. from os import system
  5. import game
  6. from assets import Textures
  7. PADDING_TITLE = 15
  8. PADDING_BUTTON = 9
  9. VERSION = {"ID":"2.2", "DATE": "19/05/15"}
  10. class Main_Window:
  11. def __init__(self, master):
  12. self.master = master
  13. self.master.title("ATTACK ON BLOCKS")
  14. self.title = Label(self.master)
  15. self.title.config(text="ATTACK ON BLOCKS!",font=("Courier New", 37))
  16. self.title.pack(side="top", padx=PADDING_TITLE, pady=PADDING_TITLE/2)
  17. self.version_info = Label(self.master)
  18. self.version_info.config(text="Version {ID} tagged {DATE}".format(**VERSION), font=("Courier New", 13))
  19. self.version_info.pack(side="top", padx=0, pady=PADDING_BUTTON/2)
  20. self.start_button = Button(self.master, style="Menu.TButton")
  21. self.start_button.config(text="Play Game")
  22. self.start_button.pack(fill=BOTH, ipadx=PADDING_BUTTON/2, ipady=PADDING_BUTTON/2, padx=PADDING_BUTTON, pady=PADDING_BUTTON)
  23. self.start_button.bind('<Button-1>', self.play_game)
  24. self.options_button = Button(self.master, style="Menu.TButton")
  25. self.options_button.config(text="Show Options")
  26. self.options_button.pack(fill=BOTH, ipadx=PADDING_BUTTON/2, ipady=PADDING_BUTTON/2, padx=PADDING_BUTTON, pady=PADDING_BUTTON)
  27. self.options_button.bind('<Button-1>', self.show_options)
  28. self.help_button = Button(self.master, style="About.TButton")
  29. self.help_button.config(text="About / Help")
  30. self.help_button.pack(fill=BOTH, ipadx=PADDING_BUTTON/4, ipady=PADDING_BUTTON/4, padx=PADDING_BUTTON, pady=PADDING_BUTTON)
  31. self.help_button.bind('<Button-1>', self.show_info)
  32. self.exit_button = Button(self.master, style="Quit.TButton")
  33. self.exit_button.config(text="Quit Game")
  34. self.exit_button.pack(ipadx=PADDING_BUTTON/3, ipady=PADDING_BUTTON, padx=PADDING_BUTTON, pady=PADDING_BUTTON)
  35. self.exit_button.bind('<Button-1>', self.close)
  36. self.about_info = Label(self.master)
  37. self.about_info.config(text="Game Created by TheOrangeOne!", font=("Courier New", 9))
  38. self.about_info.bind("<Button-1>", self.show_site)
  39. self.about_info.pack(side="top", padx=0, pady=PADDING_BUTTON/2)
  40. Style().configure("Menu.TButton", font=("Lucida", 21))
  41. Style().configure("About.TButton", font=("Lucida", 17))
  42. Style().configure("Quit.TButton", font=("Lucida", 13))
  43. self.options_window = Options_Window()
  44. logging.debug("GUI Generated.")
  45. def play_game(self, event):
  46. self.master.withdraw()
  47. exit_code = game.initialise(self.master, self.options_window.options)
  48. if exit_code != "QUIT": self.title["text"] = exit_messages[exit_code]
  49. def show_options(self, event):
  50. self.new_window = Toplevel(self.master)
  51. self.options_window.display(self.new_window)
  52. def show_info(self, event):
  53. logging.info("Loading About Page...")
  54. system("start https://bitbucket.org/theorangeone/attack-on-blocks/wiki/Home")
  55. def close(self, event):
  56. logging.critical("Closing Main Window.")
  57. self.master.destroy()
  58. def show_site(self, event):
  59. logging.info("Loading Website")
  60. system("start http://theorangeone.net")
  61. class Options_Window:
  62. def __init__(self):
  63. self.textures_object = Textures()
  64. self.options = {
  65. "Difficulty": 120,
  66. "Textures": self.textures_object,
  67. "Sounds": True
  68. }
  69. def display(self, master):
  70. self.master = master
  71. self.master.title("ATTACK ON BLOCKS - Options")
  72. self.title = Label(self.master)
  73. self.title.config(text="OPTIONS",font=("Courier New", 37))
  74. self.title.pack(side="top", padx=PADDING_TITLE, pady=PADDING_TITLE/2)
  75. self.difficulty_title = Label(self.master)
  76. self.difficulty_title.config(text="Current Difficulty: {}".format(self.options["Difficulty"]), font=("Courier New", 13))
  77. self.difficulty_title.pack(ipadx=PADDING_BUTTON/3, padx=PADDING_BUTTON)
  78. self.difficulty_scale = Scale(self.master, from_=5, to=500, orient="horizontal", command=self.update_difficulty, length=350)
  79. self.difficulty_scale.pack(ipadx=PADDING_BUTTON/3, padx=PADDING_BUTTON/3)
  80. self.difficulty_scale["value"] = self.options["Difficulty"]
  81. self.difficulty_reset_button = Button(self.master, style="Minor.TButton")
  82. self.difficulty_reset_button.config(text="Reset Difficulty")
  83. self.difficulty_reset_button.pack(ipadx=PADDING_BUTTON/3, padx=PADDING_BUTTON/3)
  84. self.difficulty_reset_button.bind('<Button-1>', self.reset_difficulty)
  85. Frame(self.master,height=23).pack()
  86. self.texture_title = Label(self.master)
  87. self.texture_title.config(text="Select Texture Pack", font=("Courier New", 13))
  88. self.texture_title.pack(ipadx=PADDING_BUTTON/3, padx=PADDING_BUTTON)
  89. self.texture_select = Listbox(master)
  90. self.texture_select.delete(0, END)
  91. for directory in self.options["Textures"].list_packs():
  92. if directory == "": continue
  93. self.texture_select.insert(END, directory)
  94. self.texture_select.pack(ipadx=PADDING_BUTTON/3, padx=PADDING_BUTTON/3)
  95. Frame(self.master,height=23).pack()
  96. self.using_sounds = IntVar()
  97. self.using_sounds.set(self.options["Sounds"])
  98. self.sounds_check = Checkbutton(self.master, text="Use Sound Effects", command=self.update_sounds, variable=self.using_sounds)
  99. self.sounds_check.pack(ipadx=PADDING_BUTTON/3, padx=PADDING_BUTTON)
  100. Style().configure("Minor.TButton", font=("Lucida", 10))
  101. self.master.protocol('WM_DELETE_WINDOW', self.close)
  102. logging.info("Options menu created.")
  103. def close(self):
  104. self.options["Textures"].pack = self.texture_select.get(ACTIVE) if self.texture_select.get(ACTIVE) != "" else "default" # Because it has no changed event
  105. logging.info("Selected Texture Pack: {}".format(self.options["Textures"].pack))
  106. self.master.destroy()
  107. def update_sounds(self):
  108. self.options["Sounds"] = self.using_sounds.get()
  109. def update_difficulty(self, event):
  110. self.options["Difficulty"] = int(self.difficulty_scale.get())
  111. self.difficulty_title["text"] = "Current Difficulty: {}".format(self.options["Difficulty"])
  112. def reset_difficulty(self, event):
  113. self.options["Difficulty"] = 120
  114. self.difficulty_title["text"] = "Current Difficulty: {}".format(self.options["Difficulty"])
  115. self.difficulty_scale["value"] = 120
  116. def display():
  117. root=Tk()
  118. root.resizable(width=False, height=False)
  119. front_end=Main_Window(root)
  120. root.mainloop()
  121. exit_messages = {
  122. "LIVES":"You ran out of lives!",
  123. "PLAYER COLLISION":"The enemies got to you!"
  124. }
  125. logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.DEBUG)
  126. if __name__ == "__main__":
  127. display()