Amazing-Python-Scripts

Форк
0
845 строк · 31.2 Кб
1
from tkinter import *  # Tkinter is used as the GUI.
2
import random
3
import tkinter.messagebox
4

5
root = Tk()
6

7
root.resizable(width=False, height=False)  # The window size of the game.
8
root.geometry('1000x750')
9
root.configure(background='lightgreen')
10
root.title("Ludo Game")
11

12
# Loading all the image files that are required in the game.
13
logo = PhotoImage(file="./Ludo_Game/whitebox.gif")
14
logo2 = PhotoImage(file="./Ludo_Game/red side.gif")
15
logo3 = PhotoImage(file="./Ludo_Game/red.gif")
16
logo4 = PhotoImage(file="./Ludo_Game/blue side.gif")
17
logo5 = PhotoImage(file="./Ludo_Game/green side.gif")
18
logo6 = PhotoImage(file="./Ludo_Game/yellow side.gif")
19
logo7 = PhotoImage(file="./Ludo_Game/center.gif")
20
logoxx = PhotoImage(file="./Ludo_Game/test.gif")
21
logog = PhotoImage(file="./Ludo_Game/greenbox.gif")
22
logogs = PhotoImage(file="./Ludo_Game/greenstop.gif")
23
logoy = PhotoImage(file="./Ludo_Game/yellowbox.gif")
24
logoys = PhotoImage(file="./Ludo_Game/yellowstop.gif")
25
logob = PhotoImage(file="./Ludo_Game/bluebox.gif")
26
logobs = PhotoImage(file="./Ludo_Game/bluestop.gif")
27
logor = PhotoImage(file="./Ludo_Game/redbox.gif")
28
logors = PhotoImage(file="./Ludo_Game/redstop.gif")
29
logoh = PhotoImage(file="./Ludo_Game/head.gif")
30
logot = PhotoImage(file="./Ludo_Game/tail.gif")
31
logoh1 = PhotoImage(file="./Ludo_Game/head1.gif")
32
logot1 = PhotoImage(file="./Ludo_Game/tail1.gif")
33
logoh2 = PhotoImage(file="./Ludo_Game/head2.gif")
34
logot2 = PhotoImage(file="./Ludo_Game/tail2.gif")
35
logoh3 = PhotoImage(file="./Ludo_Game/head3.gif")
36
logot3 = PhotoImage(file="./Ludo_Game/tail3.gif")
37
logoab= PhotoImage(file="./Ludo_Game/blue.gif")
38
logoay= PhotoImage(file="./Ludo_Game/yellow.gif")
39
logoag= PhotoImage(file="./Ludo_Game/green.gif")
40

41
Label(image=logo2, width=298, height=298).place(x=-1, y=-1)               #setting up board images
42
Label(image=logo4, width=300, height=300).place(x=(-2), y=(448))
43
Label(image=logo5, width=296, height=296).place(x=(450), y=(0))
44
Label(image=logo6, width=294, height=294).place(x=(450), y=(450))
45
Label(image=logo7, width=150, height=150).place(x=(298), y=(298))
46

47
c = 0                                #initializing variable and flags that are to be used in the game
48
lx = 0
49
bb =0
50
nc = 0
51
rollc = 0
52
rolls = []
53
RED = True
54
BLUE = False
55
GREEN = False
56
YELLOW = False
57
TURN = True
58
REDKILL = False
59
BLUEKILL = False
60
GREENKILL = False
61
YELLOWKILL = False
62

63

64
def board():                            #Drawing the board, piece by piece.
65

66
                                        #Splash Screen.
67
    tkinter.messagebox.showinfo(title=None, message="TO START GAME PRESS OKAY & TO EXIT PRESS CROSS UP IN THE WINDOW")
68
    v = 0
69
    z = 0
70

71
    while (v != 300):           #Drawing White boxes
72
        z = 0
73
        while (z != 150):
74
            Label(image=logo, width=46, height=46).place(x=(300 + z), y=(0 + v))
75
            z = z + 50
76
        v = v + 50
77

78
    z = 0
79
    v = 0
80
    while (v != 300):          #Drawing White boxes
81
        z = 0
82
        while (z != 150):
83
            Label(image=logo, width=46, height=46).place(x=(0 + v), y=(300 + z))
84
            z = z + 50
85
        v = v + 50
86

87
    #####################
88

89
    v = 0
90
    z = 0
91

92
    while (v != 300):            #Drawing White boxes
93
        z = 0
94
        while (z != 150):
95
            Label(image=logo, width=46, height=46).place(x=(300 + z), y=(450 + v))
96
            z = z + 50
97
        v = v + 50
98

99
    z = 0
100
    v = 0
101
    while (v != 300):         #Drawing White boxes
102
        z = 0
103
        while (z != 150):
104
            Label(image=logo, width=46, height=46).place(x=(450 + v), y=(300 + z))
105
            z = z + 50
106
        v = v + 50
107

108
    v = 0
109
    while (v != 250):     #Drawing Green boxes
110
        Label(image=logog, width=46, height=46).place(x=(350), y=(50 + v))
111
        v = v + 50
112

113
    Label(image=logog, width=46, height=46).place(x=(300), y=(100))
114
    Label(image=logogs, width=46, height=46).place(x=(400), y=(50))
115

116
    v = 0
117
    while (v != 250):     #Drawing Yellow boxes
118
        Label(image=logoy, width=46, height=46).place(x=(450 + v), y=(350))
119
        v = v + 50
120

121
    Label(image=logoy, width=46, height=46).place(x=(600), y=(300))
122
    Label(image=logoys, width=46, height=46).place(x=(650), y=(400))
123

124
    v = 0
125
    while (v != 250):    #Drawing Red Boxes
126
        Label(image=logor, width=46, height=46).place(x=(50 + v), y=(350))
127
        v = v + 50
128

129
    Label(image=logor, width=46, height=46).place(x=(100), y=(400))
130
    Label(image=logors, width=46, height=46).place(x=(50), y=(300))
131

132
    v = 0
133
    while (v != 250):    #Drawing Blue Boxes
134
        Label(image=logob, width=46, height=46).place(x=(350), y=(450 + v))
135
        v = v + 50
136

137
    Label(image=logobs, width=46, height=46).place(x=(300), y=(650))
138
    Label(image=logob, width=46, height=46).place(x=(400), y=(600))
139

140
    Label(image=logoh, width=46, height=46).place(x=250, y=400)        #Drawing arrows
141
    Label(image=logot, width=46, height=46).place(x=300, y=450)
142
    Label(image=logoh1, width=46, height=46).place(x=400, y=450)
143
    Label(image=logot1, width=46, height=46).place(x=450, y=400)
144
    Label(image=logoh2, width=46, height=46).place(x=450, y=300)
145
    Label(image=logot2, width=46, height=46).place(x=400, y=250)
146
    Label(image=logoh3, width=46, height=46).place(x=300, y=250)
147
    Label(image=logot3, width=46, height=46).place(x=250, y=300)
148

149
class YBox:                               #Class of yellow box
150
    rap = None
151

152
    def __init__(self, num=-1, x=0, y=0, x0=0, y0=0, double=False, ):
153
        self.num = num                #no of gamepiece acc to box
154
        self.x = x                    #initial and final co-ordinates of the boxes
155
        self.y = y
156
        self.x0 = x0
157
        self.y0 = y0
158
        self.rap = Label(image=logoay, width=20, height=20)        #image of game piece.
159
        self.double = double                                       #if one game piece on top of another.
160

161
    def swap(self):                     #Swaps the position of gamepiece according to the number on dice.
162
        self.rap.place(x=self.x0 + 13, y=self.y0 + 14)
163

164
class GBox:                             #Class of green box
165
    rap = None
166

167
    def __init__(self, num=-1, x=0, y=0, x0=0, y0=0, double=False, ):
168
        self.num = num
169
        self.x = x
170
        self.y = y
171
        self.x0 = x0
172
        self.y0 = y0
173
        self.rap = Label(image=logoag, width=20, height=20)
174
        self.double = double
175

176
    def swap(self):
177
        self.rap.place(x=self.x0 + 13, y=self.y0 + 14)
178

179
class BBox:                           #Class of Blue box
180
    rap = None
181

182
    def __init__(self, num=-1, x=0, y=0, x0=0, y0=0, double=False, ):
183
        self.num = num
184
        self.x = x
185
        self.y = y
186
        self.x0 = x0
187
        self.y0 = y0
188
        self.rap = Label(image=logoab, width=20, height=20)
189
        self.double = double
190

191
    def swap(self):
