-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chess.py
98 lines (85 loc) · 4.27 KB
/
Chess.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
from Board import Board
import pygame
from Pieces.Queen import Queen
from Pieces.Rook import Rook
from Pieces.Knight import Knight
from Pieces.Bishop import Bishop
class Chess:
size = 640
tenth = 640 / 10
board = Board(size)
board.initialize_standard_board()
pygame.display.flip()
def game(self):
done = True
promotion = False
new_game_screen = False
selection = []
while done is True:
pygame.display.flip()
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = False # exit while loop
if event.type == pygame.MOUSEBUTTONDOWN and new_game_screen:
if pygame.mouse.get_pressed()[0]: # left mouse button pressed
pos = pygame.mouse.get_pos()
x = self.tenth
# yes - new game box
if 3 * x <= pos[0] <= 4.5 * x: #and 7 * x <= pos[1] <= 8 * x:
self.board.clear_board()
self.board.initialize_standard_board()
pygame.display.flip()
self.board.white_turn = True # if white won, it should be their turn again
self.game() # make a new game
# no - new game box
if 5.5 * x <= pos[0] <= 7 * x and 7 * x <= pos[1] <= 8 * x:
done = False # exit while loop
if event.type == pygame.MOUSEBUTTONDOWN and promotion: # promotion dialogue
if pygame.mouse.get_pressed()[0]: # left mouse button pressed
pos = pygame.mouse.get_pos()
# Each box for each piece selection, if they don't press on the box, try again
x = self.tenth
if 1.5 * x <= pos[0] < 2.5 * x and 1.5 * x <= pos[1] <= 2.5 * x:
knight = Knight(color)
self.board.place_piece(square, knight)
self.board.deselect_board()
pygame.display.flip()
promotion = False
elif 3.5 * x <= pos[0] < 4.5 * x and 1.5 * x <= pos[1] <= 2.5 * x:
bishop = Bishop(color)
self.board.place_piece(square, bishop)
self.board.deselect_board()
pygame.display.flip()
promotion = False
elif 5.5 * x <= pos[0] < 6.5 * x and 1.5 * x <= pos[1] <= 2.5 * x:
rook = Rook(color)
self.board.place_piece(square, rook)
self.board.deselect_board()
pygame.display.flip()
promotion = False
elif 7.5 * x <= pos[0] < 8.5 * x and 1.5 * x <= pos[1] <= 2.5 * x:
queen = Queen(color)
self.board.place_piece(square, queen)
self.board.deselect_board()
pygame.display.flip()
promotion = False
if event.type == pygame.MOUSEBUTTONDOWN: # normal move
if pygame.mouse.get_pressed()[0]: # left mouse button pressed
pos = pygame.mouse.get_pos()
square = self.board.get_square(pos)
selection.append(square)
self.board.select_piece(square)
pygame.display.flip()
if len(selection) == 2:
output = self.board.player_move(selection[0], selection[1])
selection = []
if output[0] == "Promotion":
color = output[1]
square = output[2]
pygame.display.flip()
promotion = True
if output == "New_Game":
self.board.new_game_message()
pygame.display.flip()
new_game_screen = True
self.board.print_move_list()