def run(self,timeout): TIMEOUT = timeout board = Board() board.populate() status_black = utils.load_image(STATUS_BLACK) status_white = utils.load_image(STATUS_WHITE) status_red = utils.load_image(STATUS_RED) status_disabled = utils.load_image(STATUS_DISABLED) cmd_exit = utils.load_image(EXIT) cmd_stop = utils.load_image(PLAYER_STOP) cmd_play = utils.load_image(PLAYER_PLAY) cmd_pause = utils.load_image(PLAYER_PAUSE) cmd_pass = utils.load_image(PLAYER_END) background = utils.load_image(BOARD_BACKGROUND) background.blit(cmd_exit,(0,0)) background.blit(cmd_stop,(22,0)) background.blit(cmd_pause,(44,0)) background.blit(cmd_play,(66,0)) background.blit(cmd_pass,(88,0)) rand = random.randint(0,1000) selected = BLACK if rand % 2 == 0 : selected = RED rand = random.randint(0,1000) ai_color = RED if rand % 2 == 0 : ai_color = BLACK timeout = TIMEOUT time_left = TIMEOUT white = False winner = None status = 'stop' #menu menu = cMenu(5, 5, 2, 5, 'vertical', 100, self.screen, [( _('New Game'), 1, None), ( _('One Player'), 2, None), ( _('Two Players'), 3, None), ( _('Return'), 4, None), ( _('Exit'), 5, None)]) menu.set_center(True, True) menu.set_alignment('center', 'center') show_menu = True state = 0 prev_state = 1 rect_list = [] while 1: if show_menu: # Check if the state has changed, if it has, then post a user event to # the queue to force the menu to be shown at least once if prev_state != state: pygame.event.post(pygame.event.Event(EVENT_CHANGE_STATE, key = 0)) prev_state = state # Get the next event e = pygame.event.wait() # Update the menu, based on which "state" we are in - When using the menu # in a more complex program, definitely make the states global variables # so that you can refer to them by a name if e.type == pygame.KEYDOWN or e.type == EVENT_CHANGE_STATE: if state == 0: rect_list, state = menu.update(e, state) elif state == 1: print 'New Game' state = 0 board = Board() board.populate() status = 'play' show_menu = False elif state == 2: print 'One Player' state = 0 self.player = 1 show_menu = False elif state == 3: print 'Two Players' self.player = 2 show_menu = False state = 0 elif state == 4: print 'Return' show_menu = False state = 0 else: print 'Exit!' pygame.quit() sys.exit() # Quit if the user presses the exit button if e.type == pygame.QUIT: pygame.quit() sys.exit() # Update the screen pygame.display.update(rect_list) if show_menu: continue #AI if self.player == 1 and ai_color == selected and status == 'play': p = Player(board , ai_color) p.play(self.screen) selected = utils.invert_color(selected) timeout = TIMEOUT board.unselectedAll() for event in pygame.event.get(): x = y = -1 if event.type == QUIT: return elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: show_menu = True elif event.type == MOUSEBUTTONDOWN: m_pos = pygame.mouse.get_pos() (x , y, pass_round) = board.getSelectedPosition(m_pos) if pass_round: selected = utils.invert_color(selected) board.unselectedAll() continue if x >= 0 and y >= 0 : if board.stoneExists(x,y) : status = 'play' oStone = Stone(board,x,y) if oStone.color == selected and oStone.canMove() : sStone = Stone(board,x,y) sStone.selected() else : try: if oStone.mustChoose(): timeout = TIMEOUT sStone.choose(x,y) if sStone.canMove(): sStone.selected() else: selected = utils.invert_color(sStone.color) board.unselectedAll() except: print "object sStone doesn't exist" else: (old_x,old_y,color) = board.getSelectedStone() if old_x>=0 and old_y>=0 and color>0 : if sStone.move(x,y) : timeout = TIMEOUT if sStone.mustChoose(): sStone.selected() elif sStone.canMove() : sStone.selected() else: selected = utils.invert_color(sStone.color) sStone.unselected() #action else: (m_x , m_y) = m_pos if m_x < 22 and m_y < 22: # quit return elif m_x > 22 and m_x < 44 and m_y < 22: # stop board = Board() board.populate() # print "stop" status = 'stop' elif m_x > 44 and m_x < 66 and m_y < 22: # pause # print "pause" if status == 'pause': status = 'play' timeout = time_left time_left = 0 else: status = 'pause' time_left = timeout elif m_x > 66 and m_x < 88 and m_y < 22: # play # print "play" if status == 'pause': timeout = time_left time_left = 0 status = 'play' if winner != None: board = Board() board.populate() elif m_x > 88 and m_x < 110 and m_y < 22: # play # print "pass" selected=utils.invert_color(selected) timeout=TIMEOUT status = 'play' self.screen.blit(background, (0, 0)) #check if a player wins winner = board.checkWinner(); if winner != None: font = pygame.font.Font(pygame.font.get_default_font(), 26) text = _('%s wins') % utils.getColorName(winner) text = font.render(text, 1, (10, 10, 10)) textpos = text.get_rect() textpos.centerx = background.get_rect().centerx self.screen.blit(text, textpos) status = 'pause' #show timer font = pygame.font.Font(pygame.font.get_default_font(), 16) if status == 'play': text = _('%d seconds left') % (timeout/1000) text = font.render(text, 1, (10, 10, 10)) textpos = text.get_rect() textpos.centerx = background.get_rect().centerx self.screen.blit(text, textpos) elif status == 'pause' and winner == None : text = _('%d seconds left') % (time_left/1000) text = font.render(text, 1, (10, 10, 10)) textpos = text.get_rect() textpos.centerx = background.get_rect().centerx self.screen.blit(text, textpos) #blink if status == 'play': if timeout < 10000 and not white: if selected == BLACK : self.screen.blit(status_white, (500-40, 0)) self.screen.blit(status_disabled, (500-20, 0)) else: self.screen.blit(status_disabled, (500-40, 0)) self.screen.blit(status_white, (500-20, 0)) white = True else : if selected == BLACK : self.screen.blit(status_black, (500-40, 0)) self.screen.blit(status_disabled, (500-20, 0)) else: self.screen.blit(status_disabled, (500-40, 0)) self.screen.blit(status_red, (500-20, 0)) white = False #show board.populate_gui(self.screen) pygame.display.update() pygame.time.delay(DELAY) #time out timeout -= DELAY if status == "stop" or status =="pause": timeout = TIMEOUT if timeout < 0 : selected = utils.invert_color(selected) board.unselectedAll() timeout = TIMEOUT
def play(self, screen): """the AI plays here""" must_choose = 0 (ai_x, ai_y) = self.getSelectedStone() # print "AI x = %d y = %d " % (ai_x,ai_y) pStone = Stone(self.board, ai_x, ai_y) pStone.selected() (new_x, new_y, nb, action) = pStone.canCaptureStones() if new_x >= 0 and new_y >= 0: self.update(screen) timeout = 0 while 1: if timeout < 10000: self.update(screen) timeout += 1 (new_x, new_y, nb, action) = pStone.canCaptureStones() if new_x >= 0 and new_y >= 0: timeout = 0 # print "moves to (%d , %d)" % (new_x,new_y) if pStone.move(new_x, new_y): if pStone.mustChoose(): pStone.chooseAction(action) pStone.selected() if pStone.canMove(): pStone.selected() else: # print "can't move and capture " pStone.unselected() break else: stones = self.getFreeStones() found = False for (sx, sy) in stones: neib = utils.get_positions((sx, sy)) for (xx, yy) in neib: oStone = Stone(self.board, sx, sy) if xx >= 0 and xx < self.board.cols and yy >= 0 and yy < self.board.rows and oStone.legalMove( xx, yy): new_board = self.board.copy() new_board.unselectedAll() oStone = Stone(new_board, sx, sy) if oStone.move(xx, yy): if not oStone.canBeCaptured(): found = True pStone = Stone(self.board, sx, sy) pStone.selected() pStone.move(xx, yy) break if found: break if found: break else: print "must choose one" neib = utils.get_positions((ai_x, ai_y)) oStone = Stone(self.board, ai_x, ai_y) for (xx, yy) in neib: if xx >= 0 and xx < self.board.cols and yy >= 0 and yy < self.board.rows and oStone.legalMove( xx, yy): found = True pStone = Stone(self.board, sx, sy) pStone.selected() pStone.move(xx, yy) break break self.board.unselectedAll() self.update(screen)
def play(self,screen): """the AI plays here""" must_choose = 0 (ai_x, ai_y) = self.getSelectedStone() # print "AI x = %d y = %d " % (ai_x,ai_y) pStone = Stone(self.board , ai_x ,ai_y) pStone.selected() (new_x, new_y , nb , action) = pStone.canCaptureStones() if new_x >= 0 and new_y >= 0 : self.update(screen) timeout = 0 while 1 : if timeout<10000: self.update(screen) timeout+=1 (new_x, new_y , nb , action) = pStone.canCaptureStones() if new_x >= 0 and new_y >= 0 : timeout = 0 # print "moves to (%d , %d)" % (new_x,new_y) if pStone.move(new_x,new_y) : if pStone.mustChoose(): pStone.chooseAction(action) pStone.selected() if pStone.canMove() : pStone.selected() else : # print "can't move and capture " pStone.unselected() break else : stones = self.getFreeStones() found = False for (sx,sy) in stones : neib = utils.get_positions((sx,sy)) for (xx ,yy) in neib: oStone = Stone(self.board,sx,sy) if xx>=0 and xx < self.board.cols and yy >=0 and yy < self.board.rows and oStone.legalMove(xx,yy) : new_board = self.board.copy() new_board.unselectedAll() oStone = Stone(new_board,sx,sy) if oStone.move(xx,yy): if not oStone.canBeCaptured(): found = True pStone = Stone(self.board , sx ,sy) pStone.selected() pStone.move(xx,yy) break if found: break if found: break else: print "must choose one" neib = utils.get_positions((ai_x,ai_y)) oStone = Stone(self.board,ai_x,ai_y) for (xx ,yy) in neib: if xx>=0 and xx < self.board.cols and yy >=0 and yy < self.board.rows and oStone.legalMove(xx,yy) : found = True pStone = Stone(self.board , sx ,sy) pStone.selected() pStone.move(xx,yy) break break self.board.unselectedAll() self.update(screen)