192
        self.rap.place(x=self.x0 + 13, y=self.y0 + 14)
193

194
class Box:                           #class of red box
195
    rap = None
196

197
    def __init__(self, num=-1, x=0, y=0, x0=0, y0=0, double=False, ):
198
        self.num = num
199
        self.x = x
200
        self.y = y
201
        self.x0 = x0
202
        self.y0 = y0
203
        self.rap = Label(image=logo3, width=20, height=20)
204
        self.double = double
205

206
    def swap(self):
207
        self.rap.place(x=self.x0 + 13, y=self.y0 + 14)
208

209

210
def main():                                 # Main game function.
211

212
    global box, redbox, bluebox, greenbox, yellowbox, redhome, bluehome, yellowhome, greenhome
213
    global red, blue, yellow, green, rap, RED, BLUE, GREEN, YELLOW, dice, nc, TURN, bb
214

215
    if c == 0:                              #constructs the game pieces first time the code is ran.
216

217
        board()
218

219
        box = [Box() for i in range(52)]  
220

221
        redbox = [Box() for i in range(57)]  # list of co-ordinates of all the colored boxes, excluding home and stop.
222
        bluebox = [Box() for i in range(57)]
223
        greenbox = [Box() for i in range(57)]
224
        yellowbox = [Box() for i in range(57)]
225

226
        redhome = [Box() for i in range(4)]  # list co-ordinates of all the home positions
227
        bluehome = [Box() for i in range(4)]
228
        greenhome = [Box() for i in range(4)]
229
        yellowhome = [Box() for i in range(4)]
230

231
        red = [Box() for i in range(4)]  # list of co-ordinates of all the game pieces in their initial state
232
        blue = [BBox() for i in range(4)]  # that is equal to their respective home co-ordinates.
233
        green = [GBox() for i in range(4)]
234
        yellow = [YBox() for i in range(4)]
235

236
        for i in range(2):                        #Populates list of homeboxes, colored boxes, gamepieces and white boxes
237
            redhome[i].x = (100 + (100 * i))
238
            redhome[i].y = 100
239
            red[i].x0 = redhome[i].x
240
            red[i].y0 = redhome[i].y
241
            red[i].x = (red[i].x0) + 25
242
            red[i].y = (red[i].y0) + 25
243

244
            bluehome[i].x = (100 + (100 * i))
245
            bluehome[i].y = (550)
246
            blue[i].x0 = bluehome[i].x
247
            blue[i].y0 = bluehome[i].y
248
            blue[i].x = (blue[i].x0) + 25
249
            blue[i].y = (blue[i].y0) + 25
250

251
            yellowhome[i].x = (550 + (100 * i))
252
            yellowhome[i].y = (550)
253
            yellow[i].x0 = yellowhome[i].x
254
            yellow[i].y0 = yellowhome[i].y
255
            yellow[i].x = (yellow[i].x0) + 25
256
            yellow[i].y = (yellow[i].y0) + 25
257

258
            greenhome[i].x = (550 + (100 * i))
259
            greenhome[i].y = (100)
260
            green[i].x0 = greenhome[i].x
261
            green[i].y0 = greenhome[i].y
262
            green[i].x = (green[i].x0) + 25
263
            green[i].y = (green[i].y0) + 25
264

265
        for i in range(2, 4):
266
            redhome[i].x = (100 + (100 * (i - 2)))
267
            redhome[i].y = 200
268
            red[i].x0 = redhome[i].x
269
            red[i].y0 = redhome[i].y
270
            red[i].x = (red[i].x0) + 25
271
            red[i].y = (red[i].y0) + 25
272

273
            bluehome[i].x = (100 + (100 * (i - 2)))
274
            bluehome[i].y = (650)
275
            blue[i].x0 = bluehome[i].x
276
            blue[i].y0 = bluehome[i].y
277
            blue[i].x = (blue[i].x0) + 25
278
            blue[i].y = (blue[i].y0) + 25
279

280
            yellowhome[i].x = (550 + (100 * (i - 2)))
281
            yellowhome[i].y = (650)
282
            yellow[i].x0 = yellowhome[i].x
283
            yellow[i].y0 = yellowhome[i].y
284
            yellow[i].x = (yellow[i].x0) + 25
285
            yellow[i].y = (yellow[i].y0) + 25
286

287
            greenhome[i].x = (550 + (100 * (i - 2)))
288
            greenhome[i].y = 200
289
            green[i].x0 = greenhome[i].x
290
            green[i].y0 = greenhome[i].y
291
            green[i].x = (green[i].x0) + 25
292
            green[i].y = (green[i].y0) + 25
293

294
        for i in range(6):
295
            box[i].x = 300
296
            box[i].y = (700 - (50 * i))
297

298
        for i in range(6, 12):
299
            box[i].x = (250 - (50 * (i - 6)))
300
            box[i].y = (400)
301

302
        box[12].x = 0
303
        box[12].y = 350
304

305
        for i in range(13, 19):
306
            box[i].x = (0 + (50 * (i - 13)))
307
            box[i].y = (300)
308

309
        for i in range(19, 25):
310
            box[i].x = (300)
311
            box[i].y = (250 - (50 * (i - 19)))
312

313
        box[25].x = 350
314
        box[25].y = 0
315

316
        for i in range(26, 32):
317
            box[i].x = (400)
318
            box[i].y = (0 + (50 * (i - 26)))
319

320
        for i in range(32, 38):
321
            box[i].x = (450 + (50 * (i - 32)))
322
            box[i].y = (300)
323

324
        box[38].x = 700
325
        box[38].y = 350
326

327
        for i in range(39, 45):
328
            box[i].x = (700 - (50 * (i - 39)))
329
            box[i].y = (400)
330

331
        for i in range(45, 51):
332
            box[i].x = (400)
333
            box[i].y = (450 + (50 * (i - 45)))
334

335
        box[51].x = 350
336
        box[51].y = 700
337

338
        # teshh
339
        lx = 14
340
        for i in range(52):
341
            redbox[i].x = box[lx].x
342
            redbox[i].y = box[lx].y
343
            lx = lx + 1
344
            if lx > 51:
345
                lx = 0
346

347
        lx = 50
348
        for i in range(7):
349
            redbox[lx].x = (0 + (50 * i))
350
            redbox[lx].y = 350
351
            lx = lx + 1
352
        # blue
353
        lx = 1
354
        for i in range(52):
355

356
            bluebox[i].x = box[lx].x
357
            bluebox[i].y = box[lx].y
358
            lx = lx + 1
359
            if lx > 51:
360
                lx = 0
361

362
        lx = 50
363
        for i in range(7):
364
            bluebox[lx].x = 350
365
            bluebox[lx].y = (700 - (50 * i))
366
            lx = lx + 1
367
        # yellow
368
        lx = 40
369
        for i in range(52):
370
            yellowbox[i].x = box[lx].x
371
            yellowbox[i].y = box[lx].y
372
            lx = lx + 1
373
            if lx > 51:
374
                lx = 0
375

376
        lx = 50
377
        for i in range(7):
378
            yellowbox[lx].x = (700 - (50 * i))
379
            yellowbox[lx].y = (350)
380
            lx = lx + 1
381

382
        # green
383
        lx = 27
384
        for i in range(52):
385

386
            greenbox[i].x = box[lx].x
387
            greenbox[i].y = box[lx].y
388

389
            lx = lx + 1
390
            if lx > 51:
391
                lx = 0
392

393
        lx = 50
394
        for i in range(7):
395
            greenbox[lx].x = 350
396
            greenbox[lx].y = (0 + (50 * i))
397
            lx = lx + 1
398

399
        for i in range(4):
400
            red[i].swap()
401
            blue[i].swap()
402
            green[i].swap()
403
            yellow[i].swap()                      
404

405

406
    else:  
407

408
        if c >= 1:                                #This condition is true when a click is made.
409

410
            if RED == True and TURN == False:      #Red players turn
411
                print("Red's Turn")
412
                print("moves available: ", rolls)
413
                la = "RED"
414
                if (movecheck(red, redhome, redbox, la)) == False:  #Checks if player can take a turn.
415
                    BLUE = True
416
                    RED = False
417
                    clear()                                          #clears variable, next players turn
418

419
                if RED == True:                                   # searches if click is made on a red game piece.
420
                    for i in range(len(red)):
421
                        if ((((cx > red[i].x0 + 13) and (cx < red[i].x + 13)) and (
422
                            (cy > red[i].y0 + 14) and (cy < red[i].y + 14)))
423
                            and (red[i].x0 == redhome[i].x) and (red[i].y0 == redhome[i].y)):
