├── README.md ├── assets └── images │ ├── black bishop.png │ ├── black king.png │ ├── black knight.png │ ├── black pawn.png │ ├── black queen.png │ ├── black rook.png │ ├── white bishop.png │ ├── white king.png │ ├── white knight.png │ ├── white pawn.png │ ├── white queen.png │ └── white rook.png └── main.py /README.md: -------------------------------------------------------------------------------- 1 | # Chess-Game 2 | This shi is better than Magnus Carlsen's whole career 3 | -------------------------------------------------------------------------------- /assets/images/black bishop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafigeovazi/Chess-Game/HEAD/assets/images/black bishop.png -------------------------------------------------------------------------------- /assets/images/black king.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafigeovazi/Chess-Game/HEAD/assets/images/black king.png -------------------------------------------------------------------------------- /assets/images/black knight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafigeovazi/Chess-Game/HEAD/assets/images/black knight.png -------------------------------------------------------------------------------- /assets/images/black pawn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafigeovazi/Chess-Game/HEAD/assets/images/black pawn.png -------------------------------------------------------------------------------- /assets/images/black queen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafigeovazi/Chess-Game/HEAD/assets/images/black queen.png -------------------------------------------------------------------------------- /assets/images/black rook.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafigeovazi/Chess-Game/HEAD/assets/images/black rook.png -------------------------------------------------------------------------------- /assets/images/white bishop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafigeovazi/Chess-Game/HEAD/assets/images/white bishop.png -------------------------------------------------------------------------------- /assets/images/white king.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafigeovazi/Chess-Game/HEAD/assets/images/white king.png -------------------------------------------------------------------------------- /assets/images/white knight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafigeovazi/Chess-Game/HEAD/assets/images/white knight.png -------------------------------------------------------------------------------- /assets/images/white pawn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafigeovazi/Chess-Game/HEAD/assets/images/white pawn.png -------------------------------------------------------------------------------- /assets/images/white queen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafigeovazi/Chess-Game/HEAD/assets/images/white queen.png -------------------------------------------------------------------------------- /assets/images/white rook.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafigeovazi/Chess-Game/HEAD/assets/images/white rook.png -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | WIDTH = 1100 5 | HEIGHT = 750 6 | screen = pygame.display.set_mode([WIDTH, HEIGHT]) 7 | pygame.display.set_caption('GC(GeoSling Chess) PVP') 8 | color = (51, 0, 51) 9 | font = pygame.font.Font('freesansbold.ttf', 20) 10 | medium_font = pygame.font.Font('freesansbold.ttf', 10) 11 | big_font = pygame.font.Font('freesansbold.ttf', 20) 12 | text_position = (800, 500) 13 | timer = pygame.time.Clock() 14 | fps = 60 15 | 16 | white_pieces = ['rook', 'knight', 'bishop', 'king', 'queen', 'bishop', 'knight', 'rook', 17 | 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn'] 18 | white_locations = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), 19 | (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1),] 20 | black_pieces = ['rook', 'knight', 'bishop', 'king', 'queen', 'bishop', 'knight', 'rook', 21 | 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn'] 22 | black_locations = [(0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7), 23 | (0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6), (7, 6),] 24 | captured_pieces_white = [] 25 | captured_pieces_black = [] 26 | # 0(white turn no selection): 1(whites turn piece selected): 2(nigga turn no selection): 3(nigga turn piece selected) 27 | turn_step = 0 28 | selection = 100 #Selection pieces 29 | valid_moves = [] 30 | #load in game piece images (queen, king, rook, bishop, knight, pawn) * 2 31 | black_queen = pygame.image.load('assets/images/black queen.png') 32 | black_queen = pygame.transform.scale(black_queen, (80, 80)) 33 | black_queen_small = pygame.transform.scale(black_queen, (45, 45)) 34 | black_king = pygame.image.load('assets/images/black king.png') 35 | black_king = pygame.transform.scale(black_king, (80, 80)) 36 | black_king_small = pygame.transform.scale(black_king, (45, 45)) 37 | black_rook = pygame.image.load('assets/images/black rook.png') 38 | black_rook = pygame.transform.scale(black_rook, (80, 80)) 39 | black_rook_small = pygame.transform.scale(black_rook, (45, 45)) 40 | black_bishop = pygame.image.load('assets/images/black bishop.png') 41 | black_bishop = pygame.transform.scale(black_bishop, (80, 80)) 42 | black_bishop_small = pygame.transform.scale(black_bishop, (45, 45)) 43 | black_knight = pygame.image.load('assets/images/black knight.png') 44 | black_knight = pygame.transform.scale(black_knight, (80, 80)) 45 | black_knight_small = pygame.transform.scale(black_knight, (45, 45)) 46 | black_pawn = pygame.image.load('assets/images/black pawn.png') 47 | black_pawn = pygame.transform.scale(black_pawn, (80, 80)) 48 | black_pawn_small = pygame.transform.scale(black_pawn, (45, 45)) 49 | white_queen = pygame.image.load('assets/images/white queen.png') 50 | white_queen = pygame.transform.scale(white_queen, (80, 80)) 51 | white_queen_small = pygame.transform.scale(white_queen, (45, 45)) 52 | white_king = pygame.image.load('assets/images/white king.png') 53 | white_king = pygame.transform.scale(white_king, (80, 80)) 54 | white_king_small = pygame.transform.scale(white_king, (45, 45)) 55 | white_rook = pygame.image.load('assets/images/white rook.png') 56 | white_rook = pygame.transform.scale(white_rook, (80, 80)) 57 | white_rook_small = pygame.transform.scale(white_rook, (45, 45)) 58 | white_bishop = pygame.image.load('assets/images/white bishop.png') 59 | white_bishop = pygame.transform.scale(white_bishop, (80, 80)) 60 | white_bishop_small = pygame.transform.scale(white_bishop, (45, 45)) 61 | white_knight = pygame.image.load('assets/images/white knight.png') 62 | white_knight = pygame.transform.scale(white_knight, (80, 80)) 63 | white_knight_small = pygame.transform.scale(white_knight, (45, 45)) 64 | white_pawn = pygame.image.load('assets/images/white pawn.png') 65 | white_pawn = pygame.transform.scale(white_pawn, (80, 80)) 66 | white_pawn_small = pygame.transform.scale(white_pawn, (45, 45)) 67 | 68 | white_images = [white_pawn, white_queen, white_king, white_knight, white_rook, white_bishop] 69 | small_white_images = [white_pawn_small, white_queen_small, white_king_small, white_knight_small, white_rook_small, white_bishop_small] 70 | 71 | black_images = [black_pawn, black_queen, black_king, black_knight, black_rook, black_bishop] 72 | small_black_images = [black_pawn_small, black_queen_small, black_king_small, black_knight_small, black_rook_small, black_bishop_small] 73 | 74 | piece_list = ['pawn', 'queen', 'king', 'knight', 'rook', 'bishop'] 75 | 76 | #Check variables 77 | counter = 0 78 | winner = '' 79 | game_over = False 80 | 81 | #draw main game board 82 | def draw_board(): 83 | for i in range(32): 84 | column = i % 4 85 | row = i // 4 86 | if row % 2 == 0: 87 | pygame.draw.rect(screen, 'light gray', [600 - (column * 200), row * 100, 100, 100]) 88 | else: 89 | pygame.draw.rect(screen, 'light gray', [700 - (column * 200), row * 100, 100, 100]) 90 | #Death base 91 | pygame.draw.rect(screen, 'yellow', [800, 0, 300, HEIGHT], 2) 92 | status_text = ['Select a Piece to Move White!', 'Select a Destination White!', 93 | 'Select a Piece to Move Nigga!', 'Select a Destination Nigga!'] 94 | screen.blit(big_font.render(status_text[turn_step], True, 'white'), text_position) 95 | for i in range(9): 96 | pygame.draw.line(screen, 'yellow', (0, 100 * i), (800, 100 * i), 2) 97 | pygame.draw.line(screen, 'yellow', (100 * i, 0), (100 * i, 800), 2) 98 | screen.blit(medium_font.render('FORFEIT', True, 'black'), (810, 830)) 99 | 100 | #draw pieces onto board 101 | def draw_pieces(): 102 | for i in range(len(white_pieces)): 103 | index = piece_list.index(white_pieces[i]) 104 | if white_pieces[i] == 'pawn': 105 | screen.blit(white_pawn, (white_locations[i][0] * 100 + 10, white_locations[i][1] * 100 + 10)) 106 | else: 107 | screen.blit(white_images[index], (white_locations[i][0] * 100 + 10, white_locations[i][1] * 100 + 10)) 108 | if turn_step < 2: 109 | if selection == i: 110 | pygame.draw.rect(screen, 'green', [white_locations[i][0] * 100 + 1, white_locations[i][1] * 100 * 1, 111 | 100, 100], 5) 112 | 113 | for i in range(len(black_pieces)): 114 | index = piece_list.index(black_pieces[i]) 115 | if black_pieces[i] == 'pawn': 116 | screen.blit(black_pawn, (black_locations[i][0] * 100 + 10, black_locations[i][1] * 100 + 10)) 117 | else: 118 | screen.blit(black_images[index], (black_locations[i][0] * 100 + 10, black_locations[i][1] * 100 + 10)) 119 | if turn_step >= 2: 120 | if selection == i: 121 | pygame.draw.rect(screen, 'blue', [black_locations[i][0] * 100 + 1, black_locations[i][1] * 100 * 1, 122 | 100, 100], 5) 123 | 124 | #Function to check all pieces valid options on board 125 | 126 | def check_options(pieces, locations, turn): 127 | moves_list = [] 128 | all_moves_list = [] 129 | for i in range((len(pieces))): 130 | location = locations[i] 131 | piece = pieces[i] 132 | if piece == 'pawn': 133 | moves_list = check_pawn(location, turn) 134 | elif piece == 'rook': 135 | moves_list = check_rook(location, turn) 136 | elif piece == 'knight': 137 | moves_list = check_knight(location, turn) 138 | elif piece == 'bishop': 139 | moves_list = check_bishop(location, turn) 140 | elif piece == 'queen': 141 | moves_list = check_queen(location, turn) 142 | elif piece == 'king': 143 | moves_list = check_king(location, turn) 144 | all_moves_list.append(moves_list) 145 | return all_moves_list 146 | 147 | #Check king valid moves 148 | def check_king(position, color): 149 | moves_list = [] 150 | if color == 'white': 151 | enemies_list = black_locations 152 | friends_list = white_locations 153 | else: 154 | friends_list = black_locations 155 | enemies_list = white_locations 156 | # 8 squares to check for kings, they can go one square any direction 157 | targets = [(1, 0), (1, 1), (1, -1), (-1, 0), (-1, 1), (-1, -1), (0, 1), (0, -1)] 158 | for i in range(8): 159 | target = (position[0] + targets[i][0], position[1] + targets[i][1]) 160 | if target not in friends_list and 0 <= target[0] <= 7 and 0 <= target[1] <= 7: 161 | moves_list.append(target) 162 | return moves_list 163 | 164 | #Check queen valid moves 165 | def check_queen(position, color): 166 | moves_list = check_bishop(position, color) 167 | second_list = check_rook(position, color) 168 | for i in range(len(second_list)): 169 | moves_list.append(second_list[i]) 170 | return moves_list 171 | 172 | #Check bishop moves 173 | def check_bishop(position, color): 174 | moves_list = [] 175 | if color == 'white': 176 | enemies_list = black_locations 177 | friends_list = white_locations 178 | else: 179 | friends_list = black_locations 180 | enemies_list = white_locations 181 | for i in range(4): # up-right, up-left, down-right, down-left 182 | path = True 183 | chain = 1 184 | if i == 0: 185 | x = 1 186 | y = -1 187 | elif i == 1: 188 | x = -1 189 | y = -1 190 | elif i == 2: 191 | x = 1 192 | y = 1 193 | else: 194 | x = -1 195 | y = 1 196 | while path: 197 | if (position[0] + (chain * x), position[1] + (chain * y)) not in friends_list and \ 198 | 0 <= position[0] + (chain * x) <= 7 and 0 <= position[1] + (chain * y) <= 7: 199 | moves_list.append((position[0] + (chain * x), position[1] + (chain * y))) 200 | if (position[0] + (chain * x), position[1] + (chain * y)) in enemies_list: 201 | path = False 202 | chain += 1 203 | else: 204 | path = False 205 | return moves_list 206 | 207 | #Check rook moves 208 | def check_rook(position, color): 209 | moves_list = [] 210 | if color == 'white': 211 | enemies_list = black_locations 212 | friends_list = white_locations 213 | else: 214 | friends_list = black_locations 215 | enemies_list = white_locations 216 | for i in range(4): # down, up, right, left 217 | path = True 218 | chain = 1 219 | if i == 0: 220 | x = 0 221 | y = 1 222 | elif i == 1: 223 | x = 0 224 | y = -1 225 | elif i == 2: 226 | x = 1 227 | y = 0 228 | else: 229 | x = -1 230 | y = 0 231 | while path: 232 | if (position[0] + (chain * x), position[1] + (chain * y)) not in friends_list and \ 233 | 0 <= position[0] + (chain * x) <= 7 and 0 <= position[1] + (chain * y) <= 7: 234 | moves_list.append((position[0] + (chain * x), position[1] + (chain * y))) 235 | if (position[0] + (chain * x), position[1] + (chain * y)) in enemies_list: 236 | path = False 237 | chain += 1 238 | else: 239 | path = False 240 | return moves_list 241 | 242 | #Check valid pawn moves 243 | def check_pawn(position, color): 244 | moves_list = [] 245 | if color == 'white': 246 | if (position[0], position[1] + 1) not in white_locations and \ 247 | (position[0], position[1] + 1) not in black_locations and position[1] < 7: 248 | moves_list.append((position[0], position[1] + 1)) 249 | if (position[0], position[1] + 2) not in white_locations and \ 250 | (position[0], position[1] + 2) not in black_locations and position[1] == 1: 251 | moves_list.append((position[0], position[1] + 2)) 252 | if (position[0] + 1, position[1] + 1) in black_locations: 253 | moves_list.append((position[0] + 1, position[1] + 1)) 254 | if (position[0] - 1, position[1] + 1) in black_locations: 255 | moves_list.append((position[0] - 1, position[1] + 1)) 256 | else: 257 | if (position[0], position[1] - 1) not in white_locations and \ 258 | (position[0], position[1] - 1) not in black_locations and position[1] > 0: 259 | moves_list.append((position[0], position[1] - 1)) 260 | if (position[0], position[1] - 2) not in white_locations and \ 261 | (position[0], position[1] - 2) not in black_locations and position[1] == 6: 262 | moves_list.append((position[0], position[1] - 2)) 263 | if (position[0] + 1, position[1] - 1) in white_locations: 264 | moves_list.append((position[0] + 1, position[1] - 1)) 265 | if (position[0] - 1, position[1] - 1) in white_locations: 266 | moves_list.append((position[0] - 1, position[1] - 1)) 267 | return moves_list 268 | 269 | #Check valid knight moves 270 | def check_knight(position, color): 271 | moves_list = [] 272 | if color == 'white': 273 | enemies_list = black_locations 274 | friends_list = white_locations 275 | else: 276 | friends_list = black_locations 277 | enemies_list = white_locations 278 | # 8 squares to check for knights, they can go two squares in one direction and one in another 279 | targets = [(1, 2), (1, -2), (2, 1), (2, -1), (-1, 2), (-1, -2), (-2, 1), (-2, -1)] 280 | for i in range(8): 281 | target = (position[0] + targets[i][0], position[1] + targets[i][1]) 282 | if target not in friends_list and 0 <= target[0] <= 7 and 0 <= target[1] <= 7: 283 | moves_list.append(target) 284 | return moves_list 285 | 286 | #Check for valid moves for just selected piece 287 | def check_valid_moves(): 288 | if turn_step < 2: 289 | options_list = white_options 290 | else: 291 | options_list = black_options 292 | valid_options = options_list[selection] 293 | return valid_options 294 | 295 | 296 | #Draw valid moves on screen 297 | def draw_valid(moves): 298 | if turn_step < 2: 299 | color = 'green' 300 | else: 301 | color = 'blue' 302 | for i in range(len(moves)): 303 | pygame.draw.circle(screen, color, (moves[i][0] * 100 + 10, moves[i][1] * 100 + 10), 5) 304 | 305 | #Draw captured pieces on side of screen 306 | def draw_captured(): 307 | for i in range(len(captured_pieces_white)): 308 | captured_piece = captured_pieces_white[i] 309 | index = piece_list.index(captured_piece) 310 | screen.blit(small_black_images[index], (825, 5 + 50 * i)) 311 | for i in range(len(captured_pieces_black)): 312 | captured_piece = captured_pieces_black[i] 313 | index = piece_list.index(captured_piece) 314 | screen.blit(small_white_images[index], (925, 5 + 50 * i)) 315 | 316 | #Draw a flashing square around king if in check 317 | def draw_check(): 318 | if turn_step < 2: 319 | if 'king' in white_pieces: 320 | king_index = white_pieces.index('king') 321 | king_location = white_locations[king_index] 322 | for i in range(len(black_options)): 323 | if king_location in black_options[i]: 324 | if counter < 15: 325 | pygame.draw.rect(screen, 'dark red', [white_locations[king_index][0] * 100 + 1, 326 | white_locations[king_index][1] * 100 + 1, 100, 100], 5) 327 | else: 328 | if 'king' in black_pieces: 329 | king_index = black_pieces.index('king') 330 | king_location = black_locations[king_index] 331 | for i in range(len(white_options)): 332 | if king_location in white_options[i]: 333 | if counter < 15: 334 | pygame.draw.rect(screen, 'dark blue', [black_locations[king_index][0] * 100 + 1, 335 | black_locations[king_index][1] * 100 + 1, 100, 100], 5) 336 | 337 | 338 | def draw_game_over(): 339 | pygame.draw.rect(screen, 'black', [200, 200, 400, 70]) 340 | screen.blit(font.render(f'{winner} won the game!', True, 'white'), (210, 210)) 341 | screen.blit(font.render(f'Press ENTER to Restart!', True, 'white'), (210, 240)) 342 | 343 | #Main game loop 344 | 345 | black_options = check_options(black_pieces, black_locations, 'black') 346 | white_options = check_options(white_pieces, white_locations, 'white') 347 | run = True 348 | while run: 349 | timer.tick(fps) 350 | if counter < 30: 351 | counter += 1 352 | else: 353 | counter = 0 354 | screen.fill(color) 355 | draw_board() 356 | draw_pieces() 357 | draw_captured() 358 | draw_check() 359 | if selection != 100: 360 | valid_moves = check_valid_moves() 361 | draw_valid(valid_moves) 362 | #Event handling 363 | for event in pygame.event.get(): 364 | if event.type == pygame.QUIT: 365 | run = False 366 | if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and not game_over: 367 | x_coord = event.pos[0] // 100 368 | y_coord = event.pos[1] // 100 369 | click_coords = (x_coord, y_coord) 370 | if turn_step <= 1: #WHITE LOCATIONS 371 | if click_coords == (8, 8) or click_coords == (9, 8): 372 | winner = 'Super Nigga' 373 | if click_coords in white_locations: 374 | selection = white_locations.index(click_coords) 375 | if turn_step == 0: 376 | turn_step = 1 377 | if click_coords in valid_moves and selection != 100: 378 | white_locations[selection] = click_coords 379 | if click_coords in black_locations: 380 | black_piece = black_locations.index(click_coords) 381 | captured_pieces_white.append(black_pieces[black_piece]) 382 | if black_pieces[black_piece] == 'king': 383 | winner = 'White Racist MF' 384 | black_pieces.pop(black_piece) 385 | black_locations.pop(black_piece) 386 | black_options = check_options(black_pieces, black_locations, 'black') 387 | white_options = check_options(white_pieces, white_locations, 'white') 388 | turn_step = 2 389 | selection = 100 390 | valid_moves = [] 391 | if turn_step > 1: #NIGGA LOCATIONS 392 | if click_coords == (8, 8) or click_coords == (9, 8): 393 | winner = 'white' 394 | if click_coords in black_locations: 395 | selection = black_locations.index(click_coords) 396 | if turn_step == 2: 397 | turn_step = 3 398 | if click_coords in valid_moves and selection != 100: 399 | black_locations[selection] = click_coords 400 | if click_coords in white_locations: 401 | white_piece = white_locations.index(click_coords) 402 | captured_pieces_black.append(white_pieces[white_piece]) 403 | if white_pieces[white_piece] == 'king': 404 | winner = 'black' 405 | white_pieces.pop(white_piece) 406 | white_locations.pop(white_piece) 407 | black_options = check_options(black_pieces, black_locations, 'black') 408 | white_options = check_options(white_pieces, white_locations, 'white') 409 | turn_step = 0 410 | selection = 100 411 | valid_moves = [] 412 | if event.type == pygame.KEYDOWN and game_over: 413 | if event.key == pygame.K_RETURN: 414 | game_over = False 415 | winner = '' 416 | white_pieces = ['rook', 'knight', 'bishop', 'king', 'queen', 'bishop', 'knight', 'rook', 417 | 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn'] 418 | white_locations = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), 419 | (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)] 420 | black_pieces = ['rook', 'knight', 'bishop', 'king', 'queen', 'bishop', 'knight', 'rook', 421 | 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn'] 422 | black_locations = [(0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7), 423 | (0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6), (7, 6)] 424 | captured_pieces_white = [] 425 | captured_pieces_black = [] 426 | turn_step = 0 427 | selection = 100 428 | valid_moves = [] 429 | black_options = check_options(black_pieces, black_locations, 'black') 430 | white_options = check_options(white_pieces, white_locations, 'white') 431 | 432 | if winner != '': 433 | game_over = True 434 | draw_game_over() 435 | 436 | pygame.display.flip() 437 | pygame.quit() 438 | --------------------------------------------------------------------------------