class Game(): def __init__(self, caption): pygame.init() self.screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) pygame.display.set_caption(caption) self.clock = pygame.time.Clock() 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 + 275)) # self.buttons.append( EasyButton(self.screen, 'Easy', MAP_WIDTH + 30, BUTTON_HEIGHT + 45)) self.buttons.append( MediumButton(self.screen, 'Normal', MAP_WIDTH + 30, BUTTON_HEIGHT + 110)) self.buttons.append( DifficultyButton(self.screen, 'Hard', MAP_WIDTH + 30, BUTTON_HEIGHT + 175)) # 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.useAI = False self.winner = None self.level = None def start(self, level): self.level = level self.is_play = True self.player = MAP_ENTRY_TYPE.MAP_PLAYER_ONE self.map.reset() 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.level) self.checkClick(x, y, True) self.useAI = False if self.action is not None: self.checkClick(self.action[0], self.action[1]) self.action = None if not self.isOver(): self.changeMouseShow() if self.isOver(): self.showWinner() self.map.drawBackground(self.screen) self.map.drawChess(self.screen) self.map.drawSelection(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.map.click(x, y, self.player) if self.AI.isWin(self.map.map, self.player): self.winner = self.player self.click_button(self.buttons[0]) else: self.player = self.map.reverseTurn(self.player) if not isAI: 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 showWinner(self): def showFont(screen, text, location_x, locaiton_y, height): font = pygame.font.SysFont(None, height) font_image = font.render(text, True, (205, 85, 85), (255, 255, 255)) font_image_rect = font_image.get_rect() font_image_rect.x = location_x font_image_rect.y = locaiton_y screen.blit(font_image, font_image_rect) if self.winner == MAP_ENTRY_TYPE.MAP_PLAYER_ONE: str = 'Winner is White' else: str = 'Winner is Black' showFont(self.screen, str, MAP_WIDTH + 25, SCREEN_HEIGHT - 80, 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
class Game(): def __init__(self, caption): pygame.init() self.screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) #初始化窗口或屏幕进行显示 pygame.display.set_caption(caption) #设置窗口标题 self.clock = pygame.time.Clock() #创建一个时钟(用来帮助跟踪时间) self.buttons = [] #按钮list 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.winner = None self.AI = ChessAI(CHESS_LEN) self.useAI = False def start(self): self.is_play = True #更改游戏状态 self.player = MAP_ENTRY_TYPE.MAP_PLAYER_ONE #定义玩家????????? self.map.reset() #调用map类的函数清空棋盘 def play(self): self.clock.tick(60) #每秒循环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) self.useAI = False if self.action is not None: self.checkClick(self.action[0], self.action[1]) #下棋并轮到对方下 self.action = None 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.map.click(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: 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 showWinner(self): def showFont(screen, 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 #font_image_rect.topleft = (location_x, locaiton_y)等价于上面两句 screen.blit(font_image, font_image_rect) #写出Winner if self.winner == MAP_ENTRY_TYPE.MAP_PLAYER_ONE: str = 'Winner is White' else: str = 'Winner is Black' showFont(self.screen, str, MAP_WIDTH + 25, SCREEN_HEIGHT - 60, 30) pygame.mouse.set_visible(True) #返回鼠标的前一个可见状态 def click_button(self, button): #点击Start或Give up按钮后,重新绘制按钮,换掉底色 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