424
                            print("Interesting ")
425

426
                            if rolls[0 + nc] == 6:                 #If a six occurs and gamepiece is in home
427
                                                                    #Game piece is moved onto the home box
428
                                red[i].x0 = redbox[0].x
429
                                red[i].y0 = redbox[0].y
430
                                red[i].x = redbox[0].x + 25
431
                                red[i].y = redbox[0].y + 25
432
                                red[i].num = 0
433
                                red[i].swap()
434
                                nc = nc + 1
435

436
                                if nc > len(rolls) - 1:           # check if all moves are made. so next players turn.
437
                                    BLUE = True
438
                                    RED = False
439
                                    clear()
440
                                break
441

442
                        if ((((cx > red[i].x0 + 13) and (cx < red[i].x + 13)) and (       #if gamepiece is outside home
443
                            (cy > red[i].y0 + 14) and (cy < red[i].y + 14)))
444
                            and ((red[i].x0 > 270) or (red[i].y0 > 270))):
445
                            print("Interesting ")
446
                            bb = ((red[i].num) + rolls[0 + nc])
447
                            # Winning condition
448

449
                            if bb > 57:                        #prevents moves greater than allowed number
450
                                break
451
                                #bb = ((red[i].num) + rolls[0 + nc]) - 57
452

453
                            kill(redbox,blue,yellow,green,bluehome,yellowhome,greenhome)      #checks if a kill can be made.
454

455
                            red[i].x0 = redbox[bb].x
456
                            red[i].y0 = redbox[bb].y
457
                            red[i].x = redbox[bb].x + 25
458
                            red[i].y = redbox[bb].y + 25
459
                            red[i].swap()
460
                            red[i].num = bb
461
                            doublecheck(red)                           #checks if the gamepiece can be made as a double.
462

463
                            nc = nc + 1
464
                            if bb == 57:                              
465
                                # del red[i]
466
                                red.remove(red[i]);
467

468
                            if nc > len(rolls) - 1:
469
                                BLUE = True                         #next players turn.
470
                                RED = False
471
                                clear()
472
                            break
473

474

475
                   
476

477
            if BLUE == True and TURN == False:                        #same as REDS CODE
478
                print("Blue's Turn")
479
                print("moves available: ", rolls)
480
                la="BLUE"
481
                if (movecheck(blue, bluehome, bluebox, la)) == False:
482
                    print("NO MOVES SIR JEE")
483
                    BLUE = False
484
                    YELLOW = True
485
                    clear()
486

487
                if BLUE == True:
488

489
                    for i in range(len(blue)):
490
                        if ((((cx > blue[i].x0 + 13) and (cx < blue[i].x + 13)) and (
491
                            (cy > blue[i].y0 + 14) and (cy < blue[i].y + 14)))
492
                            and (blue[i].x0 == bluehome[i].x) and (blue[i].y0 == bluehome[i].y)):
493
                            print("Interesting ")
494

495
                            if rolls[0 + nc] == 6:
496

497
                                blue[i].x0 = bluebox[0].x
498
                                blue[i].y0 = bluebox[0].y
499
                                blue[i].x = bluebox[0].x + 25
500
                                blue[i].y = bluebox[0].y + 25
501
                                blue[i].num = 0
502
                                blue[i].swap()
503
                                nc = nc + 1
504

505
                                if nc > len(rolls) - 1:
506
                                    YELLOW = True
507
                                    BLUE = False
508
                                    clear()
509
                                break
510

511
                        if ((((cx > blue[i].x0 + 13) and (cx < blue[i].x + 13)) and (
512
                            (cy > blue[i].y0 + 14) and (cy < blue[i].y + 14)))
513
                            and ((blue[i].x0 > 270) or (blue[i].y0 < 470))):
514
                            print("Interesting ")
515
                            bb = ((blue[i].num) + rolls[0 + nc])
516
                            if bb > 57:
517
                                break
518
                                # bb= ((blue[i].num) + rolls[0 + nc]) - 52
519

520
                            kill(bluebox,red,yellow,green,redhome,yellowhome,greenhome)
521

522
                            blue[i].x0 = bluebox[bb].x
523
                            blue[i].y0 = bluebox[bb].y
524
                            blue[i].x = bluebox[bb].x + 25
525
                            blue[i].y = bluebox[bb].y + 25
