-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessMain.py
140 lines (95 loc) · 3.42 KB
/
ChessMain.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
#Main driven file. Responsible for handling user input and output
import pygame as pg
from ChessEngine import *
pg.init()
pg.display.set_caption("Chess")#titol window
WIDTH, HEIGHT = 512,512
DIMENSION = 8#dimensions chess board
SQ_SIZE = HEIGHT//DIMENSION
MAX_FPS = 15
IMAGES = {}
BROWN = (205,133,63)
LIGHT_BROWN = (255,248,220)
"""load images, intialize global dictionary of images
"""
def load_Images():
pieces= ["wp","wR","wN","wB","wK","wQ","bp","bR","bN","bB","bK","bQ"]
for piece in pieces:
IMAGES[piece] = pg.transform.scale(pg.image.load("images/" + piece+".png"), (SQ_SIZE,SQ_SIZE))
#we can accees and image by sayin "IMAGES['wp']"
"""
Responsible for all the graphics within a current game state
"""
def drawGameState(WIN,gs):
drawBoard(WIN) #draw squares on the board
drawPieces(WIN, gs.board) #draw pieces on squares
"""
Draw the squares on the board Top left square is always light.
"""
def drawBoard(WIN):
colors = [LIGHT_BROWN, BROWN]
for row in range(DIMENSION):
for col in range(DIMENSION):
color = colors[((row+col)%2)]
pg.draw.rect(WIN,color, pg.Rect(col*SQ_SIZE,row*SQ_SIZE, SQ_SIZE,SQ_SIZE))
"""
Draw the pieces on the board using the current GameState.board
"""
def drawPieces(WIN, board):
for row in range(DIMENSION):
for col in range(DIMENSION):
piece = board[row][col]
if piece != "--": #not empty space
piece_rect = pg.Rect(col*SQ_SIZE, row*SQ_SIZE, SQ_SIZE,SQ_SIZE)
WIN.blit(IMAGES[piece], piece_rect)
#Main driver for our code. This will handle user input and updating graphics
def main():
WIN = pg.display.set_mode((WIDTH,HEIGHT))
clock = pg.time.Clock()
WIN.fill(pg.Color("white"))
gs = GameState()
validMoves = gs.getValidMoves()
moveMade = False #flag variable for when a move is made, when the user has moved, we should generate another set of valid moves
load_Images()#only do this once
run = True
sqSelected = ()#square selecteed initially, keep track of the last click of the user (row,col)
playerClicks = []#keep track of the player clicks (two tupples: [(6,4),(4,4)])
while run:
clock.tick(MAX_FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
#mouse handler
elif event.type == pg.MOUSEBUTTONDOWN:
location = pg.mouse.get_pos() #x,y location of mouse
col = location[0]//SQ_SIZE
row = location[1]//SQ_SIZE
if sqSelected == (row,col): #the user clicked the same square twice
sqSelected = ()#deselect
playerClicks = []#clear player clicks
else:
sqSelected = (row,col)
playerClicks.append(sqSelected)#append for both 1st and 2nd clicks
if len(playerClicks) == 2: #after 2nd click
move = Move(playerClicks[0], playerClicks[1], gs.board)
print(move.getChessNotation())
if move in validMoves:
moveMade = True
gs.makeMove(move)
sqSelected = ()#reset user clicks
playerClicks = []
else:
playerClicks=[sqSelected]
#key handlers
elif event.type == pg.KEYDOWN:
if event.key == pg.K_z: #undo when 'z' is pressed
gs.undoMove()
moveMade = True
if moveMade:
validMoves = gs.getValidMoves()
moveMade = False
drawGameState(WIN,gs)
pg.display.update()
pg.quit()#quit pygame close the window
if __name__ == "__main__": #we only run the game when we run this file
main()