-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.py
449 lines (348 loc) · 10.5 KB
/
main2.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# events-example0.py
# Barebones timer, mouse, and keyboard events
from tkinter import *
####################################
# 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.shore = not (self.inRaft.shore)
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
self.shore = False
def draw(self, canvas):
canvas.create_oval(self.x - self.r, self.y - self.r,
self.x + self.r, self.y + self.r,
fill='yellow')
class Wolf(Passenger):
def __init__(self, ycord):
super().__init__(ycord)
self.r = 10
self.shore = False
def draw(self, canvas):
canvas.create_oval(self.x - self.r, self.y - self.r,
self.x + self.r, self.y + self.r,
fill='grey')
class Chicken(Passenger):
def __init__(self, ycord):
super().__init__(ycord)
self.r = 10
self.shore = False
def draw(self, canvas):
canvas.create_oval(self.x - self.r, self.y - self.r,
self.x + self.r, self.y + self.r,
fill='red')
# global Variables (bleh)
isFull = False
grainOnBoard = False
chickenOnBoard = False
wolfOnBoard = False
running = False
raft = Raft()
chicken = Chicken(200)
wolf = Wolf(300)
grain = Grain(400)
studentInpFile = open('studentInput2.txt', 'r')
def init(data):
pass
def pause():
return
programPause = input("Press the <ENTER> key to execute next command...")
# This function is for the execute button on UI
def mousePressed(event, data):
global running
if (running):
pass
else:
running = True
if (325 < event.x < 400) and (575 < event.y < 600):
studentInput()
def conditionalAddGrain():
pause()
global isFull
print("Checking condition...")
# broken function to be fixed by student in 2A
if(isFull):
print("Raft is full!")
else:
addGrain()
def conditionalAddChicken():
pause()
global isFull
print("Checking condition...")
# broken function to be fixed by student in 2A
if(not isFull):
addChicken()
else:
print("Raft is full!")
def conditionalAddWolf():
pause()
global isFull
print("Checking condition...")
# broken function to be fixed by student in 2A
if(not isFull):
addWolf()
else:
print("Raft is full!")
def conditionalRemoveGrain():
pause()
global grainOnBoard
print("Checking condition...")
if(grainOnBoard):
removeGrain()
else:
print("Grain is not onboard!")
def conditionalRemoveChicken():
pause()
global chickenOnBoard
print("Checking condition...")
if(chickenOnBoard):
removeChicken()
else:
print("Chicken is not onboard!")
def conditionalRemoveWolf():
pause()
global wolfOnBoard
print("Checking condition...")
if(wolfOnBoard):
removeWolf()
else:
print("Wolf is not onboard!")
def wolfOnLeft():
global wolf
return wolf.shore
def chickenOnLeft():
global chicken
return chicken.shore
def grainOnLeft():
global grain
return grain.shore
def raftOnLeft():
global raft
return raft.position
def raftOnRight():
return not raftOnLeft()
def wolfOnRight():
return not wolfOnLeft()
def chickenOnRight():
return not chickenOnLeft()
def grainOnRight():
return not grainOnLeft()
def didIWin():
# this function will be rewritten in exercise 2B
if (wolfOnLeft() and chickenOnLeft() and grainOnLeft()):
return True
else:
return False
def finish():
# this function will be rewritten in exercise 2B
if (didIWin()):
print("Done!")
else:
print("Something is not right,yet!")
# for each of the exercises the student will type solution in this function
# Solutions can be found in exercises.py
def studentInput():
global studentInpFile2
try:
(eval(studentInpFile.readline()))
except:
print("Out of commands")
finish()
return
# 2A - demo broken function
# conditionalRemoveWolf() # <---- this should present a message for failure
# conditionalAddGrain()
# conditionalAddGrain() # <---- this should present a message for failure
# 2+ (solving the puzzle)
def removeGrain():
pause()
global raft, isFull, grainOnBoard
if raft.inRaft == None:
return
if grainOnBoard:
raft.deboardRaft()
isFull = False
grainOnBoard = False
def removeChicken():
pause()
global raft, isFull, chickenOnBoard
if raft.inRaft == None:
return
if chickenOnBoard:
raft.deboardRaft()
isFull = False
chickenOnBoard = False
def removeWolf():
pause()
global raft, isFull, wolfOnBoard
if raft.inRaft == None:
return
if wolfOnBoard:
raft.deboardRaft()
isFull = False
wolfOnBoard = False
def addGrain():
pause()
global raft, isFull, grainOnBoard, grain
# Remove condition for student to implement in 2A
if raft.inRaft != None:
return
if (grain.shore == raft.position):
raft.boardRaft(grain)
grainOnBoard = True
isFull = True
else:
print("Grain is not on this shore")
def addChicken():
pause()
global raft, isFull, chickenOnBoard, chicken
# Remove condition for student to implement in 2A
if raft.inRaft != None:
return
if (chicken.shore == raft.position):
raft.boardRaft(chicken)
chickenOnBoard = True
isFull = True
else:
print("Chicken is not on this shore")
def addWolf():
pause()
global raft, isFull, wolfOnBoard, wolf
# Remove condition for student to implement in 2A
if raft.inRaft != None:
return
if (wolf.shore == raft.position):
raft.boardRaft(wolf)
wolfOnBoard = True
isFull = True
else:
print("Wolf is not on this shore!")
def isValid():
# This function will be implemented in exercise 2C
if ((raftOnLeft() and ((wolfOnRight() and chickenOnRight()) or (chickenOnRight() and grainOnRight())))
or (raftOnRight() and ((wolfOnLeft() and chickenOnLeft()) or (chickenOnLeft() and grainOnLeft())))):
return False
else:
return True
def cross():
pause()
global raft
raft.cross()
if not(isValid()):
print("That was not a valid cross, Game is over!")
def keyPressed(event, data):
pass
def timerFired(data):
pass
def redrawAll(canvas, data):
drawBackground(canvas, data)
def drawBackground(canvas, data):
global raft, grain, wolf, chicken
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="START", anchor=SE, font=40)
# raft
raft.draw(canvas)
# rice
grain.draw(canvas)
wolf.draw(canvas)
chicken.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 = 200 # 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)