-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathRace_the_car.py
995 lines (901 loc) · 35.8 KB
/
Race_the_car.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
import sys, pygame
from pygame.locals import *
from constants import *
player1Fence = [] # records the position of the fences by player1
player2Fence = [] # records the position of the fences by player2
TotalFence = [] # recors the co-ordinates of all fences
# Board space ID's
PLAYER_ONE = 1
PLAYER_TWO = 2
OPEN_SPACE = 3
# Board positions
BOARDWIDTH_CENTER = BOARDWIDTH - int(BOARDWIDTH/2) - 1
ONE_STARTING_ROW = BOARDHEIGHT - 1
TWO_STARTING_ROW = 0
def main():
global FPSCLOCK, DISPLAYSURF, BASICFONT, FENCE_SURF, FENCE_RECT, MOVE_SURF, MOVE_RECT, SOLVE_SURF, SOLVE_RECT
pygame.init()
FPSCLOCK = pygame.time.Clock()
mousex = 0
mousey = 0 # co-ordinates of mouse events
playerChance = 1 # chance of the player by default=1
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption("Race the Car")
BASICFONT = pygame.font.SysFont("comicsansms", BASICFONTSIZE)
textSurfaceObj = BASICFONT.render("Race the car....", True, BLACK, WHITE)
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (450, 10)
mainBoard = getStartingBoard()
# Store the option button add their rectangle in Options
FENCE_SURF, FENCE_RECT = makeText(
"FENCE", BUTTONTEXTCOLOR, BUTTONCOLOR, WINDOWWIDTH - 120, WINDOWHEIGHT - 450
)
MOVE_SURF, MOVE_RECT = makeText(
"MOVE", BUTTONTEXTCOLOR, BUTTONCOLOR, WINDOWWIDTH - 120, WINDOWHEIGHT - 410
)
checkForQuit()
fenceClicked = False
(spotx, spoty) = (None, None)
moveClicked = False
slideTo = None # the direction to which any slide should slide
showStartScreen()
while True:
msg = "Player turn-> " + str(playerChance)
mouseClicked = False
drawBoard(mainBoard, msg)
hasWon(mainBoard)
DISPLAYSURF.blit(textSurfaceObj, textRectObj)
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
terminate()
elif event.type == MOUSEMOTION:
mousex, mousey = event.pos
elif event.type == MOUSEBUTTONUP:
mousex, mousey = event.pos
mouseClicked = True
spotx, spoty = getSpotClicked(mainBoard, mousex, mousey)
if (spotx, spoty) == (None, None):
# check for the button pressed
if FENCE_RECT.collidepoint((mousex, mousey)) and mouseClicked:
# Fence Button pressed
fenceClicked = True
moveClicked = False
mouseClicked = False
if MOVE_RECT.collidepoint((mousex, mousey)):
# MOVe button pressed
fenceClicked = False
moveClicked = True
mouseClicked = False
else:
playerx, playery = getPlayerPosition(mainBoard, playerChance)
if playerChance == 1:
onumber = 2
else:
onumber = 1
opp_playerx, opp_playery = getPlayerPosition(mainBoard, onumber)
if spotx != opp_playerx or spoty != opp_playery:
if spotx == playerx + 1 and spoty == playery:
slideTo = LEFT
elif spotx == playerx - 1 and spoty == playery:
slideTo = RIGHT
elif spotx == playerx and spoty == playery - 1:
slideTo = DOWN
elif spotx == playerx and spoty == playery + 1:
slideTo = UP
'''
The below logic uses the opposing player's position
and the current player's position to figure out
which direction the car should jump
'''
if opp_playerx == playerx + 1 and opp_playery == playery:
if spotx == playerx + 2 and spoty == playery:
slideTo = JLEFTLEFT
if spotx == playerx + 1 and spoty == playery - 1:
slideTo = JLEFTDOWN
if spotx == playerx + 1 and spoty == playery + 1:
slideTo = JLEFTUP
elif opp_playerx == playerx - 1 and opp_playery == playery:
if spotx == playerx - 2 and spoty == playery:
slideTo = JRIGHTRIGHT
if spotx == playerx - 1 and spoty == playery - 1:
slideTo = JRIGHTDOWN
if spotx == playerx - 1 and spoty == playery + 1:
slideTo = JRIGHTUP
elif opp_playerx == playerx and opp_playery == playery - 1:
if spotx == playerx and spoty == playery - 2:
slideTo = JDOWNDOWN
if spoty == playery - 1 and spotx == playerx - 1:
slideTo = JDOWNRIGHT
if spoty == playery - 1 and spotx == playerx + 1:
slideTo = JDOWNLEFT
elif opp_playerx == playerx and opp_playery == playery + 1:
if spotx == playerx and spoty == playery + 2:
slideTo = JUPUP
if spoty == playery + 1 and spotx == playerx - 1:
slideTo = JUPRIGHT
if spoty == playery + 1 and spotx == playerx + 1:
slideTo = JUPLEFT
# print slideTo
if fenceClicked:
if fenceRemainingCount(playerChance) > 0:
drawBoard(
mainBoard,
"Player : "
+ str(playerChance)
+ " , Remaining fences : "
+ str(fenceRemainingCount(playerChance)),
)
drawFenceHighlight(spotx, spoty, HIGHLIGHTCOLOR, mousex, mousey)
pygame.draw.rect(DISPLAYSURF, BLACK, FENCE_RECT, 2)
if mouseClicked:
if fencePutting(spotx, spoty, playerChance, mousex, mousey):
if validateFence(
mainBoard, playerChance, playerx, playery
) and validateFence(mainBoard, onumber, opp_playerx, opp_playery):
playerChance = 2 if playerChance == 1 else 1
fenceClicked = False
else:
hasWon(mainBoard, onumber)
else:
drawBoard(
mainBoard,
"Player : "
+ str(playerChance)
+ " , No more fences available please make a move",
)
# tile pressed
elif moveClicked:
drawBoard(mainBoard, "Player " + str(playerChance) + " to move the car")
pygame.draw.rect(DISPLAYSURF, BLACK, MOVE_RECT, 2)
if (
slideTo
and not mouseClicked
and validateMove(mainBoard, spotx, spoty, playerChance, slideTo)
):
drawHighlightTile(spotx, spoty, BLACK)
if (
slideTo
and mouseClicked
and validateMove(mainBoard, spotx, spoty, playerChance, slideTo)
):
moveAnimation(mainBoard, slideTo, playerChance)
makeMove(mainBoard, slideTo, playerChance)
if playerChance == 1:
playerChance = 2
elif playerChance == 2:
playerChance = 1
moveClicked = False
pygame.display.update()
slideTo = None
FPSCLOCK.tick(FPS)
def terminate():
pygame.quit()
sys.exit()
def checkForQuit():
for event in pygame.event.get(QUIT):
terminate()
for event in pygame.event.get(KEYUP):
if event.key == K_ESCAPE:
terminate()
pygame.event.post(event)
def getLeftTopOfTile(tileX, tileY):
left = XMARGIN + (tileX * TILESIZE) + (tileX - 1)
top = YMARGIN + (tileY * TILESIZE) + (tileY - 1)
return (left, top)
def getStartingBoard(state=OPEN_SPACE):
# Returns the board data structure with tiles in it
board = []
for x in range(BOARDWIDTH):
column = []
for y in range(BOARDHEIGHT):
column.append(state)
board.append(column)
board[BOARDWIDTH_CENTER][ONE_STARTING_ROW] = PLAYER_ONE
board[BOARDWIDTH_CENTER][TWO_STARTING_ROW] = PLAYER_TWO
return board
def hasWon(board, playerwon=-1):
# checks for winning player
color1 = LIGHTBGCOLOR
color2 = BGCOLOR
if board[BOARDWIDTH - int(BOARDWIDTH / 2) - 1][0] == 1 or playerwon == 1:
# player 1 has won show it and exit
for i in range(13):
color1, color2 = color2, color1
drawBoard(board, "Player 1 has won", color1)
pygame.display.update()
pygame.time.wait(500)
terminate()
elif (
board[BOARDWIDTH - int(BOARDWIDTH / 2) - 1][BOARDHEIGHT - 1] == 2
or playerwon == 2
):
# player 2 has won show it and exit
for i in range(13):
color1, color2 = color2, color1
drawBoard(board, "Player 2 has won", color1)
pygame.display.update()
pygame.time.wait(500)
terminate()
def drawTile(tileX, tileY, number, adjx=0, adjy=0):
# draw a tile at tileX ad tileY
# pixels over determined by adjx and adjy
left, top = getLeftTopOfTile(tileX, tileY)
pygame.draw.rect(
DISPLAYSURF, TILECOLOR, (left + adjx, top + adjy, TILESIZE, TILESIZE)
)
if tileX == 3 and tileY == 0:
textSurf = BASICFONT.render(str(number), True, TEXTCOLOR)
textRect = textSurf.get_rect()
adjy = 11
adjx = 15
textRect.center = left + adjx, top + adjy
DISPLAYSURF.blit(flagImg1, textRect)
if tileX == 3 and tileY == BOARDHEIGHT - 1:
textSurf = BASICFONT.render(str(number), True, TEXTCOLOR)
textRect = textSurf.get_rect()
adjy = 35
adjx = 20
textRect.center = left + adjx, top + adjy
DISPLAYSURF.blit(flagImg, textRect)
if number == 2 or number == 1:
textSurf = BASICFONT.render(str(number), True, TEXTCOLOR)
textRect = textSurf.get_rect()
textRect.center = left + adjx, top + adjy
if number == 2:
adjx = 11
adjy = 11
textRect.center = left + adjx, top + adjy
DISPLAYSURF.blit(carImg1, textRect)
else:
adjx = 4
adjy = 6
textRect.center = left + adjx, top + adjy
DISPLAYSURF.blit(carImg, textRect)
for position in player1Fence:
drawFenceHighlight(
position[0][0], position[0][1], BLACK, position[1][0], position[1][1], 9
)
for position in player2Fence:
drawFenceHighlight(
position[0][0], position[0][1], BLACK, position[1][0], position[1][1], 9
)
def getPlayerPosition(board, number):
# return the x,y co=ordinates of the blank box
for x in range(BOARDWIDTH):
for y in range(BOARDHEIGHT):
if board[x][y] == number:
return (x, y)
def makeMove(board, move, number):
# just makes a move not checks if it's valid or not
playerx, playery = getPlayerPosition(board, number)
if move == UP:
board[playerx][playery], board[playerx][playery + 1] = (
board[playerx][playery + 1],
board[playerx][playery],
)
elif move == DOWN:
board[playerx][playery], board[playerx][playery - 1] = (
board[playerx][playery - 1],
board[playerx][playery],
)
elif move == RIGHT:
board[playerx][playery], board[playerx - 1][playery] = (
board[playerx - 1][playery],
board[playerx][playery],
)
elif move == LEFT:
board[playerx][playery], board[playerx + 1][playery] = (
board[playerx + 1][playery],
board[playerx][playery],
)
elif move == JUPUP:
board[playerx][playery], board[playerx][playery + 2] = (
board[playerx][playery + 2],
board[playerx][playery],
)
elif move == JUPLEFT:
board[playerx][playery], board[playerx + 1][playery + 1] = (
board[playerx + 1][playery + 1],
board[playerx][playery],
)
elif move == JUPRIGHT:
board[playerx][playery], board[playerx - 1][playery + 1] = (
board[playerx - 1][playery + 1],
board[playerx][playery],
)
elif move == JDOWNDOWN:
board[playerx][playery], board[playerx][playery - 2] = (
board[playerx][playery - 2],
board[playerx][playery],
)
elif move == JDOWNLEFT:
board[playerx][playery], board[playerx + 1][playery - 1] = (
board[playerx + 1][playery - 1],
board[playerx][playery],
)
elif move == JDOWNRIGHT:
board[playerx][playery], board[playerx - 1][playery - 1] = (
board[playerx - 1][playery - 1],
board[playerx][playery],
)
elif move == JRIGHTRIGHT:
board[playerx][playery], board[playerx - 2][playery] = (
board[playerx - 2][playery],
board[playerx][playery],
)
elif move == JRIGHTUP:
board[playerx][playery], board[playerx - 1][playery + 1] = (
board[playerx - 1][playery + 1],
board[playerx][playery],
)
elif move == JRIGHTDOWN:
board[playerx][playery], board[playerx - 1][playery - 1] = (
board[playerx - 1][playery - 1],
board[playerx][playery],
)
elif move == JLEFTLEFT:
board[playerx][playery], board[playerx + 2][playery] = (
board[playerx + 2][playery],
board[playerx][playery],
)
elif move == JLEFTUP:
board[playerx][playery], board[playerx + 1][playery + 1] = (
board[playerx + 1][playery + 1],
board[playerx][playery],
)
elif move == JLEFTDOWN:
board[playerx][playery], board[playerx + 1][playery - 1] = (
board[playerx + 1][playery - 1],
board[playerx][playery],
)
pygame.display.update()
FPSCLOCK.tick(FPS)
def drawBoard(board, message, color1=BGCOLOR):
DISPLAYSURF.fill(color1)
if message:
textSurf, textRect = makeText(message, MESSAGECOLOR, color1, 5, 5)
DISPLAYSURF.blit(textSurf, textRect)
for tilex in range(len(board)):
for tiley in range(len(board[0])):
if board[tilex][tiley]:
drawTile(tilex, tiley, board[tilex][tiley])
left, top = getLeftTopOfTile(0, 0)
width = BOARDWIDTH * TILESIZE
height = BOARDHEIGHT * TILESIZE
pygame.draw.rect(
DISPLAYSURF, BORDERCOLOR, (left - 5, top - 5, width + 11, height + 11), 6
)
DISPLAYSURF.blit(FENCE_SURF, FENCE_RECT)
DISPLAYSURF.blit(MOVE_SURF, MOVE_RECT)
# DISPLAYSURF.blit(SOLVE_SURF,SOLVE_RECT)
def getSpotClicked(board, x, y):
# from x,and y pixel co-ordinates get x and y box co-odnate
for tileX in range(len(board)):
for tileY in range(len(board[0])):
left, top = getLeftTopOfTile(tileX, tileY)
tileRect = pygame.Rect(left, top, TILESIZE, TILESIZE)
if tileRect.collidepoint(x, y):
return (tileX, tileY)
return (None, None)
def drawFenceHighlight(tileX, tileY, highLightColor, mousex, mousey, fence_thickness=4):
left, top = getLeftTopOfTile(tileX, tileY)
Ox, Oy = getLeftTopOfTile(0, 0)
if mousex > mousey and mousex + mousey < (top + left + TILESIZE):
if left <= Ox + 6 * TILESIZE and top != Oy:
pygame.draw.line(
DISPLAYSURF,
highLightColor,
(left, top),
(left + 2 * TILESIZE, top),
fence_thickness,
)
elif left > Ox + 6 * TILESIZE and top != Oy:
pygame.draw.line(
DISPLAYSURF,
highLightColor,
(Ox + 5 * TILESIZE + 5, top),
(Ox + 7 * TILESIZE + 2, top),
fence_thickness,
)
elif mousex > mousey and mousex + mousey > (top + left + TILESIZE):
if top <= Oy + 6 * TILESIZE and left < Ox + 6 * TILESIZE:
pygame.draw.line(
DISPLAYSURF,
highLightColor,
(left + TILESIZE, top),
(left + TILESIZE, top + 2 * TILESIZE),
fence_thickness,
)
elif top > Oy + 6 * TILESIZE and left < Ox + 6 * TILESIZE:
pygame.draw.line(
DISPLAYSURF,
highLightColor,
(left + TILESIZE, Oy + 5 * TILESIZE + 1),
(left + TILESIZE, Oy + 7 * TILESIZE + 1),
fence_thickness,
)
elif mousex < mousey and mousex + mousey < (top + left + TILESIZE):
if top <= Oy + 6 * TILESIZE and left != Ox:
pygame.draw.line(
DISPLAYSURF,
highLightColor,
(left, top),
(left, top + 2 * TILESIZE),
fence_thickness,
)
elif top > Oy + 6 * TILESIZE and left != Ox:
pygame.draw.line(
DISPLAYSURF,
highLightColor,
(left, Oy + 5 * TILESIZE + 1),
(left, Oy + 7 * TILESIZE + 1),
fence_thickness,
)
elif mousex < mousey and mousex + mousey > (top + left + TILESIZE):
if left <= Ox + 6 * TILESIZE and top < Oy + 6 * TILESIZE:
pygame.draw.line(
DISPLAYSURF,
highLightColor,
(left, top + TILESIZE),
(left + 2 * TILESIZE, top + TILESIZE),
fence_thickness,
)
elif left > Ox + 6 * TILESIZE and top < Oy + 6 * TILESIZE:
pygame.draw.line(
DISPLAYSURF,
highLightColor,
(Ox + 5 * TILESIZE, top + TILESIZE),
(Ox + 7 * TILESIZE + 2, top + TILESIZE),
fence_thickness,
)
def makeText(text, color, bgcolor, top, left):
# create the surface and rect object for some text
textSurf = BASICFONT.render(text, True, color, bgcolor)
textRect = textSurf.get_rect()
textRect.topleft = (top, left)
return (textSurf, textRect)
def moveAnimation(board, direction, number, message="", jx=0, jy=0):
playerx, playery = getPlayerPosition(board, number)
drawBoard(board, message)
baseSurf = DISPLAYSURF.copy()
# Drawing a base surface for temporary movement
if playerx == 3 and playery == 0:
textSurf = BASICFONT.render(str(number), True, TEXTCOLOR)
textRect = textSurf.get_rect()
adjy = 11
adjx = 15
moveLeft, moveTop = getLeftTopOfTile(playerx, playery)
textRect.center = moveLeft + adjx, moveTop + adjy
pygame.draw.rect(
baseSurf, TILECOLOR, (moveLeft, moveTop, TILESIZE - 2, TILESIZE - 4)
)
baseSurf.blit(flagImg1, textRect)
elif playerx == 3 and playery == BOARDHEIGHT - 1:
textSurf = BASICFONT.render(str(number), True, TEXTCOLOR)
textRect = textSurf.get_rect()
adjy = 35
adjx = 20
moveLeft, moveTop = getLeftTopOfTile(playerx, playery)
textRect.center = moveLeft + adjx, moveTop + adjy
pygame.draw.rect(
baseSurf, TILECOLOR, (moveLeft, moveTop, TILESIZE - 2, TILESIZE - 3)
)
baseSurf.blit(flagImg, textRect)
else:
moveLeft, moveTop = getLeftTopOfTile(playerx, playery)
pygame.draw.rect(
baseSurf, TILECOLOR, (moveLeft + 4, moveTop + 4, TILESIZE - 7, TILESIZE - 7)
)
# checking for jump conditions
if direction == JUPUP:
moveAnimation(board, UP, number)
moveAnimation(board, UP, number, "", 0, 1)
return
elif direction == JUPRIGHT:
moveAnimation(board, UP, number)
moveAnimation(board, RIGHT, number, "", 0, 1)
return
elif direction == JUPLEFT:
moveAnimation(board, UP, number)
moveAnimation(board, LEFT, number, "", 0, 1)
return
elif direction == JDOWNDOWN:
moveAnimation(board, DOWN, number)
moveAnimation(board, DOWN, number, "", 0, -1)
return
elif direction == JDOWNLEFT:
moveAnimation(board, DOWN, number)
moveAnimation(board, LEFT, number, "", 0, -1)
return
elif direction == JDOWNRIGHT:
moveAnimation(board, DOWN, number)
moveAnimation(board, RIGHT, number, "", 0, -1)
return
elif direction == JRIGHTRIGHT:
moveAnimation(board, RIGHT, number)
moveAnimation(board, RIGHT, number, "", -1, 0)
return
elif direction == JRIGHTUP:
moveAnimation(board, RIGHT, number)
moveAnimation(board, UP, number, "", -1, 0)
return
elif direction == JRIGHTDOWN:
moveAnimation(board, RIGHT, number)
moveAnimation(board, DOWN, number, "", -1, 0)
return
elif direction == JLEFTLEFT:
moveAnimation(board, LEFT, number)
moveAnimation(board, LEFT, number, "", 1, 0)
return
elif direction == JLEFTUP:
moveAnimation(board, LEFT, number)
moveAnimation(board, UP, number, "", 1, 0)
return
elif direction == JLEFTDOWN:
moveAnimation(board, LEFT, number)
moveAnimation(board, DOWN, number, "", 1, 0)
return
drawBoard(board)
# for double animation if required
moveLeft, moveTop = getLeftTopOfTile(playerx + jx, playery + jy)
for i in range(0, TILESIZE, ANIMATIONSPEED):
checkForQuit()
DISPLAYSURF.blit(baseSurf, (0, 0))
if number == 1:
if direction == UP:
# drawTile(movex,movey,board[movex][movey],0,-i)
DISPLAYSURF.blit(carImg, (moveLeft, moveTop + i))
elif direction == DOWN:
# drawTile(movex,movey,board[movex][movey],0,i)
DISPLAYSURF.blit(carImg, (moveLeft, moveTop - i))
elif direction == RIGHT:
# drawTile(movex,movey,board[movex][movey],i,0)
DISPLAYSURF.blit(carImg, (moveLeft - i, moveTop))
elif direction == LEFT:
# drawTile(movex,movey,board[movex][movey],-i,0)
DISPLAYSURF.blit(carImg, (moveLeft + i, moveTop))
else:
if direction == UP:
# drawTile(movex,movey,board[movex][movey],0,-i)
DISPLAYSURF.blit(carImg1, (moveLeft, moveTop + i))
elif direction == DOWN:
# drawTile(movex,movey,board[movex][movey],0,i)
DISPLAYSURF.blit(carImg1, (moveLeft, moveTop - i))
elif direction == RIGHT:
# drawTile(movex,movey,board[movex][movey],i,0)
DISPLAYSURF.blit(carImg1, (moveLeft - i, moveTop))
elif direction == LEFT:
# drawTile(movex,movey,board[movex][movey],-i,0)
DISPLAYSURF.blit(carImg1, (moveLeft + i, moveTop))
pygame.display.update()
FPSCLOCK.tick(FPS)
def fencePutting(tileX, tileY, playerChance, mousex, mousey):
# puts the fence on the board and record it's positon in the array
left, top = getLeftTopOfTile(tileX, tileY)
Ox, Oy = getLeftTopOfTile(0, 0)
flag = 0 # fence not on the border of the board then only append else return false
if mousex > mousey and mousex + mousey < (top + left + TILESIZE):
if (
left <= Ox + 6 * TILESIZE
and top != Oy
and fenceLine(tileX, tileY, tileX + 2, tileY)
):
TotalFence.append(((tileX, tileY), (tileX + 2, tileY)))
flag = 1
elif (
left > Ox + 6 * TILESIZE and top != Oy and fenceLine(5, tileY, 5 + 2, tileY)
):
TotalFence.append(((5, tileY), (7, tileY)))
flag = 1
elif mousex > mousey and mousex + mousey > (top + left + TILESIZE):
if (
top <= Oy + 6 * TILESIZE
and left < Ox + 6 * TILESIZE
and fenceLine(tileX + 1, tileY, tileX + 1, tileY + 2)
):
TotalFence.append(((tileX + 1, tileY), (tileX + 1, tileY + 2)))
flag = 1
elif (
top > Oy + 6 * TILESIZE
and left < Ox + 6 * TILESIZE
and fenceLine(tileX + 1, 5, tileX + 1, 5 + 2)
):
TotalFence.append(((tileX + 1, 5), (tileX + 1, 7)))
flag = 1
elif mousex < mousey and mousex + mousey < (top + left + TILESIZE):
if (
top <= Oy + 6 * TILESIZE
and left != Ox
and fenceLine(tileX, tileY, tileX, tileY + 2)
):
TotalFence.append(((tileX, tileY), (tileX, tileY + 2)))
flag = 1
elif (
top > Oy + 6 * TILESIZE and left != Ox and fenceLine(tileX, 5, tileX, 5 + 2)
):
TotalFence.append(((tileX, 5), (tileX, 5 + 2)))
flag = 1
elif mousex < mousey and mousex + mousey > (top + left + TILESIZE):
if (
left <= Ox + 6 * TILESIZE
and top < Oy + 6 * TILESIZE
and fenceLine(tileX, tileY + 1, tileX + 2, tileY + 1)
):
TotalFence.append(((tileX, tileY + 1), (tileX + 2, tileY + 1)))
flag = 1
elif (
left > Ox + 6 * TILESIZE
and top < Oy + 6 * TILESIZE
and fenceLine(5, tileY + 1, 5 + 2, tileY + 1)
):
TotalFence.append(((5, tileY + 1), (7, tileY + 1)))
flag = 1
if playerChance == 1 and flag == 1:
player1Fence.append(((tileX, tileY), (mousex, mousey)))
return True
elif playerChance == 2 and flag == 1:
player2Fence.append(((tileX, tileY), (mousex, mousey)))
return True
return False
def drawHighlightTile(tileX, tileY, highLightColor, tile_thickness=4):
# highlight the box where car may move based on mouse hover
left, top = getLeftTopOfTile(tileX, tileY)
pygame.draw.rect(
DISPLAYSURF,
highLightColor,
(left - 4, top - 4, TILESIZE + 5, TILESIZE + 5),
tile_thickness,
)
def gameWonAnimation():
color1 = LIGHTBGCOLOR
color2 = BGCOLOR
for i in range(13):
color1, color2 = color2, color1
DISPLAYSURF.fill(color1)
def validateMove(board, tileX, tileY, playerChance, direction, playerx=-1, playery=-1):
if playerx == -1: # no parameter recieved, initialize to actual parameter
playerx, playery = getPlayerPosition(board, playerChance)
if playerChance == 1:
onumber = 2
else:
onumber = 1
opp_playerx, opp_playery = getPlayerPosition(board, onumber)
for position in TotalFence:
if position[0][0] == position[1][0]:
# equation x-position[0][0]
if position[0][1] > position[1][1]:
maxy = position[0][1]
miny = position[1][1]
else:
miny = position[0][1]
maxy = position[1][1]
if tileY < maxy and tileY >= miny and playery < maxy and playery >= miny:
if playerx < position[0][0]:
if (playerx - position[0][0]) * (tileX - position[0][0] + 0.1) < 0:
return False # Invalid move
elif playerx == position[0][0]:
if (playerx - position[0][0] + 0.1) * (tileX - position[0][0]) < 0:
return False
else:
if (playerx - position[0][0] - 0.1) * (
tileX - position[0][0] + 0.1
) < 0:
return False # Invalid move
if position[0][1] == position[1][1]:
# equation y-position[1][1]
if position[0][0] > position[1][0]:
maxx = position[0][0]
minx = position[1][0]
else:
minx = position[0][0]
maxx = position[1][0]
if tileX < maxx and tileX >= minx and playerx < maxx and playerx >= minx:
if playery < position[0][1]:
if (playery - position[0][1]) * (tileY - position[0][1] + 0.1) < 0:
return False
elif playery == position[0][1]:
if (playery - position[0][1] + 0.1) * (tileY - position[0][1]) < 0:
return False
else:
if (playery - position[0][1] - 0.1) * (
tileY - position[0][1] + 0.1
) < 0:
return False
# In case of jump fence should not be crossed
if direction == JUPRIGHT:
return validateMove(
board, playerx, playery + 1, playerChance, UP
) and validateMove(board, tileX, tileY, onumber, RIGHT)
if direction == JUPLEFT:
return validateMove(
board, playerx, playery + 1, playerChance, UP
) and validateMove(board, tileX, tileY, onumber, LEFT)
if direction == JDOWNLEFT:
return validateMove(
board, playerx, playery - 1, playerChance, DOWN
) and validateMove(board, tileX, tileY, onumber, LEFT)
if direction == JDOWNRIGHT:
return validateMove(
board, playerx, playery - 1, playerChance, DOWN
) and validateMove(board, tileX, tileY, onumber, RIGHT)
if direction == JRIGHTUP:
return validateMove(
board, playerx - 1, playery, playerChance, RIGHT
) and validateMove(board, tileX, tileY, onumber, UP)
if direction == JRIGHTDOWN:
return validateMove(
board, playerx - 1, playery, playerChance, RIGHT
) and validateMove(board, tileX, tileY, onumber, DOWN)
if direction == JLEFTUP:
return validateMove(
board, playerx + 1, playery, playerChance, RIGHT
) and validateMove(board, tileX, tileY, onumber, UP)
if direction == JLEFTDOWN:
return validateMove(
board, playerx + 1, playery, playerChance, RIGHT
) and validateMove(board, tileX, tileY, onumber, DOWN)
return True
def fenceRemainingCount(playerChance):
"""
returns the number of fence remainning for the respective player
"""
if playerChance == 1:
return FENCELIMIT - len(player1Fence)
else:
return FENCELIMIT - len(player2Fence)
def validateFence(board, playerChance, startx, starty):
"""
checks whether the fence is valid or not
"""
if playerChance == 1:
finalx = BOARDWIDTH - int(BOARDWIDTH / 2) - 1
finaly = 0
else:
finalx = BOARDWIDTH - int(BOARDWIDTH / 2) - 1
finaly = BOARDHEIGHT - 1
duplicateBoard = getStartingBoard(0)
duplicateBoard[(BOARDWIDTH - int(BOARDWIDTH / 2) - 1)][0] = 0
duplicateBoard[(BOARDWIDTH - int(BOARDWIDTH / 2) - 1)][
BOARDHEIGHT - 1
] = 0 # getting board with all zeroes
start = (startx, starty)
final = (finalx, finaly)
s = [start]
while s:
current = s.pop()
if current == final:
return True
while s:
s.pop() # to end the loop
elif duplicateBoard[current[0]][current[1]] == 0:
duplicateBoard[current[0]][current[1]] = 1
if (
current[0] - 1 >= 0
and validateMove(
board,
current[0] - 1,
current[1],
playerChance,
RIGHT,
current[0],
current[1],
)
and duplicateBoard[current[0] - 1][current[1]] == 0
):
s = s + [(current[0] - 1, current[1])]
if (
current[0] + 1 < BOARDWIDTH
and validateMove(
board,
current[0] + 1,
current[1],
playerChance,
LEFT,
current[0],
current[1],
)
and duplicateBoard[current[0] + 1][current[1]] == 0
):
s = s + [(current[0] + 1, current[1])]
if (
current[1] - 1 >= 0
and validateMove(
board,
current[0],
current[1] - 1,
playerChance,
DOWN,
current[0],
current[1],
)
and duplicateBoard[current[0]][current[1] - 1] == 0
):
s = s + [(current[0], current[1] - 1)]
if (
current[1] + 1 < BOARDHEIGHT
and validateMove(
board,
current[0],
current[1] + 1,
playerChance,
UP,
current[0],
current[1],
)
and duplicateBoard[current[0]][current[1] + 1] == 0
):
s = s + [(current[0], current[1] + 1)]
return False
def fenceLine(xi, yi, xf, yf):
# function to check if fence has been put on the valid space or not
for fence in TotalFence:
if (fence[0][0] + fence[1][0]) == (xi + xf) and (fence[0][1] + fence[1][1]) == (
yi + yf
): # is not the same fence
return False
if (
(fence[0][0] == fence[1][0])
and xi == xf
and xi == fence[0][0]
and (
(yi == fence[0][1] - 1 and yf == fence[1][1] - 1)
or (yi == fence[0][1] + 1 and yf == fence[1][1] + 1)
)
):
# print (xi,xf,yi,yf," ",fence[0][0],fence[0][1],fence[1][0],fence[1][1])
return False
if (
(fence[0][1] == fence[1][1])
and yi == yf
and yi == fence[0][1]
and (
(xi == fence[0][0] - 1 and xf == fence[1][0] - 1)
or (xi == fence[0][0] + 1 and xf == fence[1][0] + 1)
)
):
return False
return True
def showStartScreen():
BASICFONT = pygame.font.SysFont("comicsansms", 3 * BASICFONTSIZE)
OPTIONFONT = pygame.font.SysFont("comicsansms", 2 * BASICFONTSIZE)
titleSurf1 = BASICFONT.render("Race The Car", True, WHITE)
titleSurf2 = OPTIONFONT.render("RaceON", True, WHITE)
# titleSurf2 = BASICFONT.render('Let the race begin..', True, GREEN)
# degrees1 = 0
# degrees2 = 0
while True:
DISPLAYSURF.fill(BGCOLOR)
# rotatedSurf1 = pygame.transform.rotate(titleSurf1, degrees1)
# rotatedRect1 = rotatedSurf1.get_rect(6
# rotatedRect1.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 4)
DISPLAYSURF.blit(hmImg1, DISPLAYSURF.get_rect())
DISPLAYSURF.blit(titleSurf1, (WINDOWWIDTH / 3.4, WINDOWHEIGHT / 20))
DISPLAYSURF.blit(titleSurf2, (WINDOWWIDTH / 1.4, WINDOWHEIGHT / 2))
# rotatedSurf2 = pygame.transform.rotate(titleSurf2, degrees2)
# rotatedRect2 = rotatedSurf2.get_rect()
# rotatedRect2.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 3)
# DISPLAYSURF.blit(rotatedSurf2, rotatedRect2)
drawPressKeyMsg()
if checkForKeyPress():
pygame.event.get() # clear event queue
return
pygame.display.update()
FPSCLOCK.tick(FPS)
# degrees1 += 3 # rotate by 3 degrees each frame
# degrees2 += 7 # rotate by 7 degrees each frame
def drawPressKeyMsg():
pressKeySurf = BASICFONT.render("Press a key to play.", True, DARKGRAY)
pressKeyRect = pressKeySurf.get_rect()
pressKeyRect.topleft = (WINDOWWIDTH - 200, WINDOWHEIGHT - 30)
DISPLAYSURF.blit(pressKeySurf, pressKeyRect)
def checkForKeyPress():
if len(pygame.event.get(QUIT)) > 0:
terminate()
keyUpEvents = pygame.event.get(KEYUP)
if len(keyUpEvents) == 0:
return None
if keyUpEvents[0].key == K_ESCAPE:
terminate()
return keyUpEvents[0].key
if __name__ == "__main__":
main()