1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
| import pygame
import random
class Board:
def __init__(self, width, height):
self.width = width
self.height = height
self.board = [[0] * width for _ in range(height)]
self.left = 10
self.top = 10
self.cell_size = 30
def set_view(self, left, top, cell_size):
self.left = left
self.top = top
self.cell_size = cell_size
def render(self, screen):
color = pygame.Color('white')
for y in range(self.height):
for x in range(self.width):
coor = (x * self.cell_size + self.left, y * self.cell_size + self.top, self.cell_size, self.cell_size)
pygame.draw.rect(screen, color, coor, 1)
def get_cell(self, mouse_pos):
cell_x = (mouse_pos[0] - self.left) // self.cell_size
cell_y = (mouse_pos[1] - self.top) // self.cell_size
if cell_x < 0 or cell_x >= self.width or cell_y < 0 or cell_y >= self.height:
return None
return cell_x, cell_y
def on_click(self, cell):
pass
def get_click(self, mouse_pos):
cell = self.get_cell(mouse_pos)
if cell:
self.on_click(cell)
class Minesweeper(Board):
def __init__(self, width, height, need):
super().__init__(width, height)
self.board = [[-1] * width for inet in range(height)]
ter = 0
while ter < need:
x = random.randint(0, self.width - 1)
y = random.randint(0, self.height - 1)
if self.board[y][x] == -1:
self.board[y][x] = 10
ter += 1
def open_click(self, cell):
x, y = cell
if self.board[y][x] == 10:
return
s = 0
for dy in range(-1, 2):
for dx in range(-1, 2):
if x + dx < 0 or x + dx >= self.width or y + dy < 0 or y + dy >= self.height:
continue
if self.board[y + dy][x + dx] == 10:
s += 1
if s == 0:
s1 = 0
for x1 in range(self.width):
for y1 in range(self.height):
s1 = 0
if self.board[y1][x1] == -1:
for dy1 in range(-1, 2):
for dx1 in range(-1, 2):
if x1 + dx1 < 0 or x1 + dx1 >= self.width or y1 + dy1 < 0 or y1 + dy1 >= self.height:
continue
if self.board[y1 + dy1][x1 + dx1] == 10:
s1 += 1
if s1 == 0:
self.board[y1][x1] = s1
self.board[y][x] = s
def on_click(self, cell):
self.open_click(cell)
def render(self, screen):
color = pygame.Color('red')
for y in range(self.height):
for x in range(self.width):
coor = (x * self.cell_size + self.left, y * self.cell_size + self.top,
self.cell_size, self.cell_size)
coor1 = (x * self.cell_size + self.left + 2, y * self.cell_size + self.top + 2,
self.cell_size, self.cell_size)
if self.board[y][x] == 10:
pygame.draw.rect(screen, color, coor)
pygame.draw.rect(screen, pygame.Color('white'), coor, 1)
if self.board[y][x] >= 0 and self.board[y][x] != 10:
font = pygame.font.Font(None, self.cell_size - 7)
text = font.render(str(self.board[y][x]), 1, (100, 255, 100))
screen.blit(text, coor1)
def main():
pygame.init()
size = 500, 500
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Папа сапёра')
clock = pygame.time.Clock()
board = Minesweeper(10, 15, 10)
ticks = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
board.get_click(event.pos)
screen.fill((0, 0, 0))
board.render(screen)
pygame.display.flip()
clock.tick(50)
ticks += 1
pygame.quit()
if __name__ == '__main__':
main() |