Skip to content

Instantly share code, notes, and snippets.

@GoldsteinE
Created November 21, 2017 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GoldsteinE/bf6b9ce20e522b1f8530896a1718d841 to your computer and use it in GitHub Desktop.
Save GoldsteinE/bf6b9ce20e522b1f8530896a1718d841 to your computer and use it in GitHub Desktop.
Minesweeper 127
import arcade
import random
CELL_SIZE = 32
class Objects:
MINE = 1
FLAG = 2
OPEN = 4
COLORS = {
'1': arcade.color.BLACK,
'2': arcade.color.GREEN,
'3': arcade.color.BLUE,
'4': arcade.color.ORANGE,
'5': arcade.color.OCHRE,
'6': arcade.color.PLUM,
'7': arcade.color.LIGHT_BLUE,
'8': arcade.color.RED,
'': arcade.color.BLACK
}
class MainWindow(arcade.Window):
def __init__(self, cols, rows, difficulty=0.15):
super().__init__(cols * CELL_SIZE, rows * CELL_SIZE, title='Minesweeper 127')
self.grid = []
self.rows = rows
self.cols = cols
for i in range(rows):
self.grid.append([])
for j in range(cols):
self.grid[-1].append(int(random.random() < difficulty))
self.in_game = True
self.win = False
self.old_sprites = []
arcade.set_background_color(arcade.color.WHITE)
def check_coord(self, x, y):
return 0 <= x < self.cols and 0 <= y < self.rows
def count_mines(self, x, y, obj=Objects.MINE):
coords = [(x - 1, y - 1), (x - 1, y), (x - 1, y + 1), (x, y - 1), (x, y + 1), (x + 1, y - 1), (x + 1, y), (x + 1, y + 1)]
result = 0
for coord in coords:
if self.check_coord(*coord):
if self.grid[coord[0]][coord[1]] & obj:
result += 1
return result
def recurse_open(self, x, y, visited=[], force=False):
if self.grid[x][y] & Objects.MINE:
return
self.grid[x][y] |= Objects.OPEN
coords = [(x - 1, y - 1), (x - 1, y), (x - 1, y + 1), (x, y - 1), (x, y + 1), (x + 1, y - 1), (x + 1, y), (x + 1, y + 1)]
if self.count_mines(x, y) == 0 or force:
for coord in coords:
if self.check_coord(*coord) and coord not in visited:
visited.append(coord)
self.recurse_open(*coord, visited)
def on_draw(self):
arcade.start_render()
for sprite in self.old_sprites:
sprite.kill()
self.old_sprites = []
for row in range(self.rows):
for col in range(self.cols):
if self.grid[row][col] & Objects.FLAG:
flag = arcade.Sprite('flag.png', center_x=((col + 0.5) * CELL_SIZE), center_y=((row + 0.5) * CELL_SIZE))
flag.draw()
self.old_sprites.append(flag)
elif self.grid[row][col] & Objects.MINE and not self.in_game:
mine = arcade.Sprite('mine.png', center_x=((col + 0.5) * CELL_SIZE), center_y=((row + 0.5) * CELL_SIZE))
mine.draw()
self.old_sprites.append(mine)
elif self.grid[row][col] & Objects.OPEN:
count = self.count_mines(row, col)
if count == 0:
count = ''
arcade.draw_text(str(count), col * CELL_SIZE, row * CELL_SIZE, COLORS[str(count)], CELL_SIZE)
else:
arcade.draw_rectangle_filled((col + 0.5) * CELL_SIZE, (row + 0.5) * CELL_SIZE, CELL_SIZE, CELL_SIZE, arcade.color.GRAY)
if not self.in_game and not self.win:
arcade.draw_text('U FAIL', self.cols / 2 * CELL_SIZE, self.rows / 2 * CELL_SIZE, arcade.color.RED, CELL_SIZE * 3, anchor_x='center', anchor_y='center', align='center')
elif not self.in_game:
arcade.draw_text('U WIN', self.cols / 2 * CELL_SIZE, self.rows / 2 * CELL_SIZE, arcade.color.GREEN, CELL_SIZE * 3, anchor_x='center', anchor_y='center', align='center')
def on_mouse_press(self, x, y, button, modifiers):
if self.in_game:
col = x // CELL_SIZE
row = y // CELL_SIZE
if button == arcade.MOUSE_BUTTON_LEFT:
if self.grid[row][col] & Objects.OPEN and self.count_mines(row, col) > 0 and self.count_mines(row, col) <= self.count_mines(row, col, Objects.FLAG):
self.recurse_open(row, col, force=True)
if not self.grid[row][col] & Objects.FLAG:
self.recurse_open(row, col)
if self.grid[row][col] & Objects.MINE:
self.in_game = False
if button == arcade.MOUSE_BUTTON_RIGHT:
if not self.grid[row][col] & Objects.OPEN:
self.grid[row][col] ^= Objects.FLAG
for row in range(self.rows):
for col in range(self.cols):
if self.grid[row][col] & Objects.MINE and not self.grid[row][col] & Objects.FLAG:
return
self.in_game = False
self.win = True
def on_mouse_scroll(self, *args, **kwargs): # Hack to prevent crashing
pass
def main():
MainWindow(20, 20)
arcade.run()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment