-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
121 lines (86 loc) · 2.88 KB
/
main.py
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
# Simple pygame program
# Import and initialize the pygame library
import pygame
import random
pygame.init()
font = pygame.font.SysFont('arial', 50)
width = 10
height = 10
scaling = 50
numOfMines = 15
screen = pygame.display.set_mode([width*scaling, height*scaling])
# Background
hintergrund = pygame.image.load("background.jpg")
hintergrund = pygame.transform.scale(hintergrund, (1280, 720))
running = True
# (x,y) -> -1 == bomb, else num of neighbors
grid = list()
for x in range(width):
grid.append(list())
for y in range(height):
grid[-1].append(0)
for _ in range(numOfMines):
x = int(random.uniform(0, width))
y = int(random.uniform(0, height))
grid[x][y] = -1
for q in range (width):
for a in range (height):
if grid[q][a]== -1:
for v in range (-1,2):
for s in range(-1, 2):
if v + q < 0 or v + q > width - 1 or s + a < 0 or s + a > height - 1:
continue
if grid[q+v][a+s]!= -1:
if s== 0 and v==0:
continue
grid[q+v][a+s] += 1
for row in grid:
print(row)
screen.blit(hintergrund,(0, 0))
blue = (0,0,255)
#linien x-achse
for i in range(1,10):
pygame.draw.line(screen, (0,0,0), [0+50*i,0], [0+50*i, 500] )
pygame.draw.line(screen, (0, 0, 0), [0, 0 + 50 * i], [500, 0 + 50 * i])
def draw_number(number, x,y):
text = font.render(str(number), True, (0, 0, 0))
screen.blit(text, (x, y))
pygame.display.update()
def reveal(x,y):
if(grid[x][y] == -2):
return
draw_number(grid[x][y], x*scaling + 10,y*scaling)
def reveal_recurse(x,y):
for x_offset in range(-1, 2, 1):
for y_offset in range(-1, 2, 1):
x_t = x + x_offset
y_t = y + y_offset
if(x_t < 0 or y_t < 0 or x_t >= width or y_t >= height):
continue
print("reveal: ",x_t,y_t)
reveal(x_t,y_t)
if(grid[x_t][y_t] == 0):
reveal_recurse(x_t,y_t)
grid[x][y] = -2
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
left, middle, right = pygame.mouse.get_pressed()
if(left):
pos1 = pygame.mouse.get_pos()
print(int(pos1[0] /scaling), int(pos1[1] / scaling))
x, y = int(pos1[0] /scaling), int(pos1[1] / scaling)
if(grid[x][y] == 0):
reveal_recurse(x,y)
draw_number(0, x*scaling + 10,y*scaling)
elif(grid[x][y] == -1):
running = False
else:
reveal(x,y)
grid[x][y] = -2
# Flip the display
pygame.display.flip()
# Done! Time to quit.
pygame.quit()