overactive

hgf

Jul 28th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.98 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import time
  4.  
  5. WIDTH, HEIGHT = 600, 700
  6. WHITE = (255, 255, 255)
  7. PINK = (255,0,120)
  8. board = []
  9. END = False
  10. openWindow = True
  11.  
  12. click = (0,0,0)
  13. clickPos = [0,0]
  14. player = 'X'
  15.  
  16. 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))
  17.  
  18. canvas = pygame.display.set_mode((WIDTH,HEIGHT))
  19. pygame.display.set_caption('TicTacToe')
  20. pygame.init()
  21. font = pygame.font.SysFont('arial', 50)
  22. font2 = pygame.font.SysFont('arial', 100)
  23.  
  24. def printBoard():
  25.     pygame.draw.line(canvas, WHITE, [200, 100], [200,700], 2)
  26.     pygame.draw.line(canvas, WHITE, [400, 100], [400,700], 2)
  27.     pygame.draw.line(canvas, WHITE, [0, 100], [600,100], 2)
  28.     pygame.draw.line(canvas, WHITE, [0, 300], [600,300], 2)
  29.     pygame.draw.line(canvas, WHITE, [0, 500], [600,500], 2)
  30.    
  31. def printTurn():
  32.     text = 'Player:' + str(player)
  33.     canvas.fill((0,0,0), (0,0,600,100))
  34.     textSet = font.render(text, True, (PINK))
  35.     canvas.blit(textSet,(20,20))
  36.  
  37. def printEvent(winner):
  38.     text = ''
  39.     if takenPos == True:
  40.         text = str(player) + ': choose again'
  41.     elif takenPos == False:
  42.         text = 'Player:' + str(player)
  43.     if winner == 'X' or winner == 'O':
  44.         text = 'WINNER: ' + str(winner)
  45.     elif winner == 'TIE':
  46.         text = 'TIE: NOBODY WINS'
  47.  
  48.     canvas.fill((0,0,0), (0,0,600,100))
  49.     textSet = font.render(text, True, (PINK))
  50.     canvas.blit(textSet,(20,20))
  51.  
  52. def getClickPos():
  53.     global clickPos
  54.     global board
  55.     global takenPos
  56.  
  57.     x, y = pygame.mouse.get_pos()
  58.     x = int(x / 200)
  59.     y = int((y-100) / 200)
  60.  
  61.     if board[x][y]==0:
  62.         takenPos = False
  63.         board[x][y] = player
  64.     else:
  65.         takenPos = True
  66.         board[x][y]==0
  67.  
  68.     if x == 0:
  69.         if y == 0:
  70.             clickPos = [100, 200]
  71.         if y == 1:
  72.             clickPos = [100, 400]
  73.         if y == 2:
  74.             clickPos = [100, 600]
  75.     if x == 1:
  76.         if y == 0:
  77.             clickPos = [300, 200]
  78.         if y == 1:
  79.             clickPos = [300, 400]
  80.         if y == 2:
  81.             clickPos = [300, 600]
  82.     if x == 2:
  83.         if y == 0:
  84.             clickPos = [500, 200]
  85.         if y == 1:
  86.             clickPos = [500, 400]
  87.         if y == 2:
  88.             clickPos = [500, 600]
  89.  
  90.     return x, y, takenPos
  91.  
  92. def printPlayer(player, clickPos, x, y):
  93.     if board[x][y] == player:
  94.    
  95.         if player == 'O':
  96.             pygame.draw.circle(canvas, WHITE, clickPos, 50, 5)
  97.             player = 'X'
  98.          
  99.         elif player == 'X':
  100.             pygame.draw.line(canvas, WHITE, [clickPos[0] - 50,clickPos[1] + 50], [clickPos[0] + 50,clickPos[1] - 50], 5)
  101.             pygame.draw.line(canvas, WHITE, [clickPos[0] + 50,clickPos[1] + 50], [clickPos[0] - 50,clickPos[1] - 50], 5)
  102.             player = 'O'
  103.      
  104.     return player
  105.  
  106. def getWinner():
  107.     global winner
  108.     winComb = []
  109.     turnCounter = 0
  110.    
  111.     END = False
  112.  
  113.     for i in range(len(board)):
  114.         for j in range(len(board[i])):
  115.             winComb.append(board[i][j])
  116.  
  117.     for i in range(len(board)):
  118.         for j in range(len(board[i])):
  119.             if board[i][j] == 'X' or board[i][j] == 'O':
  120.                 turnCounter += 1
  121.     if turnCounter > 8:
  122.         winner = 'TIE'
  123.         END = True
  124.    
  125.     for i in win_commbinations:
  126.         if winComb[i[0]] == winComb[i[1]] == winComb[i[2]] == player:
  127.             winner = player
  128.             END = True
  129.     return winner, END
  130.  
  131. def quitGame():
  132.     for event in pygame.event.get():
  133.             if event.type == pygame.QUIT:
  134.                 sys.exit(0)
  135.                 END = False
  136.  
  137.  
  138. def theGame():  
  139.     openWindow = True
  140.  
  141.     global board
  142.     board = [[0,0,0],[0,0,0],[0,0,0]]
  143.    
  144.     global winner
  145.     winner = ''
  146.    
  147.     global takenPos
  148.     takenPos = False
  149.    
  150.     global player
  151.     global END
  152.  
  153.     while  True:
  154.         printBoard()
  155.         if winner == '' and takenPos == False:
  156.             printTurn()
  157.  
  158.         for event in pygame.event.get():
  159.             if event.type == pygame.MOUSEBUTTONUP:
  160.                 x, y, takenPos = getClickPos()
  161.                 winner, END = getWinner()
  162.                 player = printPlayer(player, clickPos, x, y)
  163.                 printEvent(winner)
  164.  
  165.         pygame.display.flip()
  166.        
  167.         if END:
  168.             time.sleep(1)
  169.         while END == True:
  170.             canvas.fill((0,0,0,))
  171.             text = 'The End press P to play again press Q to quit'
  172.             textSet = font.render(text, True, (PINK))
  173.             canvas.blit(textSet,(20,20))
  174.             pygame.display.flip()
  175.            
  176.             quitGame()
  177.             for event in pygame.event.get():
  178.                 if event.type == pygame.KEYDOWN:
  179.                     if event.key == pygame.K_p:
  180.                         END = False
  181.                         theGame()
  182.                     elif event.key == pygame.K_q:
  183.                         openWindow = False
  184.                         END = False
  185.     quitGame()
  186. theGame()
Advertisement
Add Comment
Please, Sign In to add comment