0% found this document useful (0 votes)
80 views5 pages

Code

This document outlines a Python program using Pygame to create a two-player chess game. It includes setup for game variables, piece images, and the main game loop, where players can select and move pieces. The code also handles turn management and valid move checking for both white and black pieces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views5 pages

Code

This document outlines a Python program using Pygame to create a two-player chess game. It includes setup for game variables, piece images, and the main game loop, where players can select and move pieces. The code also handles turn management and valid move checking for both white and black pieces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

# two player chess in python with Pygame!

# part one, set up variables images and game loop

import pygame

[Link]()

#common board variables


WIDTH = 1000
HEIGHT = 900
background_color = 'light gray'
square_color = 'dark gray'

screen = [Link].set_mode([WIDTH, HEIGHT])


[Link].set_caption('Two-Player Pygame Chess!')
font = [Link]('[Link]', 20)
medium_font = [Link]('[Link]', 40)
big_font = [Link]('[Link]', 40)
timer = [Link]()

fps = 60
# game variables and images
white_pieces = ['rook', 'knight', 'bishop', 'king', 'queen', 'bishop', 'knight',
'rook',
'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn']
white_locations = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0),
(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)]
black_pieces = ['rook', 'knight', 'bishop', 'king', 'queen', 'bishop', 'knight',
'rook',
'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn', 'pawn']
black_locations = [(0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7),
(0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6), (7, 6)]
captured_pieces_white = []
captured_pieces_black = []
# 0 - whites turn no selection: 1-whites turn piece selected: 2- black turn no
selection, 3 - black turn piece selected
turn_step = 0
selection = 100
valid_moves = []
# load in game piece images (queen, king, rook, bishop, knight, pawn) x 2
black_queen = [Link]('assets/images/black [Link]')
black_queen = [Link](black_queen, (80, 80))
black_queen_small = [Link](black_queen, (45, 45))
black_king = [Link]('assets/images/black [Link]')
black_king = [Link](black_king, (80, 80))
black_king_small = [Link](black_king, (45, 45))
black_rook = [Link]('assets/images/black [Link]')
black_rook = [Link](black_rook, (80, 80))
black_rook_small = [Link](black_rook, (45, 45))
black_bishop = [Link]('assets/images/black [Link]')
black_bishop = [Link](black_bishop, (80, 80))
black_bishop_small = [Link](black_bishop, (45, 45))
black_knight = [Link]('assets/images/black [Link]')
black_knight = [Link](black_knight, (80, 80))
black_knight_small = [Link](black_knight, (45, 45))
black_pawn = [Link]('assets/images/black [Link]')
black_pawn = [Link](black_pawn, (65, 65))
black_pawn_small = [Link](black_pawn, (45, 45))
white_queen = [Link]('assets/images/white [Link]')
white_queen = [Link](white_queen, (80, 80))
white_queen_small = [Link](white_queen, (45, 45))
white_king = [Link]('assets/images/white [Link]')
white_king = [Link](white_king, (80, 80))
white_king_small = [Link](white_king, (45, 45))
white_rook = [Link]('assets/images/white [Link]')
white_rook = [Link](white_rook, (80, 80))
white_rook_small = [Link](white_rook, (45, 45))
white_bishop = [Link]('assets/images/white [Link]')
white_bishop = [Link](white_bishop, (80, 80))
white_bishop_small = [Link](white_bishop, (45, 45))
white_knight = [Link]('assets/images/white [Link]')
white_knight = [Link](white_knight, (80, 80))
white_knight_small = [Link](white_knight, (45, 45))
white_pawn = [Link]('assets/images/white [Link]')
white_pawn = [Link](white_pawn, (65, 65))
white_pawn_small = [Link](white_pawn, (45, 45))
white_images = [white_pawn, white_queen, white_king, white_knight, white_rook,
white_bishop]
small_white_images = [white_pawn_small, white_queen_small, white_king_small,
white_knight_small,
white_rook_small, white_bishop_small]
black_images = [black_pawn, black_queen, black_king, black_knight, black_rook,
black_bishop]
small_black_images = [black_pawn_small, black_queen_small, black_king_small,
black_knight_small,
black_rook_small, black_bishop_small]
piece_list = ['pawn', 'queen', 'king', 'knight', 'rook', 'bishop']
# check variables/ flashing counter

def draw_board():
for i in range(32):
column = i % 4
row = i // 4
if row % 2 == 0 :
[Link](screen, square_color, [600 - (column * 200), row *
100, 100, 100 ])

