forked from amalkhatib90/Project01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.py
More file actions
149 lines (106 loc) · 4.27 KB
/
menu.py
File metadata and controls
149 lines (106 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# @file menu.py
# Source file for the menu object
#
# Project: Minesweeper
# Author: Ayah Alkhatib
# Created: 09/08/18
from setup import Setup
import tkinter as Tk
from window_functions import center_window
# @class Menu
# @brief Prints menu and rules; Manages Executive instance
class Menu:
"""
Menu class to act as game menu
Attributes:
setup: Instance of the Setup class for getting user input to setup the board
root: instance of Tk root window
rules_window: tkinter window for the rules to display in
rules_displayed: flag if the rules are currently displayed or not
"""
# Constructor; initializes class variables
# @author: Ayah
def __init__(self, root):
"""
Constructor for Menu class
Initializes all attributes
Args:
root: instance of Tk root window
"""
# instance of the executive class
self.setup = None
# save root Tk window and set dimensions and centering
self.root = root
root.geometry("400x200")
center_window(self.root)
bg = "lightgreen"
# configure window title and color
root.title("Minesweeper 2018")
root.configure(bg=bg, bd=10, relief="ridge", pady=30)
root.resizable(width=False, height=False)
# create welcome text and display it
Tk.Label(root, text="Welcome to Minesweeper", bg=bg, font=('copperplate', 24)).pack()
# create buttons
play_button = Tk.Button(root, text="Play", command=self.start_game, highlightbackground=bg)
rules_button = Tk.Button(root, text="Rules", command=self.game_rules, highlightbackground=bg)
quit_button = Tk.Button(root, text="Quit", command=self.root.destroy, highlightbackground=bg)
# display buttons
play_button.pack()
rules_button.pack()
quit_button.pack()
# create member variable for rules window, but don't create the actual window yet
self.rules_window = None
# flag to prevent rules window from being created multiple times
self.rules_displayed = False
def start_game(self):
"""
Hides main menu window and displays the board setup window
"""
# hide the menu window, and the rules window if it is open
self.root.withdraw()
if self.rules_displayed:
self.rules_window.withdraw()
def show_callback():
# after game finishes (by either loss or win), display the windows again
self.root.deiconify()
if self.rules_displayed:
self.rules_window.deiconify()
# create board setup window
self.setup = Setup(self.root, show_callback)
def game_rules(self):
"""
Reads game instructions from game_instructions.txt and prints data
"""
if self.rules_displayed:
# if rules are already displayed, move them to the front and return
self.rules_window.lift()
return
# set displayed flag
self.rules_displayed = True
# create a new window
self.rules_window = Tk.Toplevel(self.root)
self.rules_window.geometry("550x425")
center_window(self.rules_window)
bg = "khaki"
# configure window title and color
self.rules_window.title("Rules")
self.rules_window.configure(bg=bg, bd=10, relief="ridge")
self.rules_window.resizable(width=False, height=False)
def on_rules_close():
"""
Destroys the rules window; sets window flag to false
"""
self.rules_displayed = False
self.rules_window.destroy()
# when window is closed, call function to set flag
self.rules_window.protocol("WM_DELETE_WINDOW", on_rules_close)
# also allow escape to close the window
self.rules_window.bind("<Escape>", lambda _: on_rules_close())
# create and show header label
Tk.Label(self.rules_window, bg=bg, text="----- How to Play -----").pack()
# read rules text from file
file = open("game_instructions.txt", "r")
rules_text = file.read()
file.close()
# display rules text
Tk.Label(self.rules_window, bg=bg, justify=Tk.LEFT, text=rules_text).pack()