-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-part-1.py
216 lines (169 loc) · 5.38 KB
/
content-part-1.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# events-example0.py
# Barebones timer, mouse, and keyboard events
from tkinter import *
import time
####################################
# customize these functions
####################################
class Raft(object):
def __init__(self):
self.position = False # true is left, False is right
self.inRaft = None
self.leftX = 110
self.rightX = 290
self.x = self.rightX # center coordinates
self.y = 270
self.width = 35
self.height = 30
def cross(self):
self.position = not self.position
if self.position:
self.x = self.leftX
else:
self.x = self.rightX
if self.inRaft != None:
self.inRaft.x = self.x
self.inRaft.y = self.y
def draw(self, canvas):
canvas.create_rectangle(self.x - self.width / 2, self.y - self.height / 2,
self.x + self.width / 2, self.y + self.height / 2, fill="brown")
def boardRaft(self, obj):
self.inRaft = obj
obj.getInRaft(self)
def deboardRaft(self):
self.inRaft.getOffRaft(raft)
self.inRaft = None
class Passenger(object): # any passenger. Inherit from this for grain/wolf/chicken
def __init__(self, ycord):
self.position = False # true=left, None = inRaft, false = right
self.leftX = 75
self.rightX = 325
self.x = self.rightX # current spot
self.restingY = ycord # y cord for while not in boat
self.y = ycord
def getInRaft(self, raft):
self.position = None
self.x = raft.x
self.y = raft.y
def getOffRaft(self, raft):
self.position = raft.position
self.y = self.restingY
if self.position == False:
self.x = self.rightX
else:
self.x = self.leftX
class Grain(Passenger):
def __init__(self, ycord):
super().__init__(ycord)
self.r = 10
def draw(self, canvas):
canvas.create_oval(self.x - self.r, self.y - self.r,
self.x + self.r, self.y + self.r)
# global Variables (bleh)
raft = Raft()
grain1 = Grain(200)
grain2 = Grain(300)
grain3 = Grain(400)
grainList = [grain1, grain2, grain3]
def init(data):
pass
def mousePressed(event, data):
if (325 < event.x < 400) and (575 < event.y < 600):
studentInput()
def studentInput():
addGrain()
cross()
# removeGrain()
# cross()
# addGrain()
# cross()
# removeGrain()
# cross()
# addGrain()
# cross()
# removeGrain()
def removeGrain():
global raft, grainList
if raft.inRaft == None:
print("Raft already empty!")
return
raft.deboardRaft()
def addGrain():
global raft, grainList
if raft.inRaft != None:
print("Raft already has something in it!")
return
for grain in grainList:
if grain.position == raft.position:
raft.boardRaft(grain)
break
def cross():
global raft, grainList
raft.cross()
def keyPressed(event, data):
pass
def timerFired(data):
pass
def redrawAll(canvas, data):
drawBackground(canvas, data)
def drawBackground(canvas, data):
global raft, grainList
canvas.create_rectangle(0, 0, 400, 600, fill="green")
canvas.create_rectangle(100, 0, 300, 600, fill="blue")
canvas.create_rectangle(325, 575, 400, 600, fill="white")
canvas.create_text(400, 600, text="EXECUTE", anchor=SE, font=40)
# raft
raft.draw(canvas)
# rice
for grain in grainList:
grain.draw(canvas)
# person
# canvas.create_oval(raftX + 2, 260, raftX + 8, 266, fill = "black")
# canvas.create_line(raftX + 5, 266, raftX + 5, 280)
# canvas.create_line(raftX, 266, raftX + 5, 272)
# canvas.create_line(raftX + 5, 272, raftX + 10, 266)
# canvas.create_line(raftX, 286, raftX + 5, 280)
# canvas.create_line(raftX + 5, 280, raftX + 10, 286)
####################################
# use the run function as-is
####################################
def run(width, height):
def redrawAllWrapper(canvas, data):
canvas.delete(ALL)
canvas.create_rectangle(0, 0, data.width, data.height,
fill='white', width=0)
redrawAll(canvas, data)
canvas.update()
def mousePressedWrapper(event, canvas, data):
mousePressed(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 = 100 # milliseconds
init(data)
# create the root and the canvas
root = Tk()
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))
timerFiredWrapper(canvas, data)
# and launch the app
root.mainloop() # blocks until window is closed
print("bye!")
run(400, 600)