overactive

hgkc

Jul 28th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.44 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.    
  58.     if 100 < y < HEIGHT:
  59.         x = int(x / 200)
  60.         y = int((y-100) / 200)
  61.  
  62.         if board[x][y]==0:
  63.             takenPos = False
  64.             board[x][y] = player
  65.         else:
  66.             takenPos = True
  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.     else:
  90.         ???????????????????
  91.                
  92.     return x, y, takenPos
  93.  
  94. def printPlayer(player, clickPos, x, y):
  95.    
  96.     if board[x][y] == player:
  97.    
  98.         if player == 'O':
  99.             pygame.draw.circle(canvas, WHITE, clickPos, 50, 5)
  100.             player = 'X'
  101.          
  102.         elif player == 'X':
  103.             pygame.draw.line(canvas, WHITE, [clickPos[0] - 50,clickPos[1] + 50], [clickPos[0] + 50,clickPos[1] - 50], 5)
  104.             pygame.draw.line(canvas, WHITE, [clickPos[0] + 50,clickPos[1] + 50], [clickPos[0] - 50,clickPos[1] - 50], 5)
  105.             player = 'O'
  106.      
  107.     return player
  108.  
  109. def getWinner():
  110.     global winner
  111.     END = False
  112.     winComb = []
  113.     turnCounter = 0
  114.    
  115.     for i in range(len(board)):
  116.         for j in range(len(board[i])):
  117.             winComb.append(board[i][j])
  118.  
  119.     for i in range(len(board)):
  120.         for j in range(len(board[i])):
  121.             if board[i][j] == 'X' or board[i][j] == 'O':
  122.                 turnCounter += 1
  123.     if turnCounter > 8:
  124.         winner = 'TIE'
  125.         END = True
  126.    
  127.     for i in win_commbinations:
  128.         if winComb[i[0]] == winComb[i[1]] == winComb[i[2]] == player:
  129.             winner = player
  130.             END = True
  131.     return winner, END
  132.  
  133. def quitGame():
  134.     pygame.quit()
  135.     sys.exit(0)
  136.  
  137. def theGame():  
  138.     openWindow = True
  139.     global board
  140.     board = [[0,0,0],[0,0,0],[0,0,0]]
  141.     global winner
  142.     winner = ''
  143.     global takenPos
  144.     takenPos = False
  145.     global player
  146.     player = 'X'
  147.     END = False
  148.  
  149.     while  True:
  150.         printBoard()
  151.         if winner == '' and takenPos == False:
  152.             printTurn(player)
  153.  
  154.         for event in pygame.event.get():
  155.             if event.type == pygame.QUIT:
  156.                 quitGame()
  157.             if event.type == pygame.MOUSEBUTTONUP:
  158.                 x, y, takenPos = getClickPos()
  159.                 winner, END = getWinner()
  160.                 player = printPlayer(player, clickPos, x, y)
  161.                 printEvent(winner)
  162.         pygame.display.flip()
  163.        
  164.         if END:
  165.             time.sleep(1)
  166.  
  167.         while END == True:
  168.             canvas.fill((0,0,0,))
  169.  
  170.             line1 = 'The End'
  171.             line2 = 'The winner is: ' + str(winner)
  172.             line3 = 'press P to play again'
  173.             line4 = 'press Q to quit'
  174.  
  175.             textSet1 = font.render(line1, True, (PINK))
  176.             textSet2 = font.render(line2, True, (PINK))
  177.             textSet3 = font.render(line3, True, (PINK))
  178.             textSet4 = font.render(line4, True, (PINK))
  179.  
  180.             canvas.blit(textSet1,(20,20))
  181.             canvas.blit(textSet2,(20,80))
  182.             canvas.blit(textSet3,(20,140))
  183.             canvas.blit(textSet4,(20,200))
  184.             pygame.display.flip()
  185.            
  186.             for event in pygame.event.get():
  187.                 if event.type == pygame.QUIT:
  188.                     quitGame()
  189.                 if event.type == pygame.KEYDOWN:
  190.                     if event.key == pygame.K_p:
  191.                         return
  192.                     elif event.key == pygame.K_q:
  193.                         quitGame()
  194.  
  195.  
  196. while True:
  197.     canvas.fill((0,0,0))
  198.     theGame()
Advertisement
Add Comment
Please, Sign In to add comment