Advertisement
lenyaplay

Tic Tac Toe

Sep 4th, 2021
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. # pip install pygame and pip install numpy
  2. import pygame
  3. from pygame import *
  4. from pygame.font import Font
  5. from pygame import gfxdraw
  6. import numpy as np
  7.  
  8. pygame.init()
  9. size = 300
  10. screen = pygame.display.set_mode([size] * 2)
  11. pygame.display.set_caption("Tic Tac Toe")
  12. font = Font('./Fonts/Roboto-Light.ttf', 30)
  13.  
  14.  
  15. class TicTacToe():
  16.     field = []
  17.     field_rect = []
  18.  
  19.     text = font.render("", True, Color("black"))
  20.     padding = 10
  21.     turn = 1  # X - first
  22.     end = False
  23.  
  24.     def __init__(self):
  25.         padding = self.padding
  26.         fSize = size / 3 - padding / 2
  27.         self.field = np.zeros([3, 3])
  28.         self.field_rect = np.zeros([3, 3, 4])
  29.  
  30.         for i in range(3):
  31.             for j in range(3):
  32.                 # j, i - because i will be column, and j will be row
  33.                 self.field_rect[j, i] = np.array(
  34.                     [i * fSize + padding, j * fSize + padding, fSize - padding, fSize - padding])
  35.  
  36.     def Event(self, event):
  37.         pos = pygame.mouse.get_pos()
  38.         for i in range(3):
  39.             for j in range(3):
  40.                 if Rect(self.field_rect[i, j]).collidepoint(pos) and not self.field[i][j]:
  41.                     self.field[i][j] = self.turn
  42.                     self.turn *= -1
  43.  
  44.         self.CheckAndDoWin()
  45.  
  46.     def CheckAndDoWin(self):
  47.         check_array = [*self.field.sum(axis=1), *self.field.T.sum(axis=1),
  48.                        self.field.trace(), np.trace(self.field[:][::-1])]
  49.  
  50.         self.end = True
  51.         str = ""
  52.         if 3 in check_array:
  53.             str = "Win X"
  54.         elif -3 in check_array:
  55.             str = "Win O"
  56.         elif 0 not in self.field:
  57.             str = "Standoff"
  58.  
  59.         self.end = bool(str) #or len(str)
  60.         self.text = font.render(str, True, Color("black"))
  61.  
  62.     def DrawO(self, x, y, r, color, width=1):
  63.         for i in range(0, width):
  64.             gfxdraw.aacircle(screen, x, y, r + i - width // 2, color)
  65.  
  66.     def DrawX(self, x1, x2, y1, y2, color, width=1):
  67.         pygame.draw.line(screen, color, [x1, y1], [x2, y2], width)
  68.         pygame.draw.line(screen, color, [x2, y1], [x1, y2], width)
  69.  
  70.     def Draw(self):
  71.         if self.end:
  72.             screen.blit(self.text, (np.array([size, size]) - np.array(self.text.get_size())) / 2)
  73.             return
  74.  
  75.         for rect in self.field_rect.reshape(3 * 3, 4):
  76.             gfxdraw.rectangle(screen, rect, Color("black"))
  77.  
  78.         line_width = 2
  79.         for i in range(0, 3):
  80.             for j in range(0, 3):
  81.                 rect = Rect(self.field_rect[i, j])
  82.                 if self.field[i][j] == 1:
  83.                     x1, y1 = rect.topleft
  84.                     x2, y2 = rect.bottomright
  85.                     self.DrawX(x1, x2, y1, y2, Color("black"), line_width)
  86.                 if self.field[i][j] == -1:
  87.                     self.DrawO(*rect.center, rect.width // 2, Color("black"), line_width)
  88.  
  89.  
  90. Running = True
  91. Clock = pygame.time.Clock()
  92. game = TicTacToe()
  93.  
  94. while Running:
  95.     screen.fill(Color("white"))
  96.     for event in pygame.event.get():
  97.         if event.type == QUIT:
  98.             Running = False
  99.         if event.type == MOUSEBUTTONDOWN and game.end:
  100.             game = TicTacToe()
  101.         elif event.type == MOUSEBUTTONDOWN:
  102.             game.Event(event)
  103.  
  104.     game.Draw()
  105.     Clock.tick(60)
  106.     pygame.display.flip()
  107.  
  108. pygame.quit()
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement