Advertisement
fevzi02

крестики-нолики

Nov 11th, 2021
770
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame, sys
  2.  
  3. def check_win(mas, sign):
  4.     zeroes = 0
  5.     for row in mas:
  6.         zeroes += row.count(0)
  7.         if row.count(sign) == 3:
  8.             return sign
  9.  
  10.     for col in range(3):
  11.         if mas[0][col] == sign and mas[1][col] == sign and mas[2][col] == sign:
  12.             return sign
  13.  
  14.     if mas[0][0] == sign and mas[1][1] == sign and mas[2][2] == sign:
  15.         return sign
  16.  
  17.     if mas[0][2] == sign and mas[1][1] == sign and mas[2][0] == sign:
  18.         return sign
  19.  
  20.     if zeroes == 0:
  21.         return 'Piece'
  22.  
  23.     return
  24.  
  25. pygame.init()
  26. size_block = 100
  27. margin = 15
  28. width = height = size_block*3 + margin*4
  29.  
  30. size_window = (width, height)
  31. screen = pygame.display.set_mode(size_window)
  32. pygame.display.set_caption("Крестки - Нолики")
  33.  
  34. BLACK = (0, 0, 0)
  35. RED = (255, 0, 0)
  36. GREEN = (0, 255, 0)
  37. WHITE = (255, 255, 255)
  38. mas = [[0]*3 for i in range(3)]
  39. query = 0
  40. game_over = False
  41.  
  42. while True:
  43.     for event in pygame.event.get():
  44.         key = pygame.key.get_pressed()
  45.  
  46.         if event.type == pygame.QUIT or key[pygame.K_ESCAPE] :
  47.             pygame.quit()
  48.             sys.exit(0)
  49.  
  50.         elif event.type == pygame.MOUSEBUTTONDOWN and not game_over:
  51.             x_mouse, y_mouse = pygame.mouse.get_pos()
  52.             col = x_mouse // (size_block + margin)
  53.             row = y_mouse // (size_block + margin)
  54.             if mas[row][col] == 0:
  55.                 if query%2 == 0:
  56.                     mas[row][col] = 'x'
  57.                 else:
  58.                     mas[row][col] = 'o'
  59.                 query+=1
  60.         elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
  61.             game_over = False
  62.             mas = [[0] * 3 for i in range(3)]
  63.             query = 0
  64.             screen.fill(BLACK)
  65.     if not game_over:
  66.         for row in range(3):
  67.             for col in range(3):
  68.                 if mas[row][col] == 'x':
  69.                     color = RED
  70.                 elif mas[row][col] == 'o':
  71.                     color = GREEN
  72.                 else:
  73.                     color = WHITE
  74.                 x = col * size_block + (col+1) * margin
  75.                 y = row * size_block + (row+1) * margin
  76.                 pygame.draw.rect(screen, color, (x, y, size_block, size_block))
  77.                 if color == RED:
  78.                     pygame.draw.line(screen, WHITE, (x + 5, y + 5), (x + size_block - 5, y + size_block - 5), 3 )
  79.                     pygame.draw.line(screen, WHITE, (x + size_block - 5, y + 5), (x + 5, y + size_block - 5), 3)
  80.                 elif color == GREEN:
  81.                     pygame.draw.circle(screen, WHITE, (x + size_block//2, y + size_block//2), size_block//2 - 3, 3)
  82.     if (query - 1) % 2 == 0:
  83.         game_over = check_win(mas, "x")
  84.     else:
  85.         game_over = check_win(mas, "o")
  86.     if game_over:
  87.         screen.fill(BLACK)
  88.         font = pygame.font.SysFont('stxingkai', 80)
  89.         font2 = pygame.font.SysFont('stxingkai', 20)
  90.         text1 = font.render(game_over, True, WHITE)
  91.         text2 = font2.render("Нажмите пробел для перезагрузки игры", True, WHITE)
  92.         text_rect = text1.get_rect()
  93.         text2_rect = text2.get_rect()
  94.         text_x = screen.get_width() / 2 - text_rect.width / 2
  95.         text_y = screen.get_height() / 2 - text_rect.height / 2
  96.         text1_x = screen.get_width() / 2 - text2_rect.width / 2
  97.         text1_y = screen.get_height() - 30
  98.         screen.blit(text1, [text_x, text_y])
  99.         screen.blit(text2, [text1_x, text1_y])
  100.  
  101.     pygame.display.update()
  102.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement