This repository has been archived by the owner on May 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modeController.py
92 lines (76 loc) · 2.77 KB
/
modeController.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
'''
Main program
'''
from tkinter import *
from startMode import *
from typeMode import *
from gameMode import *
from winMode import *
from loseMode import *
import sys
import webbrowser
def init(data):
data.curMode = startMode();root.geometry("600x600")
def mousePressed(event, data):
message = data.curMode.mousePressed(event)
if message == "Quit": root.quit();root.destroy();sys.exit()
elif message == "Help": webbrowser.open("https://oscarthzhang.github.io/")
elif message == "Type": data.curMode = typeMode();root.geometry("600x600")
elif isinstance(message, tuple): data.curMode = gameMode(message[1]);root.geometry("800x400")
elif message == "Start": data.curMode = startMode();root.geometry("600x600")
# geometry: https://stackoverflow.com/questions/13327659/python-tkinter-attempt-to-get-widget-size
def mouseMoved(event, data):
data.curMode.mouseMoved(event)
def keyPressed(event, data):
data.curMode.keyPressed(event)
def timerFired(data):
message = data.curMode.timerFired()
if message == 'Win': data.curMode = winMode()
elif message == 'Lose': data.curMode = loseMode()
def redrawAll(canvas, data):
data.curMode.redrawAll(canvas)
###############################################################################
# Run function
def run(width=800, height=600):
def redrawAllWrapper(canvas, data):
canvas.delete(ALL)
redrawAll(canvas, data)
canvas.update()
def mousePressedWrapper(event, canvas, data):
mousePressed(event, data)
redrawAllWrapper(canvas, data)
def mouseMovedWrapper(event, canvas, data):
mouseMoved(event, data)
redrawAllWrapper(canvas, data)
def keyPressedWrapper(event, canvas, data):
keyPressed(event, data)
redrawAllWrapper(canvas, data)
def timerFiredWrapper(canvas, data):
timerFired(data)
redrawAllWrapper(canvas, data)
# pause, then call timerFired again
canvas.after(data.timerDelay, timerFiredWrapper, canvas, data)
# Set up data and call init
class Struct(object): pass
data = Struct()
data.width = width
data.height = height
data.timerDelay = 10 # milliseconds
# create the root and the canvas
global root # globalize root; bad style !
root = Tk()
root.title("Zombie Valley")
canvas = Canvas(root, width=data.width, height=data.height)
canvas.pack()
# set up events
root.bind("<Button-1>", lambda event:
mousePressedWrapper(event, canvas, data))
root.bind("<Key>", lambda event:
keyPressedWrapper(event, canvas, data))
root.bind("<Motion>", lambda event:
mouseMovedWrapper(event, canvas, data))
init(data)
timerFiredWrapper(canvas, data)
root.mainloop() # blocks until window is closed
print("bye!")
run()