else:
[Link](screen, square_color, [700 - (column * 200), row *
100, 100, 100 ])

[Link](screen, 'gray', [0, 800, WIDTH, 100] )


[Link](screen, 'gold', [0, 800, WIDTH, 100], 5)
[Link](screen, 'gold', [800, 0, 200, HEIGHT], 5)

status_text = ["White's turn. Select a piece to move!", "White's turn.


Perform the move!",
"Black's turn. Select a piece to move!", "Black's turn.
Perform the move!"
]
[Link](big_font.render(status_text[turn_step], True, 'black'), (15,
820))

for i in range(9):
[Link](screen, 'black', (0, 100 * i), (800, 100 * i), 2)
[Link](screen, 'black', (100 * i, 0), (100 * i, 800), 2)

def draw_pieces():

for i in range(len(white_pieces)):
index = piece_list.index(white_pieces[i])
if white_pieces[i] == 'pawn':
[Link](white_pawn, (white_locations[i][0] * 100 + 19,
white_locations[i][1] * 100 + 30))
else:
[Link](white_images[index], (white_locations[i][0] * 100 + 10,
white_locations[i][1] * 100 + 10))

#highlights the piece that we have selected


if turn_step < 2:
if selection == i:
[Link](screen, 'red' , [white_locations[i][0] * 100 + 1,
white_locations[i][1] * 100 + 1, 100 ,100], 2)

for i in range(len(black_pieces)):
index = piece_list.index(black_pieces[i])
if black_pieces[i] == 'pawn':
[Link](black_pawn, (black_locations[i][0] * 100 + 19,
black_locations[i][1] * 100 + 30))
else:
[Link](black_images[index], (black_locations[i][0] * 100 + 10,
black_locations[i][1] * 100 + 10))

#highlights the piece that we have selected


if turn_step >= 2:
if selection == i:
[Link](screen, 'blue' , [white_locations[i][0] * 100 +
1, white_locations[i][1] * 100 + 1, 100 ,100], 2)

#cheking all valid moves


def check_options(pieces, locations, turns):

moves_list = []
all_moves_list = []

for i in range(len(pieces)):
locations = locations[i]
piece = pieces[i]

if piece == 'pawn':
moves_list = check_pawn(locations, turns)

elif piece == 'rook':


moves_list = check_rook(locations, turns)

elif piece == 'knight':


moves_list = check_knight(locations, turns)
elif piece == 'bishop':
moves_list = check_bishop(locations, turns)

elif piece == 'queen':


moves_list = check_queen(locations, turns)

elif piece == 'king':


moves_list = check_king(locations, turns)

all_moves_list.append(moves_list)

return all_moves_list

#check valid pawn moves


def check_pawn(position, color):
moves_list = []
if color == 'white':
if (position[0], position[1] +1) not in white_locations and (position[0],
position[1] +1) not in black_locations and position[1] < 7:
moves_list.append((position[0], position[1] +1))

if (position[0], position[1] + 2) not in white_locations and (position[0],


position[1] + 2) not in black_locations and position[1] == 1:
moves_list.append((position[0], position[1] +1))

#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:
[Link](fps)
[Link](background_color)
draw_board()
draw_pieces()

#this handles the events

for event in [Link]():


if [Link] == [Link]:
run = False

if [Link] == [Link] and [Link] == 1:


x_coord = [Link][0] // 100
y_coord = [Link][1] // 100
click_coord = (x_coord, y_coord)

if turn_step <= 1:
if click_coord in white_locations:
selection = white_locations.index(click_coord)

if turn_step == 0:
turn_step = 1

if click_coord in valid_moves and selection != 100:


white_locations[selection] = click_coord

if click_coord in black_locations:
black_piece = black_locations.index(click_coord)
captured_pieces_white.append[black_pieces[black_piece]]
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
valid_moves = []

if turn_step > 1:

if click_coord in black_locations:
selection = black_locations.index(click_coord)

if turn_step == 2:
turn_step = 3

if click_coord in valid_moves and selection != 100:


black_locations[selection] = click_coord

if click_coord in white_locations:
white_piece = white_locations.index(click_coord)
captured_pieces_black.append[white_pieces[white_piece]]
white_pieces.pop(white_piece)
white_locations.pop(white_piece)

black_options = check_options(black_pieces, black_locations,


'black')
white_options = check_options(white_pieces, white_locations,
'white')

turn_step = 0
valid_moves = []

[Link]()

[Link]

You might also like