overactive

ekgfh

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