import pygame from pygame.locals import * #makes the tic tac toe board def initBoard(ttt): background = pygame.Surface(ttt.get_size()) background = background.convert() background.fill((255, 255, 255)) #verticle lines pygame.draw.line (background, (0,0,0), (100, 0), (100, 300), 2) pygame.draw.line (background, (0,0,0), (200, 0), (200, 300), 2) #horizontal lines pygame.draw.line (background, (0,0,0), (0, 100), (300, 100), 2) pygame.draw.line (background, (0,0,0), (0, 200), (300, 200), 2) return background #Draws the board onto the screen def showBoard(ttt, board): #redraws the board (board) on the screen (ttt) ttt.blit(board, (0,0)) pygame.display.flip() #determines the mouse cordinates and translates into the board's row and columb def boardPos(mouseX, MouseY): #determine the row the user clicked if (mouseY < 100): row = 0 elif (mouseY < 200): row = 1 else: row = 2 #determine the column the user clicked if (mouseX < 100): row = 0 elif (mouseX < 200): row = 1 else: row = 2 #return the row and column return (row, col) #determine where the user clicked on the board and draw X or O def clickBoard (board): global grid, XO (mouseX, mouseY) = pygame.mouse.get_pos() (row, col) = boardPos(mouseX, mouseY) #make sure this space isnt used if ((grid[row][col] == "X") or (grid[row][col] == "O")): return #draw an X or O drawMove(board,row,col,XO) #toggle other player's move if (XO == "X"): XO = "O" else: XO = "X" #determines where the curser is and draws an x or an o when left clicked def drawMove(board, boardRow, boardCol, Piece): #determine the center of the space centerX = boardCol * 100 + 50 centerY = boardRow * 100 + 50 #draw the peice if (Peice == "O"): pygame.draw.circle(board, (0,0,0), (centerX, centerY), 44, 2) else: pygame.draw.line(board, (0,0,0), (centerX - 22, centerY - 22), \ (centerX + 22, centerY +22), 2) pygame.draw.line(board, (0,0,0), (centerX + 22, centerY - 22), \ (centerX - 22, centerY + 22), 2) #mark the board space as used grid[boardRow][boardCol] = Peice #tracks which spaces have been used and who's turn it is XO = "X" #declares an empty grid grid = [[None, None, None], [None, None, None], [None, None, None]] def gameWon(board): # determine if anyone has won the game # -------------------------------- # board : the game board surface global grid, winner # check for winning rows for row in range (0, 3): if ((grid [row][0] == grid[row][1] == grid[row][2]) and \ (grid [row][0] is not None)): # this row won winner = grid[row][0] pygame.draw.line (board, (250,0,0), (0, (row + 1)*100 - 50), \ (300, (row + 1)*100 - 50), 2) break # check for winning columns for col in range (0, 3): if (grid[0][col] == grid[1][col] == grid[2][col]) and \ (grid[0][col] is not None): # this column won winner = grid[0][col] pygame.draw.line (board, (250,0,0), ((col + 1)* 100 - 50, 0), \ ((col + 1)* 100 - 50, 300), 2) break # check for diagonal winners if (grid[0][0] == grid[1][1] == grid[2][2]) and \ (grid[0][0] is not None): # game won diagonally left to right winner = grid[0][0] pygame.draw.line (board, (250,0,0), (50, 50), (250, 250), 2) if (grid[0][2] == grid[1][1] == grid[2][0]) and \ (grid[0][2] is not None): # game won diagonally right to left winner = grid[0][2] pygame.draw.line (board, (250,0,0), (250, 50), (50, 250), 2) pygame.init() ttt = pygame.display.set_mode((300, 325)) pygame.display.set_caption = ('Tic Tac Toe') running = 1 board = initBoard(ttt) while(running ==1): for event in pygame.event.get(): if event.type is QUIT: running = 0 elif event.type is MOUSEBUTTONDOWN: clickBoard(board) #checks if game has a winner gameWon(board) #updates the display showBoard(ttt, board) ############################################ #Traceback (most recent call last): # File "/home/surgy/pygame learning scripts/tictactoe.py", line 179, in ? # clickBoard(board) # File "/home/surgy/pygame learning scripts/tictactoe.py", line 51, in clickBoard # (row, col) = boardPos(mouseX, mouseY) # File "/home/surgy/pygame learning scripts/tictactoe.py", line 31, in boardPos # if (mouseY < 100): #NameError: global name 'mouseY' is not defined