526
                            blue[i].swap()
527
                            blue[i].num = bb
528
                            doublecheck(blue)
529
                            nc = nc + 1
530
                            if bb == 57:
531
                                # del red[i]
532
                                blue.remove(blue[i]);
533

534
                            if nc > len(rolls) - 1:
535
                                YELLOW = True
536
                                BLUE = False
537
                                clear()
538
                            break
539

540
                        
541

542
            if YELLOW == True and TURN == False:                     #Same as RED's code
543
                print("Yellows's Turn")
544
                print("moves available: ", rolls)
545
                la="YELLOW"
546
                if (movecheck(yellow, yellowhome, yellowbox,la)) == False:
547
                    print("NO MOVES SIR JEE")
548
                    YELLOW = False
549
                    GREEN = True
550
                    clear()
551

552
                if YELLOW == True:
553

554
                    for i in range(len(yellow)):
555
                        if ((((cx > yellow[i].x0 + 13) and (cx < yellow[i].x + 13)) and (
556
                                    (cy > yellow[i].y0 + 14) and (cy < yellow[i].y + 14)))
557
                            and (yellow[i].x0 == yellowhome[i].x) and (yellow[i].y0 == yellowhome[i].y)):
558
                            print("Interesting ")
559

560
                            if rolls[0 + nc] == 6:
561

562
                                yellow[i].x0 = yellowbox[0].x
563
                                yellow[i].y0 = yellowbox[0].y
564
                                yellow[i].x = yellowbox[0].x + 25
565
                                yellow[i].y = yellowbox[0].y + 25
566
                                yellow[i].num = 0
567
                                yellow[i].swap()
568
                                nc = nc + 1
569

570
                                if nc > len(rolls) - 1:
571
                                    YELLOW = False
572
                                    GREEN = True
573
                                    clear()
574
                                break
575

576
                        if ((((cx > yellow[i].x0 + 13) and (cx < yellow[i].x + 13)) and (
577
                                    (cy > yellow[i].y0 + 14) and (cy < yellow[i].y + 14)))
578
                            and ((yellow[i].x0 < 470) or (yellow[i].y0 < 470))):
579
                            print("Interesting ")
580
                            bb = ((yellow[i].num) + rolls[0 + nc])
581
                            if bb > 57:
582
                                break
583
                                #bb = ((yellow[i].num) + rolls[0 + nc]) - 52
584

585
                            kill(yellowbox,blue,red,green,bluehome,redhome,greenhome)
586

587
                            yellow[i].x0 = yellowbox[bb].x
588
                            yellow[i].y0 = yellowbox[bb].y
589
                            yellow[i].x = yellowbox[bb].x + 25
590
                            yellow[i].y = yellowbox[bb].y + 25
591
                            yellow[i].swap()
592
                            yellow[i].num = bb
593
                            doublecheck(yellow)
594
                            nc = nc + 1
595
                            if bb == 57:
596
                                # del red[i]
597
                                yellow.remove(yellow[i]);
598

599
                            if nc > len(rolls) - 1:
600
                                YELLOW = False
601
                                GREEN = True
602
                                clear()
603
                            break
604

605

606
                        
607

608
            if GREEN == True and TURN == False:                     #Same as RED's code
609
                print("Green's Turn")
610
                print("moves available: ", rolls)
611
                la="GREEN"
612
                if (movecheck(green, greenhome, greenbox,la)) == False:
613
                    print("NO MOVES SIR JEE")
614
                    GREEN = False
615
                    RED = True
616
                    clear()
617

618
                if GREEN == True:
619

620
                    for i in range(len(green)):
621
                        if ((((cx > green[i].x0 + 13) and (cx < green[i].x + 13)) and (
622
                                    (cy > green[i].y0 + 14) and (cy < green[i].y + 14)))
623
                            and (green[i].x0 == greenhome[i].x) and (green[i].y0 == greenhome[i].y)):
624
                            print("Interesting ")
625

626
                            if rolls[0 + nc] == 6:
627

628
                                green[i].x0 = greenbox[0].x
629
                                green[i].y0 = greenbox[0].y
630
                                green[i].x = greenbox[0].x + 25
631
                                green[i].y = greenbox[0].y + 25
632
                                green[i].num = 0
633
                                green[i].swap()
634
                                nc = nc + 1
635
                                print("green x.y: ", green[i].x0, green[i].y0)
