overactive

daff

Jul 28th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.87 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 = [[0,0,0],[0,0,0],[0,0,0]]
  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.    
  33. def printHeader(winner):
  34.     if winner == 'X' or winner == 'O':
  35.         text = 'WINNER: ' + str(winner)
  36.     elif winner == 'TIE':
  37.         text = 'TIE: NOBODY WINS'
  38.     else:
  39.         text = 'Player:' + str(player)
  40.     if takenPos == True:
  41.         text = str(player) + ': choose again'
  42.  
  43.     canvas.fill((0,0,0), (0,0,600,100))
  44.     textSet = font.render(text, True, (PINK))
  45.     canvas.blit(textSet,(20,20))
  46.  
  47. def getClickPos():
  48.     global clickPos
  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 player
  130.     global takenPos
  131.     global END
  132.  
  133.     while openWindow == True:
  134.         printBoard()
  135.         for event in pygame.event.get():
  136.             if event.type == pygame.QUIT:
  137.                 openWindow = False
  138.  
  139.             elif event.type == pygame.MOUSEBUTTONUP:
  140.                 x, y, takenPos = getClickPos()
  141.                 winner, END = getWinner()
  142.                 player = printPlayer(player, clickPos, x, y)
  143.                 printHeader(winner)
  144.  
  145.         pygame.display.flip()
  146.  
  147.         while END == True:
  148.             canvas.fill((0,0,0,))
  149.             text = 'The End press P to play again press Q to quit'
  150.             textSet = font.render(text, True, (PINK))
  151.             canvas.blit(textSet,(20,20))
  152.             pygame.display.update()
  153.  
  154.             for event in pygame.event.get():
  155.                 if event.type == pygame.QUIT:
  156.                     openWindow = False
  157.  
  158.                 if event.type == pygame.KEYDOWN:
  159.                     if event.key == pygame.K_p:
  160.                         END = False
  161.                         theGame()
  162.                     elif event.key == pygame.K_q:
  163.                         openWindow = False
  164.                         END = False
  165.                
  166. theGame()
  167.      
  168.    
  169.  
  170. ####################################################
  171. """
  172. - bug #2 - po wygranej grze dalej można uzupełniać planszę
  173. - po zakonczonej grze brak możliwości ponownej gry
  174. """
  175. ####################################################
Advertisement
Add Comment
Please, Sign In to add comment