-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.py
211 lines (176 loc) · 6.23 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import pygame
from pygame.locals import *
from GameMap import *
from ChessAI import *
class Button():
def __init__(self, screen, text, x, y, color, enable):
self.screen = screen
self.width = BUTTON_WIDTH
self.height = BUTTON_HEIGHT
self.button_color = color
self.text_color = (255, 255, 255)
self.enable = enable
self.font = pygame.font.SysFont(None, BUTTON_HEIGHT*2//3)
self.rect = pygame.Rect(0, 0, self.width, self.height)
self.rect.topleft = (x, y)
self.text = text
self.init_msg()
def init_msg(self):
if self.enable:
self.msg_image = self.font.render(self.text, True, self.text_color, self.button_color[0])
else:
self.msg_image = self.font.render(self.text, True, self.text_color, self.button_color[1])
self.msg_image_rect = self.msg_image.get_rect()
self.msg_image_rect.center = self.rect.center
def draw(self):
if self.enable:
self.screen.fill(self.button_color[0], self.rect)
else:
self.screen.fill(self.button_color[1], self.rect)
self.screen.blit(self.msg_image, self.msg_image_rect)
class StartButton(Button):
def __init__(self, screen, text, x, y):
super().__init__(screen, text, x, y, [(26, 173, 25),(158, 217, 157)], True)
def click(self, game):
if self.enable:
game.start()
game.winner = None
self.msg_image = self.font.render(self.text, True, self.text_color, self.button_color[1])
self.enable = False
return True
return False
def unclick(self):
if not self.enable:
self.msg_image = self.font.render(self.text, True, self.text_color, self.button_color[0])
self.enable = True
class GiveupButton(Button):
def __init__(self, screen, text, x, y):
super().__init__(screen, text, x, y, [(230, 67, 64),(236, 139, 137)], False)
def click(self, game):
if self.enable:
game.is_play = False
if game.winner is None:
game.winner = game.map.reverseTurn(game.player)
self.msg_image = self.font.render(self.text, True, self.text_color, self.button_color[1])
self.enable = False
return True
return False
def unclick(self):
if not self.enable:
self.msg_image = self.font.render(self.text, True, self.text_color, self.button_color[0])
self.enable = True
class Game():
def __init__(self, caption, play_mode, AI_first):
pygame.init()
self.screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption(caption)
self.clock = pygame.time.Clock()
self.mode = play_mode
self.buttons = []
self.buttons.append(StartButton(self.screen, 'Start', MAP_WIDTH + 30, 15))
self.buttons.append(GiveupButton(self.screen, 'Giveup', MAP_WIDTH + 30, BUTTON_HEIGHT + 45))
self.is_play = False
self.map = Map(CHESS_LEN, CHESS_LEN)
self.player = MAP_ENTRY_TYPE.MAP_PLAYER_ONE
self.action = None
self.AI = ChessAI(CHESS_LEN)
self.AI_first = AI_first
self.winner = None
def start(self):
self.is_play = True
self.player = MAP_ENTRY_TYPE.MAP_PLAYER_ONE
self.map.reset()
self.AI.number = 0
if (self.mode == USER_VS_AI_MODE and self.AI_first) or self.mode == AI_VS_AI_MODE:
self.useAI = True
else:
self.useAI = False
def play(self):
self.clock.tick(60)
light_yellow = (247, 238, 214)
pygame.draw.rect(self.screen, light_yellow, pygame.Rect(0, 0, MAP_WIDTH, SCREEN_HEIGHT))
pygame.draw.rect(self.screen, (255, 255, 255), pygame.Rect(MAP_WIDTH, 0, INFO_WIDTH, SCREEN_HEIGHT))
for button in self.buttons:
button.draw()
if self.is_play and not self.isOver():
if self.useAI:
x, y = self.AI.findBestChess(self.map.map, self.player)
self.checkClick(x, y, True)
if self.mode == USER_VS_AI_MODE:
self.useAI = False
if self.mode != AI_VS_AI_MODE:
if self.action is not None:
self.checkClick(self.action[0], self.action[1])
self.action = None
if self.mode == USER_VS_AI_MODE:
self.showAIThink()
if not self.isOver():
self.changeMouseShow()
if self.isOver():
self.showWinner()
self.map.drawBackground(self.screen)
self.map.drawChess(self.screen)
def changeMouseShow(self):
map_x, map_y = pygame.mouse.get_pos()
x, y = self.map.MapPosToIndex(map_x, map_y)
if self.map.isInMap(map_x, map_y) and self.map.isEmpty(x, y):
pygame.mouse.set_visible(False)
light_red = (213, 90, 107)
pos, radius = (map_x, map_y), CHESS_RADIUS
pygame.draw.circle(self.screen, light_red, pos, radius)
else:
pygame.mouse.set_visible(True)
def checkClick(self,x, y, isAI=False):
self.AI.click(self.map, x, y, self.player)
if self.AI.isWin(self.map.map, self.player):
self.winner = self.player
self.click_button(self.buttons[1])
else:
self.player = self.map.reverseTurn(self.player)
if not isAI and self.mode != USER_VS_USER_MODE:
self.useAI = True
def mouseClick(self, map_x, map_y):
if self.is_play and self.map.isInMap(map_x, map_y) and not self.isOver():
x, y = self.map.MapPosToIndex(map_x, map_y)
if self.map.isEmpty(x, y):
self.action = (x, y)
def isOver(self):
return self.winner is not None
def showFont(self, text, location_x, locaiton_y, height):
font = pygame.font.SysFont(None, height)
font_image = font.render(text, True, (0, 0, 255), (255, 255, 255))
font_image_rect = font_image.get_rect()
font_image_rect.x = location_x
font_image_rect.y = locaiton_y
self.screen.blit(font_image, font_image_rect)
def showAIThink(self):
self.showFont('AI is thinking...', MAP_WIDTH + 25, SCREEN_HEIGHT//2-30, 30)
def showWinner(self):
if self.winner == MAP_ENTRY_TYPE.MAP_PLAYER_ONE:
str = 'Winner is White'
else:
str = 'Winner is Black'
self.showFont(str, MAP_WIDTH + 25, SCREEN_HEIGHT - 60, 30)
pygame.mouse.set_visible(True)
def click_button(self, button):
if button.click(self):
for tmp in self.buttons:
if tmp != button:
tmp.unclick()
def check_buttons(self, mouse_x, mouse_y):
for button in self.buttons:
if button.rect.collidepoint(mouse_x, mouse_y):
self.click_button(button)
break
game = Game("FIVE CHESS " + GAME_VERSION, GAME_PLAY_MODE, AI_RUN_FIRST)
while True:
game.play()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
game.mouseClick(mouse_x, mouse_y)
game.check_buttons(mouse_x, mouse_y)