-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
151 lines (124 loc) · 4.06 KB
/
game.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import pygame
import sys
import random
from tkinter import *
from tkinter import messagebox
WINDOW_SIZE = 400
GRIDBOX_SIZE = 40
MINES = 10
ROWS = 10
COLS = 10
# RGB
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (20, 200, 20)
GRAY = (140, 140, 140)
mineImg = pygame.image.load("mine.png")
explosionImg = pygame.image.load("explosion.png")
def main():
global SCREEN, CLOCK
pygame.init()
SCREEN = pygame.display.set_mode((WINDOW_SIZE, WINDOW_SIZE))
pygame.display.set_caption("MINESWEEPER")
CLOCK = pygame.time.Clock()
SCREEN.fill(WHITE)
realBoard = [["-"]*COLS for i in range(ROWS)]
num_font = pygame.font.SysFont("Helvetica", 30)
placeMines(realBoard)
boardOverlay()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
r = getGridRect(x, y)
checkForMines(x, y, r, num_font, realBoard)
pygame.display.update()
CLOCK.tick(60)
def boardOverlay():
for x in range(WINDOW_SIZE//GRIDBOX_SIZE):
for y in range(WINDOW_SIZE//GRIDBOX_SIZE):
rect = pygame.Rect(x*GRIDBOX_SIZE, y*GRIDBOX_SIZE,
GRIDBOX_SIZE, GRIDBOX_SIZE)
pygame.draw.rect(SCREEN, GRAY, rect)
pygame.draw.rect(SCREEN, BLACK, rect, 1)
def isValidMove(x, y, realBoard):
if x >= 0 and y >= 0:
try:
if realBoard[x][y] == "*":
return True
except IndexError:
return False
else:
return False
else:
return False
def checkForMines(x, y, r, num_font, realBoard):
x = x//GRIDBOX_SIZE
y = y//GRIDBOX_SIZE
count = 0
if realBoard[x][y] == "*":
showMines(realBoard)
elif realBoard[x][y] != "*":
if isValidMove(x, y-1, realBoard):
count = count + 1
if isValidMove(x, y+1, realBoard):
count = count + 1
if isValidMove(x+1, y, realBoard):
count = count + 1
if isValidMove(x-1, y, realBoard):
count = count + 1
if isValidMove(x-1, y-1, realBoard):
count = count + 1
if isValidMove(x+1, y+1, realBoard):
count = count + 1
if isValidMove(x-1, y+1, realBoard):
count = count + 1
if isValidMove(x+1, y-1, realBoard):
count = count + 1
pygame.draw.rect(SCREEN, WHITE, r)
if count == 0:
pass
else:
n = num_font.render(str(count), True, BLACK)
SCREEN.blit(n, (r.x+10, r.y+4))
def placeMines(realBoard):
for i in range(MINES+1):
mine_x = random.randrange(40, WINDOW_SIZE)
mine_y = random.randrange(40, WINDOW_SIZE)
r = getGridRect(mine_x, mine_y)
x = mine_x//GRIDBOX_SIZE
y = mine_y//GRIDBOX_SIZE
realBoard[x][y] = "*"
def showMines(realBoard):
for x in range(ROWS):
for y in range(COLS):
if realBoard[x][y] == "*":
r = pygame.Rect(x*GRIDBOX_SIZE, y*GRIDBOX_SIZE,
GRIDBOX_SIZE, GRIDBOX_SIZE)
SCREEN.blit(mineImg, r)
pygame.display.update()
pygame.time.wait(100)
SCREEN.blit(explosionImg, r)
pygame.display.update()
gameOver()
def gameOver():
root = Tk().wm_withdraw()
MsgBox = messagebox.askquestion(
'OOPS YOU BLEW UP THE MINES!', 'Do you want to play again?', icon='question')
if MsgBox == 'yes':
main()
else:
sys.exit()
def getGridRect(x, y):
rect = pygame.Rect(0, 0, GRIDBOX_SIZE, GRIDBOX_SIZE)
for i in range(0, (x//GRIDBOX_SIZE)+1):
for j in range(0, (y//GRIDBOX_SIZE)+1):
rect = pygame.Rect(i*GRIDBOX_SIZE, j*GRIDBOX_SIZE,
GRIDBOX_SIZE, GRIDBOX_SIZE)
return rect
if __name__ == "__main__":
main()