Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- import time
- WIDTH, HEIGHT = 600, 700
- WHITE = (255, 255, 255)
- PINK = (255,0,120)
- board = []
- openWindow = True
- click = (0,0,0)
- clickPos = [0,0]
- win_commbinations = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))
- canvas = pygame.display.set_mode((WIDTH,HEIGHT))
- pygame.display.set_caption('TicTacToe')
- pygame.init()
- font = pygame.font.SysFont('arial', 50)
- font2 = pygame.font.SysFont('arial', 100)
- def printText(text):
- canvas.fill((0,0,0), (0,0,600,100))
- textSet = font.render(text, True, (PINK))
- canvas.blit(textSet,(20,20))
- def printBoard():
- pygame.draw.line(canvas, WHITE, [200, 100], [200,700], 2)
- pygame.draw.line(canvas, WHITE, [400, 100], [400,700], 2)
- pygame.draw.line(canvas, WHITE, [0, 100], [600,100], 2)
- pygame.draw.line(canvas, WHITE, [0, 300], [600,300], 2)
- pygame.draw.line(canvas, WHITE, [0, 500], [600,500], 2)
- def printTurn(player):
- text = 'Player:' + str(player)
- printText(text)
- def printEvent(winner):
- text = ''
- if takenPos == True:
- text = str(player) + ': choose again'
- elif takenPos == False:
- text = 'Player:' + str(player)
- if winner == 'X' or winner == 'O':
- text = 'WINNER: ' + str(winner)
- elif winner == 'TIE':
- text = 'TIE: NOBODY WINS'
- printText(text)
- def getClickPos():
- global clickPos
- global board
- global takenPos
- x, y = pygame.mouse.get_pos()
- if 100 < y < HEIGHT:
- x = int(x / 200)
- y = int((y-100) / 200)
- if board[x][y]==0:
- takenPos = False
- board[x][y] = player
- else:
- takenPos = True
- if x == 0:
- if y == 0:
- clickPos = [100, 200]
- if y == 1:
- clickPos = [100, 400]
- if y == 2:
- clickPos = [100, 600]
- if x == 1:
- if y == 0:
- clickPos = [300, 200]
- if y == 1:
- clickPos = [300, 400]
- if y == 2:
- clickPos = [300, 600]
- if x == 2:
- if y == 0:
- clickPos = [500, 200]
- if y == 1:
- clickPos = [500, 400]
- if y == 2:
- clickPos = [500, 600]
- else:
- ???????????????????
- return x, y, takenPos
- def printPlayer(player, clickPos, x, y):
- if board[x][y] == player:
- if player == 'O':
- pygame.draw.circle(canvas, WHITE, clickPos, 50, 5)
- player = 'X'
- elif player == 'X':
- pygame.draw.line(canvas, WHITE, [clickPos[0] - 50,clickPos[1] + 50], [clickPos[0] + 50,clickPos[1] - 50], 5)
- pygame.draw.line(canvas, WHITE, [clickPos[0] + 50,clickPos[1] + 50], [clickPos[0] - 50,clickPos[1] - 50], 5)
- player = 'O'
- return player
- def getWinner():
- global winner
- END = False
- winComb = []
- turnCounter = 0
- for i in range(len(board)):
- for j in range(len(board[i])):
- winComb.append(board[i][j])
- for i in range(len(board)):
- for j in range(len(board[i])):
- if board[i][j] == 'X' or board[i][j] == 'O':
- turnCounter += 1
- if turnCounter > 8:
- winner = 'TIE'
- END = True
- for i in win_commbinations:
- if winComb[i[0]] == winComb[i[1]] == winComb[i[2]] == player:
- winner = player
- END = True
- return winner, END
- def quitGame():
- pygame.quit()
- sys.exit(0)
- def theGame():
- openWindow = True
- global board
- board = [[0,0,0],[0,0,0],[0,0,0]]
- global winner
- winner = ''
- global takenPos
- takenPos = False
- global player
- player = 'X'
- END = False
- while True:
- printBoard()
- if winner == '' and takenPos == False:
- printTurn(player)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- quitGame()
- if event.type == pygame.MOUSEBUTTONUP:
- x, y, takenPos = getClickPos()
- winner, END = getWinner()
- player = printPlayer(player, clickPos, x, y)
- printEvent(winner)
- pygame.display.flip()
- if END:
- time.sleep(1)
- while END == True:
- canvas.fill((0,0,0,))
- line1 = 'The End'
- line2 = 'The winner is: ' + str(winner)
- line3 = 'press P to play again'
- line4 = 'press Q to quit'
- textSet1 = font.render(line1, True, (PINK))
- textSet2 = font.render(line2, True, (PINK))
- textSet3 = font.render(line3, True, (PINK))
- textSet4 = font.render(line4, True, (PINK))
- canvas.blit(textSet1,(20,20))
- canvas.blit(textSet2,(20,80))
- canvas.blit(textSet3,(20,140))
- canvas.blit(textSet4,(20,200))
- pygame.display.flip()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- quitGame()
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_p:
- return
- elif event.key == pygame.K_q:
- quitGame()
- while True:
- canvas.fill((0,0,0))
- theGame()
Advertisement
Add Comment
Please, Sign In to add comment