Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, sys, time, math
- from pygame.locals import *
- # Create gameloop, and prompt players for their turn
- def StartGame():
- global PlayerTurn
- global GameOver
- Display()
- while ~GameOver: # While game is not over
- for event in pygame.event.get():
- # handle MOUSEBUTTONUP
- if (event.type == pygame.MOUSEBUTTONUP):
- position = pygame.mouse.get_pos()
- gameboardSpot = findSpotPosition(position)
- try:
- # if gameboardSpot already taken, ignore action and return
- index = findGameboardIndex(gameboardSpot)
- if (board[index[0]][index[1]] != 0):
- break
- claimSpot(PlayerTurn, gameboardSpot)
- except Exception as error:
- print(error)
- if (event.type == QUIT):
- pygame.quit()
- sys.exit()
- pygame.display.update()
- # display text game board
- def Display():
- print("\n")
- for group in board:
- word = ""
- for state in group:
- word += str(state) + " "
- print(word)
- def Move(x, y, s): # int, int, State(int)
- global MoveCount
- if (board[x][y] == 0): # if state is empty
- board[x][y] = s
- MoveCount += 1
- CheckWinner(x, y, s)
- # Return the spot position of the mouse click
- def findSpotPosition(pos_XY):
- spotWidth = 100
- spotHeight = 100
- index = 0
- # find which range the mouse click landed in, and return the spot position
- for array in spotsPosition:
- if (array[0] <= pos_XY[0] <= array[0] + spotWidth) and (array[1] <= pos_XY[1] <= array[1] + spotHeight):
- return index + 1
- index += 1
- return 0
- # Claim a spot on the gameboard for a player
- def claimSpot(playerNumber, gameboardSpot):
- global PlayerTurn
- global MoveCount
- global GameOver
- # index holds the x, y values of the spot that is trying to be claimed on the gameboard
- index = findGameboardIndex(gameboardSpot)
- # After we know what spot we want claimed, we check to see if it is valid
- # If the spot is not valid, prompt for a new gameboardSpot
- # If the spot is valid, claim and redisplay the gameboard
- if (board[index[0]][index[1]] != 0):
- #gameboardSpot = int(input("\r\nSpot taken, choose a new one: "))
- claimSpot(playerNumber, gameboardSpot)
- else: # SPOT CLAIM SUCESSFUL
- board[index[0]][index[1]] = playerNumber
- drawLetter(gameboardSpot)
- CheckWinner(index[0], index[1], PlayerTurn)
- if (PlayerTurn == 1):
- PlayerTurn = 2
- elif (PlayerTurn == 2):
- PlayerTurn = 3
- elif (PlayerTurn == 3):
- PlayerTurn = 1
- font = pygame.font.Font('freesansbold.ttf', 24)
- text2 = font.render('Player #' + str(PlayerTurn) +'\'s turn', True, WHITE, GREY)
- DISPLAY.blit(text2, textRect2)
- Display()
- if (MoveCount == (math.pow(BoardLength, 2) - 1)):
- print("\nDRAWWWWW \n\n\n\n\n\n\n\n")
- text2 = font.render("Game Over! It's a draw!", True, WHITE, GREY)
- DISPLAY.blit(text2, textRect2)
- if (GameOver == True):
- print("win row #" + str(PlayerTurn) + "WINNER WINNNER \n\n\n\n\n\n\n\n")
- text2 = font.render("Game Over! Player #" + str(PlayerTurn) + " has won!", True, WHITE, GREY)
- MoveCount += 1
- # index1, and index2 uses position to determine which spot is trying to be claimed from gameboard
- def findGameboardIndex(position):
- if (1 <= position <= 5):
- index1 = 0
- index2 = position - 1
- elif (6 <= position <= 10):
- index1 = 1
- index2 = position - 1 - 5
- elif (11 <= position <= 15):
- index1 = 2
- index2 = position - 1 - 5 - 5
- elif (16 <= position <= 20):
- index1 = 3
- index2 = position - 1 - 5 - 5 - 5
- elif (21 <= position <= 25):
- index1 = 4
- index2 = position - 1 - 5 - 5 - 5 - 5
- return [index1, index2]
- def drawLetter(position):
- global PlayerTurn
- letter = 'Oops'
- color = BLACK
- if (PlayerTurn == 1):
- letter = 'X'
- color = RED
- elif (PlayerTurn == 2):
- letter = 'O'
- color = BLUE
- elif (PlayerTurn == 3):
- letter = 'Y'
- color = GREEN
- # Find the coordinates for the X or O we are drawing for
- placement = spotsPosition[position - 1]
- # Render, and position the two text objects
- font = pygame.font.Font('freesansbold.ttf', 80)
- placement = (placement[0], placement[1])
- text = font.render(letter, True, color, GREY)
- textRect = text.get_rect()
- textRect.center = (placement[0] + 50, placement[1] + 55)
- DISPLAY.blit(text, textRect)
- def CheckWinner(x, y, s):
- global Winlength
- global MoveCount
- global GameOver
- #check end conditions
- #check row
- for i in range (BoardLength):
- if(board[x][i] != s):
- break
- if(i == Winlength):
- print("win row #" + str(s) + "WINNER WINNNER \n\n\n\n\n\n\n\n")
- text2 = font.render("Game Over! Player #" + str(PlayerTurn) + " has won!", True, WHITE, GREY)
- GameOver = True
- #report win for s
- #check rocolw
- for i in range (BoardLength):
- if(board[i][y] != s):
- break
- if(i == Winlength):
- print("win col #" + str(s) + "WINNER WINNNER \n\n\n\n\n\n\n\n")
- text2 = font.render("Game Over! Player #" + str(PlayerTurn) + " has won!", True, WHITE, GREY)
- GameOver = True
- #report win for s
- #check diag
- if (x == y):
- #we're on a diagonal
- for i in range (BoardLength):
- if (board[i][i] != s):
- break
- if (i == Winlength):
- print("win diag #" + str(s) + "WINNER WINNNER \n\n\n\n\n\n\n\n")
- text2 = font.render("Game Over! Player #" + str(PlayerTurn) + " has won!", True, WHITE, GREY)
- GameOver = True
- #report win for s
- #check anti diag
- if (x + y == BoardLength - 1):
- for i in range (BoardLength):
- if (board[i][(BoardLength - 1) - i] != s):
- break
- if (i == Winlength):
- print("win anti-diag #" + str(s) + "WINNER WINNNER \n\n\n\n\n\n\n\n")
- text2 = font.render("Game Over! Player #" + str(PlayerTurn) + " has won!", True, WHITE, GREY)
- GameOver = True
- #report win for s
- #check draw
- if (MoveCount == (math.pow(BoardLength, 2) - 1)):
- print("\nDRAWWWWW \n\n\n\n\n\n\n\n")
- text2 = font.render("Game Over! It's a draw!", True, WHITE, GREY)
- DISPLAY.blit(text2, textRect2)
- GameOver = True
- #report draw
- # Main
- # Start PyGame
- pygame.init()
- DISPLAY = pygame.display.set_mode((500, 550), 0, 32)
- pygame.display.set_caption('Tic Tac Toe')
- # Colors
- RED = (255, 0, 0)
- GREEN = ( 0, 255, 0)
- BLUE = ( 0, 0, 255)
- BLACK = ( 0, 0, 0)
- BROWN = (180, 150, 110)
- GREY = (80, 80, 80)
- WHITE = (255, 255, 255)
- # Draw TicTacToe lines, and background
- DISPLAY.fill(GREY)
- xPos = 0
- yPos = 50
- height = 3
- length = 500
- for x in range(6): # Draw Horrizontal Lines
- pygame.draw.rect(DISPLAY, BLACK, (xPos, yPos, length, height))
- yPos += 100
- xPos = 0
- yPos = 50
- height = 500
- length = 3
- for x in range(6): # Draw Vertical Lines
- pygame.draw.rect(DISPLAY, BLACK, (xPos, yPos, length, height))
- xPos += 100
- # Render, and position the two text objects
- X1, X2, Y = 150, 600, 50
- font = pygame.font.Font('freesansbold.ttf', 24)
- text1 = font.render('', True, BLACK, GREY)
- text2 = font.render('Player #' + "1" +'\'s turn', True, WHITE, GREY)
- textRect1, textRect2 = text1.get_rect(), text2.get_rect()
- textRect1.center = (X1 // 2, Y // 2)
- textRect2.center = (X2 // 2, Y // 2)
- DISPLAY.blit(text1, textRect1)
- DISPLAY.blit(text2, textRect2)
- # Variables to handle mouse click detection for positions
- BoardLength = 5
- xPos = 0
- yPos = 50
- spotsPosition = []
- for i in range(BoardLength):
- for j in range(BoardLength):
- spotsPosition.append([xPos, yPos])
- xPos += 100
- yPos += 100
- xPos = 0
- # Game creation
- GameOver = False
- Winlength = 3 - 1
- MoveCount = 0 # int
- PlayerTurn = 1
- board = [[0] * BoardLength for _ in range(BoardLength)] # [[int]]
- StartGame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement