Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- pygame.init()
- height = 950
- width = 1000
- screen = pygame.display.set_mode([width, height])
- pygame.display.set_caption('Checkers match')
- font = pygame.font.SysFont('Arial', 20)
- big_font = pygame.font.SysFont('calibri', 45)
- timer = pygame.time.Clock()
- fps = 60
- # game variables and images
- white_pieces = ['man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man']
- white_locations = [(0, 7), (2, 7), (4, 7), (6, 7), (1, 6), (3, 6), (5, 6), (7, 6), (0, 5), (2, 5), (4, 5), (6, 5)]
- black_pieces = ['man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man']
- black_locations = [(1, 0), (3, 0), (5, 0), (7, 0), (0, 1), (2, 1), (4, 1), (6, 1), (1, 2), (3, 2), (5, 2), (7, 2)]
- captured_pieces_white = []
- captured_pieces_black = []
- turn_step = 0
- selection = 100
- valid_moves = []
- # piece images
- white_man = pygame.image.load('Checkers Piece White(man).png')
- white_man = pygame.transform.scale(white_man, (150, 150))
- white_man_small = pygame.transform.scale(white_man, (45, 45))
- white_prmotions = ['king']
- white_king = pygame.image.load('Checkers Piece White(king).png')
- white_king = pygame.transform.scale(white_king, (150, 150))
- white_king_small = pygame.transform.scale(white_king, (45, 45))
- white_images = [white_man, white_king]
- small_white_images = [white_man_small, white_king_small]
- black_man = pygame.image.load('Checkers Piece Black(man).png')
- black_man = pygame.transform.scale(black_man, (150, 150))
- black_man_small = pygame.transform.scale(black_man, (45, 45))
- black_promotions = ['king']
- black_king = pygame.image.load('Checkers Piece Black(king).png')
- black_king = pygame.transform.scale(black_king, (150, 150))
- black_king_small = pygame.transform.scale(black_king, (45, 45))
- black_images = [black_man, black_king]
- small_black_images = [black_man_small, black_king_small]
- piece_list = ['man', 'king']
- # check variables
- winner = ''
- game_over = False
- white_promote = False
- black_promote = False
- promo_index = 100
- # draw main game board
- def draw_board():
- for i in range(32):
- column = i % 4
- row = i // 4
- if row % 2 == 0:
- pygame.draw.rect(screen, 'light gray', [600 - (column * 200), row * 100, 100, 100])
- else:
- pygame.draw.rect(screen, 'light gray', [700 - (column * 200), row * 100, 100, 100])
- pygame.draw.rect(screen, 'gray', [0, 800, width, 200])
- pygame.draw.rect(screen, 'red', [0, 800, width, 150], 5)
- pygame.draw.rect(screen, 'red', [800, 0, 200, height], 5)
- status_text = ['WHITE: Select a piece to move', 'WHITE: Select a place to move to',
- 'BLACK: Select a piece to move', 'BLACK: Select a place to move to']
- screen.blit(big_font.render(status_text[turn_step], True, 'black'), (20, 820))
- for j in range(9):
- pygame.draw.line(screen, 'black', (0, 100 * j), (800, 100 * j), 2)
- pygame.draw.line(screen, 'black', (100 * j, 0), (100 * j, 800), 2)
- screen.blit(big_font.render('FORFEIT', True, 'black'), (825, 850))
- if white_promote or black_promote:
- pygame.draw.rect(screen, 'gray', [0, 800, width - 200])
- pygame.draw.rect(screen, 'red', [0, 800, width - 150], 5)
- screen.blit(big_font.render('Select the king piece to promote',True,'black')(20,820))
- # draw pieces
- def draw_pieces():
- square_size = 100
- piece_size = 150
- offset = (square_size - piece_size) // 2
- for i in range(len(black_pieces)):
- index = piece_list.index(white_pieces[i])
- x_pos = white_locations[i][0] * square_size + offset
- y_pos = white_locations[i][1] * square_size + offset
- if white_pieces[i] == 'man':
- screen.blit(white_man, (x_pos, y_pos))
- else:
- screen.blit(white_images[index], (x_pos, y_pos))
- if turn_step < 2:
- if selection == i:
- pygame.draw.rect(screen, 'gold',
- [white_locations[i][0] * 100 + 1, white_locations[i][1] * 100 + 1, 100, 100], 2)
- if len(black_pieces) != len(black_locations):
- print(
- f"Error: Mismatch in lengths. black_pieces: {len(black_pieces)}, black_locations: {len(black_locations)}")
- return
- for i in range(len(black_pieces)):
- if i >= len(black_locations): # Safety check
- print(f"Error: Index {i} is out of range for black_locations")
- break
- index = piece_list.index(black_pieces[i])
- x_pos = black_locations[i][0] * square_size + offset
- y_pos = black_locations[i][1] * square_size + offset
- if black_pieces[i] == 'man':
- screen.blit(black_man, (x_pos, y_pos))
- else:
- screen.blit(black_images[index], (x_pos, y_pos))
- if turn_step >= 2:
- if selection == i:
- pygame.draw.rect(screen, 'silver',
- [black_locations[i][0] * 100 + 1, black_locations[i][1] * 100 + 1, 100, 100], 2)
- # check all valid piece options on board
- def check_options(pieces, locations, turn):
- if len(pieces) != len(locations):
- print(f"Error: Mismatch in lengths. Pieces: {len(pieces)}, Locations: {len(locations)}")
- return []
- moves_list = []
- all_moves_list = []
- for i in range(len(pieces)):
- location = locations[i]
- piece = pieces[i]
- if piece == 'man':
- moves_list = check_man(location, turn)
- elif piece == 'king':
- moves_list = check_king(location, turn)
- all_moves_list.append(moves_list)
- return all_moves_list
- # check valid man moves
- def check_man(position, colour):
- moves_list = []
- if colour == 'white':
- if (position[0] - 1 >= 0 and position[1] - 1 >= 0 and (
- position[0] - 1, position[1] - 1) not in white_locations and (
- position[0] - 1, position[1] - 1) not in black_locations):
- moves_list.append((position[0] - 1, position[1] - 1))
- if (position[0] + 1 < 8 and position[1] - 1 >= 0 and (
- position[0] + 1, position[1] - 1) not in white_locations and (
- position[0] + 1, position[1] - 1) not in black_locations):
- moves_list.append((position[0] + 1, position[1] - 1))
- # Capture pieces
- if (position[0] - 2 >= 0 and position[1] - 2 >= 0 and (
- position[0] - 1, position[1] - 1) in black_locations and (
- position[0] - 2, position[1] - 2) not in white_locations and (
- position[0] - 2, position[1] - 2) not in black_locations):
- moves_list.append((position[0] - 2, position[1] - 2))
- black_locations.remove((position[0] - 1, position[1] - 1))
- if (position[0] + 2 < 8 and position[1] - 2 >= 0 and (position[0] + 1, position[1] - 1) in black_locations and (
- position[0] + 2, position[1] - 2) not in white_locations and (
- position[0] + 2, position[1] - 2) not in black_locations):
- moves_list.append((position[0] + 2, position[1] - 2))
- black_locations.remove((position[0] + 1, position[1] - 1))
- else:
- if (position[0] - 1 >= 0 and position[1] + 1 < 8 and (
- position[0] - 1, position[1] + 1) not in white_locations and (
- position[0] - 1, position[1] + 1) not in black_locations):
- moves_list.append((position[0] - 1, position[1] + 1))
- if (position[0] + 1 < 8 and position[1] + 1 < 8 and (
- position[0] + 1, position[1] + 1) not in white_locations and (
- position[0] + 1, position[1] + 1) not in black_locations):
- moves_list.append((position[0] + 1, position[1] + 1))
- # capture pieces
- if (position[0] - 2 >= 0 and position[1] + 2 < 8 and (position[0] - 1, position[1] + 1) in white_locations and (
- position[0] - 2, position[1] + 2) not in white_locations and (
- position[0] - 2, position[1] + 2) not in black_locations):
- moves_list.append((position[0] - 2, position[1] + 2))
- white_locations.remove((position[0] - 1, position[1] + 1))
- if (position[0] + 2 < 8 and position[1] + 2 < 8 and (position[0] + 1, position[1] + 1) in white_locations and (
- position[0] + 2, position[1] + 2) not in white_locations and (
- position[0] + 2, position[1] + 2) not in black_locations):
- moves_list.append((position[0] + 2, position[1] + 2))
- white_locations.remove((position[0] + 1, position[1] + 1))
- return moves_list
- # check valid king moves
- def check_king(position, colour):
- moves_list = []
- if colour == 'white':
- ally_list = white_locations
- else:
- ally_list = black_locations
- directions = [(-1, -1), (-1, 1), (1, -1), (1, 1)]
- for i in range(4):
- target = (position[0] + directions[i][0], position[1] + directions[i][1])
- if target not in ally_list and 0 <= target[0] <= 7 and 0 <= target[1] <= 7:
- moves_list.append(target)
- return moves_list
- # check valid moves only for selected piece
- def check_valid_moves():
- if turn_step < 2:
- options_list = white_options
- else:
- options_list = black_options
- valid_options = options_list[selection]
- return valid_options
- # draw valid moves on screen
- def draw_valid(moves):
- if turn_step < 2:
- colour = 'gold'
- else:
- colour = 'blue'
- for i in range(len(moves)):
- pygame.draw.circle(screen, colour, (moves[i][0] * 100 + 50, moves[i][1] * 100 + 50), 5)
- # draw captured pieces on side of screen
- def draw_captured():
- for i in range(len(captured_pieces_white)):
- captured_piece = captured_pieces_white[i]
- index = piece_list.index(captured_piece)
- screen.blit(small_black_images[index], (825, 5 + 50 + i))
- for i in range(len(captured_pieces_black)):
- captured_piece = captured_pieces_black[i]
- index = piece_list.index(captured_piece)
- screen.blit(small_white_images[index], (925, 5 + 50 + i))
- #draw game over sign
- def draw_game_over():
- pygame.draw.rect(screen, 'black', [200, 200, 400, 70])
- screen.blit(font.render(f'{winner} is victorious', True, 'yellow'), (210, 210))
- screen.blit(font.render(f'Press ENTER to start new game', True, 'green'), (210, 240))
- #man promotion
- def check_promotion():
- man_indexes = []
- white_promotion = False
- black_promotion = False
- promote_index = 100
- for i in range(len(white_pieces)):
- if white_pieces[i]== 'man':
- man_indexes.append(i)
- for i in range(len(man_indexes)):
- if white_locations[man_indexes[i]][1] == 0:
- white_promotion = True
- promote_index = man_indexes[i]
- man_indexes = []
- for i in range(len(black_pieces)):
- if black_pieces[i]== 'man':
- man_indexes.append(i)
- for i in range(len(man_indexes)):
- if black_locations[man_indexes[i]][1] == 0:
- black_promotion = True
- promote_index = man_indexes[i]
- return white_promotion, black_promotion, promote_index
- def draw_promotion():
- pygame.draw.rect(screen,'dark gray',[800,0,200,420])
- if white_promote:
- colour = 'white'
- for i in range(len(white_promotions)):
- piece = white_promotions[i]
- index = piece_list.index(piece)
- screen.blit(white_images[index],(800,5+100*i))
- elif black_promote:
- colour = 'black'
- for i in range(len(black_promotions)):
- piece = black_promotions[i]
- index = piece_list.index(piece)
- screen.blit(black_images[index],(800,5+100*i))
- pygame.draw.rect(screen,colour,[800,0,200,420],8)
- def check_promo_select():
- mouse_pos = pygame.mouse.get_pos()
- left_click = pygame.mouse.get_pressed()[0]
- x_position = mouse_pos[0]//100
- y_position = mouse_pos[1]//100
- if white_promote and left_click and x_position < 0 and y_position < 2:
- white_pieces[promo_index] = white_promotions[y_position]
- elif black_promote and left_click and x_position < 0 and y_position < 2:
- black_pieces[promo_index] = black_promotions[y_position]
- # main game loop
- black_options = check_options(black_pieces, black_locations, 'black')
- white_options = check_options(white_pieces, white_locations, 'white')
- run = True
- while run:
- timer.tick(fps)
- screen.fill('dimgray')
- draw_board()
- draw_pieces()
- draw_captured()
- if not game_over:
- white_promote,black_promote, promo_index = check_promotion()
- if white_promote or black_promote:
- draw_promotion()
- check_promo_select()
- if selection != 100:
- valid_moves = check_valid_moves()
- draw_valid(valid_moves)
- # event handling
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- run = False
- if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and not game_over:
- x_coord = event.pos[0] // 100
- y_coord = event.pos[1] // 100
- click_coords = (x_coord, y_coord)
- if turn_step <= 1:
- if click_coords == (8, 8) or click_coords == (9, 8):
- winner = 'Black'
- game_over = True
- if click_coords in white_locations:
- selection = white_locations.index(click_coords)
- if turn_step == 0:
- turn_step = 1
- if click_coords in valid_moves and selection != 100:
- white_locations[selection] = click_coords
- if click_coords in black_locations:
- black_piece = black_locations.index(click_coords)
- captured_pieces_white.append(black_pieces[black_piece])
- if black_pieces[black_piece] == 0:
- winner = 'White'
- game_over = True
- black_pieces.pop(black_piece)
- black_locations.pop(black_piece)
- black_options = check_options(black_pieces, black_locations, 'black')
- white_options = check_options(white_pieces, white_locations, 'white')
- turn_step = 2
- selection = 100
- valid_moves = []
- if turn_step > 1:
- if len(black_pieces) == 0:
- winner = 'White'
- game_over = True
- if click_coords in black_locations:
- selection = black_locations.index(click_coords)
- if turn_step == 2:
- turn_step = 3
- black_piece = black_locations.index(click_coords)
- captured_pieces_white.append(black_pieces[black_piece])
- black_pieces.pop(black_piece)
- black_locations.pop(black_piece)
- if click_coords in valid_moves and selection != 100:
- black_locations[selection] = click_coords
- if click_coords in white_locations:
- white_piece = white_locations.index(click_coords)
- captured_pieces_black.append(white_pieces[white_piece])
- white_pieces.pop(white_piece)
- white_locations.pop(white_piece)
- if len(white_pieces) == 0:
- winner = 'Black'
- game_over = True
- black_options = check_options(black_pieces, black_locations, 'black')
- white_options = check_options(white_pieces, white_locations, 'white')
- turn_step = 0
- selection = 100
- valid_moves = []
- if event.type == pygame.KEYDOWN and game_over:
- if event.key == pygame.K_RETURN:
- game_over = False
- winner = ''
- white_pieces = ['man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man']
- white_locations = [(0, 7), (2, 7), (4, 7), (6, 7), (1, 6), (3, 6), (5, 6), (7, 6), (0, 5), (2, 5),
- (4, 5), (6, 5)]
- black_pieces = ['man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man']
- black_locations = [(1, 0), (3, 0), (5, 0), (7, 0), (0, 1), (2, 1), (4, 1), (6, 1), (1, 2), (3, 2),
- (5, 2), (7, 2)]
- turn_step = 0
- selection = 100
- valid_moves = []
- black_options = check_options(black_pieces, black_locations, 'black')
- white_options = check_options(white_pieces, white_locations, 'white')
- captured_pieces_white = []
- captured_pieces_black = []
- if winner != '':
- game_over = True
- draw_game_over()
- pygame.display.flip()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment