|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Minesweeper |
| 4 | +# Language - Python |
| 5 | +# Modules - pygame, sys, random |
| 6 | +# |
| 7 | +# Controls - Single Mouse Click on any box, r to reset the grid |
| 8 | +# |
| 9 | +# By - Jatin Kumar Mandav |
| 10 | +# |
| 11 | +# Website - https://jatinmandav.wordpress.com |
| 12 | +# |
| 13 | +# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ |
| 14 | +# Twitter - @jatinmandav |
| 15 | +# |
| 16 | +# ----------------------------------------------------------------------------- |
| 17 | + |
| 18 | +import pygame |
| 19 | +import sys |
| 20 | +import random |
| 21 | + |
| 22 | +pygame.init() |
| 23 | + |
| 24 | +width = 510 |
| 25 | +height = 510 |
| 26 | +display = pygame.display.set_mode((width, height)) |
| 27 | +pygame.display.set_caption("Minesweeper") |
| 28 | +clock = pygame.time.Clock() |
| 29 | + |
| 30 | +background = (51, 51, 51) |
| 31 | +white = (236, 240, 241) |
| 32 | +darkWhite = (174, 182, 191) |
| 33 | +gray = (52, 73, 94) |
| 34 | +yellow = (244, 208, 63) |
| 35 | +lightred = (236, 112, 99) |
| 36 | + |
| 37 | +grid = [] |
| 38 | +size = 10 |
| 39 | +mineProb = 30 |
| 40 | + |
| 41 | +safeSpots = 0 |
| 42 | +revealedSpots = 0 |
| 43 | + |
| 44 | +numberFont = pygame.font.SysFont("Times New Roman", width/(size*2)) |
| 45 | +font = pygame.font.SysFont("Times New Roman", 35) |
| 46 | + |
| 47 | +# Property of Each Cell on Grid |
| 48 | +class Spot: |
| 49 | + def __init__(self, x, y, w, h, mineState): |
| 50 | + self.x = x |
| 51 | + self.y = y |
| 52 | + self.w = w |
| 53 | + self.h = h |
| 54 | + self.isMine = mineState |
| 55 | + self.neighbors = 0 |
| 56 | + self.reveal = False |
| 57 | + |
| 58 | + # Draw Cells |
| 59 | + def draw(self): |
| 60 | + if not self.reveal: |
| 61 | + pygame.draw.rect(display, white, (self.x, self.y, self.w, self.h)) |
| 62 | + else: |
| 63 | + pygame.draw.rect(display, darkWhite, (self.x, self.y, self.w, self.h)) |
| 64 | + |
| 65 | + if self.reveal: |
| 66 | + if self.isMine: |
| 67 | + pygame.draw.ellipse(display, gray, (self.x + self.w/4, self.y + self.h/4, self.w/2, self.h/2)) |
| 68 | + else: |
| 69 | + if not self.neighbors == 0: |
| 70 | + num = numberFont.render(str(self.neighbors), True, gray) |
| 71 | + display.blit(num, (self.x + self.w/4, self.y + self.h/4)) |
| 72 | + |
| 73 | + # Check if the Cell is a Mine and reveal others if Block has no Mine Surrounding |
| 74 | + def checkForMine(self, i, j): |
| 75 | + global revealedSpots |
| 76 | + self.reveal = True |
| 77 | + revealedSpots += 1 |
| 78 | + |
| 79 | + if self.isMine: |
| 80 | + for i in range(size): |
| 81 | + for j in range(size): |
| 82 | + grid[i][j].reveal = True |
| 83 | + |
| 84 | + drawGrid() |
| 85 | + pygame.display.update() |
| 86 | + gameLost() |
| 87 | + |
| 88 | + elif grid[i][j].neighbors == 0: |
| 89 | + if i > 0: |
| 90 | + if not grid[i-1][j].isMine: |
| 91 | + grid[i-1][j].reveal = True |
| 92 | + revealedSpots += 1 |
| 93 | + if j > 0: |
| 94 | + if not grid[i][j-1].isMine: |
| 95 | + grid[i][j-1].reveal = True |
| 96 | + revealedSpots += 1 |
| 97 | + if i < size - 1: |
| 98 | + if not grid[i+1][j].isMine: |
| 99 | + grid[i+1][j].reveal = True |
| 100 | + revealedSpots += 1 |
| 101 | + if j < size - 1: |
| 102 | + if not grid[i][j+1].isMine: |
| 103 | + grid[i][j+1].reveal = True |
| 104 | + revealedSpots += 1 |
| 105 | + if i > 0 and j > 0: |
| 106 | + if not grid[i-1][j-1].isMine: |
| 107 | + grid[i-1][j-1].reveal = True |
| 108 | + revealedSpots += 1 |
| 109 | + if i > 0 and j < size - 1: |
| 110 | + if not grid[i-1][j+1].isMine: |
| 111 | + grid[i-1][j+1].reveal = True |
| 112 | + revealedSpots += 1 |
| 113 | + if i < size - 1 and j > 0: |
| 114 | + if not grid[i+1][j-1].isMine: |
| 115 | + grid[i+1][j-1].reveal = True |
| 116 | + revealedSpots += 1 |
| 117 | + if i < size - 1 and j < size - 1: |
| 118 | + if not grid[i+1][j+1].isMine: |
| 119 | + grid[i+1][j+1].reveal = True |
| 120 | + revealedSpots += 1 |
| 121 | + |
| 122 | + # Count Neighboring Mines |
| 123 | + def countNeighborMines(self, i, j): |
| 124 | + if not self.isMine: |
| 125 | + if i > 0: |
| 126 | + if grid[i-1][j].isMine: |
| 127 | + self.neighbors += 1 |
| 128 | + if j > 0: |
| 129 | + if grid[i][j-1].isMine: |
| 130 | + self.neighbors += 1 |
| 131 | + if i < size - 1: |
| 132 | + if grid[i+1][j].isMine: |
| 133 | + self.neighbors += 1 |
| 134 | + if j < size - 1: |
| 135 | + if grid[i][j+1].isMine: |
| 136 | + self.neighbors += 1 |
| 137 | + if i > 0 and j > 0: |
| 138 | + if grid[i-1][j-1].isMine: |
| 139 | + self.neighbors += 1 |
| 140 | + if i > 0 and j < size - 1: |
| 141 | + if grid[i-1][j+1].isMine: |
| 142 | + self.neighbors += 1 |
| 143 | + if i < size - 1 and j > 0: |
| 144 | + if grid[i+1][j-1].isMine: |
| 145 | + self.neighbors += 1 |
| 146 | + if i < size - 1 and j < size - 1: |
| 147 | + if grid[i+1][j+1].isMine: |
| 148 | + self.neighbors += 1 |
| 149 | + |
| 150 | + |
| 151 | +# Initialize the Grid |
| 152 | +def generateGrid(): |
| 153 | + global grid, safeSpots |
| 154 | + grid = [] |
| 155 | + for i in range(size): |
| 156 | + grid.append([]) |
| 157 | + for j in range(size): |
| 158 | + prob = random.randint(1, 100) |
| 159 | + if prob < mineProb: |
| 160 | + newObj = Spot((width/size)*(j), (height/size)*(i), width/size - 3, height/size - 3, True) |
| 161 | + else: |
| 162 | + safeSpots += 1 |
| 163 | + newObj = Spot((width/size)*(j), (height/size)*(i), width/size - 3, height/size - 3, False) |
| 164 | + |
| 165 | + grid[i].append(newObj) |
| 166 | + |
| 167 | + for i in range(size): |
| 168 | + for j in range(size): |
| 169 | + grid[i][j].countNeighborMines(i, j) |
| 170 | + |
| 171 | +# Check if Grid is solved |
| 172 | +def gameWon(): |
| 173 | + while True: |
| 174 | + for event in pygame.event.get(): |
| 175 | + if event.type == pygame.QUIT: |
| 176 | + close() |
| 177 | + if event.type == pygame.KEYDOWN: |
| 178 | + if event.key == pygame.K_q: |
| 179 | + close() |
| 180 | + if event.key == pygame.K_r: |
| 181 | + reset() |
| 182 | + |
| 183 | + font.set_bold(True) |
| 184 | + text = font.render("You Won the Minesweeper!", True, yellow) |
| 185 | + display.blit(text, (width/2 - 250, height/2)) |
| 186 | + pygame.display.update() |
| 187 | + clock.tick(60) |
| 188 | + |
| 189 | +# Check if Game is Lost |
| 190 | +def gameLost(): |
| 191 | + while True: |
| 192 | + for event in pygame.event.get(): |
| 193 | + if event.type == pygame.QUIT: |
| 194 | + close() |
| 195 | + if event.type == pygame.KEYDOWN: |
| 196 | + if event.key == pygame.K_q: |
| 197 | + close() |
| 198 | + if event.key == pygame.K_r: |
| 199 | + reset() |
| 200 | + |
| 201 | + font.set_bold(True) |
| 202 | + text = font.render("You Lost the Game!", True, (236, 112, 99)) |
| 203 | + display.blit(text, (width/2 - 250, height/2)) |
| 204 | + pygame.display.update() |
| 205 | + clock.tick(60) |
| 206 | + |
| 207 | +# Draw the Grid |
| 208 | +def drawGrid(): |
| 209 | + for i in range(size): |
| 210 | + for j in range(size): |
| 211 | + grid[i][j].draw() |
| 212 | + |
| 213 | +# Reset the Grid |
| 214 | +def reset(): |
| 215 | + minesweeper() |
| 216 | + |
| 217 | +# Close the Game |
| 218 | +def close(): |
| 219 | + pygame.quit() |
| 220 | + sys.exit() |
| 221 | + |
| 222 | +# The Game |
| 223 | +def minesweeper(): |
| 224 | + loop = True |
| 225 | + |
| 226 | + generateGrid() |
| 227 | + |
| 228 | + while loop: |
| 229 | + for event in pygame.event.get(): |
| 230 | + if event.type == pygame.QUIT: |
| 231 | + close() |
| 232 | + if event.type == pygame.KEYDOWN: |
| 233 | + if event.key == pygame.K_q: |
| 234 | + close() |
| 235 | + if event.key == pygame.K_r: |
| 236 | + reset() |
| 237 | + if event.type == pygame.MOUSEBUTTONDOWN: |
| 238 | + pos = pygame.mouse.get_pos() |
| 239 | + j = pos[0]/(width/size) |
| 240 | + i = pos[1]/(width/size) |
| 241 | + grid[i][j].checkForMine(i, j) |
| 242 | + |
| 243 | + display.fill(background) |
| 244 | + |
| 245 | + drawGrid() |
| 246 | + |
| 247 | + if revealedSpots == safeSpots: |
| 248 | + gameWon() |
| 249 | + |
| 250 | + pygame.display.update() |
| 251 | + clock.tick(60) |
| 252 | + |
| 253 | +minesweeper() |
0 commit comments