-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard16.py
606 lines (505 loc) · 24 KB
/
keyboard16.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
from direct.showbase.ShowBase import ShowBase
from panda3d.core import CollisionTraverser, CollisionNode
from panda3d.core import CollisionHandlerQueue, CollisionRay
from panda3d.core import AmbientLight, DirectionalLight, LightAttrib
from panda3d.core import LPoint3, LVector3, BitMask32, LPoint3f
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task
# Mapping the key Presed
keyMap = {
"up" : False,
"down" : False,
"left": False,
"right": False,
"u": False,
"d": False,
"enter":False,
"space":False}
# Changes the above mapping of the keys
def updateKeyMap(key):
keyMap[key] = True
# Checks whether to tupples are equal????
def check(p1,p2):
return p1 == p2
def color(r,g,b):
return((r/255,g/255,b/255))
class MilleniumChess(ShowBase):
# Forms Board place the pieces
# And draws the selected (floating) square
def formBoardAndPieces(self):
index = -1
for z in range (-1,2):
count= 1
for x in range(-3,5):
# going to next row
for y in range(13,5,-1):
index += 1
# going throug a constant row
if count % 2 == 0:
self.squares[index] = loader.loadModel("models/square.egg")
self.squares[index].reparentTo(render)
self.squares[index].setPos(x,y+(8 * z) ,(z))
self.squares[index].setColor(0,0,0)
# count+= 1
else:
self.squares[index] = loader.loadModel("models/square.egg")
self.squares[index].reparentTo(render)
self.squares[index].setPos(x ,y+(8 * z),(z))
self.squares[index].setColor(1,1,1)
if y == 12 and z != 0 and z != -1:
self.pieces[index] = loader.loadModel("models/pawn.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x ,y+(8 * z),(z))
self.pieces[index].setColor(1,1,1)
elif y == 7 and z != 0 and z != 1:
self.pieces[index] = loader.loadModel("models/pawn.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y+(8 * z),(z))
self.pieces[index].setColor(color(150, 75, 0)[0],color(150, 75, 0)[1],color(150, 75, 0)[2])
elif (x, y) in [(-3, 13), (4,13), (-3,6), (4,6)] and z != 0:
if y == 13 and z != -1:
self.pieces[index] = loader.loadModel("models/rook.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y+(8 * z),(z))
self.pieces[index].setColor(1,1,1)
elif y == 6 and z != 1:
self.pieces[index] = loader.loadModel("models/rook.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y+(8 * z),(z))
self.pieces[index].setColor(color(150, 75, 0)[0],color(150, 75, 0)[1],color(150, 75, 0)[2])
elif (x, y) in [(-2, 13), (3,13), (-2,6), (3,6)]and z != 0:
if y == 13 and z != -1:
self.pieces[index] = loader.loadModel("models/knight.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y+(8 * z),(z))
self.pieces[index].setColor(1,1,1)
elif y == 6 and z != 1:
self.pieces[index] = loader.loadModel("models/knight.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y+(8 * z),(z))
self.pieces[index].setColor(color(150, 75, 0)[0],color(150, 75, 0)[1],color(150, 75, 0)[2])
elif (x, y) in [(-1, 13), (2,13), (-1,6), (2,6)]and z != 0:
if y == 13 and z != -1:
self.pieces[index] = loader.loadModel("models/bishop.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y+(8 * z),(z))
self.pieces[index].setColor(1,1,1)
elif y == 6 and z != 1:
self.pieces[index] = loader.loadModel("models/bishop.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y+(8 * z),(z))
self.pieces[index].setColor(color(150, 75, 0)[0],color(150, 75, 0)[1],color(150, 75, 0)[2])
elif (x, y) in [(0, 13), (0,6)]and z != 0:
if y == 13 and z != -1:
self.pieces[index] = loader.loadModel("models/king.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y+(8 * z),(z))
self.pieces[index].setColor(1,1,1)
elif y == 6 and z != 1:
self.pieces[index] = loader.loadModel("models/king.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y+(8 * z),(z))
self.pieces[index].setColor(color(150, 75, 0)[0],color(150, 75, 0)[1],color(150, 75, 0)[2])
elif (x, y) in [(1, 13), (1,6)]and z != 0:
if y == 13 and z != -1:
self.pieces[index] = loader.loadModel("models/queen.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y+(8 * z),(z))
self.pieces[index].setColor(1,1,1)
elif y == 6 and z != 1:
self.pieces[index] = loader.loadModel("models/queen.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y+(8 * z),(z))
self.pieces[index].setColor(color(150, 75, 0)[0],color(150, 75, 0)[1],color(150, 75, 0)[2])
count+= 1
count+= 1
self.square = loader.loadModel("models/square.egg")
self.square.reparentTo(render)
self.square.setPos(4,0,-0.99)
self.square.setColor(0,0,1)
for element in self.pieces[:]:
if isinstance(element, int):
self.pieces.remove(element)
# Resets the color
def redrawSquares(self):
index = -1
for z in range (-1,2):
count= 1
for x in range(-3,5):
# going to next row
for y in range(13,5,-1):
index += 1
# going throug a constant row
if count % 2 == 0:
# self.squares[index].setPos(x,y+(8 * z) ,(z))
self.squares[index].setColor(0,0,0)
# count+= 1
else:
# self.squares[index].setPos(x ,y+(8 * z),(z))
self.squares[index].setColor(1,1,1)
count += 1
count += 1
# Not my function
def setupLights(self): # This function sets up some default lighting
ambientLight = AmbientLight("ambientLight")
ambientLight.setColor((.8, .8, .8, 1))
directionalLight = DirectionalLight("directionalLight")
directionalLight.setDirection(LVector3(0, 45, -45))
directionalLight.setColor((0.2, 0.2, 0.2, 1))
render.setLight(render.attachNewNode(directionalLight))
render.setLight(render.attachNewNode(ambientLight))
# checks whether to cords are same?
def compare(self, p1, p2):
if abs(p1.getX() - p2.getX()) <= 0.05 and p1.getY() == p2.getY():
if abs(p1.getZ() - p2.getZ()) <= 0.05:
return True
else:
return False
else:
return False
# CALLED every frame
# Changes the position based on blue sq
def update(self, task):
# gets the current position of the blue sq
posOfSq = self.square.getPos()
# Now start check which button is pressed and change position accordingly
if keyMap["left"]:
posOfSq.x -= self.dx
# resetting the values
keyMap["left"] = False
if posOfSq.x < -3: # restart blue sq from right
posOfSq.setX(4)
elif keyMap["right"]:
posOfSq.x += self.dx
# resetting the values
keyMap["right"] = False
if posOfSq.x > 4: # restart blue sq from left
posOfSq.setX(-3)
elif keyMap["up"]:
posOfSq.y += self.dx
keyMap["up"] = False
if 13 >= posOfSq.y > 5 : # Going to 2nd level in from 1st level
posOfSq.setZ(0.01)
elif 21 >= posOfSq.y > 13 : # Going to 3 level in from 2nd level
posOfSq.setZ(1.01)
elif 5 >= posOfSq.y > -2: # Staying in the first level
posOfSq.setZ(-0.99)
elif posOfSq.y > 21: # Going back to 1st chess board from 3rd board
posOfSq.setY(-2)
posOfSq.setZ(-0.99)
elif keyMap["down"]:
posOfSq.y -= self.dx
keyMap["down"] = False
if 13 >= posOfSq.y > 5 :
posOfSq.setZ(0.01)
elif 21 >= posOfSq.y > 13 :
posOfSq.setZ(1.01)
elif 5 >= posOfSq.y > -2:
posOfSq.setZ(-0.99)
elif posOfSq.y < -2:
posOfSq.setY(21)
posOfSq.setZ(1.01)
elif keyMap["u"]:
posOfSq.z += self.dx
if 0 < posOfSq.z < 1 and -2 <= posOfSq.y <= 5:
posOfSq.y += 8
elif 1 < posOfSq.z < 2 and 6 <= posOfSq.y <= 13:
posOfSq.y += 8
keyMap["u"] = False
elif keyMap["d"]:
posOfSq.z -= self.dx
keyMap["d"] = False
if keyMap["enter"]:
if self.select == None and self.pieceSelected == False:
l = []
for i in range(32):
if isinstance(self.pieces[i], int) == False:
posOfPiece = self.pieces[i].getPos()
if self.compare(posOfPiece, posOfSq) :
self.select = i
self.pieceSelected = True
keyMap["enter"] = False
print(self.pieces[self.select])
self.showMoves(self.pieces[self.select])
self.initialPosition = self.pieces[self.select].getPos()
break
else:
keyMap["enter"] = False
elif self.select != None and self.pieceSelected != False:
if self.checkPlacement(posOfSq):
self.pieces[self.select].setPos(posOfSq)
print(self.pieces.count(self.pieces[self.select]))
self.pieceSelected = False
keyMap["enter"] = False
self.redrawSquares()
self.initialPosition = None
posOfSq.z = self.pieces[self.select].getZ() + 0.01
self.select = None
else:
print("not allowed")
self.pieces[self.select].setPos(self.initialPosition)
self.pieceSelected = False
self.redrawSquares()
self.initialPosition = None
posOfSq.z = self.pieces[self.select].getZ() + 0.01
self.select = None
elif self.select != None and keyMap["enter"] == False:
self.pieces[self.select].setPos(posOfSq)
self.square.setPos(posOfSq)
return task.cont
def checkPlacement(self, position):
position.setZ(round(position.getZ()))
for index in range(192):
targetSq = self.squares[index]
if targetSq.getPos() == position and targetSq.getColor() == (0,1,0,1):
return True
def checkForSameColorPieces(self, squares, piece):
for square in squares:
if square != tuple(piece.getPos()):
for piece in self.pieces:
if tuple(piece.getPos()) == square:
self.changeColor(square, (1,0,0))
else:
print(square, "hi")
def changeColor(self, cords, color):
for index in range(len(self.squares)):
if tuple(self.squares[index].getPos()) == cords:
self.squares[index].setColor(1, 0, 0)
break
def checkForBlockingPiece(self, squares, piece):
nameOfPiece = str(piece)
if "rook" in nameOfPiece:
self.checkForBlockingRook(squares, piece)
pass
def checkForBlockingRook(self,squares, piece):
blockedSquares = []
for square in squares:
if self.findPiece(square):
blockedSquares += [square]
for square1 in squares[:]:
for square2 in blockedSquares[:]:
if self.sameRow(square1, square2):
blockedSquares += [square1]
elif self.sameCol(square1, square2):
blockedSquares += [square1]
for square in blockedSquares:
self.changeColor(square, (1,0,0))
# Checks whther a two sqs are at same row
def sameRow(self, square1, square2):
return square1[0] == square2[0] and square1[2] == square2[2]
# Checks whther a two sqs are at same col
def sameCol(self, square1, square2):
return square1[1] == square2[0] and square1[2] == square2[2]
#
def showMoves(self, piece):
legalSquares = self.getMoves(piece)
if legalSquares != None:
for m in legalSquares:
for index in range(192):
if check(m,tuple(self.squares[index].getPos()) ):
self.squares[index].setColor(0,1,0,1)
self.checkForSameColorPieces(legalSquares, piece)
self.checkForBlockingPiece(legalSquares, piece)
def getMoves(self, piece):
nameOfPiece= str(piece).split("/")[1]
if "pawn" in nameOfPiece:
return self.findPawnMoves(piece)
elif "knight" in nameOfPiece:
return self.findKnightMoves(piece)
elif "bishop" in nameOfPiece:
return self.findBishopMoves(piece)
elif "queen" in nameOfPiece:
return self.findQueenMoves(piece)
elif "king" in nameOfPiece:
return self.findKingMoves(piece)
elif "rook" in nameOfPiece:
return self.findRookMoves(piece)
pass
def findPawnMoves(self, piece):
print("pawn time")
posOfPawn = piece.getPos()
print(posOfPawn)
colorOfPawn = piece.getColor()
x = posOfPawn.getX()
y = posOfPawn.getY()
z = round(posOfPawn.getZ())
points = []
if colorOfPawn[0] < 1 and colorOfPawn[1] < 1 and colorOfPawn[2] < 1 :
points += [(x, y + 1, z)]
points += [(x, y, z)] + [(x, y + 8, z + 1)] + [(x, y + 9, z + 1)]
points += [(x, y - 8, z - 1)] + [(x, y - 7, z - 1)]
if y == -1:
points += [(x, y + 2, z)]
points += [(x, y + 18, z+2)]
points += [(x, y + 16, z + 2 )]
elif colorOfPawn[0] == 1 and colorOfPawn[1] == 1 and colorOfPawn[2] == 1 :
points += [(x, y - 1, z)]
points += [(x, y, z)] + [(x, y - 8, z - 1)] + [(x, y - 9, z - 1)]
points += [(x, y - 8, z - 1)] + [(x, y - 9, z - 1)]
points += [(x, y + 8, z + 1)] + [(x, y + 7, z + 1)]
if y == 20:
points += [(x, y - 2, z)]
points += [(x, y - 18, z - 2)]
points += [(x, y - 16, z - 2 )]
return points
def findKnightMoves(self, piece):
print("knight itme")
posOfKnight = piece.getPos()
colorOfKnight = piece.getColor()
x = posOfKnight.getX()
y = posOfKnight.getY()
z = round(posOfKnight.getZ())
m1 = (x + 1, y + 2 , z)
m2 = (x - 1, y + 2, z)
m3 = (x + 2, y + 1, z)
m4 = (x -2, y + 1, z)
m5 = (x + 1, y - 2 , z)
m6 = (x - 1, y - 2, z)
m7 = (x + 2, y - 1, z)
m8 = (x -2, y - 1, z)
m9 = (x, y + 10, z + 1)
m10 = (x, y + 6, z + 1)
m11 = (x, y - 10, z - 1)
m12 = (x, y - 6, z -1)
m13 = (x + 2, y + 8, z + 1)
m14 = (x - 2, y + 8, z + 1)
m15 = (x + 2, y - 8, z -1 )
m16 = (x - 2, y - 8, z - 1)
m17 = (x, y + 17, z + 2)
m18 = (x, y + 15, z + 2)
m19 = (x, y - 17, z - 2)
m20 = (x, y - 15, z -2)
m21 = (x + 1, y + 16, z + 2)
m22 = (x - 1, y + 16, z + 2)
m23 = (x + 1, y - 16, z -2 )
m24 = (x - 1, y - 16, z - 2)
print(posOfKnight)
print([m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,m13,m14,m15,m16])
return [m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,m13,m14,m15,m16,m17,m18,m19,m20,m21,m22,m23,m24]
def findBishopMoves(self, piece):
print("bishop itme")
posOfBishop = piece.getPos()
x = posOfBishop.getX()
y = posOfBishop.getY()
z = round(posOfBishop.getZ())
points = []
for i in range (-8, 9):
points += [(x + i, y + i, z)]
points += [(x - i, y + i, z)]
if i in [-2, -1, 0, 1, 2]:
points += [(x + i, y + i + (i * 8), z + i)]
points += [(x - i, y + i + (i * 8), z + i)]
points += [(x + i, y + i - (i * 8), z - i)]
points += [(x - i, y + i - (i * 8), z - i)]
points = self.removeExtras(points)
return points
def findRookMoves(self, piece):
print("rook time")
posOfRook = piece.getPos()
x = posOfRook.getX()
y = posOfRook.getY()
z = round(posOfRook.getZ())
points = []
for i in range(-8, 9):
points += [(x, y + i, z)] + [(x + i, y, z)]
for i in range(-2,3):
points += [(x, y+i+ (8 * i), z + i )]
points += [(x, y - i + (8 * i), z + i)]
points += [(x + i, y + (8 * i), z + i )]
points += [(x - i, y + (8 * i), z + i)]
points += [(x, y + (8 * i), z + i)]
points = self.removeExtras(points)
return points
def findPiece(self, square):
cords = square
for piece in self.pieces:
if piece.getPos() == cords:
return True
else:
return False
def findQueenMoves(self, piece):
print("queens time")
posOfQueen = piece.getPos()
x = posOfQueen.getX()
y = posOfQueen.getY()
z = round(posOfQueen.getZ())
points = []
for i in range(-8, 9):
points += [(x, y + i, z)] + [(x + i, y, z)]
for i in range(-2,3):
points += [(x, y + i + (8 * i), z + i )]
points += [(x, y - i + (8 * i), z + i)]
points += [(x + i, y + (8 * i), z + i )]
points += [(x - i, y + (8 * i), z + i)]
points += [(x, y + (8 * i), z + i)]
for i in range (-8, 9):
points += [(x + i, y + i, z)]
points += [(x - i, y + i, z)]
if i in [-2, -1, 0, 1, 2]:
points += [(x + i, y + i + (i * 8), z + i)]
points += [(x - i, y + i + (i * 8), z + i)]
points += [(x + i, y + i - (i * 8), z - i)]
points += [(x - i, y + i - (i * 8), z - i)]
points = self.removeExtras(points)
return points
def findKingMoves(self, piece):
print("king time")
posOfKing = piece.getPos()
x = posOfKing.getX()
y = posOfKing.getY()
z = round(posOfKing.getZ())
points = []
points += [(x + 1, y, z)] + [(x - 1, y, z)]
points += [(x , y+ 1, z)] + [(x , y - 1, z)]
points += [(x + 1, y + 1, z)] + [(x - 1, y - 1, z)]
points += [(x - 1, y+ 1, z)] + [(x + 1, y - 1, z)]
points += [(x , y - 8, z - 1)] + [(x - 1, y + 8, z + 1)]
points += [(x + 1, y + 8, z + 1)] + [(x - 1, y + 8, z + 1)]
points += [(x + 1, y - 8, z - 1)] + [(x - 1, y - 8, z - 1)]
points += [(x , y+ 9, z+1)] + [(x , y - 9, z-1)]
points += [(x, y + 8, z + 1)] + [(x, y - 8, z - 1)] + [(x, y, z)]
points += [(x, y + 7, z + 1)] + [(x, y - 7, z - 1)]
points += [(x + 1, y + 7, z + 1)] + [(x + 1, y - 7, z - 1)]
points += [(x - 1, y + 7, z + 1)] + [(x - 1, y - 7, z - 1)]
points += [(x + 1 , y+ 9, z+1)] + [(x + 1 , y - 9, z-1)]
points += [(x - 1 , y+ 9, z+1)] + [(x - 1 , y - 9, z-1)]
return points
def removeExtras(self, l):
validCords = []
for square in self.squares:
validCords += [square.getPos()]
for cords in l[:]:
if cords not in validCords:
l.remove(cords)
return l
def __init__(self):
super().__init__()
self.disableMouse()
self.squares = []
self.pieces = []
for index in range(192):
self.squares += [index]
self.pieces += [index]
self.formBoardAndPieces()
self.setBackgroundColor(color(0,0,125)[0],color(0,125,0)[1],color(0,0,125)[2],)
self.camera.setPos(0.5,-20,25)
self.camera.setHpr(0,-42.5,0)
# self.camera.setPos(-10,-7,25)
# self.camera.setHpr(0,-42.5,0)
self.setupLights()
self.inputDevice = "keyboard"
self.accept("arrow_left", updateKeyMap, ["left"])
self.accept("arrow_right", updateKeyMap, ["right"])
self.accept("arrow_up", updateKeyMap, ["up"])
self.accept("arrow_down", updateKeyMap, ["down"])
self.accept("u", updateKeyMap, ["u"])
self.accept("d", updateKeyMap, ["d"] )
self.accept("enter", updateKeyMap, ["enter"])
self.dx = 1
self.pieceSelected = False
self.taskMgr.add(self.update, "update")
self.select = None
pass
game = MilleniumChess()
game.run()