-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
2087 lines (1719 loc) · 82.5 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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
============================================
| |
| 10.014 Computational Thinking for Design |
| 1D Project |
| |
| F05 2022 |
| Group C |
| |
| Zayne Siew (1007180) |
| Isaiah Rafael (1007211) |
| Tan Shin Herng (1006660) |
| Jeriel Ng Le Xuan (1006642) |
| Raymond Chia (1006911) |
| |
============================================
"""
import random
import os
import tkinter as tk
import math
# Constants for the game
_GAME_NAME = 'PixelCooked!'
_BACKGROUND = '#C0C0C0'
_OUTLINE = 'black'
_OUTLINE_WIDTH = 2
_REFRESH_IN_MS = 15
# NOTE: For Replit, 500x300 is the maximum dimensions
_LENGTH = 1320
_WIDTH = 660
# Constants for the players
_DIRECTIONS = ((0, -1), (0, 1), (-1, 0), (1, 0))
_P1 = ('w', 's', 'a', 'd', 'z')
_P2 = ('Up','Down', 'Left', 'Right', 'Return')
_P3 = ('t', 'g', 'f', 'h', 'space')
_P4 = ('i', 'k', 'j', 'l', 'm')
_PLAYERS = (_P1, _P2, _P3, _P4)
_COLOURS = ('blue', 'purple', 'orange', 'yellow')
_PLAYER_SIZE_PERCENT = 0.8
# Constants for the ingredients
_FISH_FILL = '#B245A5'
_LETTUCE_FILL = '#B9E3AB'
_BREAD_FILL = '#FDF3E8'
_INGREDIENT_SIZE_PERCENT = 0.6
all_ingredients = set()
"""
Classes for the different types of blocks
"""
class Block:
"""
Represents a simple block on the map.
Allows for the following:
- Placing ingredients on the block
"""
def __init__(self, screen, x, y, length, fill, outline=_OUTLINE):
self._block = screen.create_rectangle(x, y, x + length, y + length,
outline=outline,
fill=fill,
width=0 if outline is None else _OUTLINE_WIDTH)
self._ingredient = None
self._screen = screen
def get_coords(self):
return self._screen.coords(self._block)
def has_ingredient(self):
return self._ingredient is not None
def receive_ingredient(self, ingredient):
if not self.has_ingredient():
self._ingredient = ingredient
x1, y1, x2, y2 = self.get_coords()
ingredient.moveto(x1 + (1 - _INGREDIENT_SIZE_PERCENT) / 2 * (x2 - x1),
y1 + (1 - _INGREDIENT_SIZE_PERCENT) / 2 * (y2 - y1))
def remove_ingredient(self):
ingredient = self._ingredient
self._ingredient = None
return ingredient
class Placeholder(Block):
"""
Represents an inaccessible block on the map.
Does not allow for placing of ingredients.
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill='gray', outline=None)
def receive_ingredient(self, ingredient):
# Overrides the parent function to disable receiving of ingredients
pass
class Trash(Block):
"""
Represents the trash block on the map.
Allows for the following:
- Deletion of ingredients by placing them on the trash block
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill='#111111')
def receive_ingredient(self, ingredient):
# Overrides the parent function to delete ingredients once received
global all_ingredients
self._screen.delete(ingredient.get_ingredient())
all_ingredients.discard(ingredient)
class Crate(Block):
"""
Represents an ingredient crate on the map.
Allows for the following:
- Retrieval of ingredient by removing them from the crate block
"""
def receive_ingredient(self, ingredient):
# Overrides the parent function to disable receiving of ingredients
pass
def remove_ingredient(self):
# Abstract function that should be overriden in the child classes
pass
class FishCrate(Crate):
"""
Represents a crate of fish on the map.
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill=_FISH_FILL)
def remove_ingredient(self):
# Overrides the parent function to return Fish
global all_ingredients
# Create the new ingredient
x1, y1, x2, y2 = self.get_coords()
new_ingredient = Fish(self._screen,
x1 + (1 - _INGREDIENT_SIZE_PERCENT) / 2 * (x2 - x1),
y1 + (1 - _INGREDIENT_SIZE_PERCENT) / 2 * (y2 - y1),
(x2 - x1) * _INGREDIENT_SIZE_PERCENT)
# Update the list of all ingredients and return the ingredient
all_ingredients.add(new_ingredient)
return new_ingredient
class LettuceCrate(Crate):
"""
Represents a crate of lettuce on the map.
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill=_LETTUCE_FILL)
def remove_ingredient(self):
# Overrides the parent function to return Lettuce
global all_ingredients
# Create the new ingredient
x1, y1, x2, y2 = self.get_coords()
new_ingredient = Lettuce(self._screen,
x1 + (1 - _INGREDIENT_SIZE_PERCENT) / 2 * (x2 - x1),
y1 + (1 - _INGREDIENT_SIZE_PERCENT) / 2 * (y2 - y1),
(x2 - x1) * _INGREDIENT_SIZE_PERCENT)
# Update the list of all ingredients and return the ingredient
all_ingredients.add(new_ingredient)
return new_ingredient
class BreadCrate(Crate):
"""
Represents a crate of bread on the map.
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill=_BREAD_FILL)
def remove_ingredient(self):
# Overrides the parent function to return Bread
global all_ingredients
# Create the new ingredient
x1, y1, x2, y2 = self.get_coords()
new_ingredient = Bread(self._screen,
x1 + (1 - _INGREDIENT_SIZE_PERCENT) / 2 * (x2 - x1),
y1 + (1 - _INGREDIENT_SIZE_PERCENT) / 2 * (y2 - y1),
(x2 - x1) * _INGREDIENT_SIZE_PERCENT)
# Update the list of all ingredients and return the ingredient
all_ingredients.add(new_ingredient)
return new_ingredient
class ServingBlock(Block):
"""
Represents a serving block on the map for players to deliver the completed orders.
Allows for the following:
- Serving of ingredients by placing them on the serving block
- Completing orders by using one or more ingredients stored here
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill='green')
self.container = {'Sashimi': 0, 'FishFillet': 0, 'FriedFish': 0, 'Fish': 0, 'Salad': 0, 'Lettuce': 0, 'Crouton': 0, 'Breadpiece': 0, 'Toast': 0, 'Bread': 0}
self.current_order = []
self.current_order.append(self.generate_order()) #appends 3 generated orders to current_order
self.current_order.append(self.generate_order())
self.current_order.append(self.generate_order())
self.order_name = []
self.update_order()
self.score = 0
self.framelength = 0
self.orderframe = 0
def ingre_color(self,food_type):
#returns colour of food to be used by label for order menu
colordict = {'Sashimi':'#F56C57' , 'FishFillet':'#D3A44A' , 'FriedFish':'#A26F25' , 'Fish':'#B245A5' , 'Salad':'#9B9246' , 'Lettuce':'#B9E3AB' , 'Crouton':'#80471C' , 'Breadpiece':'#E3D5C5' , 'Toast':'#E3A10F' , 'Bread':'#FDF3E8' , '-':'grey'}
return colordict[food_type]
def check_order(self):
#checks current order and if self.container has enough then removes that set of ingredients from self.container and prompts next order. Does nothing if container does not have enough ingredients.
x = 0
for i in range(1, 4):
if self.current_order[0][i]== '-' or self.container[self.current_order[0][i]] > 0:
x += 1
if x == 3:
for i in range(1, len(self.current_order[0])):
if self.current_order[0][i] =='-': pass
else: self.container[self.current_order[0][i]] -= 1
self.update_order()
self.score += 1
print('score: ', self.score)
print('order done, order menu should refresh')
def receive_ingredient(self, ingredient):
# Overrides the parent function to delete ingredients once received, then increases quantity of ingredient in container by 1 and runs check_order()
global all_ingredients
self.container[ingredient.get_food_type()] += 1
self._screen.delete(ingredient.get_ingredient())
all_ingredients.discard(ingredient)
self.check_order()
self.update_menu(self.orderframe,self.framelength)
def remove_ingredient(self):
pass #disable removing of ingredients
def destroy_menu(self,order_frame):
#destroys menu
for widgets in order_frame.winfo_children():
widgets.destroy()
def update_menu(self,order_frame, length):
#updates menu by destroying then drawing another order menu
self.destroy_menu(order_frame)
self.draw_order_menu(order_frame, length)
def draw_order_menu(self, order_frame, length):
header_font = ('Comic Sans MS', 15)
subheader_font = ('Comic Sans MS', 12)
caption_font = ('Comic Sans MS', 10)
#score frame
score_frame = tk.Frame(order_frame,bg='#c06c84', width=length * 0.3) #main frame for order 1
score_frame.pack(side=tk.LEFT, anchor=tk.CENTER, padx=3, fill=tk.X)
score_label = tk.Label(score_frame, text = f'score: {self.score}', bd = 2, font=header_font,bg='grey')
score_label.pack(side=tk.LEFT, anchor=tk.CENTER, fill=tk.X)
"""
#inventory of serving block
inventory_frame = tk.Frame(order_frame,bg='#c06c84', width=length * 0.3) #main frame for order 1
inventory_frame.pack(side = tk.RIGHT)
inventory = tk.Label(inventory_frame, text = self.container, bd = 2, font=caption_font,bg='#c06c84')
inventory.pack(side= tk.RIGHT)
"""
order_label = tk.Label(order_frame, bg=_BACKGROUND, text='Orders:',
font=header_font, justify=tk.CENTER)
order_label.pack(side=tk.LEFT, anchor=tk.CENTER, padx=2)
for title, ingredient_1, ingredient_2, ingredient_3 in self.current_order:
# Create main frame for order
suborder_frame = tk.Frame(order_frame,bg='#355c7d', width=length * 0.3,
borderwidth=0, highlightbackground='black', highlightthickness=2)
suborder_frame.pack(side=tk.LEFT, anchor=tk.CENTER, padx=1, fill=tk.BOTH, expand=True)
# Create top frame for order
title_frame = tk.Frame(suborder_frame,bg='#355c7d', width=length * 0.3)
title_frame.pack(side = tk.TOP, anchor=tk.CENTER, fill=tk.X, expand=True)
title_label = tk.Label(title_frame, bd=3, bg='#c06c84', text=title,
font=header_font, justify=tk.CENTER)
title_label.pack(side=tk.TOP, anchor=tk.CENTER, fill=tk.BOTH, expand=True)
# Create bottom frame for ingredients required
ingredient_frame = tk.Frame(suborder_frame, bg='#355c7d', width=length * 0.3)
ingredient_frame.pack(side = tk.TOP, anchor=tk.CENTER, fill=tk.X, expand=True)
ingredient_1_label = tk.Label(ingredient_frame, bg=self.ingre_color(ingredient_1), bd=2,
text=ingredient_1 + ('' if ingredient_1 == '-' else f' - {self.container[ingredient_1]}'),
font=subheader_font, justify=tk.CENTER)
ingredient_1_label.pack(side=tk.LEFT, anchor=tk.CENTER, padx=1, fill=tk.BOTH, expand=True)
ingredient_2_label = tk.Label(ingredient_frame, bg=self.ingre_color(ingredient_2), bd=2,
text=ingredient_2 + ('' if ingredient_2 == '-' else f' - {self.container[ingredient_2]}'),
font=subheader_font, justify=tk.CENTER)
ingredient_2_label.pack(side=tk.LEFT, anchor=tk.CENTER, padx=1, fill=tk.BOTH, expand=True)
ingredient_3_label = tk.Label(ingredient_frame, bg=self.ingre_color(ingredient_3), bd=2,
text=ingredient_3 + ('' if ingredient_3 == '-' else f' - {self.container[ingredient_3]}'),
font=subheader_font, justify=tk.CENTER)
ingredient_3_label.pack(side=tk.LEFT, anchor=tk.CENTER, padx=1, fill=tk.BOTH, expand=True)
"""
#order-3 frame
order_3_frame = tk.Frame(order_frame, width=length * 0.3,bg='#355c7d',
borderwidth=0, highlightbackground='black', highlightthickness=2) #main frame for order 1
order_3_frame.pack(side = tk.RIGHT)
order_3a_frame = tk.Frame(order_3_frame,bg='white', width=length * 0.3) #top frame for order 3
order_3a_frame.pack(side = tk.TOP)
order_3a= tk.Label(order_3a_frame, text = self.current_order[2][0], bd = 3, font=header_font,bg='#c06c84')
order_3a.pack()
order_3b_frame = tk.Frame(order_3_frame,bg='white', width=length * 0.3)
order_3b_frame.pack(side = tk.TOP)
order_3b_1 = tk.Label(order_3b_frame,text= self.current_order[2][1], bd = 2, font=subheader_font,bg=self.ingre_color(self.current_order[2][1]))
order_3b_1.pack(side=tk.LEFT)
order_3b_2 = tk.Label(order_3b_frame, text= self.current_order[2][2], bd = 2, font=subheader_font,bg=self.ingre_color(self.current_order[2][2]))
order_3b_2.pack(side=tk.LEFT)
order_3b_3 = tk.Label(order_3b_frame, text= self.current_order[2][3], bd = 2, font=subheader_font,bg=self.ingre_color(self.current_order[2][3]))
order_3b_3.pack(side=tk.LEFT)
#order-2 frame
order_2_frame = tk.Frame(order_frame, width=length * 0.3,bg='#355c7d',
borderwidth=0, highlightbackground='black', highlightthickness=2) #main frame for order 2
order_2_frame.pack(side = tk.RIGHT)
order_2a_frame = tk.Frame(order_2_frame,bg='white', width=length * 0.3) #top frame for order 2
order_2a_frame.pack(side = tk.TOP)
order_2a= tk.Label(order_2a_frame, text = self.current_order[1][0], bd = 3, font=header_font,bg='#c06c84')
order_2a.pack()
order_2b_frame = tk.Frame(order_2_frame,bg='white', width=length * 0.3)
order_2b_frame.pack(side = tk.TOP)
order_2b_1 = tk.Label(order_2b_frame,text= self.current_order[1][1], bd = 2, font=subheader_font,bg=self.ingre_color(self.current_order[1][1]))
order_2b_1.pack(side=tk.LEFT)
order_2b_2 = tk.Label(order_2b_frame, text= self.current_order[1][2], bd = 2, font=subheader_font,bg=self.ingre_color(self.current_order[1][2]))
order_2b_2.pack(side=tk.LEFT)
order_2b_3 = tk.Label(order_2b_frame, text= self.current_order[1][3], bd = 2, font=subheader_font,bg=self.ingre_color(self.current_order[1][3]))
order_2b_3.pack(side=tk.LEFT)
"""
self.orderframe = order_frame
self.orderLength = length
def update_order(self):
#is called when current order is completed. shifts all orders up by 1 and generates a new order using generate_order()
self.current_order[0] = self.current_order[1]
self.current_order[1] = self.current_order[2]
self.current_order[2] = self.generate_order()
def generate_order(self):
#randomly chooses an order from a list that is stored here and returns it as a list of ingredients. 1st element is name of dish, subsequent elements are the ingredients needed
list_of_orders = [('Sashimi','Sashimi', '-', '-'), ('FishFillet','FishFillet', '-', '-'), ('SashimiSalad','Sashimi','Salad', '-'), ('Salad','Salad', '-', '-'), ('Crouton','Crouton', '-', '-'), ('CroutonSalad','Salad','Crouton', '-'), ('FishFilletSandwich','FishFillet','Salad','Toast'),('Toast','Toast', '-', '-')]
order = random.choice(list_of_orders)
return order
class ProcessBlock(Block):
"""
Represents a processing block on the map.
Allows for the following:
- Placing and removing of ingredients
- Changing one ingredient to another via the specified process
"""
_BAR_WIDTH_PERCENT = 0.1
def __init__(self, screen, x, y, length, fill, process, time):
# process is a callable function (either chop() or cook() function)
# time is the amount of time that should pass for the process to complete
super().__init__(screen, x, y, length, fill)
self._timer = self._max_time = time
self._process = process
self._to_process = False
# create the base outline for the loading bar
# the base outline is represented by 4 white rectangles on the border of the block
_ = self._draw_border_rectangles(1, 'white')
# initialise the loading bar
# the loading bar is represented by 4 rectangles on top of the base outline
# they will be updated in both position and colour to reflect an actual loading bar
self._loading_bar = []
def _draw_border_rectangles(self, frac, fill):
"""
This function draws the 4 rectangles that border the block to reflect a loading bar.
The 4 rectangles are returned in an anticlockwise manner, starting from the top left.
"""
x1, y1, x2, y2 = self.get_coords()
length = x2 - x1
return \
None if frac == 0 else self._screen.create_rectangle(
x1,
y1,
x1 + length * (1 - self._BAR_WIDTH_PERCENT) * 4 * min(frac, 0.25),
y1 + length * self._BAR_WIDTH_PERCENT,
fill=fill, width=0
), \
None if frac <= 0.25 else self._screen.create_rectangle(
x2 - length * self._BAR_WIDTH_PERCENT,
y1,
x2,
y1 + length * (1 - self._BAR_WIDTH_PERCENT) * 4 * (min(frac, 0.5) - 0.25),
fill=fill, width=0
), \
None if frac <= 0.5 else self._screen.create_rectangle(
x2,
y2 - length * self._BAR_WIDTH_PERCENT,
x2 - length * (1 - self._BAR_WIDTH_PERCENT) * 4 * (min(frac, 0.75) - 0.5),
y2,
fill=fill, width=0
), \
None if frac <= 0.75 else self._screen.create_rectangle(
x1,
y2,
x1 + length * self._BAR_WIDTH_PERCENT,
y2 - length * (1 - self._BAR_WIDTH_PERCENT) * 4 * (frac - 0.75),
fill=fill, width=0
)
def _clear_loading_bar(self):
"""
This function clears the displayed loading bar on the screen.
"""
while self._loading_bar:
self._screen.delete(self._loading_bar.pop())
def _update_loading_bar(self):
"""
This function reflects the current timer on screen
by displaying a loading bar wrapped around the block on screen.
The bar is filled in proportion to how much time has passed from the timer
and coloured on a scale from red to green using the same proportion.
The RGB values from red to green increases from 0 to 255 from green first,
followed by decreasing from 255 to 0 from red.
"""
# remove the previous loading bar from the screen
self._clear_loading_bar()
# obtain colour based on fraction of time passed
frac = (self._max_time - self._timer) / self._max_time
red = round(255 * (1 - 2 * max(frac - 0.5, 0)))
green = round(255 * 2 * min(frac, 0.5))
r1, r2 = divmod(red, 16)
g1, g2 = divmod(green, 16)
chars = '0123456789ABCDEF'
fill = f'#{chars[r1]}{chars[r2]}{chars[g1]}{chars[g2]}00'
# draw rectangles
self._loading_bar.extend(self._draw_border_rectangles(frac, fill))
def update_timer(self, dt):
"""
This functions takes in dt, the number of milliseconds that have passed.
Updates the timer and loading bar to reflect the passing of dt milliseconds.
Once the timer hits zero, performs the process and resets the timer.
"""
self._timer-=dt
#self._update_loading_bar()
if self._timer <0:
return 0
else:
return self._timer
def reset_timer(self):
"""
This function resets the timer and loading bar of the block.
"""
self._timer = self._max_time
self._update_loading_bar() #add loading bar if time is !=0
def receive_ingredient(self, ingredient):
# Overrides the parent function to start the timer once the ingredient is received
# The timer is started by indicating that the ingredient should be processed
self._to_process = self.has_ingredient()
super().receive_ingredient(ingredient)
def remove_ingredient(self):
# Overrides the parent function to stop and reset the timer first
# before returning the ingredient on the block
self.reset_timer()
return super().remove_ingredient()
class ChoppingBlock(ProcessBlock):
"""
Represents a chopping block on the map.
Allows for the following:
- Placing and removing of ingredients
- Chopping (calling the chop() function) of ingredients
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length,
fill='#F5F5DC',
process=lambda ingredient: ingredient.chop(),
time=5)
class CookingBlock(ProcessBlock):
"""
Represents a cooking block on the map.
Allows for the following:
- Placing and removing of items
- Cooking (calling of the cook() function) of ingredients
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length,
fill='#ED820E',
process=lambda ingredient: ingredient.cook(),
time=10)
"""
Classes for the different types of ingredients
"""
class Ingredient:
"""
Represents a generic interactible ingredient on the map.
Allows for the following:
- Picking up / placing down
- Moving around
- Chopping and cooking
"""
def __init__(self, screen, x, y, length, fill, *, can_chop=False, can_cook=False):
# (x, y) is the top left corner
self._ingredient = screen.create_oval(x, y, x + length, y + length,
outline=_OUTLINE,
fill=fill,
width=_OUTLINE_WIDTH)
self._player = None
self._screen = screen
self._can_chop = can_chop
self._can_cook = can_cook
self._type = 'None'
def get_food_type(self):
return self._type
def get_ingredient(self):
return self._ingredient
def get_coords(self):
return self._screen.coords(self._ingredient)
def move(self, x, y):
self._screen.move(self._ingredient, x, y)
def moveto(self, x, y):
self._screen.moveto(self._ingredient, x, y)
def drop(self, x=None, y=None):
"""
This function takes an (x, y) coordinate to drop the ingredient to.
Moves the ingredient to those coordinates and removes any player owner.
"""
if self._player and x is not None and y is not None:
self.moveto(x, y)
self._player = None
def pick_up(self, player):
"""
This function takes the player that is about to pick up this ingredient.
Moves the ingredient to the player coordinates and sets the player as owner.
"""
if self._player:
# another player has already picked this ingredient up
return
self._player = player
x1, y1, x2, y2 = player.get_coords()
x3, _, x4, _ = self.get_coords()
x = x1 + x3 - x4 if player.get_direction() == Player.LEFT() else \
x2 if player.get_direction() == Player.RIGHT() else \
(x1 + x2 + x3 - x4) / 2
y = y1 + x3 - x4 if player.get_direction() == Player.UP() else \
y2 if player.get_direction() == Player.DOWN() else \
(y1 + y2 + x3 - x4) / 2
self.moveto(x, y)
def can_chop(self):
"""
This function indicates if the ingredient can be chopped.
"""
return self._can_chop
def can_cook(self):
"""
This function indicates if the ingredient can be cooked.
"""
return self._can_cook
def chop(self):
"""
This function returns self if the ingredient cannot be chopped.
Otherwise, it return the resultant ingredient from the chopping process.
Abstract method to be overriden in child(ren) class(es).
"""
return self
def cook(self):
"""
This function returns self if the ingredient cannot be cooked.
Otherwise, it return the resultant ingredient from the cooking process.
Abstract method to be overriden in child(ren) class(es).
"""
return self
class Sashimi(Ingredient):
"""
Represents the sashimi ingredient.
Allows for the following:
- Combines with Salad to form SashimiSalad
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill='#F56C57')
self._type = 'Sashimi'
class FishFillet(Ingredient):
"""
Represents the fish fillet ingredient.
Allows for the following:
- Combines with Salad and Toast to form FishFilletSandwich
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill='#D3A44A')
self._type = 'FishFillet'
class FriedFish(Ingredient):
"""
Represents the fried fish ingredient.
Allows for the following:
- Can be chopped to form FishFillet
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill='#A26F25', can_chop=True)
self._type = 'FriedFish'
def chop(self):
# Overrides the parent function to return FishFillet
global all_ingredients
# Update the game screen
x1, y1, x2, _ = self.get_coords()
self._screen.delete(self._ingredient)
new_ingredient = FishFillet(self._screen, x1, y1, x2 - x1)
# Update the list of all ingredients
all_ingredients.discard(self)
all_ingredients.add(new_ingredient)
# Return the new FishFillet ingredient
return new_ingredient
class Fish(Ingredient):
"""
Represents the raw fish ingredient.
Allows for the following:
- Can be chopped to form Sashimi
- Can be cooked to form FriedFish
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill=_FISH_FILL, can_chop=True, can_cook=True)
self._type = 'Fish'
def chop(self):
# Overrides the parent function to return Sashimi
global all_ingredients
# Update the game screen
x1, y1, x2, _ = self.get_coords()
self._screen.delete(self._ingredient)
new_ingredient = Sashimi(self._screen, x1, y1, x2 - x1)
# Update the list of all ingredients
all_ingredients.discard(self)
all_ingredients.add(new_ingredient)
# Return the new Sashimi ingredient
return new_ingredient
def cook(self):
# Overrides the parent function to return FriedFish
global all_ingredients
# Update the game screen
x1, y1, x2, _ = self.get_coords()
self._screen.delete(self._ingredient)
new_ingredient = FriedFish(self._screen, x1, y1, x2 - x1)
# Update the list of all ingredients
all_ingredients.discard(self)
all_ingredients.add(new_ingredient)
# Return the new FriedFish ingredient
return new_ingredient
class Salad(Ingredient):
"""
Represents the salad ingredient.
Allows for the following:
- Combines with Sashimi to form SashimiSalad
- Combines with FishFillet and Toast to form FishFilletSandwich
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill='#9B9246')
self._type ='Salad'
class Lettuce(Ingredient):
"""
Represents the lettuce ingredient.
Allows for the following:
- Can be chopped to form Salad
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill=_LETTUCE_FILL, can_chop=True)
self._type = 'Lettuce'
def chop(self):
# Overrides the parent function to return Salad
global all_ingredients
# Update the game screen
x1, y1, x2, _ = self.get_coords()
self._screen.delete(self._ingredient)
new_ingredient = Salad(self._screen, x1, y1, x2 - x1)
# Update the list of all ingredients
all_ingredients.discard(self)
all_ingredients.add(new_ingredient)
# Return the new Salad ingredient
return new_ingredient
class Crouton(Ingredient):
"""
Represents the croutons ingredient.
Allows for the following:
- Combines with Salad to form CroutonSalad
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill='#80471C')
self._type = 'Crouton'
class BreadPiece(Ingredient):
"""
Represents the bread pieces ingredient.
Allows for the following:
- Can be cooked to form Crouton
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill='#E3D5C5', can_cook=True)
self._type = 'BreadPiece'
def cook(self):
# Overrides the parent function to return Crouton
global all_ingredients
# Update the game screen
x1, y1, x2, _ = self.get_coords()
self._screen.delete(self._ingredient)
new_ingredient = Crouton(self._screen, x1, y1, x2 - x1)
# Update the list of all ingredients
all_ingredients.discard(self)
all_ingredients.add(new_ingredient)
# Return the new Crouton ingredient
return new_ingredient
class Toast(Ingredient):
"""
Represents the toast ingredient.
Allows for the following:
- Combines with FishFillet and Salad to form FishFilletSandwich
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill='#E3A10F')
self._type = 'Toast'
class Bread(Ingredient):
"""
Represents the bread ingredient.
Allows for the following:
- Can be chopped to form BreadPiece
- Can be cooked to form Toast
"""
def __init__(self, screen, x, y, length):
super().__init__(screen, x, y, length, fill=_BREAD_FILL, can_chop=True, can_cook=True)
self._type = 'Bread'
def chop(self):
# Overrides the parent function to return BreadPiece
global all_ingredients
# Update the game screen
x1, y1, x2, _ = self.get_coords()
self._screen.delete(self._ingredient)
new_ingredient = BreadPiece(self._screen, x1, y1, x2 - x1)
# Update the list of all ingredients
all_ingredients.discard(self)
all_ingredients.add(new_ingredient)
# Return the new BreadPiece ingredient
return new_ingredient
def cook(self):
# Overrides the parent function to return Toast
global all_ingredients
# Update the game screen
x1, y1, x2, _ = self.get_coords()
self._screen.delete(self._ingredient)
new_ingredient = Toast(self._screen, x1, y1, x2 - x1)
# Update the list of all ingredients
all_ingredients.discard(self)
all_ingredients.add(new_ingredient)
# Return the new Toast ingredient
return new_ingredient
"""
The player class
"""
class Player:
"""
Represents a human player.
Allows for the following:
- Moving around (with keyboard keys)
- Picking up / move / drop ingredients
- Collision with blocks
"""
_UP, _DOWN, _LEFT, _RIGHT = range(4)
_RADIUS = 1.5
def __init__(self, screen, x, y, length, velocity, fill):
# (x, y) is the top left corner
self._ingredient = None
self._direction = self._UP
self._vx = self._vy = 0
self._interact = False
self._screen = screen
self._velocity = velocity
self._fill = fill
triangle = ((x, y + length),
(x + 0.5 * length, y),
(x + length, y + length))
self._player = self._create(triangle)
def _create(self, triangle):
return self._screen.create_polygon(triangle,
outline=_OUTLINE,
fill=self._fill,
width=_OUTLINE_WIDTH)
@classmethod
def UP(cls):
return cls._UP
@classmethod
def DOWN(cls):
return cls._DOWN
@classmethod
def LEFT(cls):
return cls._LEFT
@classmethod
def RIGHT(cls):
return cls._RIGHT
def get_coords(self):
x1, y1, x2, y2, x3, y3 = self._screen.coords(self._player)
return min(x1, x2, x3), min(y1, y2, y3), max(x1, x2, x3), max(y1, y2, y3)
def get_direction(self):
return self._direction
def interact(self, block, ingredient):
"""
This function handles the interaction update of the player.
Whenever this function is called, the function first checks if the button
for the player to interact has been pressed.
If so, the function takes into account the nearest block and ingredient
(accounting for player direction) and performs the relevant interaction
according to the type and scenario of the interaction.
"""
# Check if there is a need to interact
if self._interact == False:
return
dist_block=distance(self,block)
dist_ing = float('inf') if ingredient is None else distance(self,ingredient)
# dist_min is the minimum distance from the player
# to either the block or the ingredient
if dist_block < dist_ing:
dist_min = dist_block
else:
dist_min = dist_ing
# check if object is within radius
x1, _, x2, _ = self.get_coords()
radius = (x2 - x1) * self._RADIUS
print(dist_min, radius)
if dist_min > radius:
# if player has ingredient, drop ingredient
# Case 5
if self.has_ingredient():
self.drop_ingredient()
# else, do nothing
# Case 0