-
Notifications
You must be signed in to change notification settings - Fork 0
/
2000.py
1707 lines (1493 loc) · 63.1 KB
/
2000.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
################################################################################
## Krishna Dave, kdave
## Section FF
## Term Project
# Mentor: Chaya Wurman
################################################################################
'''
References:
mode-demo.py, Class Notes
*****************************************************************
Cropping Images Using PIL
*****************************************************************
https://pillow.readthedocs.io/en/3.0.0/reference/ImageColor.html
http://matthiaseisen.com/pp/patterns/p0202/
****************************************************************
PIL Handbook
****************************************************************
http://effbot.org/imagingbook/pil-index.htm
****************************************************************
Thresholding and Color Detection:
****************************************************************
http://vkedco.blogspot.com/2012/08/edge-detection-with-python-pil.html
https://www.youtube.com/watch?v=jdgTNRDt7Ik
http://stackoverflow.com/questions/9319767/image-outline-using-python-pil
*******************************************************************
'''
import math
import random
from tkinter import *
import PIL
from PIL import ImageTk, Image, ImageDraw, ImageFont
from PIL import Image, ImageFilter
import os
import time
####################################
# init
####################################
def init(data, canvas):
data.mode = "intro"
data.prevmode = "intro"
############################################################################
# Intro Screen
loadBackground(data, canvas)
############################################################################
# Animation
loadAnim1(data, canvas)
loadAnim2(data,canvas)
loadAnim3(data, canvas)
loadAnim4(data, canvas)
data.Anim1X1, data.Anim1Y1 = 100, 110
data.Anim2X1, data.Anim2Y1 = 110, data.height-100
data.Anim3X1, data.Anim3Y1 = data.width-110, 110
data.Anim4X1, data.Anim4Y1 =data.width-100, data.height-100
data.Anim1dx, data.Anim4dx, data.Anim3dy, data.Anim2dy = 8, 8, 8, 8
############################################################################
# Help
data.helpRadius = 25
data.margin = 50
data.helpCx = data.width - data.margin
data.helpCy = data.height - data.margin
loadInstructions(data, canvas)
# Buttons: Build and Solve
data.introBuildX1 = data.width/2-170
data.introBuildX2 = data.width/2-30
data.introBuildY1 = data.height/2+50
data.introBuildY2 = data.height/2+100
data.introSolveX1 = data.width/2+30
data.introSolveX2 = data.width/2+170
data.introSolveY1 = data.height/2+50
data.introSolveY2 = data.height/2+100
############################################################################
# Constructor
loadConBackground(data, canvas)
data.step = 150
data.easyX1 = data.width/3 - 50
data.easyX2 = data.width/3 + 50
data.easyY1 = data.height/2 - 25 + 100
data.easyY2 = data.height/2 + 25 + 100
data.hardX1 = data.width *2/3 - 50
data.hardX2 = data.width *2/3 + 50
data.hardY1 = data.height/2 - 25 + 100
data.hardY2 = data.height/2 + 25 + 100
data.medX1 = data.width/2 - 50
data.medX2 = data.width/2 + 50
data.medY1 = data.height/2 - 25 + 100
data.medY2 = data.height/2 + 25 + 100
data.nextX1, data.nextY1 = data.width/2 - 50, data.height - 90
data.nextX2, data.nextY2 = data.width/2 + 50, data.height-50
data.level = 0
data.photo = 0
# The resized image on the left of the screen
data.finalImg = None
# The puzzle cut into pieces
data.finalPuzzle = None
loadImg1(data, canvas)
loadImg2(data, canvas)
loadImg3(data, canvas)
data.img1X1, data.img1X2 = 70, 170+70
data.img1Y1, data.img1Y2 = 70, 70+106
data.img2X1, data.img2X2 = 270, 270+170
data.img2Y1, data.img2Y2 = 70, 70+119
data.img3X1, data.img3X2 = 470, 470+170
data.img3Y1, data.img3Y2 = 70, 70+127
data.countPressed = 0
data.buttonX1, data.buttonY1 = data.width/2+90, 240
data.buttonX2, data.buttonY2 = data.width/2 + 180, 280
##########################################################################
# Grid
data.gridX1 = data.nextX1+30
data.gridY1 = data.nextY1
data.gridX2 = data.nextX2 + 30
data.gridY2 = data.nextY2
loadgrid1(data, canvas)
loadgrid2(data, canvas)
loadgrid3(data, canvas)
data.gridSelectP1 = False
data.gridSelectP2 = False
data.gridSolved = False
data.hintTimerCounter = 10
data.hintDelayCounter = 0
data.gridHint = False
data.gridHintOnce = 0
###########################################################################
# Puzzle
loadPuzzle1(data, canvas)
loadPuzzle2(data, canvas)
loadPuzzle3(data, canvas)
data.piecesMade = False
data.definedPiece = []
data.dashboardX0 = 250
data.dashboardY0 = 50
data.selectBool = False
data.pieceSelected = None
data.timer = 0
# After the game is won
data.solvedX1 = data.width/2 - 60
data.solvedY1 = data.height/2+150
data.solvedX2 = data.width/2 + 60
data.solvedY2 = data.height/2+185
# Hint Box
data.hintX1 = 30
data.hintY1 = 190
data.hintX2 = 100
data.hintY2 = 220
data.hintPiece = None
data.hintColor = "red"
############################################################################
# Solver
data.solved = False
data.solverFinal = None
data.solverShuffled = None
data.finalX1, data.finalY1 = data.width/2 , 250
data.finalX2, data.finalY2 = data.width/2 + 110, 290
data.shuffledX1, data.shuffledY1 = data.width/2, 310
data.shuffledX2, data.shuffledY2 = data.width/2 + 110, 350
############################################################################
# FINAL
data.numbering = False
data.shuffNumbering = False
data.shuffDisplay = True
####################################
# mode dispatcher
####################################
def mousePressed(event, data, canvas):
if (data.mode == "intro"): introMousePressed(event, data)
elif (data.mode == "constructor"):
constructorMousePressed(event, data, canvas)
elif (data.mode == "puzzle"): puzzleMousePressed(event, data, canvas)
elif (data.mode == "grid"): gridMousePressed(event, data, canvas)
elif (data.mode == "solver"): solverMousePressed(event, data, canvas)
elif (data.mode == "final"): finalMousePressed(event, data, canvas)
elif (data.mode == "help"): helpMousePressed(event, data)
def keyPressed(event, data):
if (data.mode == "intro"): introKeyPressed(event, data)
elif (data.mode == "constructor"): constructorKeyPressed(event, data)
elif(data.mode == "puzzle"): puzzleKeyPressed(event, data)
elif(data.mode == "grid"): gridKeyPressed(event, data)
elif (data.mode == "solver"): solverKeyPressed(event, data)
elif (data.mode == "final"): finalKeyPressed(event, data)
elif (data.mode == "help"): helpKeyPressed(event, data)
def mouseMotion(canvas, event, data):
if (data.mode == "puzzle"): puzzleMouseMotion(canvas, event, data)
def mouseRelease(canvas, event, data):
if (data.mode == "puzzle"): puzzleMouseRelease(canvas, event, data)
def timerFired(data):
if (data.mode == "intro"): introTimerFired(data)
elif (data.mode == "constructor"): constructorTimerFired(data)
elif (data.mode == "puzzle"): puzzleTimerFired(data)
elif (data.mode == "grid"): gridTimerFired(data)
elif (data.mode == "solver"): solverTimerFired(data)
elif (data.mode == "final"): finalTimerFired(data)
elif (data.mode == "help"): helpTimerFired(data)
def redrawAll(canvas, data):
if (data.mode == "intro"): introRedrawAll(canvas, data)
elif (data.mode == "constructor"): constructorRedrawAll(canvas, data)
elif (data.mode == "puzzle"): puzzleRedrawAll(canvas, data)
elif (data.mode == "grid"): gridRedrawAll(canvas, data)
elif (data.mode == "solver"): solverRedrawAll(canvas, data)
elif (data.mode == "final"): finalRedrawAll(canvas, data)
elif (data.mode == "help"): helpRedrawAll(canvas, data)
####################################
# intro mode
####################################
# Loading the Animations
def loadBackground(data, canvas):
filename = "background1.gif"
data.background = PhotoImage(file = filename, master=canvas)
def loadAnim1(data, canvas):
filename = "introAnim1.gif"
data.Anim1 = PhotoImage(file = filename, master=canvas)
def loadAnim2(data, canvas):
filename = "introAnim1.gif"
data.Anim2 = PhotoImage(file = filename, master=canvas)
def loadAnim3(data, canvas):
filename = "introAnim1.gif"
data.Anim3 = PhotoImage(file = filename, master=canvas)
def loadAnim4(data, canvas):
filename = "introAnim1.gif"
data.Anim4 = PhotoImage(file = filename, master=canvas)
def getDistance(x1, x2, y1, y2):
distance = math.sqrt( (x1-x2)**2 + (y1-y2)**2 )
return distance
def introMousePressed(event, data):
# If the mouse was pressed on buttons
if (data.introBuildX1<event.x<data.introBuildX2 and
data.introBuildY1<event.y<data.introBuildY2):
data.mode = "constructor"
elif (data.introSolveX1 <event.x< data.introSolveX2 and
data.introSolveY1 <event.y< data.introSolveY2):
data.mode = "solver"
elif (getDistance(event.x, data.helpCx, event.y, data.helpCy)
<= data.helpRadius):
data.mode = "help"
def introKeyPressed(event, data):
pass
def introTimerFired(data):
# Dictates the direction of the animations
sign = sign2 = sign3 = sign4 = -1
if data.Anim1X1+ 100 >= data.width or data.Anim1X1 - 90 <= 0:
data.Anim1dx *= sign
data.Anim1X1 += data.Anim1dx
if data.Anim2Y1 + 70 >= data.height or data.Anim2Y1 - 100 <= 0:
data.Anim2dy *= sign2
data.Anim2Y1 += data.Anim2dy
if data.Anim3Y1 + 70 >= data.height or data.Anim3Y1 - 100 <= 0:
data.Anim3dy *= sign3
data.Anim3Y1 += data.Anim3dy
if data.Anim4X1+ 100 >= data.width or data.Anim4X1 - 90 <= 0:
data.Anim4dx *= sign4
data.Anim4X1 += data.Anim4dx
# Draws the Constructor (Helper Function)
def drawConstructor(canvas, data):
canvas.create_rectangle(data.introBuildX1, data.introBuildY1,
data.introBuildX2, data.introBuildY2, fill = "orange",
outline = "brown", width = 3)
canvas.create_text(data.width/2-140, data.height/2+60, anchor = NW,
text="Build", font="courier 20 bold")
# Draws the Solver (Helper Function)
def drawSolver(canvas, data):
canvas.create_rectangle(data.introSolveX1, data.introSolveY1,
data.introSolveX2, data.introSolveY2, fill = "orange",
outline = "brown", width = 3)
canvas.create_text(data.width/2+60, data.height/2+60, anchor = NW,
text="Solve", font="courier 20 bold")
# Draws the Helper (Helper Function)
def drawHelper(canvas, data):
canvas.create_oval(data.helpCx-data.helpRadius, data.helpCy-data.helpRadius,
data.helpCx+data.helpRadius, data.helpCy+data.helpRadius, fill = "red",
outline = "Black", width = 2)
canvas.create_text(data.helpCx, data.helpCy, text = "Help",
font = "Arial 15")
# Draws the Animations (Helper Function)
def drawAnimations(canvas, data):
canvas.create_image(data.Anim1X1, data.Anim1Y1, image = data.Anim1)
canvas.create_image(data.Anim2X1, data.Anim2Y1, image = data.Anim2)
canvas.create_image(data.Anim3X1, data.Anim3Y1, image = data.Anim3)
canvas.create_image(data.Anim4X1, data.Anim4Y1, image = data.Anim4)
def introRedrawAll(canvas, data):
canvas.create_image(data.width/2, data.height/2, image=data.background)
drawAnimations(canvas, data)
canvas.create_text(data.width/2, data.height/2-20,
text="Puzzle Mania", fill = "brown", font="courier 35 bold")
#### Buttons #####
# Constructor
drawConstructor(canvas, data)
# Solve
drawSolver(canvas, data)
# Help Sign
drawHelper(canvas, data)
####################################
# Help mode
####################################
def loadInstructions(data, canvas):
filename = "instr.gif"
data.instr = PhotoImage(file = filename, master = canvas)
def helpMousePressed(event, data):
pass
def helpKeyPressed(event, data):
if (event.keysym == "space"):
data.mode = "intro"
def helpTimerFired(data):
pass
def helpRedrawAll(canvas, data):
canvas.create_image(data.width/2, data.height/2, image=data.background)
canvas.create_text(data.width/2, 70, fill = "white",
text="Instructions",
font="courier 25 bold")
canvas.create_image(data.width/2, data.height/2+20, image = data.instr)
####################################
# constructor mode
####################################
from tkinter import filedialog
def loadConBackground(data, canvas):
filename = "background1.gif"
data.conBack = PhotoImage(file = filename, master=canvas)
# Draws an outline for a selection (Helper Function)
def drawOutline(x1, y1, x2, y2, canvas):
canvas.create_rectangle(x1, y1, x2, y2, fill = None, outline = "red",
width = 3)
# Draws an outline around the level selected (Helper Function)
def drawLevelOutline(data, canvas):
if data.level == 1:
drawOutline(data.easyX1, data.easyY1, data.easyX2, data.easyY2, canvas)
elif data.level == 2:
drawOutline(data.medX1, data.medY1, data.medX2, data.medY2, canvas)
elif data.level == 3:
drawOutline(data.hardX1, data.hardY1, data.hardX2, data.hardY2, canvas)
# Draws an outline around the picture from the picture library selected
def drawPhotoOutline(data, canvas):
if data.photo == 1:
drawOutline(data.img1X1, data.img1Y1, data.img1X2, data.img1Y2, canvas)
elif data.photo == 2:
drawOutline(data.img2X1, data.img2Y1, data.img2X2, data.img2Y2, canvas)
elif data.photo == 3:
drawOutline(data.img3X1, data.img3Y1, data.img3X2, data.img3Y2, canvas)
# Draws the back button
def drawBack(canvas, data):
canvas.create_oval(data.helpCx-data.helpRadius, data.helpCy-data.helpRadius,
data.helpCx+data.helpRadius, data.helpCy+data.helpRadius, fill = "red",
outline = "Black", width = 2)
canvas.create_text(data.helpCx, data.helpCy, text = "Back",
font = "Arial 15")
# Checks the level selected
def checkLevelSelect(event, data, canvas):
if (data.easyX1 < event.x < data.easyX2 and
data.easyY1 <event.y < data.easyY2):
data.step = 150
data.level = 1
elif (data.medX1 < event.x < data.medX2 and
data.medY1 <event.y < data.medY2):
data.step = 100
data.level = 2
elif (data.hardX1 < event.x < data.hardX2 and
data.hardY1 <event.y < data.hardY2):
data.step = 80
data.level = 3
# Checks the photo selected
def checkPhotoSelect(event, data, canvas):
if (data.img1X1 < event.x < data.img1X2 and
data.img1Y1 <event.y < data.img1Y2):
data.photo = 1
data.finalImg = data.img1
elif (data.img2X1 < event.x < data.img2X2 and
data.img2Y1 <event.y < data.img2Y2):
data.photo = 2
data.finalImg = data.img2
elif (data.img3X1 < event.x < data.img3X2 and
data.img3Y1 <event.y < data.img3Y2):
data.photo = 3
data.finalImg = data.img3
# Draw the next button
def drawButton(canvas, data):
canvas.create_rectangle(data.buttonX1, data.buttonY1, data.buttonX2+10,
data.buttonY2, fill = "orange", outline = "brown", width = 3)
canvas.create_text(data.buttonX1+10, data.buttonY1+10, anchor = NW,
text="Upload!", font="courier 15 bold")
def checkButtonPressed(event, data, canvas):
if (data.buttonX1 < event.x < data.buttonX2 and
data.buttonY1 < event.y < data.buttonY2):
data.countPressed += 1
if data.countPressed == 1:
uploadImage(data, canvas)
def constructorMousePressed(event, data, canvas):
checkLevelSelect(event, data, canvas)
checkPhotoSelect(event, data, canvas)
if data.countPressed == 0:
checkButtonPressed(event, data, canvas)
if (data.nextX1-60 < event.x < data.nextX2-60 and
data.nextY1 < event.y < data.nextY2):
data.mode ="puzzle"
elif (getDistance(event.x, data.helpCx, event.y, data.helpCy)
<= data.helpRadius):
init(data, canvas)
data.mode = "intro"
elif data.gridX1 < event.x < data.gridX2 and data.gridY1 <event.y<data.gridY2:
data.mode = "grid"
# Loading the images in the picture library
def loadImg1(data, canvas):
filename = "puzzle1_170.gif"
data.img1 = PhotoImage(file = filename, master=canvas)
def loadImg2(data, canvas):
filename = "puzzle2_170.gif"
data.img2 = PhotoImage(file = filename, master=canvas)
def loadImg3(data, canvas):
filename = "puzzle3_170.gif"
data.img3 = PhotoImage(file = filename, master=canvas)
def loadFinalImage(filename, data, canvas):
filename = "background1.gif"
data.conBack = PhotoImage(file = filename, master=canvas)
# Function to pop the file explorer
def uploadImage(data, canvas):
filename = filedialog.askopenfilename(
initialdir = "/",
title = "Select an Image...", filetypes =
(("*.png", "*.gif"), ("all files", "*.*")))
#size_170 = (300, 300)
#img = Image.open(filename)
#img.thumbnail(size_170)
#img.save(filename)
data.finalImg = PhotoImage(file = filename, master=canvas)
# Draws the picture library
def drawImages(canvas, data):
canvas.create_image(data.width/2, data.height/2, image=data.conBack)
canvas.create_image(70, 70, anchor = NW, image = data.img1)
canvas.create_image(270, 70, anchor = NW, image = data.img2)
canvas.create_image(470, 70, anchor = NW, image = data.img3)
canvas.create_text(data.width/2 , 40,
text="Pick a Picture.", font="Arial 17 bold")
canvas.create_text(data.width/2-80 , 260,
text="Or upload your own picture:", font="Arial 17 bold")
def constructorKeyPressed(event, data):
pass
def constructorTimerFired(data):
pass
# Draws levels
def drawLevels(canvas, data):
canvas.create_rectangle(data.easyX1, data.easyY1,
data.easyX2, data.easyY2, fill = "orange",
outline = "brown", width = 3)
canvas.create_rectangle(data.medX1, data.medY1,
data.medX2, data.medY2, fill = "orange",
outline = "brown", width = 3)
canvas.create_rectangle(data.hardX1, data.hardY1,
data.hardX2, data.hardY2, fill = "orange",
outline = "brown", width = 3)
canvas.create_text(data.width/2 , data.height/2+40,
text="Pick a level.", font="Arial 17 bold")
canvas.create_text(data.easyX1+20, data.easyY1 + 15, anchor = NW,
text="Easy", font="courier 18 bold")
canvas.create_text(data.medX1+7, data.medY1 + 15, anchor = NW,
text="Medium", font="courier 18 bold")
canvas.create_text(data.hardX1+20, data.hardY1 + 15, anchor = NW,
text="Hard", font="courier 18 bold")
def drawNext(canvas, data):
data.gridX1 = data.nextX1 + 60
data.gridY1 = data.nextY1
data.gridX2 = data.nextX2 + 60
data.gridY2 = data.nextY2
canvas.create_rectangle(data.nextX1-60, data.nextY1,
data.nextX2-60, data.nextY2, fill = "green", outline = "black",
width = 3)
canvas.create_text(data.width/2-100, data.height-85, anchor = NW,
text = "Puzzle", font = "courier 18 bold")
canvas.create_rectangle(data.gridX1, data.gridY1,
data.gridX2, data.gridY2, fill = "green", outline = "black",
width = 3)
canvas.create_text(data.gridX1 + 18, data.gridY1 + 5, anchor = NW,
text = "Grid", font = "courier 18 bold")
def constructorRedrawAll(canvas, data):
canvas.create_image(data.width/2, data.height/2, image=data.conBack)
# Images
drawImages(canvas, data)
drawPhotoOutline(data, canvas)
# Levels
drawLevels(canvas, data)
drawLevelOutline(data, canvas)
# Upload Button:
drawButton(canvas, data)
# Go back option
drawBack(canvas, data)
# Next
drawNext(canvas, data)
####################################
# grid mode
####################################
def gridMousePressed(event, data, canvas):
if (getDistance(event.x, data.helpCx, event.y, data.helpCy)
<= data.helpRadius):
init(data, canvas)
data.mode = "constructor"
if (data.solvedX1 < event.x< data.solvedX2 and
data.solvedY1 <event.y < data.solvedY2):
init(data, canvas)
data.mode = "constructor"
# Checks for mouse pressed on the hint button
if (data.gridHintX1 < event.x < data.gridHintX2 and
data.gridHintY1 < event.y < data.gridHintY2):
if data.gridHintOnce == 0:
data.gridHint = True
data.gridHintOnce += 1
# After winning the game, checks for mouse pressed on the solve more
# option and changes the mode
if data.solved == True:
if (data.solvedX1 < event.x < data.solvedX2 and data.solvedY1 <
event.y < data.solvedY2):
init(data, canvas)
data.mode = "constructor"
# During the game, checks for mousepressed on the first piece
if data.gridSelectP1 == False:
for piece in data.definedPiece:
if piece.containsPoint(event.x, event.y):
print("HI!")
if piece.cx1 != piece.fx1 or piece.cy1 != piece.fy1:
data.gridPiece1 = piece
data.gridSelectP1 = True
print(data.gridSelectP1)
# During the game, checks for mousepressed on the second piece
if (data.gridSelectP1 == True and data.gridSelectP2 == False):
print("Hey")
for piece in data.definedPiece:
if piece.containsPoint(event.x, event.y):
if ((piece != data.gridPiece1) and
(piece.cx1 != piece.fx1 or piece.cy1 != piece.fy1)):
data.gridPiece2 = piece
data.gridSelectP2 = True
print(data.gridPiece1.dimension, data.gridPiece2.dimension)
# helper functions for redrawall
def makeOutline1(data, canvas):
curr = data.gridPiece1
canvas.create_rectangle(curr.cx1+1, curr.cy1+1, curr.cx2-1, curr.cy2-1,
fill = None, outline = "red", width = 2)
def makeOutline2(data, canvas):
curr = data.gridPiece2
canvas.create_rectangle(curr.cx1, curr.cy1, curr.cx2, curr.cy2,
fill = None, outline = "red", width = 2)
# Changes the position of the pieces based on the rules of each level
def makeMove(data, canvas):
if data.level == 1 or data.level == 2:
p1 = data.gridPiece1
p2 = data.gridPiece2
tempX1, tempX2, tempY1, tempY2 = p1.cx1, p1.cx2, p1.cy1, p1.cy2
p1.cx1, p1.cx2, p1.cy1, p1.cy2 = p2.cx1, p2.cx2, p2.cy1, p2.cy2
p2.cx1, p2.cx2, p2.cy1, p2.cy2 = tempX1, tempX2, tempY1, tempY2
data.gridSelectP1 = False
data.gridSelectP2 = False
elif data.level == 3:
p1 = data.gridPiece1
p2 = data.gridPiece2
h = data.gridPieceHeight
w = data.gridPieceWidth
data.gridError = False
if ((p1.cx1 + w != p2.cx1 and p1.cx1 - w != p2.cx1 and p1.cx1 != p2.cx1) or
(p1.cy1 + h != p2.cy1 and p1.cy1 - h != p2.cy1 and p1.cy1 != p2.cy1)):
data.gridError = True
showErrorScreen(data, canvas)
data.gridSelectP1 = False
data.gridSelectP2 = False
return
else:
p1 = data.gridPiece1
p2 = data.gridPiece2
tempX1, tempX2, tempY1, tempY2 = p1.cx1, p1.cx2, p1.cy1, p1.cy2
p1.cx1, p1.cx2, p1.cy1, p1.cy2 = p2.cx1, p2.cx2, p2.cy1, p2.cy2
p2.cx1, p2.cx2, p2.cy1, p2.cy2 = tempX1, tempX2, tempY1, tempY2
data.gridSelectP1 = False
data.gridSelectP2 = False
def showErrorScreen(data, canvas):
canvas.create_text(data.width/2, data.height/2,
text = "Not a valid move!",
fill = "brown", font = "Arial 35 bold")
def gridKeyPressed(event, data):
pass
def gridTimerFired(data):
# The timerFired loops through every 100 milliseconds or .1 second
# So, every 10 loops of 10 milliseconds will be a second
# Therefore, timerDelayCounter is set up such that it will
# decrement timerCounter by -1 every 10 milliseconds
if data.gridHint:
data.hintDelayCounter += 1
if (data.hintDelayCounter%4 == 0):
if data.hintColor == "red":
data.hintColor = "yellow"
else: data.hintColor = "red"
if (data.hintDelayCounter%10 == 0):
data.hintTimerCounter -= 1
if data.hintTimerCounter <= 0: data.gridHint = False
if data.gridSolved == False:
data.timer += 100
# Crops the grid pieces according to the piece size and level selected
def makeGridPieces(data, canvas):
data.piecesMade = True
data.puzzleWidth = data.puzzle.width()
data.puzzleHeight = data.puzzle.height()
if data.level == 1:
stepHor, stepVer = 140, 140
data.gridPieceHeight = 140
data.gridPieceWidth = 140
else:
stepHor, stepVer = 70, 70
data.gridPieceHeight = 70
data.gridPieceWidth = 70
# A 2D list containing tuples with the coordinate points of the pieces
data.allPieces = []
# Creates a new folder to store the images
newpath = "C:/Users/krishna/Desktop/112/TermProject/puzzlePieces"
if not os.path.exists(newpath):
os.makedirs(newpath)
height = data.puzzleHeight
width = data.puzzleWidth
for y in range(0, height, stepVer):
heights = []
for x in range(0, width, stepHor):
heights.append((x, y, x+stepHor, y+stepVer))
data.allPieces.append(heights)
rows, cols = len(data.allPieces), len(data.allPieces[0])
picNum = 1
# Storing the dimensions of each piece in the start Positions list
startPositions = []
for row in range(rows):
for col in range(cols):
x1, y1, x2, y2 = data.allPieces[row][col]
startPositions.append( (x1+150, y1+150) )
# Saving the cropped pieces
for row in range(rows):
for col in range(cols):
x1, y1, x2, y2 = data.allPieces[row][col]
finalPosition = (x1+150, y1+150, x2+150, y2+150)
randIndex = random.randint(0, len(startPositions)-1)
randPos = startPositions.pop(randIndex)
x = randPos[0]
y = randPos[1]
currPosition = (x, y)
img = Image.open(data.puzzleName)
pieceImg = img.crop(data.allPieces[row][col])
picName = "C:/Users/krishna/Desktop/112/TermProject/puzzlePieces/img%d.gif"% picNum
pieceImg.save(picName)
pieceImport = PhotoImage(file = picName, master=canvas)
picNum += 1
data.definedPiece.append(piece(pieceImport, data.allPieces[row][col], finalPosition, currPosition))
# Helper Functions for the redrawall
def drawPieces(data, canvas):
for piece in data.definedPiece:
piece.draw(canvas)
def drawDashboard(canvas, data):
canvas.create_text(270, 90, anchor = NW, text = "Dashboard",
fill = "white", font = "Arial 25 bold")
imgWidth = data.puzzle.width()
imgHeight = data.puzzle.height()
data.dashboardX0 = 150
data.dashboardY0 = 150
canvas.create_rectangle(data.dashboardX0, data.dashboardY0,
data.dashboardX0+imgWidth, data.dashboardY0+imgHeight,
fill = None, outline = "white", width = 7)
def checkgridImage(data, canvas):
if data.finalImg == data.img1:
data.puzzleName = "puzzle1_x2.gif"
data.puzzle = data.grid1
elif data.finalImg == data.img2:
data.puzzleName = "puzzle2_x2.gif"
data.puzzle = data.grid2
elif data.finalImg == data.img3:
data.puzzleName = "puzzle3_x2.gif"
data.puzzle = data.grid3
else:
data.puzzle = data.finalImg
# loading images for the grid
def loadgrid1(data, canvas):
filename = "grid1.gif"
data.grid1 = PhotoImage(file = filename, master=canvas)
def loadgrid2(data, canvas):
filename = "grid2.gif"
data.grid2 = PhotoImage(file = filename, master=canvas)
def loadgrid3(data, canvas):
filename = "grid3.gif"
data.grid3 = PhotoImage(file = filename, master=canvas)
def checkForWinGrid(canvas, data):
for piece in data.definedPiece:
if piece.cx1 != piece.fx1 or piece.cy1 != piece.fy1:
return
textmsg = random.choice(["Good job!"])
data.gridSolved = True
canvas.create_text(data.width/2, data.height/2+90, text = textmsg,
font="courier 40 bold", fill = "yellow")
gameTime = "Time: " + str(str(time.strftime("%M:%S",
time.gmtime(data.timer//1000))))
canvas.create_text(data.width/2, data.height/2+130, text = gameTime,
font = "courier 16 bold", fill = "yellow")
canvas.create_rectangle(data.solvedX1, data.solvedY1,
data.solvedX2, data.solvedY2, fill = "green",
width = 2)
canvas.create_text(data.width/2 - 53, data.height/2+155,
text = "Solve more!", anchor = NW,
font="courier 13 bold", fill = "black")
def drawGridHint(canvas, data):
data.gridHintX1 = data.width/2 - 40
data.gridHintY1 = data.height - 100
data.gridHintX2 = data.width/2 + 40
data.gridHintY2 = data.height - 60
canvas.create_rectangle(data.gridHintX1, data.gridHintY1, data.gridHintX2,
data.gridHintY2, fill = "green", outline = "black", width = 2)
if data.gridHintOnce == 0:
textHint = "One"
else: textHint = "Zero"
canvas.create_text(data.gridHintX1-120, data.gridHintY1 - 40,
text = "Number of hints left: " + textHint,
anchor = NW, font = "courier 16 bold")
canvas.create_text(data.gridHintX1+15, data.gridHintY1 + 10,
text = "Hint", anchor = NW, font = "courier 15 bold")
def drawHintImage(canvas, data):
canvas.create_image(150, 150, anchor = NW, image = data.puzzle)
canvas.create_rectangle(data.dashboardX0, data.dashboardY0,
data.dashboardX0+data.puzzle.width(),
data.dashboardY0+data.puzzle.height(),
fill = None, outline = data.hintColor, width = 8)
def gridRedrawAll(canvas, data):
canvas.create_image(data.width/2, data.height/2, image=data.background)
checkgridImage(data, canvas)
if data.piecesMade == False:
makeGridPieces(data, canvas)
drawPieces(data, canvas)
drawBack(canvas, data)
drawDashboard(canvas, data)
drawTimer(data, canvas)
if data.gridSelectP1 == True:
makeOutline1(data, canvas)
if data.gridSelectP2 == True:
makeOutline2(data, canvas)
if (data.gridSelectP2 and data.gridSelectP1):
makeMove(data, canvas)
# Draw Hint
if data.gridSolved == False:
drawGridHint(canvas, data)
if data.gridHint == True:
drawHintImage(canvas, data)
# Check for and draw the winning screen
checkForWinGrid(canvas, data)
####################################
# Puzzle mode
####################################
# Piece Class
class piece(object):
# Initializing the piece object with its dimensions, current
# position and final position
def __init__(self, image, dimensions, finalPos, currPos):
self.image = image
self.dimension = dimensions # (x1, y1, x2, y2)
self.width = self.dimension[2] - self.dimension[0]
self.height = self.dimension[3] - self.dimension[1]
self.finalPos = finalPos
self.fx1 = finalPos[0]
self.fy1 = finalPos[1]
self.fx2 = finalPos[2]
self.fy2 = finalPos[3]
self.currPos = currPos
self.cx1 = currPos[0]
self.cy1 = currPos[1]
self.cx2 = self.cx1 + (self.width)
self.cy2 = self.cy1 + (self.height)
# Checks whether a point is contained in the dimensions of a piece
def containsPoint(self, x, y):
return (self.cx1 < x < self.cx2 and self.cy1 < y < self.cy2)
def changePos(self, dx, dy):
self.cx1 = dx
self.cy1 = dy
self.cx2 = dx
self.cy2 = dy
# Draws the piece at the current position
def draw(self, canvas):
canvas.create_image(self.cx1, self.cy1, anchor = NW, image = self.image)
# Checking the final image and setting final puzzle to be cut to the x2.5 image
def checkPuzzleImage(data, canvas):
if data.finalImg == data.img1:
data.puzzleName = "puzzle1_x2.gif"
data.puzzle = data.puzzle1
elif data.finalImg == data.img2:
data.puzzleName = "puzzle2_x2.gif"
data.puzzle = data.puzzle2
elif data.finalImg == data.img3:
data.puzzleName = "puzzle3_x2.gif"
data.puzzle = data.puzzle3
else:
data.puzzle = data.finalImg
# Loading the Puzzle Images
def loadPuzzle1(data, canvas):
filename = "puzzle1_x2.gif"
data.puzzle1 = PhotoImage(file = filename, master=canvas)
def loadPuzzle2(data, canvas):
filename = "puzzle2_x2.gif"
data.puzzle2 = PhotoImage(file = filename, master=canvas)
def loadPuzzle3(data, canvas):
filename = "puzzle3_x2.gif"
data.puzzle3 = PhotoImage(file = filename, master=canvas)
# Helper Functions for Redrawall
def displayDashboard(canvas, data):
canvas.create_text(250, 20, anchor = NW, text = "Dashboard",
fill = "white", font = "Arial 15 bold")
imgWidth = data.finalImg.width()
imgHeight = data.finalImg.height()
data.dashboardX0 = 250
data.dashboardY0 = 50
canvas.create_rectangle(data.dashboardX0, data.dashboardY0,
data.dashboardX0+imgWidth*2.5, data.dashboardY0+imgHeight*2.5,
fill = None, outline = "white", width = 4)
def displayFinalPuzzle(canvas, data):
canvas.create_text(30, 20, anchor = NW, text = "Final Image",
font = "Arial 15 bold")
canvas.create_image(30, 50, anchor = NW, image= data.finalImg)
def giveHint(data, canvas):
for piece in data.definedPiece:
if piece.cx1 != piece.fx1 or piece.cy1 != piece.fy1:
print("Hi")
data.hintPiece = piece
return
def drawHintPiece(data, canvas):
if (data.hintPiece != None and (data.hintPiece.cx1 != data.hintPiece.fx1 or
data.hintPiece.cy1 != data.hintPiece.cy2)):
Piece = data.hintPiece
canvas.create_rectangle(Piece.cx1, Piece.cy1, Piece.cx2,
Piece.cy2, fill = None, outline = "green", width = 3)
canvas.create_rectangle(Piece.fx1, Piece.fy1, Piece.fx2,
Piece.fy2, fill = None, outline = "green", width = 3)
def puzzleMouseRelease(canvas, event, data):
if data.selectBool == True:
currPiece = data.pieceSelected
if (currPiece.fx1 < event.x < currPiece.fx2 and
currPiece.fy1 < event.y < currPiece.fy2):
if currPiece == data.hintPiece:
data.hintPiece = None
print("Hi")
currPiece.cx1 = currPiece.fx1
currPiece.cy1 = currPiece.fy1
currPiece.cx2 = currPiece.cx1 + currPiece.width
currPiece.cy2 = currPiece.cy1 + currPiece.height
else:
currPiece.cx1 = event.x-20
currPiece.cy1 = event.y-20
currPiece.cx2 = currPiece.cx1 + currPiece.width
currPiece.cy2 = currPiece.cy1 + currPiece.height
data.selectBool = False
def puzzleMouseMotion(canvas, event, data):
if data.selectBool == True:
currPiece = data.pieceSelected
currPiece.cx1 = event.x-20
currPiece.cy1 = event.y-20
currPiece.cx2 = currPiece.cx1 + currPiece.width
currPiece.cy2 = currPiece.cy1 + currPiece.height
def puzzleMousePressed(event, data, canvas):
if (getDistance(event.x, data.helpCx, event.y, data.helpCy)
<= data.helpRadius):
init(data, canvas)
data.mode = "constructor"
# Check for mouse press on hint button
if (data.hintX1<event.x<data.hintX2 and data.hintY1<event.y<data.hintY2):
giveHint(data, canvas)
# After the puzzle is solved
if data.solved == True:
if (data.solvedX1 < event.x < data.solvedX2 and data.solvedY1 <
event.y < data.solvedY2):