def newGame(): if (DEBUG): print "Welcome to Chess!" pygame.init() #initialize all imported pygame modules window_size = [1000, 650] # width, height screen = pygame.display.set_mode(window_size) updateText(screen, "Welcome to Chess!") pygame.display.set_caption("Colleen's Chess") buttons = pygame.sprite.RenderPlain() buttons = Button.makeButton(buttons, screen, "Quit", Button.ButtonType.Quit, white, black) Button.makeButton(buttons, screen, "Castle", Button.ButtonType.Castle, green, white) board = Board() turnCount = 0 clock = pygame.time.Clock() mainLoop(screen, board, turnCount, clock, buttons, False)
def mainLoop(screen, board, turnCount, clock, buttons, done): board.squares.draw(screen) #draw Sprites (Squares) if board.border != None: pygame.draw.rect(screen, red, board.border, BORDER_THICKNESS) board.piecesOnBoard.draw(screen) #draw Sprites (Pieces) pygame.display.flip() #update screen if done == True: #Button.makeButton(buttons, screen, "Play Again", Button.ButtonType.PlayAgain, blue, white) while done == True: for e in pygame.event.get(): if e.type == pygame.QUIT: #user clicks close if (DEBUG): print "Close clicked" done = True elif e.type == pygame.MOUSEBUTTONUP: x, y = e.pos clickedButton = Button.clicked(buttons, e.pos) if (clickedButton != False): if (clickedButton.ButtonType == Button.ButtonType.Quit): clickedButton.kill() makeButton(buttons, screen, "Play Again", Button.ButtonType.PlayAgain, blue, white) elif (clickedButton.ButtonType == Button.ButtonType.PlayAgain): newGame() else: print "no button type" #again = raw_input("Would you like to play again? If yes, type 'yes'\n") #if again == 'yes': # newGame() while done == False: currentMouseOverSquare = board.mouseOverSquare currentLoc = None for e in pygame.event.get(): if e.type == pygame.QUIT: #user clicks close if (DEBUG): print "Close clicked" done = True elif e.type == pygame.MOUSEBUTTONUP: x, y = e.pos clickedButton = Button.clicked(buttons, e.pos) if (clickedButton != False): if (clickedButton.ButtonType == Button.ButtonType.Quit): clickedButton.kill() castleButton = buttons.sprites()[0] buttons.empty() # takes castle button out of list, but it's still showing pygame.draw.rect(screen, black, castleButton.rect) # draws black over where the castle button used to be Button.makeButton(buttons, screen, "Play Again", Button.ButtonType.PlayAgain, blue, white) updateText(screen, "Do you want to play again?") mainLoop(screen, board, turnCount, clock, buttons, True) elif (clickedButton.ButtonType == Button.ButtonType.PlayAgain): newGame() #sets done=False, emptys buttons (and puts new quit button in) else: print "no button type" row = get_row_from_mouse_y(y) col = get_col_from_mouse_x(x) currentLoc = colStr(col)+str(row) if currentLoc in board.squareDic.keys(): if board.selectedPiece == None: board.selectedPiece = board.squareDic[currentLoc].piece board.border = board.squareDic[currentLoc].get_rect() elif (board.selectedPiece.row == str(row) and board.selectedPiece.col == colStr(col)): board.border = None board.selectedPiece = None else: oldLocation = board.selectedPiece.row + "," + board.selectedPiece.col newLocation = str(row) + "," + str(colStr(col)) if (DEBUG): print oldLocation #new location print newLocation #old location madeMove, exp = makeMove(screen, board, colStr(col), str(row), turnCount, clock) # returns (bool, string explaination ) if madeMove: if (DEBUG): print "made move" print exp if turnCount%2==0: turnColor = 'black' else: turnColor = 'white' if 'Game Over' in exp: updateText(screen, exp) buttons.empty() # takes buttons out of list Button.makeButton(buttons, screen, "Play Again", Button.ButtonType.PlayAgain, blue, white) mainLoop(screen, board, turnCount, clock, buttons, True) else: updateText(screen, turnColor + " moved from " + oldLocation + " to " + newLocation) turnCount +=1 board.border = None board.selectedPiece = None for hSquare in board.highlightedSquares: hSquare.un_highlight() # unhighlight old possible moves else: #error print "exp: " + exp if (DEBUG): print exp updateText(screen, exp) elif e.type == pygame.MOUSEMOTION: x, y = e.pos row = get_row_from_mouse_y(y) col = get_col_from_mouse_x(x) currentLoc = colStr(col)+str(row) if currentLoc in board.squareDic.keys(): board.mouseOverSquare = board.squareDic[currentLoc] if board.mouseOverSquare != currentMouseOverSquare: # hovering over new square if board.selectedPiece == None: for hSquare in board.highlightedSquares: hSquare.un_highlight() # unhighlight old possible moves if isinstance(board.mouseOverSquare, Square): board.highlight_legal_moves() # ^ highlight new available moves board.squares.draw(screen) #draw Sprites (Squares) if board.border != None: pygame.draw.rect(screen, red, board.border, BORDER_THICKNESS) board.piecesOnBoard.draw(screen) #draw Sprites (Pieces) labelSquares(screen) # displays letters for columns and numbers for rows pygame.display.flip() #update screen clock.tick(5) pygame.quit() #closes things, keeps idle from freezing sys.exit()