636

637
                                if nc > len(rolls) - 1:
638
                                    GREEN = False
639
                                    RED = True
640
                                    clear()
641
                                break
642

643
                        if ((((cx > green[i].x0 + 13) and (cx < green[i].x + 13)) and (
644
                                    (cy > green[i].y0 + 14) and (cy < green[i].y + 14)))
645
                            and ((green[i].x0 < 470) or (green[i].y0 < 470))):
646
                            print("Interesting ")
647
                            bb = ((green[i].num) + rolls[0 + nc])
648
                            if bb > 57:
649
                                break
650
                                # bb = ((green[i].num) + rolls[0 + nc]) - 52
651

652
                            kill(greenbox,blue,yellow,red,bluehome,yellowhome,redhome)
653

654
                            green[i].x0 = greenbox[bb].x
655
                            green[i].y0 = greenbox[bb].y
656
                            green[i].x = greenbox[bb].x + 25
657
                            green[i].y = greenbox[bb].y + 25
658
                            green[i].swap()
659
                            green[i].num = bb
660
                            nc = nc + 1
661
                            doublecheck(green)
662
                            if bb == 57:
663
                                # del red[i]
664
                                green.remove(green[i]);
665

666
                            if nc > len(rolls) - 1:
667
                                GREEN = False
668
                                RED = True
669
                                clear()
670
                            break
671

672

673
main()    #Main functin is called once when c==0 to intialize all the gamepieces.
674

675

676
def leftClick(event):  # Main play function is called on every left click.
677

678
    global c, cx, cy, RED, YELLOW
679
    c = c + 1
680
    cx = root.winfo_pointerx() - root.winfo_rootx()  # This formula returns the x,y co-ordinates of the mouse pointer relative to the board.
681
    cy = root.winfo_pointery() - root.winfo_rooty()
682

683
    print("Click at: ", cx, cy)
684

685
    main()           #Main function called on every click to progress the game
686

687

688
root.bind("<Button-1>", leftClick)
689

690

691
def turn():   #Prints whoose turn is it
692

693
    if RED == True:
694
        L2 = Label(root, text="   Red's Turn    ", fg='Black', background='green', font=("Arial", 24, "bold"))
695
        L2.place(x=770, y=50)
696

697
    if BLUE == True:
698
        L2 = Label(root, text="   Blue's Turn   ", fg='Black', background='green', font=("Arial", 24, "bold"))
699
        L2.place(x=770, y=50)
700

701
    if GREEN == True:
702
        L2 = Label(root, text="Green's Turn  ", fg='Black', background='green', font=("Arial", 24, "bold"))
703
        L2.place(x=770, y=50)
704

705
    if YELLOW == True:
706
        L2 = Label(root, text="Yellow's Turn", fg='Black', background='green', font=("Arial", 24, "bold"))
707
        L2.place(x=770, y=50)
708

709

710
def roll():   #Rolling function that rolls a dice, goes again if its a six
711
    global rollc, dice, dice1, dice2, TURN, rolls
712

713
    if TURN == True:
714

715
        rollc = rollc + 1
716
        print("roll: ", rollc)
717

718
        if rollc == 1:
719
            dice = random.randint(1, 6)
720
            L1 = Label(root, text=dice, fg='Black', background='green', font=("Arial", 24, "bold"))
721
            L1.place(x=800, y=200)
722
            print("dice: ", dice)
723
            rolls.append(dice)
724
            if dice != 6:
725
                rollc = 0
726
                TURN = False
727

728
        if rollc == 2:
729
            if dice == 6:
730
                dice1 = random.randint(1, 6)
731
                L3 = Label(root, text=dice1, fg='Black', background='green', font=("Arial", 24, "bold"))
732
                L3.place(x=800, y=250)
733
                rolls.append(dice1)
734
                if dice1 != 6:
735
                    rollc = 0
736
                    TURN = False
737

738
        if rollc == 3:
739
            if dice1 == 6:
740
                dice2 = random.randint(1, 6)
741
                L4 = Label(root, text=dice2, fg='Black', background='green', font=("Arial", 24, "bold"))
742
                L4.place(x=800, y=300)
743
                rolls.append(dice2)
744
                rollc = 0
745
                TURN = False
746

747

748
def clear():        #clears all the variable prior to next player's turn
749
    global nc, rolls, TURN, L1, L3, L4
