Amazing-Python-Scripts
487 строк · 20.1 Кб
1################################################### Scientific Calculator ###################################################
2############################################### Contributed By Avdhesh Varshney #############################################
3############################################ (https://github.com/Avdhesh-Varshney) ##########################################
4
5# Importing Modules
6import pygame7
8# Initialising pygame module
9pygame.init()10
11# Setting Width and height of the Chess Game screen
12WIDTH = 80013HEIGHT = 80014
15screen = pygame.display.set_mode([WIDTH, HEIGHT])16pygame.display.set_caption('Two-Player Chess Game')17
18font = pygame.font.Font('freesansbold.ttf', 20)19medium_font = pygame.font.Font('freesansbold.ttf', 40)20big_font = pygame.font.Font('freesansbold.ttf', 50)21
22timer = pygame.time.Clock()23fps = 6024
25# game variables and images
26white_pieces = ['rook', 'knight', 'bishop', 'king', 'queen', 'bishop', 'knight', 'rook',27'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn']28white_locations = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0),29(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)]30black_pieces = ['rook', 'knight', 'bishop', 'king', 'queen', 'bishop', 'knight', 'rook',31'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn']32black_locations = [(0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7),33(0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6), (7, 6)]34
35captured_pieces_white = []36captured_pieces_black = []37
38# 0 - whites turn no selection: 1-whites turn piece selected: 2- black turn no selection, 3 - black turn piece selected
39turn_step = 040selection = 10041valid_moves = []42
43# load in game piece images (queen, king, rook, bishop, knight, pawn) x 2
44black_queen = pygame.image.load('./images/black queen.png')45black_queen = pygame.transform.scale(black_queen, (80, 80))46black_queen_small = pygame.transform.scale(black_queen, (45, 45))47black_king = pygame.image.load('./images/black king.png')48black_king = pygame.transform.scale(black_king, (80, 80))49black_king_small = pygame.transform.scale(black_king, (45, 45))50black_rook = pygame.image.load('./images/black rook.png')51black_rook = pygame.transform.scale(black_rook, (80, 80))52black_rook_small = pygame.transform.scale(black_rook, (45, 45))53black_bishop = pygame.image.load('./images/black bishop.png')54black_bishop = pygame.transform.scale(black_bishop, (80, 80))55black_bishop_small = pygame.transform.scale(black_bishop, (45, 45))56black_knight = pygame.image.load('./images/black knight.png')57black_knight = pygame.transform.scale(black_knight, (80, 80))58black_knight_small = pygame.transform.scale(black_knight, (45, 45))59black_pawn = pygame.image.load('./images/black pawn.png')60black_pawn = pygame.transform.scale(black_pawn, (65, 65))61black_pawn_small = pygame.transform.scale(black_pawn, (45, 45))62white_queen = pygame.image.load('./images/white queen.png')63white_queen = pygame.transform.scale(white_queen, (80, 80))64white_queen_small = pygame.transform.scale(white_queen, (45, 45))65white_king = pygame.image.load('./images/white king.png')66white_king = pygame.transform.scale(white_king, (80, 80))67white_king_small = pygame.transform.scale(white_king, (45, 45))68white_rook = pygame.image.load('./images/white rook.png')69white_rook = pygame.transform.scale(white_rook, (80, 80))70white_rook_small = pygame.transform.scale(white_rook, (45, 45))71white_bishop = pygame.image.load('./images/white bishop.png')72white_bishop = pygame.transform.scale(white_bishop, (80, 80))73white_bishop_small = pygame.transform.scale(white_bishop, (45, 45))74white_knight = pygame.image.load('./images/white knight.png')75white_knight = pygame.transform.scale(white_knight, (80, 80))76white_knight_small = pygame.transform.scale(white_knight, (45, 45))77white_pawn = pygame.image.load('./images/white pawn.png')78white_pawn = pygame.transform.scale(white_pawn, (65, 65))79white_pawn_small = pygame.transform.scale(white_pawn, (45, 45))80
81white_images = [white_pawn, white_queen, white_king,82white_knight, white_rook, white_bishop]83small_white_images = [white_pawn_small, white_queen_small, white_king_small, white_knight_small,84white_rook_small, white_bishop_small]85
86black_images = [black_pawn, black_queen, black_king,87black_knight, black_rook, black_bishop]88small_black_images = [black_pawn_small, black_queen_small, black_king_small, black_knight_small,89black_rook_small, black_bishop_small]90
91piece_list = ['pawn', 'queen', 'king', 'knight', 'rook', 'bishop']92
93# check variables/ flashing counter
94counter = 095winner = ''96game_over = False97
98
99# draw main game board
100def draw_board():101for i in range(32):102column = i % 4103row = i // 4104if row % 2 == 0:105pygame.draw.rect(screen, 'light gray', [106600 - (column * 200), row * 100, 100, 100])107else:108pygame.draw.rect(screen, 'light gray', [109700 - (column * 200), row * 100, 100, 100])110pygame.draw.rect(screen, 'gray', [0, 800, WIDTH, 100])111pygame.draw.rect(screen, 'gold', [0, 800, WIDTH, 100], 5)112pygame.draw.rect(screen, 'gold', [800, 0, 200, HEIGHT], 5)113status_text = ['White: Select a Piece to Move!', 'White: Select a Destination!',114'Black: Select a Piece to Move!', 'Black: Select a Destination!']115screen.blit(big_font.render(116status_text[turn_step], True, 'black'), (20, 820))117for i in range(9):118pygame.draw.line(screen, 'black', (0, 100 * i), (800, 100 * i), 2)119pygame.draw.line(screen, 'black', (100 * i, 0), (100 * i, 800), 2)120screen.blit(medium_font.render('FORFEIT', True, 'black'), (810, 830))121
122
123# draw pieces onto board
124def draw_pieces():125for i in range(len(white_pieces)):126index = piece_list.index(white_pieces[i])127if white_pieces[i] == 'pawn':128screen.blit(129white_pawn, (white_locations[i][0] * 100 + 22, white_locations[i][1] * 100 + 30))130else:131screen.blit(white_images[index], (white_locations[i]132[0] * 100 + 10, white_locations[i][1] * 100 + 10))133if turn_step < 2:134if selection == i:135pygame.draw.rect(screen, 'red', [white_locations[i][0] * 100 + 1, white_locations[i][1] * 100 + 1,136100, 100], 2)137
138for i in range(len(black_pieces)):139index = piece_list.index(black_pieces[i])140if black_pieces[i] == 'pawn':141screen.blit(142black_pawn, (black_locations[i][0] * 100 + 22, black_locations[i][1] * 100 + 30))143else:144screen.blit(black_images[index], (black_locations[i]145[0] * 100 + 10, black_locations[i][1] * 100 + 10))146if turn_step >= 2:147if selection == i:148pygame.draw.rect(screen, 'blue', [black_locations[i][0] * 100 + 1, black_locations[i][1] * 100 + 1,149100, 100], 2)150
151
152# function to check all pieces valid options on board
153def check_options(pieces, locations, turn):154moves_list = []155all_moves_list = []156for i in range((len(pieces))):157location = locations[i]158piece = pieces[i]159if piece == 'pawn':160moves_list = check_pawn(location, turn)161elif piece == 'rook':162moves_list = check_rook(location, turn)163elif piece == 'knight':164moves_list = check_knight(location, turn)165elif piece == 'bishop':166moves_list = check_bishop(location, turn)167elif piece == 'queen':168moves_list = check_queen(location, turn)169elif piece == 'king':170moves_list = check_king(location, turn)171all_moves_list.append(moves_list)172return all_moves_list173
174
175# check king valid moves
176def check_king(position, color):177moves_list = []178if color == 'white':179enemies_list = black_locations180friends_list = white_locations181else:182friends_list = black_locations183enemies_list = white_locations184# 8 squares to check for kings, they can go one square any direction185targets = [(1, 0), (1, 1), (1, -1), (-1, 0),186(-1, 1), (-1, -1), (0, 1), (0, -1)]187for i in range(8):188target = (position[0] + targets[i][0], position[1] + targets[i][1])189if target not in friends_list and 0 <= target[0] <= 7 and 0 <= target[1] <= 7:190moves_list.append(target)191return moves_list192
193
194# check queen valid moves
195def check_queen(position, color):196moves_list = check_bishop(position, color)197second_list = check_rook(position, color)198for i in range(len(second_list)):199moves_list.append(second_list[i])200return moves_list201
202
203# check bishop moves
204def check_bishop(position, color):205moves_list = []206if color == 'white':207enemies_list = black_locations208friends_list = white_locations209else:210friends_list = black_locations211enemies_list = white_locations212for i in range(4): # up-right, up-left, down-right, down-left213path = True214chain = 1215if i == 0:216x = 1217y = -1218elif i == 1:219x = -1220y = -1221elif i == 2:222x = 1223y = 1224else:225x = -1226y = 1227while path:228if (position[0] + (chain * x), position[1] + (chain * y)) not in friends_list and \2290 <= position[0] + (chain * x) <= 7 and 0 <= position[1] + (chain * y) <= 7:230moves_list.append(231(position[0] + (chain * x), position[1] + (chain * y)))232if (position[0] + (chain * x), position[1] + (chain * y)) in enemies_list:233path = False234chain += 1235else:236path = False237return moves_list238
239
240# check rook moves
241def check_rook(position, color):242moves_list = []243if color == 'white':244enemies_list = black_locations245friends_list = white_locations246else:247friends_list = black_locations248enemies_list = white_locations249for i in range(4): # down, up, right, left250path = True251chain = 1252if i == 0:253x = 0254y = 1255elif i == 1:256x = 0257y = -1258elif i == 2:259x = 1260y = 0261else:262x = -1263y = 0264while path:265if (position[0] + (chain * x), position[1] + (chain * y)) not in friends_list and \2660 <= position[0] + (chain * x) <= 7 and 0 <= position[1] + (chain * y) <= 7:267moves_list.append(268(position[0] + (chain * x), position[1] + (chain * y)))269if (position[0] + (chain * x), position[1] + (chain * y)) in enemies_list:270path = False271chain += 1272else:273path = False274return moves_list275
276
277# check valid pawn moves
278def check_pawn(position, color):279moves_list = []280if color == 'white':281if (position[0], position[1] + 1) not in white_locations and \282(position[0], position[1] + 1) not in black_locations and position[1] < 7:283moves_list.append((position[0], position[1] + 1))284if (position[0], position[1] + 2) not in white_locations and \285(position[0], position[1] + 2) not in black_locations and position[1] == 1:286moves_list.append((position[0], position[1] + 2))287if (position[0] + 1, position[1] + 1) in black_locations:288moves_list.append((position[0] + 1, position[1] + 1))289if (position[0] - 1, position[1] + 1) in black_locations:290moves_list.append((position[0] - 1, position[1] + 1))291else:292if (position[0], position[1] - 1) not in white_locations and \293(position[0], position[1] - 1) not in black_locations and position[1] > 0:294moves_list.append((position[0], position[1] - 1))295if (position[0], position[1] - 2) not in white_locations and \296(position[0], position[1] - 2) not in black_locations and position[1] == 6:297moves_list.append((position[0], position[1] - 2))298if (position[0] + 1, position[1] - 1) in white_locations:299moves_list.append((position[0] + 1, position[1] - 1))300if (position[0] - 1, position[1] - 1) in white_locations:301moves_list.append((position[0] - 1, position[1] - 1))302return moves_list303
304
305# check valid knight moves
306def check_knight(position, color):307moves_list = []308if color == 'white':309enemies_list = black_locations310friends_list = white_locations311else:312friends_list = black_locations313enemies_list = white_locations314# 8 squares to check for knights, they can go two squares in one direction and one in another315targets = [(1, 2), (1, -2), (2, 1), (2, -1),316(-1, 2), (-1, -2), (-2, 1), (-2, -1)]317for i in range(8):318target = (position[0] + targets[i][0], position[1] + targets[i][1])319if target not in friends_list and 0 <= target[0] <= 7 and 0 <= target[1] <= 7:320moves_list.append(target)321return moves_list322
323
324# check for valid moves for just selected piece
325def check_valid_moves():326if turn_step < 2:327options_list = white_options328else:329options_list = black_options330valid_options = options_list[selection]331return valid_options332
333
334# draw valid moves on screen
335def draw_valid(moves):336if turn_step < 2:337color = 'red'338else:339color = 'blue'340for i in range(len(moves)):341pygame.draw.circle(342screen, color, (moves[i][0] * 100 + 50, moves[i][1] * 100 + 50), 5)343
344
345# draw captured pieces on side of screen
346def draw_captured():347for i in range(len(captured_pieces_white)):348captured_piece = captured_pieces_white[i]349index = piece_list.index(captured_piece)350screen.blit(small_black_images[index], (825, 5 + 50 * i))351for i in range(len(captured_pieces_black)):352captured_piece = captured_pieces_black[i]353index = piece_list.index(captured_piece)354screen.blit(small_white_images[index], (925, 5 + 50 * i))355
356
357# draw a flashing square around king if in check
358def draw_check():359if turn_step < 2:360if 'king' in white_pieces:361king_index = white_pieces.index('king')362king_location = white_locations[king_index]363for i in range(len(black_options)):364if king_location in black_options[i]:365if counter < 15:366pygame.draw.rect(screen, 'dark red', [white_locations[king_index][0] * 100 + 1,367white_locations[king_index][1] * 100 + 1, 100, 100], 5)368else:369if 'king' in black_pieces:370king_index = black_pieces.index('king')371king_location = black_locations[king_index]372for i in range(len(white_options)):373if king_location in white_options[i]:374if counter < 15:375pygame.draw.rect(screen, 'dark blue', [black_locations[king_index][0] * 100 + 1,376black_locations[king_index][1] * 100 + 1, 100, 100], 5)377
378
379def draw_game_over():380pygame.draw.rect(screen, 'black', [200, 200, 400, 70])381screen.blit(font.render(382f'{winner} won the game!', True, 'white'), (210, 210))383screen.blit(font.render(f'Press ENTER to Restart!',384True, 'white'), (210, 240))385
386
387# main game loop
388black_options = check_options(black_pieces, black_locations, 'black')389white_options = check_options(white_pieces, white_locations, 'white')390run = True391while run:392timer.tick(fps)393if counter < 30:394counter += 1395else:396counter = 0397screen.fill('dark gray')398draw_board()399draw_pieces()400draw_captured()401draw_check()402if selection != 100:403valid_moves = check_valid_moves()404draw_valid(valid_moves)405# event handling406for event in pygame.event.get():407if event.type == pygame.QUIT:408run = False409if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and not game_over:410x_coord = event.pos[0] // 100411y_coord = event.pos[1] // 100412click_coords = (x_coord, y_coord)413if turn_step <= 1:414if click_coords == (8, 8) or click_coords == (9, 8):415winner = 'black'416if click_coords in white_locations:417selection = white_locations.index(click_coords)418if turn_step == 0:419turn_step = 1420if click_coords in valid_moves and selection != 100:421white_locations[selection] = click_coords422if click_coords in black_locations:423black_piece = black_locations.index(click_coords)424captured_pieces_white.append(black_pieces[black_piece])425if black_pieces[black_piece] == 'king':426winner = 'white'427black_pieces.pop(black_piece)428black_locations.pop(black_piece)429black_options = check_options(430black_pieces, black_locations, 'black')431white_options = check_options(432white_pieces, white_locations, 'white')433turn_step = 2434selection = 100435valid_moves = []436if turn_step > 1:437if click_coords == (8, 8) or click_coords == (9, 8):438winner = 'white'439if click_coords in black_locations:440selection = black_locations.index(click_coords)441if turn_step == 2:442turn_step = 3443if click_coords in valid_moves and selection != 100:444black_locations[selection] = click_coords445if click_coords in white_locations:446white_piece = white_locations.index(click_coords)447captured_pieces_black.append(white_pieces[white_piece])448if white_pieces[white_piece] == 'king':449winner = 'black'450white_pieces.pop(white_piece)451white_locations.pop(white_piece)452black_options = check_options(453black_pieces, black_locations, 'black')454white_options = check_options(455white_pieces, white_locations, 'white')456turn_step = 0457selection = 100458valid_moves = []459if event.type == pygame.KEYDOWN and game_over:460if event.key == pygame.K_RETURN:461game_over = False462winner = ''463white_pieces = ['rook', 'knight', 'bishop', 'king', 'queen', 'bishop', 'knight', 'rook',464'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn']465white_locations = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0),466(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)]467black_pieces = ['rook', 'knight', 'bishop', 'king', 'queen', 'bishop', 'knight', 'rook',468'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn']469black_locations = [(0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7),470(0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6), (7, 6)]471captured_pieces_white = []472captured_pieces_black = []473turn_step = 0474selection = 100475valid_moves = []476black_options = check_options(477black_pieces, black_locations, 'black')478white_options = check_options(479white_pieces, white_locations, 'white')480
481if winner != '':482game_over = True483draw_game_over()484
485pygame.display.flip()486
487pygame.quit()488