overactive

sfvjh

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