Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. pygame.init()
  5. size = 470, 470
  6. screen = pygame.display.set_mode(size)
  7.  
  8.  
  9. class Board:
  10. # создание поля
  11. def __init__(self, width, height, left=10, top=10, cell_size=30):
  12. self.width = width
  13. self.height = height
  14. self.board = [[0] * width for _ in range(height)]
  15. # значения по умолчанию
  16. self.left = 0
  17. self.top = 0
  18. self.cell_size = 0
  19. self.set_view(left, top, cell_size)
  20.  
  21. def render(self):
  22. for y in range(self.height):
  23. for x in range(self.width):
  24. pygame.draw.rect(screen, pygame.Color(255, 255, 255),
  25. (x * self.cell_size + self.left, y * self.cell_size + self.top, self.cell_size,
  26. self.cell_size), 1)
  27.  
  28. # настройка внешнего вида
  29. def set_view(self, left, top, cell_size):
  30. self.left = left
  31. self.top = top
  32. self.cell_size = cell_size
  33.  
  34. # cell - кортеж (x, y)
  35. def on_click(self, cell):
  36. # заглушка для реальных игровых полей
  37. pass
  38.  
  39. def get_cell(self, mouse_pos):
  40. cell_x = (mouse_pos[0] - self.left) // self.cell_size
  41. cell_y = (mouse_pos[1] - self.top) // self.cell_size
  42. if cell_x < 0 or cell_x >= self.width or cell_y < 0 or cell_y >= self.height:
  43. return None
  44. return cell_x, cell_y
  45.  
  46. def get_click(self, mouse_pos):
  47. cell = self.get_cell(mouse_pos)
  48. if cell:
  49. self.on_click(cell)
  50.  
  51.  
  52. class Minesweeper(Board):
  53. def __init__(self, width, height, n):
  54. super().__init__(width, height)
  55. self.board = [[-1] * width for _ in range(height)]
  56. i = 0
  57. while i < n:
  58. x = random.randint(0, self.width - 1)
  59. y = random.randint(0, self.height - 1)
  60. if self.board[y][x] == -1:
  61. self.board[y][x] = 10
  62. i += 1
  63.  
  64. def on_click(self, cell):
  65. self.open_cell(cell)
  66.  
  67. def open_cell(self, cell):
  68. x, y = cell
  69. if self.board[y][x] == 10:
  70. return
  71. s = 0
  72. for dy in range(-1, 2):
  73. for dx in range(-1, 2):
  74. if x + dx < 0 or x + dx >= self.width or y + dy < 0 or y + dy >= self.height:
  75. continue
  76. if self.board[y + dy][x + dx] == 10:
  77. s += 1
  78. self.board[y][x] = s
  79. if s == 0:
  80. for dy in range(-1, 2):
  81. for dx in range(-1, 2):
  82. if x + dx < 0 or x + dx >= self.width or y + dy < 0 or y + dy >= self.height:
  83. continue
  84. if self.board[y + dy][x + dx] == -1:
  85. self.open_cell((x + dx, y + dy))
  86.  
  87. def render(self):
  88. for y in range(self.height):
  89. for x in range(self.width):
  90. if self.board[y][x] == 10:
  91. pygame.draw.rect(screen, pygame.Color("red"),
  92. (x * self.cell_size + self.left, y * self.cell_size + self.top, self.cell_size,
  93. self.cell_size))
  94. if self.board[y][x] >= 0 and self.board[y][x] != 10:
  95. font = pygame.font.Font(None, self.cell_size - 6)
  96. text = font.render(str(self.board[y][x]), 1, (100, 255, 100))
  97. screen.blit(text, (x * self.cell_size + self.left + 3, y * self.cell_size + self.top + 3))
  98. pygame.draw.rect(screen, pygame.Color(255, 255, 255),
  99. (x * self.cell_size + self.left, y * self.cell_size + self.top, self.cell_size,
  100. self.cell_size), 1)
  101.  
  102.  
  103. running = True
  104. board = Minesweeper(10, 15, 10)
  105. board.set_view(10, 10, 30)
  106. while running:
  107. for event in pygame.event.get():
  108. if event.type == pygame.QUIT:
  109. running = False
  110. if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  111. board.get_click(event.pos)
  112. screen.fill((0, 0, 0))
  113. board.render()
  114. pygame.display.flip()
  115. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement