Skip to content

Commit 8c84723

Browse files
committed
chess game
1 parent d578d7e commit 8c84723

File tree

13 files changed

+152
-0
lines changed

13 files changed

+152
-0
lines changed

β€Ž0x24-Chess_Game/ChessGame.py

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import pygame as p
2+
import ChessEngine
3+
4+
WIDTH = HEIGHT = 512
5+
DIMENSIONS = 8
6+
SQ_SIZE = HEIGHT// DIMENSIONS
7+
MAX_FPS = 15
8+
IMAGES = {}
9+
10+
def loadImages():
11+
pieces = ['wp', 'wR', 'wN', 'wB', 'wQ', 'wK', 'bp', 'bR', 'bN', 'bB', 'bQ', 'bK' ]
12+
for piece in pieces:
13+
IMAGES[piece] = p.transform.scale(p.image.load("images/" + piece + ".png"), (SQ_SIZE, SQ_SIZE))
14+
15+
def main():
16+
p.init()
17+
screen = p.display.set_mode((WIDTH, HEIGHT))
18+
clock = p.time.Clock()
19+
screen.fill(p.Color("white"))
20+
gs = ChessEngine.GameState()
21+
validMoves = gs.getValidMoves()
22+
moveMade = False
23+
animate = False
24+
loadImages()
25+
running = True
26+
sqSelected = ()
27+
playerClicks = []
28+
gameOver = False
29+
while running:
30+
for e in p.event.get():
31+
if e.type == p.QUIT:
32+
running = False
33+
elif e.type == p.MOUSEBUTTONDOWN:
34+
if not gameOver:
35+
location = p.mouse.get_pos()
36+
col = location[0]//SQ_SIZE
37+
row = location[1]//SQ_SIZE
38+
if sqSelected == (row, col):
39+
sqSelected = ()
40+
playerClicks = []
41+
else:
42+
sqSelected = (row, col)
43+
playerClicks.append(sqSelected)
44+
if len(playerClicks) == 1 and (gs.board[row][col] == "--"):
45+
sqSelected = ()
46+
playerClicks = []
47+
if len(playerClicks) == 2:
48+
move = ChessEngine.Move(playerClicks[0], playerClicks[1], gs.board)
49+
for i in range(len(validMoves)):
50+
if move == validMoves[i]:
51+
gs.makeMove(move)
52+
moveMade = True
53+
animate = True
54+
sqSelected = ()
55+
playerClicks = []
56+
if not moveMade:
57+
playerClicks = [sqSelected]
58+
elif e.type == p.KEYDOWN:
59+
if e.key == p.K_z:
60+
gs.undoMove()
61+
moveMade = True
62+
animate = False
63+
if e.key == p.K_r:
64+
gs = ChessEngine.GameState()
65+
validMoves = gs.getValidMoves()
66+
sqSelected = ()
67+
playerClicks = []
68+
moveMade = False
69+
animate = False
70+
if moveMade:
71+
if animate:
72+
animatedMoves(gs.moveLog[-1], screen, gs.board,clock)
73+
validMoves = gs.getValidMoves()
74+
moveMade = False
75+
animate = False
76+
drawGameState(screen, gs, validMoves, sqSelected)
77+
if gs.checkMate:
78+
gameOver = True
79+
if gs.whiteToMove:
80+
drawText(screen, 'Black wins by checkmate')
81+
else:
82+
drawText(screen, 'White wins by checkmate')
83+
elif gs.staleMate:
84+
gameOver =True
85+
drawText(screen, 'Stalemate');
86+
clock.tick(MAX_FPS)
87+
p.display.flip()
88+
89+
def highlightSquares(screen, gs, validMoves, sqSelected):
90+
if sqSelected != ():
91+
r, c = sqSelected
92+
if gs.board[r][c][0] == ('w' if gs.whiteToMove else 'b'):
93+
s = p.Surface((SQ_SIZE, SQ_SIZE))
94+
s.set_alpha(100)
95+
s.fill(p.Color('blue'))
96+
screen.blit(s, (c*SQ_SIZE, r*SQ_SIZE))
97+
s.fill(p.Color("yellow"))
98+
for moves in validMoves:
99+
if moves.startRow == r and moves.startCol == c:
100+
screen.blit(s, (SQ_SIZE*moves.endCol, SQ_SIZE*moves.endRow))
101+
102+
def drawGameState(screen, gs, validMoves, sqSelected):
103+
drawBoard(screen)
104+
highlightSquares(screen, gs, validMoves, sqSelected)
105+
drawPieces(screen, gs.board)
106+
107+
def drawBoard(screen):
108+
global colors
109+
colors = [p.Color("white"), p.Color("grey")]
110+
for r in range(DIMENSIONS):
111+
for c in range(DIMENSIONS):
112+
color = colors[(r+c) % 2]
113+
p.draw.rect(screen, color, p.Rect(c*SQ_SIZE, r*SQ_SIZE, SQ_SIZE, SQ_SIZE))
114+
115+
def drawPieces(screen, board):
116+
for r in range(DIMENSIONS):
117+
for c in range(DIMENSIONS):
118+
piece = board[r][c]
119+
if piece != "--":
120+
screen.blit(IMAGES[piece], p.Rect(c*SQ_SIZE, r*SQ_SIZE, SQ_SIZE, SQ_SIZE))
121+
122+
def animatedMoves(move, screen,board, clock):
123+
global colors
124+
dR = move.endRow - move.startRow
125+
dC = move.endCol - move.startCol
126+
framesPerSquare = 5
127+
frameCount = (abs(dR) + abs(dC)) * framesPerSquare
128+
for frame in range(frameCount + 1):
129+
r,c =((move.startRow + dR*frame/frameCount, move.startCol + dC*frame/frameCount))
130+
drawBoard(screen)
131+
drawPieces(screen, board)
132+
color = colors[(move.endRow + move.endCol)%2]
133+
endSquare = p.Rect(move.endCol*SQ_SIZE, move.endRow*SQ_SIZE, SQ_SIZE, SQ_SIZE)
134+
p.draw.rect(screen, color, endSquare)
135+
if move.pieceCaptured != "--":
136+
screen.blit(IMAGES[move.pieceCaptured], endSquare)
137+
138+
screen.blit(IMAGES[move.pieceMoved], p.Rect(c*SQ_SIZE, r*SQ_SIZE, SQ_SIZE, SQ_SIZE))
139+
p.display.flip()
140+
clock.tick(60)
141+
142+
def drawText(screen, text):
143+
font = p.font.SysFont("Helvitca", 32, True, False)
144+
textObject = font.render(text, True, p.Color('Gray'))
145+
textLocation = p.Rect(0, 0, WIDTH, HEIGHT).move(WIDTH/2 - textObject.get_width()/2, HEIGHT/2 - textObject.get_height()/2)
146+
screen.blit(textObject, textLocation)
147+
textObject = font.render(text, True, p.Color("Black"))
148+
screen.blit(textObject, textLocation.move(2,2))
149+
150+
151+
if __name__ == "__main__":
152+
main()

β€Ž0x24-Chess_Game/images/bB.png

1.23 KB
Loading

β€Ž0x24-Chess_Game/images/bK.png

2.43 KB
Loading

β€Ž0x24-Chess_Game/images/bN.png

1.48 KB
Loading

β€Ž0x24-Chess_Game/images/bQ.png

2.21 KB
Loading

β€Ž0x24-Chess_Game/images/bR.png

725 Bytes
Loading

β€Ž0x24-Chess_Game/images/bp.png

797 Bytes
Loading

β€Ž0x24-Chess_Game/images/wB.png

1.9 KB
Loading

β€Ž0x24-Chess_Game/images/wK.png

2.23 KB
Loading

β€Ž0x24-Chess_Game/images/wN.png

1.83 KB
Loading

β€Ž0x24-Chess_Game/images/wQ.png

2.58 KB
Loading

β€Ž0x24-Chess_Game/images/wR.png

933 Bytes
Loading

β€Ž0x24-Chess_Game/images/wp.png

1.26 KB
Loading

0 commit comments

Comments
Β (0)