MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / runGame

Function runGame

src/gamesbyexample/pygame_games/flippy.py:72–220  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

70
71
72def runGame():
73 # Plays a single game of reversi each time this function is called.
74
75 # Reset the board and game.
76 mainBoard = getNewBoard()
77 resetBoard(mainBoard)
78 showHints = False
79 turn = random.choice(['computer', 'player'])
80
81 # Draw the starting board and ask the player what color they want.
82 drawBoard(mainBoard)
83 playerTile, computerTile = enterPlayerTile()
84
85 # Make the Surface and Rect objects for the "New Game" and "Hints" buttons
86 newGameSurf = FONT.render('New Game', True, TEXTCOLOR, TEXTBGCOLOR2)
87 newGameRect = newGameSurf.get_rect()
88 newGameRect.topright = (WINDOWWIDTH - 8, 10)
89 hintsSurf = FONT.render('Hints', True, TEXTCOLOR, TEXTBGCOLOR2)
90 hintsRect = hintsSurf.get_rect()
91 hintsRect.topright = (WINDOWWIDTH - 8, 40)
92
93 while True: # main game loop
94 # Keep looping for player and computer's turns.
95 if turn == 'player':
96 # Player's turn:
97 if getValidMoves(mainBoard, playerTile) == []:
98 # If it's the player's turn but they
99 # can't move, then end the game.
100 break
101 movexy = None
102 while movexy == None:
103 # Keep looping until the player clicks on a valid space.
104
105 # Determine which board data structure to use for display.
106 if showHints:
107 boardToDraw = getBoardWithValidMoves(mainBoard, playerTile)
108 else:
109 boardToDraw = mainBoard
110
111 checkForQuit()
112 for event in pygame.event.get(): # event handling loop
113 if event.type == MOUSEBUTTONUP:
114 # Handle mouse click events
115 mousex, mousey = event.pos
116 if newGameRect.collidepoint( (mousex, mousey) ):
117 # Start a new game
118 return True
119 elif hintsRect.collidepoint( (mousex, mousey) ):
120 # Toggle hints mode
121 showHints = not showHints
122 # movexy is set to a two-item tuple XY coordinate, or None value
123 movexy = getSpaceClicked(mousex, mousey)
124 if movexy != None and not isValidMove(mainBoard, playerTile, movexy[0], movexy[1]):
125 movexy = None
126
127 # Draw the game board.
128 drawBoard(boardToDraw)
129 drawInfo(boardToDraw, playerTile, computerTile, turn)

Callers 1

mainFunction · 0.70

Calls 13

resetBoardFunction · 0.85
enterPlayerTileFunction · 0.85
getSpaceClickedFunction · 0.85
drawInfoFunction · 0.85
getNewBoardFunction · 0.70
drawBoardFunction · 0.70
getValidMovesFunction · 0.70
getBoardWithValidMovesFunction · 0.70
checkForQuitFunction · 0.70
isValidMoveFunction · 0.70
makeMoveFunction · 0.70
getComputerMoveFunction · 0.70

Tested by

no test coverage detected