750
    nc = 0
751
    del rolls[:]
752
    TURN = True
753
    L1 = Label(root, text="        ", fg='Black', background='green', font=("Arial", 24, "bold"))
754
    L1.place(x=800, y=200)
755
    L3 = Label(root, text="        ", fg='Black', background='green', font=("Arial", 24, "bold"))
756
    L3.place(x=800, y=250)
757
    L4 = Label(root, text="        ", fg='Black', background='green', font=("Arial", 24, "bold"))
758
    L4.place(x=800, y=300)
759
    print("cleared")
760
    turn()
761

762

763
def movecheck(r, rh, rb, la):       #Check if the player can make a move
764

765
    if (dice == 6 and dice1 == 6 and dice2 == 6):
766
        return False
767

768
    win=True                                                  #Checking if the game is won or the player can make any moves.
769
    for j in range(4):
770
        if (r[j].x0 != rb[56].x) and (r[j].y0 != rb[56].y):
771
             win=False
772

773
    if win == True:                                         #If all gamepieces home, prints that the player has won
774
        print("YOU HAVE WON")
775
        L2 = Label(root, text=(la + "Wins"), fg='Black', background='green', font=("Arial", 24, "bold"))
776
        L2.place(x=770, y=500)
777
        return False
778

779
    if win == False and dice != 6:                    #if its not a 6 and all game pieces inside home, then next players turn
780
        for i in range(len(r)):
781
            if(r[i].num != -1):
782
                (print("Move Ahead"))
783
                return True
784
        print("All Cleared")
785
        return False
786

787
def kill(a,b,c,d,bh,ch,dh):   #function that determines if a gamepiece can be killed
788

789
    #if the game piece is not on a stop
790
    if ((a[bb].x0 != box[1].x and a[bb].y0 != box[1].y) and (a[bb].x0 != box[14].x and a[bb].y0 != box[14].y) and
791
        (a[bb].x0 != box[9].x and a[bb].y0 != box[9].y) and (a[bb].x0 != box[22].x and a[bb].y0 != box[22].y) and
792
        (a[bb].x0 != box[27].x and a[bb].y0 != box[27].y) and (a[bb].x0 != box[35].x and a[bb].y0 != box[35].y) and
793
        (a[bb].x0 != box[40].x and a[bb].y0 != box[40].y) and (a[bb].x0 != box[48].x and a[bb].y0 != box[48].y)):
794

795

796
        #if the game piece of another color and its on the same block and it is not a double, a kill is made
797
        for i in range (len(b)):
798
            if (b[i].x0 == a[bb].x and b[i].y0 == a[bb].y and (b[i].double == False)):
799
                b[i].x0 = bh[i].x
800
                b[i].y0 = bh[i].y
801
                b[i].x = bh[i].x + 25
802
                b[i].y = bh[i].y + 25
803
                b[i].num=-1
804
                b[i].swap()
805
                break
806

807
        for i in range (len(c)):
808
            if (c[i].x0 == a[bb].x and c[i].y0 == a[bb].y and (c[i].double == False)):
809
                c[i].x0 = ch[i].x
810
                c[i].y0 = ch[i].y
811
                c[i].x = ch[i].x + 25
812
                c[i].y = ch[i].y + 25
813
                c[i].num=-1
814
                c[i].swap()
815
                break
816

817
        for i in range (len(d)):
818
            if (d[i].x0 == a[bb].x and d[i].y0 == a[bb].y and (d[i].double == False)):
819
                d[i].x0 = dh[i].x
820
                d[i].y0 = dh[i].y
821
                d[i].x = dh[i].x + 25
822
                d[i].y = dh[i].y + 25
823
                d[i].num=-1
824
                d[i].swap()
825
                break
826

827
def doublecheck(a):        
828

829
    for k in range (len(a)):
830
        a[k].double = False
831

832
    for i in range (len(a)):
833
        for j in range (len(a)):
834
            if (a[i].num == a[j].num) and (i != j):
835
                a[j].double = True
836
                a[i].double = True
837

838

839
turn()            #prints the "red player's turn" initially
840

841
button = Button(root, text="   ROLL   ", relief="raised", font=("Arial", 20),
842
                command=roll)  # call roll function evertime this button is clicked
843
button.place(x=805, y=120)
844

845
root.mainloop()
846

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.