-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminesweepergame.py
More file actions
102 lines (85 loc) · 3.11 KB
/
minesweepergame.py
File metadata and controls
102 lines (85 loc) · 3.11 KB
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
import tkinter
import random
gameOver = False
score = 0
squaresToClear = 0
def play_minesweeper():
create_bombfield(bombfield)
window = tkinter.Tk()
layout_window(window)
window.mainloop()
bombfield = []
def create_bombfield(bombfield):
global squaresToClear
for row in range(0, 10):
rowList = []
for column in range(0, 10):
if random.randint(1, 100) < 20:
rowList.append(1)
else:
rowList.append(0)
squaresToClear = squaresToClear + 1
bombfield.append(rowList)
# printfield(bombfield)
def printfield(bombfield):
for rowList in bombfield:
print(rowList)
def layout_window(window):
for rowNumber, rowList in enumerate(bombfield):
for columnNumber, columnEntry in enumerate(rowList):
if random.randint(1, 100) < 25:
square = tkinter.Label(window, text=" ", bg="darkgreen")
elif random.randint(1, 100) > 75:
square = tkinter.Label(window, text=" ", bg="seagreen")
else:
square = tkinter.Label(window, text=" ", bg="green")
square.grid(row=rowNumber, column=columnNumber)
square.bind("<Button-1>", on_click)
def on_click(event):
global score
global gameOver
global squaresToClear
square = event.widget
row = int(square.grid_info()["row"])
column = int(square.grid_info()["column"])
currentText = square.cget("text")
if gameOver == False:
if bombfield[row][column] == 1:
gameOver = True
square.config(bg="red")
print("Game over. Bomb hit!")
print("Your score:", score)
elif currentText == " ":
square.config(bg="brown")
totalBombs = 0
if row < 9:
if bombfield[row + 1][column] == 1:
totalBombs = totalBombs + 1
if row > 0:
if bombfield[row - 1][column] == 1:
totalBombs = totalBombs + 1
if column < 9:
if bombfield[row][column + 1] == 1:
totalBombs = totalBombs + 1
if column > 0:
if bombfield[row][column - 1] == 1:
totalBombs = totalBombs + 1
if row > 0 and column > 0:
if bombfield[row - 1][column - 1] == 1:
totalBombs = totalBombs + 1
if row < 9 and column > 0:
if bombfield[row + 1][column - 1] == 1:
totalBombs = totalBombs + 1
if row > 0 and column < 9:
if bombfield[row - 1][column + 1] == 1:
totalBombs = totalBombs + 1
if row < 9 and column < 9:
if bombfield[row + 1][column + 1] == 1:
totalBombs = totalBombs + 1
square.config(text=" " + str(totalBombs) + " ")
squaresToClear = squaresToClear - 1
score = score + 1
if squaresToClear == 0:
gameOver = True
print("Well done! score:", score)
play_minesweeper()