Advertisement
Korotkodul

main pygame

Sep 17th, 2022
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.68 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.  
  65. while not over:
  66.     for event in pygame.event.get():
  67.         """if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
  68.            mtr = []
  69.            for i in range(3):
  70.                m = ['a'] * 3
  71.                mtr.append(m)
  72.            over = False"""
  73.         if event.type == pygame.QUIT:
  74.             pygame.quit()
  75.         elif over:
  76.             pygame.quit()
  77.         elif event.type == pygame.MOUSEBUTTONDOWN:
  78.             x, y = pygame.mouse.get_pos()
  79.             #print("x y = ", x, y)
  80.             col = x // (size_block + margin)
  81.             row = y // (size_block + margin)
  82.             print("cnt = ", cnt)
  83.  
  84.             if mtr[col][row] == 'a':
  85.                 if cnt == 0:
  86.                     mtr[col][row] = 'x'
  87.                 else:
  88.                     mtr[col][row] = '0'
  89.             #print("col row = ", col, row)
  90.             cnt = (cnt + 1) % 2
  91.  
  92.  
  93.             #print(mtr)
  94.  
  95.     for i in range(3):
  96.         for j in range(3):
  97.             x = i * size_block + (i + 1) * margin
  98.             y = j * size_block + (j + 1) * margin
  99.             color = WHITE
  100.             if mtr[i][j] == '0':
  101.                 color = BLUE
  102.             elif mtr[i][j] == 'x':
  103.                 color = RED
  104.             pygame.draw.rect(screen, color, (x, y, size_block, size_block))
  105.             if color == RED:
  106.                 pygame.draw.line(screen, WHITE, (x,y), (x+size_block, y+size_block))
  107.                 pygame.draw.line(screen, WHITE, (x + size_block, y), (x, y + size_block))
  108.             elif color == BLUE:
  109.                 pygame.draw.circle(screen, WHITE, (x + size_block / 2, y + size_block / 2), size_block / 2 - 30)
  110.             pygame.display.update()
  111.  
  112.             res = check()
  113.             final = ""
  114.             if res == 'x':
  115.                 final = "Cross wins"
  116.             elif res == '0':
  117.                 final = "Zero wins"
  118.             elif res == 'draw':
  119.                 final = "DRAW"
  120.  
  121.             if final != "":
  122.                 over = True
  123.                 screen.fill(BLACK)
  124.                 font = pygame.font.SysFont('stxingkai', 80)
  125.                 text1 = font.render(final, True, WHITE)
  126.                 text_rect = text1.get_rect()
  127.                 text_x = screen.get_width() / 2 - text_rect.width / 2
  128.                 text_y = screen.get_height() / 2 - text_rect.height / 2
  129.                 screen.blit(text1, [text_x, text_y])
  130.                 break
  131.  
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement