overactive

jdjdjd

Jul 26th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. import pygame
  2. import sys
  3.  
  4. WIDTH, HEIGHT = 600, 700
  5. WHITE = (255, 255, 255)
  6. PINK = (255,0,120)
  7. END = False
  8. openWindow = True
  9.  
  10. click = (0,0,0)
  11. clickPos = [0,0]
  12. player = 'X'
  13. WIN=''
  14. board = [[0,0,0],[0,0,0],[0,0,0]]
  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.  
  21. def printBoard():
  22.     canvas.fill((0,0,0), (0,0,600,100))
  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.     if WIN == 'X' or WIN == 'O':
  30.         text = ('WINNER: ', WIN)
  31.     elif WIN == 'TIE':
  32.         text = 'TIE: NOBODY WINS'
  33.     else:
  34.         text = ('Player: ', str(player))
  35.    
  36.     textSet = font.render(''.join(text), True, (255, 0, 100))
  37.     canvas.blit(textSet,(20,20))
  38.  
  39. def whereClicked():
  40.     x, y = pygame.mouse.get_pos()
  41.     global clickPos
  42.  
  43.     if 0 < x < 200 and 100 < y < 300:
  44.         clickPos = [100, 200]
  45.         board[0][0] = player
  46.     elif 0 < x < 200 and 300 < y < 500:
  47.         clickPos = [100, 400]
  48.         board[1][0] = player
  49.     elif 0 < x < 200 and 500 < y < 700:
  50.         clickPos = [100, 600]
  51.         board[2][0] = player
  52.    
  53.     elif 200 < x < 400 and 100 < y < 300:
  54.         clickPos = [300, 200]
  55.         board[0][1] = player
  56.     elif 200 < x < 400 and 300 < y < 500:
  57.         clickPos = [300, 400]
  58.         board[1][1] = player
  59.     elif 200 < x < 400 and 500 < y < 700:
  60.         clickPos = [300, 600]
  61.         board[2][1] = player
  62.    
  63.     elif 400 < x < 600 and 100 < y < 300:
  64.         clickPos = [500, 200]
  65.         board[0][2] = player
  66.     elif 400 < x < 600 and 300 < y < 500:
  67.         clickPos = [500, 400]
  68.         board[1][2] = player
  69.     elif 400 < x < 600 and 500 < y < 700:
  70.         clickPos = [500, 600]
  71.         board[2][2] = player
  72.  
  73. def printPlayer(player, clickPos):
  74.     if clickPos != [0, 0]:
  75.         if player == 'O':
  76.             pygame.draw.circle(canvas, WHITE, clickPos, 50, 5)
  77.             player = 'X'
  78.  
  79.         elif player == 'X':
  80.             pygame.draw.line(canvas, WHITE, [clickPos[0] - 50,clickPos[1] + 50], [clickPos[0] + 50,clickPos[1] - 50], 5)
  81.             pygame.draw.line(canvas, WHITE, [clickPos[0] + 50,clickPos[1] + 50], [clickPos[0] - 50,clickPos[1] - 50], 5)
  82.             player = 'O'
  83.  
  84.         return player
  85.  
  86. def checkWin():
  87.     global WIN
  88.  
  89.     if board[0][0] == board[0][1] == board[0][2] == player:
  90.         WIN = player
  91.     elif board[1][0] == board[1][1] == board[1][2] == player:
  92.         WIN = player
  93.     elif board[2][0] == board[2][1] == board[2][2] == player:
  94.         WIN = player
  95.     elif board[0][0] == board[1][1] == board[2][2] == player:
  96.         WIN = player
  97.     elif board[0][2] == board[1][1] == board[2][0] == player:
  98.         WIN = player
  99.     elif board[0][0] == board[1][0] == board[2][0] == player:
  100.         WIN = player
  101.     elif board[0][1] == board[1][1] == board[2][1] == player:
  102.         WIN = player
  103.     elif board[0][2] == board[1][2] == board[2][2] == player:
  104.         WIN = player
  105.    
  106.     turnCounter = 0
  107.     for i in range(len(board)):
  108.         for j in range(len(board[i])):
  109.             if board[i][j] == 'X' or board[i][j] == 'O':
  110.                 turnCounter += 1
  111.     print(turnCounter)
  112.     if turnCounter > 8:
  113.         WIN = 'TIE'
  114.    
  115. while openWindow == True:
  116.     printBoard()
  117.    
  118.     for event in pygame.event.get():
  119.         if event.type == pygame.QUIT:
  120.             openWindow = False
  121.  
  122.         elif event.type == pygame.MOUSEBUTTONUP:
  123.             whereClicked()
  124.             checkWin()
  125.             player = printPlayer(player, clickPos)
  126.            
  127.     pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment