Advertisement
Korotkodul

pygame

Sep 15th, 2022 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. import pygame
  2.  
  3. pygame.init()
  4.  
  5. RED = (255, 0, 0)
  6. GREEN = (0, 255, 0)
  7. BLUE = (0, 0, 255)
  8. WHITE = (255, 255, 255)
  9. BLACK = (0, 0, 0)
  10.  
  11. size_block = 100
  12. margin = 10
  13. width = heigth = size_block * 3 + margin * 4
  14.  
  15. size_window = (width, heigth)
  16.  
  17. screen = pygame.display.set_mode(size_window)
  18.  
  19. mtr = []
  20. for i in range(3):
  21.     m = ['a'] * 3
  22.     mtr.append(m)
  23. #print(mtr)
  24. #
  25.  
  26. def check():
  27.     for i in range(3):
  28.         if mtr[i][0] == mtr[i][1] == mtr[i][2]:
  29.             if mtr[i][0] == 'x':
  30.                 return 'x'
  31.             elif mtr[i][0] == '0':
  32.                 return '0'
  33.  
  34.     for j in range(3):
  35.         if mtr[0][j] == mtr[1][j] == mtr[2][j]:
  36.             if mtr[0][j] == 'x':
  37.                 return 'x'
  38.             elif mtr[0][j] == '0':
  39.                 return '0'
  40.  
  41.     if mtr[0][0] == mtr[1][1] == mtr[2][2]:
  42.         if mtr[0][0] == 'x':
  43.             return 'x'
  44.         elif mtr[0][0] == '0':
  45.             return '0'
  46.  
  47.     if mtr[0][2] == mtr[1][1] == mtr[2][0]:
  48.         if mtr[0][2] == 'x':
  49.             return 'x'
  50.         elif mtr[0][2] == '0':
  51.             return '0'
  52.  
  53.     draw = True
  54.     for i in range(3):
  55.         for j in range(3):
  56.             if mtr[i][j] == 'a':
  57.                 draw = False
  58.     if draw:
  59.         return 'draw'
  60.  
  61.     return 'a'
  62. cnt = 0
  63. over = False
  64. while True:
  65.     for event in pygame.event.get():
  66.         """if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
  67.            mtr = []
  68.            for i in range(3):
  69.                m = ['a'] * 3
  70.                mtr.append(m)
  71.            over = False"""
  72.         if event.type == pygame.QUIT:
  73.             pygame.quit()
  74.         elif over:
  75.             pygame.quit()
  76.         elif event.type == pygame.MOUSEBUTTONDOWN:
  77.             x, y = pygame.mouse.get_pos()
  78.             #print("x y = ", x, y)
  79.             col = x // (size_block + margin)
  80.             row = y // (size_block + margin)
  81.             print("cnt = ", cnt)
  82.  
  83.             if mtr[col][row] == 'a':
  84.                 if cnt == 0:
  85.                     mtr[col][row] = 'x'
  86.                 else:
  87.                     mtr[col][row] = '0'
  88.             #print("col row = ", col, row)
  89.             cnt = (cnt + 1) % 2
  90.  
  91.  
  92.             #print(mtr)
  93.  
  94.     for i in range(3):
  95.         for j in range(3):
  96.             x = i * size_block + (i + 1) * margin
  97.             y = j * size_block + (j + 1) * margin
  98.             color = WHITE
  99.             if mtr[i][j] == '0':
  100.                 color = BLUE
  101.             elif mtr[i][j] == 'x':
  102.                 color = RED
  103.             pygame.draw.rect(screen, color, (x, y, size_block, size_block))
  104.             if color == RED:
  105.                 pygame.draw.line(screen, WHITE, (x,y), (x+size_block, y+size_block))
  106.                 pygame.draw.line(screen, WHITE, (x + size_block, y), (x, y + size_block))
  107.             elif color == BLUE:
  108.                 pygame.draw.circle(screen, WHITE, (x + size_block / 2, y + size_block / 2), size_block / 2 - 30)
  109.             pygame.display.update()
  110.  
  111.             res = check()
  112.             final = ""
  113.             if res == 'x':
  114.                 final = "Cross wins"
  115.             elif res == '0':
  116.                 final = "Zero wins"
  117.             elif res == 'draw':
  118.                 final = "DRAW"
  119.  
  120.             if final != "":
  121.                 over = True
  122.                 screen.fill(BLACK)
  123.                 font = pygame.font.SysFont('stxingkai', 80)
  124.                 text1 = font.render(final, True, WHITE)
  125.                 text_rect = text1.get_rect()
  126.                 text_x = screen.get_width() / 2 - text_rect.width / 2
  127.                 text_y = screen.get_height() / 2 - text_rect.height / 2
  128.                 screen.blit(text1, [text_x, text_y])
  129.  
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement