-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
114 lines (93 loc) · 3.46 KB
/
main.py
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
from tkinter_ex import *
from PIL import Image, ImageTk
from time import time
from stateView import StateView
from settingsWindow import SettingsWindow
from numberDisplay import NumberDisplay
import highscores
from state import State
class ImageButton(Button):
def __init__(self, root, filename, **kwargs):
self.photo = ImageTk.PhotoImage(Image.open(filename))
Button.__init__(self, root, image=self.photo, **kwargs)
def set_image(self, filename):
self.photo = ImageTk.PhotoImage(Image.open(filename))
self.config(image = self.photo)
def state_changed(event, *args):
global start_time
mine_counter.set(state.num_mines - state.state_count(state.flagged))
if event == "started":
start_time = time()
elif event == "game_ended":
if state.game_state == state.lost:
reset_button.set_image("images/mine.png")
elif state.game_state == state.won:
reset_button.set_image("images/flagged.png")
cur_time = int(time() - start_time)
if highscores.qualifies(cur_difficulty[0], cur_time):
name = askstring("Enter name", "New high score! Enter name:", initialvalue = "Anonymous", parent=root)
if name:
highscores.update_scores(cur_difficulty[0], name, cur_time)
def on_idle():
if state.game_state == state.in_progress:
cur_time = int(time() - start_time)
timer.set(min(cur_time, 999))
root.after(100, on_idle)
def new_game():
global state_view, state, start_time
if state_view is not None:
state_view.grid_forget()
state.unbind(state_view.state_changed) #bit of an encapsulation violation here...
state = State(*cur_difficulty[1:])
state.bind(state_changed)
state_view = StateView(root, state)
state_view.grid(row=1, column=0)
reset_button.set_image("images/covered.png")
mine_counter.set(state.num_mines)
timer.set(None)
def show_settings():
global cur_difficulty
window = SettingsWindow(root, cur_difficulty)
window.grab_set()
root.wait_window(window)
if window.setting_chosen != None:
cur_difficulty = window.setting_chosen
new_game()
def show_high_scores():
window = highscores.HighScoreWindow(root)
window.grab_set()
root.wait_window(window)
def debug():
pass
import random
#beginner: 9,9,10
#intermediate: 16, 16, 40
#advanced: 16, 30, 99
cur_difficulty = ("Intermediate", 16, 16, 40)
root = Tk()
menubar = Menu(root)
root.config(menu=menubar)
gamemenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Game", menu=gamemenu)
gamemenu.add_command(label = "New", command=new_game)
gamemenu.add_command(label = "Difficulty", command=show_settings)
gamemenu.add_command(label = "High Scores", command=show_high_scores)
#gamemenu.add_command(label = "Debug", command=debug)
gamemenu.add_command(label = "Exit", command=root.quit)
action_bar = Frame(root)
action_bar.grid_columnconfigure(0, weight=1)
action_bar.grid_columnconfigure(1, weight=1)
action_bar.grid_columnconfigure(2, weight=1)
timer = NumberDisplay(action_bar)
timer.grid(row=0, column=0, sticky="w")
reset_button = ImageButton(action_bar, "images/covered.png", command=new_game)
reset_button.grid(row=0, column=1)
mine_counter = NumberDisplay(action_bar)
mine_counter.grid(row=0,column=2, sticky="e")
action_bar.grid(row=0, column=0, sticky="we")
state = None
state_view = None
start_time = None
new_game()
root.after(100, on_idle)
root.mainloop()