-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.py
More file actions
504 lines (393 loc) · 16.1 KB
/
Game.py
File metadata and controls
504 lines (393 loc) · 16.1 KB
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
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from MPL import *
from MCL import *
from Board import *
from Paths import *
from GameHelpers import *
import random
W_Width, W_Height = 1500, 1000
# 375 and 250 on either side
BOX_SIZE = 50
POSSIBLE_MOVES_BOXES_COLOR = (0.82, 0.96, 0.96)
## Detections
def convert_coordinate(x,y):
global W_Width, W_Height
a = x - (W_Width/2)
b = (W_Height/2) - y
return a/2,b/2
# Game Data
players = {
"yellowPlayer": {
"tokenColor": (0.75, 0.75, 0.0),
"tokenPositions": {
0: (120, 0),
1: (125, -155),
2: (155, -125),
3: (155, -155)
},
"tokenPathProgress": {
0: 53,
1: None,
2: None,
3: None,
},
"tokenDocks": {
0: (125, -125),
1: (125, -155),
2: (155, -125),
3: (155, -155)
}
},
"bluePlayer": {
"tokenColor": (0.0, 0.0, 0.5),
"tokenPositions": {
0: (-145, 145),
1: (-145, 115),
2: (-115, 145),
3: (-115, 115)
},
"tokenPathProgress": {
0: None,
1: None,
2: None,
3: None,
},
"tokenDocks": {
0: (-145, 145),
1: (-145, 115),
2: (-115, 145),
3: (-115, 115)
}
},
"greenPlayer": {
"tokenColor": (0.0, 0.5, 0.0),
"tokenPositions": {
0: (-145, -125),
1: (-145, -155),
2: (-115, -125),
3: (-115, -155)
},
"tokenPathProgress": {
0: None,
1: None,
2: None,
3: None,
},
"tokenDocks": {
0: (-145, -125),
1: (-145, -155),
2: (-115, -125),
3: (-115, -155)
}
},
"redPlayer": {
"tokenColor": (0.5, 0.0, 0.0),
"tokenPositions": {
0: (125, 145),
1: (125, 115),
2: (155, 145),
3: (155, 115),
},
"tokenPathProgress": {
0: None,
1: None,
2: None,
3: None,
},
"tokenDocks": {
0: (125, 145),
1: (125, 115),
2: (155, 145),
3: (155, 115),
}
}
}
gameSituation = {
"moveSelectionColor": (0.6, 0, 1),
"turnOrder": ["yellowPlayer", "greenPlayer", "bluePlayer", "redPlayer"],
"currentTurn": "yellowPlayer",
"tokensLeft": {
"yellowPlayer": 4,
"greenPlayer": 4,
"bluePlayer": 4,
"redPlayer": 4
},
"selectedToken": None,
"possibleMovesOriginalColor": [],
"possibleMoves": [],
"animationDestination": None,
"waitingForDiceRoll": True,
"waitingForTokenSelection": False,
"waitingForMoveSelection": False,
"animationIsOngoing": False,
"diceValue": None,
"numOfMovesLeft": 0,
}
def onClickDetectTokenSelection(x, y):
for token in players[gameSituation["currentTurn"]]["tokenPositions"]:
token_x, token_y = players[gameSituation["currentTurn"]]["tokenPositions"][token]
if x >= token_x - 15 and x <= token_x + 15 and y >= token_y - 15 and y <= token_y + 15:
return token
return None
# Mouse Controller
def mouseListener(button, state, x, y):
global pos
if button==GLUT_LEFT_BUTTON:
if(state == GLUT_DOWN):
x, y = convert_coordinate(x, y)
if gameSituation["waitingForTokenSelection"]:
selectedToken = onClickDetectTokenSelection(x, y)
if selectedToken is not None and players[gameSituation["currentTurn"]]["tokenPathProgress"][selectedToken] is not None:
gameSituation["selectedToken"] = selectedToken
changeColorOfPossibleMoves()
print("Ran the seond time")
gameSituation["waitingForTokenSelection"] = False
gameSituation["waitingForMoveSelection"] = True
elif selectedToken is not None and players[gameSituation["currentTurn"]]["tokenPathProgress"][selectedToken] is None and gameSituation["numOfMovesLeft"] >= 6:
gameSituation["selectedToken"] = selectedToken
# Undock token
players[gameSituation["currentTurn"]]["tokenPathProgress"][selectedToken] = 0
players[gameSituation["currentTurn"]]["tokenPositions"][selectedToken] = paths[gameSituation["currentTurn"] + "Path"][0]
gameSituation["numOfMovesLeft"] -= 6
gameSituation["selectedToken"] = None
if gameSituation["numOfMovesLeft"] > 0:
gameSituation["waitingForTokenSelection"] = True
gameSituation["waitingForMoveSelection"] = False
gameSituation["animationIsOngoing"] = False
gameSituation["waitingForDiceRoll"] = False
else:
changeTurns()
elif gameSituation["waitingForMoveSelection"]:
move = detectClickOnPossibleMove(x, y)
print("Move", move)
if move is not None:
delta = gameSituation["possibleMoves"].index(move)+1
gameSituation["numOfMovesLeft"] -= (delta - 1)
print("Delta", delta)
# Move token
players[gameSituation["currentTurn"]]["tokenPathProgress"][gameSituation["selectedToken"]] = players[gameSituation["currentTurn"]]["tokenPathProgress"][gameSituation["selectedToken"]] + delta
players[gameSituation["currentTurn"]]["tokenPositions"][gameSituation["selectedToken"]] = paths[gameSituation["currentTurn"] + "Path"][players[gameSituation["currentTurn"]]["tokenPathProgress"][gameSituation["selectedToken"]] - 1]
if players[gameSituation["currentTurn"]]["tokenPathProgress"][gameSituation["selectedToken"]] == 57:
print("Token reached destination")
return
# Restore original color of possible moves
for move in gameSituation["possibleMoves"]:
pier = onClickDetectPier(move[0], move[1])
coordinates = onClickDetectCoordinates(move[0], move[1])
board[pier][coordinates][1] = gameSituation["possibleMovesOriginalColor"].pop(0)
gameSituation["possibleMovesOriginalColor"] = []
if gameSituation["numOfMovesLeft"] == 0:
changeTurns()
else:
# Wait for new token selection
gameSituation["waitingForMoveSelection"] = False
gameSituation["waitingForTokenSelection"] = True
print(gameSituation)
if button==GLUT_RIGHT_BUTTON:
global i, mod
if state == GLUT_DOWN:
# Dice roll
if gameSituation["waitingForDiceRoll"]:
diceValue = [6, 3, 6, 4][i] # random.randint(1, 6)
i = (i + 1) % mod
print("Dice value:", diceValue)
gameSituation["diceValue"] = diceValue
gameSituation["numOfMovesLeft"] += diceValue
if diceValue == 6:
return
if checkPossibilityForMove():
gameSituation["waitingForDiceRoll"] = False
gameSituation["waitingForTokenSelection"] = True
else:
changeTurns()
glutPostRedisplay()
i = 0
mod = 4
def changeTurns():
global gameSituation
currentTurnIndex = gameSituation["turnOrder"].index(gameSituation["currentTurn"])
nextTurnIndex = (currentTurnIndex + 1) % 4
gameSituation["currentTurn"] = gameSituation["turnOrder"][nextTurnIndex]
gameSituation["selectedToken"] = None
gameSituation["possibleMoves"] = []
gameSituation["possibleMovesOriginalColor"] = []
gameSituation["waitingForMoveSelection"] = False
gameSituation["numOfMovesLeft"] = 0
gameSituation["diceValue"] = None
gameSituation["waitingForTokenSelection"] = False
gameSituation["animationIsOngoing"] = False
gameSituation["waitingForDiceRoll"] = True
print("########### Turn", gameSituation["currentTurn"], "###########\nRight click to roll dice")
def changeColorOfPossibleMoves():
selectedTokenCurrentPathProgress = players[gameSituation["currentTurn"]]["tokenPathProgress"][gameSituation["selectedToken"]]
numberOfMoves = gameSituation["numOfMovesLeft"]
# pathPoints = paths[gameSituation["currentTurn"] + "Path"].keys()[selectedTokenCurrentPathProgress:selectedTokenCurrentPathProgress+numberOfMoves+1]
end_point = min(56, players[gameSituation["currentTurn"]]["tokenPathProgress"][gameSituation["selectedToken"]]+gameSituation["numOfMovesLeft"]+1) + 1
for point in range(players[gameSituation["currentTurn"]]["tokenPathProgress"][gameSituation["selectedToken"]], end_point):
pier = onClickDetectPier(paths[gameSituation["currentTurn"] + "Path"][point][0], paths[gameSituation["currentTurn"] + "Path"][point][1])
coordinates = onClickDetectCoordinates(paths[gameSituation["currentTurn"] + "Path"][point][0], paths[gameSituation["currentTurn"] + "Path"][point][1])
gameSituation["possibleMoves"].append(paths[gameSituation["currentTurn"] + "Path"][point])
if pier and coordinates and point < 56:
print(pier, coordinates, paths[gameSituation["currentTurn"] + "Path"][point][0])
gameSituation["possibleMovesOriginalColor"].append(board[pier][coordinates][1])
board[pier][coordinates][1] = gameSituation["moveSelectionColor"]
print("POSSIBLE MOVES", gameSituation["possibleMoves"])
# for move in gameSituation["possibleMoves"]:
# pier = onClickDetectPier(move[0], move[1])
# coordinates = onClickDetectCoordinates(move[0], move[1])
# gameSituation["possibleMovesOriginalColor"].append(board[pier][coordinates][1])
# board[pier][coordinates] = gameSituation["moveSelectionColor"]
def checkPossibilityForMove():
global gameSituation, players, paths
currentTurnTokenProgress = players[gameSituation["currentTurn"]]["tokenPathProgress"]
# Checking if there are undocked tokens
for token in currentTurnTokenProgress:
if currentTurnTokenProgress[token] is not None:
break
return False if gameSituation["numOfMovesLeft"] < 6 else True
# Checking if token has enough path to be covered
for token in currentTurnTokenProgress:
if token + gameSituation["numOfMovesLeft"] <= 56:
return True
return False
def detectClickOnCurrentTurnToken(x, y):
global gameSituation, players
currentTurn = gameSituation["currentTurn"]
for token in players[currentTurn]["tokenPositions"]:
token_x, token_y = players[currentTurn]["tokenPositions"][token]
if x >= token_x - 15 and x <= token_x + 15 and y >= token_y - 15 and y <= token_y + 15:
return token
return None
def detectClickOnPossibleMove(x, y):
global gameSituation, players
currentTurn = gameSituation["currentTurn"]
for move in gameSituation["possibleMoves"]:
move_x, move_y = move
if x >= move_x - 15 and x <= move_x + 15 and y >= move_y - 15 and y <= move_y + 15:
return move
return None
# Drawing Functions
def drawTokens():
for key in players:
tokenColor = players[key]["tokenColor"]
tokenPositions = players[key]["tokenPositions"]
glColor3f(tokenColor[0], tokenColor[1], tokenColor[2])
for token in tokenPositions:
x, y = tokenPositions[token]
MCL(x, y, 10)
def drawBox(center, size, color): # Draw a cross at the top right
glColor3f(0, 0, 0)
x, y = center
s = size // 2
MPL(x+s, y+s, x+s, y-s)
MPL(x+s, y-s, x-s, y-s)
MPL(x-s, y-s, x-s, y+s)
MPL(x-s, y+s, x+s, y+s)
fill(color, center, size)
def fill(color, center, size):
x, y = center
s = size // 2
r, g, b = color
glColor3f(r, g, b)
y_max = y + s
y_min = y - s
x_max = x + s
x_min = x - s
# Color left and bottom edges
# First loop leaves out y_max and includes y_min
y = y_max-0.5
x = x_min+0.5
while y > y_min:
while x < x_max:
glBegin(GL_POINTS)
glVertex2d(x, y)
glEnd()
x += 0.5
x = x_min+0.5
y -= 0.5
def drawBoard():
for pier in board.keys():
if pier in ["pierHorizontalRight", "pierHorizontalLeft", "pierVerticalTop", "pierVerticalBottom"]:
for key in board[pier].keys():
color = board[pier][key][1]
center = board[pier][key][0]
drawBox(center, 30, color)
else:
for key in board[pier].keys():
if key == (0, 0):
color = board[pier][key][1]
center = board[pier][key][0]
drawBox(center, 180, color)
else:
color = board[pier][key][1]
center = board[pier][key][0]
drawBox(center, 120, color)
def display():
global pos
#//clear the display
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glClearColor(1,1,1,1); # WHITE BACKGROUND
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
#//load the correct matrix -- MODEL-VIEW matrix
glMatrixMode(GL_MODELVIEW)
#//initialize the matrix
glLoadIdentity()
#//now give three info
#//1. where is the camera (viewer)?
#//2. where is the camera looking?
#//3. Which direction is the camera's UP direction?
gluLookAt(0,0,200, 0,0,0, 0,1,0)
glMatrixMode(GL_MODELVIEW)
drawBoard()
drawTokens()
# Game controls
# if gameSituation["numOfMovesLeft"] > 0:
# gameSituation["waitingForTokenSelection"] = True
# elif gameSituation["waitingForTokenSelection"]:
# print("Select token")
glutSwapBuffers()
pos = 0
# player color # token pos #token num
# players["yellowPlayer"]["tokenPositions"][0] = paths["yellowPlayerPath"][1]
def animate():
#//codes for any changes in Models, Camera
glutPostRedisplay()
global pos, players, paths, gameSituation
# if animationIsOngoing:
# if pos <= 56:
# players["yellowPlayer"]["tokenPositions"][0] = paths["yellowPath"][pos]
# players["greenPlayer"]["tokenPositions"][0] = paths["greenPath"][pos]
# players["bluePlayer"]["tokenPositions"][0] = paths["bluePath"][pos]
# players["redPlayer"]["tokenPositions"][0] = paths["redPath"][pos]
# pos += 1
# print(pos)
def init():
#//clear the screeng
global pos
pos = 0
glClearColor(0,0,0,0)
#//load the PROJECTION matrix
glMatrixMode(GL_PROJECTION)
#//initialize the matrix
glLoadIdentity()
#//give PERSPECTIVE parameters
gluPerspective(104, 1.5, 1, 1500.0)
# **(important)**aspect ratio that determines the field of view in the X direction (horizontally). The bigger this angle is, the more you can see of the world - but at the same time, the objects you can see will become smaller.
#//near distance
#//far distance
print(f"########### Turn {gameSituation['currentTurn']} ###########\nRight click to roll dice")
glutInit()
glutInitWindowSize(W_Width, W_Height)
glutInitWindowPosition(0, 0)
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB) # //Depth, Double buffer, RGB color
# glutCreateWindow("My OpenGL Program")
wind = glutCreateWindow(b"Ludo project")
init()
glutDisplayFunc(display) # Display callback function
glutIdleFunc(animate) # What you want to do in the idle time (when no drawing is occuring)
glutMouseFunc(mouseListener)
# glutSpecialFunc(specialKeyListener)
# glutMouseFunc(mouseListener)
glutMainLoop() #The main loop of OpenGL