mainGame.py
Mon Aug 08 2022 20:07:33 GMT+0000 (Coordinated Universal Time)
Saved by @ammu123
# mainGame will store the class/functions # used to run/monitor the main game import pygame import sys from board import Board from gameFunctions import gamefunctions as gf import time # create a class called TicTacToe # has overall code to manage game behaviors class TicTacToe: def __init__(self): #Initialize game/resources pygame.init() #Create screen attribute, -which controls #screen using pygame's built-in screen #method #add a title(Tic-Tac-Toe) self.screen = pygame.display.set_mode((800, 800)) pygame.display.set_caption("Tic-Tac-Toe") #Create an attribute that stores the board object self.board = Board(self) #Create an attribute that stores the game functions object self.gf = gf(self) #A boolean that sees if it's the first run through the loop self.firstRun = True #the value of the move the player makes self.player1Move = 0 self.player2Move = 0 #Draw board to screen self.board.blitme() #stores the instructions to run the main game def run_game(self): #Create a while loop that runs until we need the #game to stop while True: #Check all keyboard events that the user inputs for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if self.firstRun == False: #Take a move from the user and draw it if self.gf.player1Turn: self.player1Move = self.gf.makeMove() self.gf.player1Moves.append(self.gf.moveData[self.player1Move]) self.gf.drawMove(self.player1Move) self.gf.endTurnP1() elif self.gf.player2Turn: self.player2Move = self.gf.makeMove() self.gf.player2Moves.append(self.gf.moveData[self.player2Move]) self.gf.drawMove(self.player2Move) self.gf.endTurnP2() #Update the screen pygame.display.flip() #Check if someone won if self.firstRun == False: isWinner = self.gf.checkWinner() if True in isWinner: print("\n\nGame Over!!!") time.sleep(3) if isWinner[0] == True: winner = "Player One" else: winner = "Player Two" print(f"The winner is {winner}!") sys.exit() elif len(self.gf.availableMoves) == 0: print('\n\nIt\'s a draw!') time.sleep(3) sys.exit() else: self.firstRun = False
Comments