overactive

khsafb

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