mainGame.py

PHOTO EMBED

Sat Aug 06 2022 01:33:38 GMT+0000 (Coordinated Universal Time)

Saved by @projectpython

import pygame
import sys
import time
from board import Board
from gameFunctions import gamefunctions as gf


class TicTacToe:

    # Overall class to manage game behaviors
    def __init__(self):
        # Initialize the game/resources
        pygame.init()

        # Create the screen attribute, which
        # uses pygame's built in screen method
        # and call it Tic-Tac-Toe
        self.screen = pygame.display.set_mode((800, 800))
        pygame.display.set_caption("Tic-Tac-Toe")

        self.board = Board(self)
        self.gf = gf(self)
        
        self.firstRun = True
        
        # set boolean attributes storing
        # whose turn it is
        
        self.player1Move = 0
        self.player2Move = 0

        # Draw board to screen
        self.board.blitme()

    def run_game(self):
        # Start the main loop for the game
        while True:
          # Check mouse/keyboard events
          for event in pygame.event.get():
            if event.type == pygame.QUIT:
              sys.exit()

          if self.firstRun == False:
            # Take move, and draw move to the screen
            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 screen
          pygame.display.flip()
          
          # check if someone won
          if self.firstRun == False:
            isWinner = self.gf.checkWinner()
            if True in isWinner:
              print("Game 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("It's a draw!")
              time.sleep(3)
              sys.exit()
          else:
            self.firstRun = False

          


content_copyCOPY