-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
101 lines (80 loc) · 2.73 KB
/
game.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
97
98
99
100
101
from board import Board
from player import Player
import pygame
pygame.init()
bg_color = (0, 0, 0)
screen_width = 480 # 660
screen_height = 480 # 640
size = (screen_width, screen_height)
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Chess')
# pygame.display.set_icon('icon.png')
game_board = Board()
white_player = Player(game_board, 'White')
black_player = Player(game_board, 'Black')
white_player.set_opponent(black_player)
black_player.set_opponent(white_player)
ended = False
def reset():
global game_board, white_player, black_player, ended, result
game_board = Board()
white_player = Player(game_board, 'White')
black_player = Player(game_board, 'Black')
white_player.set_opponent(black_player)
black_player.set_opponent(white_player)
ended = False
result = 'Continue'
Player.turn = 0
def show_game_end(res):
text_size = 30
font = pygame.font.SysFont('SansSerif', text_size)
winner = f"{'White' if Player.turn == 1 else 'Black'} wins!!" if res == 'Checkmate' else None
restart = 'Press Space to restart'
end_screen_color = (142, 164, 210)
end_screen_width = 300
end_screen_height = 150
end_screen_x = screen_width // 2 - end_screen_width // 2
end_screen_y = screen_height // 2 - end_screen_height // 2
pygame.draw.rect(screen, end_screen_color, (end_screen_x, end_screen_y, end_screen_width, end_screen_height))
pos = (end_screen_x + end_screen_width // 2, end_screen_y + end_screen_height // 2)
message = font.render(res, True, (0, 0, 0))
rect = message.get_rect()
rect.center = (pos[0], pos[1] - text_size)
screen.blit(message, rect)
if winner is not None:
message = font.render(winner, True, (0, 0, 0))
rect = message.get_rect()
rect.center = pos
screen.blit(message, rect)
message = font.render(restart, True, (0, 0, 0))
rect = message.get_rect()
rect.center = (pos[0], pos[1] + text_size)
screen.blit(message, rect)
def redraw(res):
global ended
game_board.draw(screen)
if res != 'Continue':
ended = True
show_game_end(res)
pygame.display.update()
run = True
selected = None
result = 'Continue'
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
if ended:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
reset()
else:
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
if Player.turn == 0:
result = white_player.play(x, y)
else:
result = black_player.play(x, y)
redraw(result)
pygame.quit()