Code2pdf 646c5a2901df5
Code2pdf 646c5a2901df5
import pygame
from datetime import datetime
TILE_SIZE = 30
BANNER_SIZE = 2.5 * TILE_SIZE
game_completed = False
COLORS = {
1: '#0000ff',
2: '#00FF00',
3: '#FF0000',
4: '#00008b',
5: '#8b0000',
6: '#14A3C7',
7: '#000000',
8: '#808080'
}
DIFFICULTIES = {
'Easy': [10, 8, 10],
'Medium': [18, 14, 40],
'Hard': [24, 20, 99]
}
class Tile:
def __init__(self, x, y, width, height, dark, screen, x_len, y_len, num_mines):
self.x = x
self.y = y
self.width = width
self.height = height
self.dark = dark
self.color = ['#63c7de', '#bbe8eb'] if dark else ['#79d0e1', '#bbe8eb']
self.screen = screen
self.board = ''
self.x_len = x_len
self.y_len = y_len
self.num_mines = num_mines
self.flag_image = pygame.image.load('flag.png')
self.flag_image = pygame.transform.scale(self.flag_image, (TILE_SIZE, TILE_SIZE))
self.bomb_image = pygame.image.load('bomb.png')
self.bomb_image = pygame.transform.scale(self.bomb_image, (TILE_SIZE, TILE_SIZE))
self.flagged = False
self.chorded = False
self.mined = False
self.nearby = False
self.clicked = False
def render(self):
global game_completed
self.surface.fill(self.color[0])
if game_completed:
self.flagged = False
if self.board[int((self.y - BANNER_SIZE) / TILE_SIZE)][int(self.x / TILE_SIZE)] == 'm':
self.color = ['#808080', '#BEBEBE'] if self.dark else ['#949494', '#BEBEBE']
self.screen.blit(self.surface, self.hitbox)
self.screen.blit(self.bomb_image, (self.x, self.y))
return
if self.chorded == 'highlight':
self.surface.fill(self.color[1])
if self.hitbox.collidepoint(pygame.mouse.get_pos()):
self.surface.fill(self.color[1])
mouse_state = pygame.mouse.get_pressed(num_buttons=3)
if mouse_state[0] ^ mouse_state[1] ^ mouse_state[2]:
if not self.clicked and not game_completed:
self.clicked = True
event = 'mine' if mouse_state[0] else 'flag' if mouse_state[2] else 'chord'
self.handler(event)
else:
self.clicked = False
self.screen.blit(self.surface, self.hitbox)
def render_event(self):
if self.flagged:
if self.color[1] == '#BEBEBE':
self.flagged = False
else:
self.screen.blit(self.flag_image, (self.x, self.y))
if self.mined:
space = self.board[int((self.y - BANNER_SIZE) / TILE_SIZE)][int(self.x / TILE_SIZE)]
font = pygame.font.SysFont("monospace", TILE_SIZE, True)
label = font.render(f"{space}", 0.1, COLORS[space])
self.screen.blit(label, (self.x + TILE_SIZE / 5, self.y))
class DifficultyButton:
def __init__(self, screen, x, y, size, difficulty):
self.screen = screen
self.x = x
self.y = y
self.size = size
self.difficulty = difficulty
self.clicked = False
def render(self):
if self.hitbox.collidepoint(pygame.mouse.get_pos()):
self.color = '#BEBEBE'
mouse_state = pygame.mouse.get_pressed(num_buttons=3)
if mouse_state[0]:
self.clicked = True
else:
self.color = '#808080'
self.surface.fill(self.color)
self.screen.blit(self.surface, self.hitbox)
self.screen.blit(self.label, (self.x + self.size / 5, self.y))
def main(settings):
global game_completed
game_completed = False
pygame.init()
pygame.display.set_caption('Minesweeper')
before_time = round(datetime.today().timestamp())
tiles = []
subset_tiles = []
square_color = False
data = calculate_coordinates(x_len, y_len)
for i, coordinate in enumerate(data):
if i % x_len == 0 and i != 0:
tiles.append(subset_tiles)
subset_tiles = []
if x_len % 2 == 0:
square_color = not square_color
square_color = not square_color
subset_tiles.append(Tile(
coordinate[0],
coordinate[1],
TILE_SIZE,
TILE_SIZE,
square_color,
screen,
x_len,
y_len,
num_mines
))
tiles.append(subset_tiles)
difficulty_buttons = []
for i, difficulty in enumerate(DIFFICULTIES.keys()):
size = (BANNER_SIZE / 2)
x_pos = (x_len / 10) * (size * i)
y_pos = BANNER_SIZE / 4
difficulty_buttons.append(DifficultyButton(screen, x_pos, y_pos, size, difficulty))
while True:
event = pygame.event.poll()
if event.type == pygame.QUIT:
pygame.quit()
return False
screen.fill('#4EA3B7')
mouse_state = pygame.mouse.get_pressed(num_buttons=3)
for y, row in enumerate(tiles):
for x, tile in enumerate(row):
if not tile.board:
tile.board = board
tile.render()
tile.render_event()
if tile.chorded:
chordable = chord(tiles, board, [x, y])
if chordable['can_chord']:
for coordinate in chordable['tiles']:
if board[coordinate[1]][coordinate[0]] == 0:
tile.nearby = nearby_empty(board, coordinate)
else:
tiles[coordinate[1]][coordinate[0]].mined = True
tiles[coordinate[1]][coordinate[0]].color = ['#808080', '#BEBEBE'] if \
tiles[coordinate[1]][coordinate[0]].dark else ['#949494', '#BEBEBE']
tile.chorded = False
else:
if mouse_state[1]:
for coordinate in chordable['tiles']:
tiles[coordinate[1]][coordinate[0]].chorded = False
chordable = chord(tiles, board, mouse_pos(tiles))
for coordinate in chordable['tiles']:
tiles[coordinate[1]][coordinate[0]].chorded = 'highlight'
else:
tile.chorded = False
if tile.nearby:
for near in tile.nearby['empty']:
tiles[near[1]][near[0]].color = ['#808080', '#BEBEBE'] if tiles[near[1]][near[0]].dark else [
'#949494', '#BEBEBE']
for num in tile.nearby['nums']:
tiles[num[1]][num[0]].mined = True
tiles[num[1]][num[0]].color = ['#808080', '#BEBEBE'] if tiles[num[1]][num[0]].dark else [
'#949494', '#BEBEBE']
tile.nearby = False
if not game_completed:
if not (game_finished(tiles, num_mines)):
flags = num_flagged(tiles, num_mines)
if x_len <= 10:
flag_label = [font.render(f'flags:{flags}', 0.1, '#000000'),
font.render(f'time:{round(datetime.today().timestamp()) - before_time}', 0.1,
'#000000')
]
else:
flag_label = [
font.render(f"flags:{flags} time:{round(datetime.today().timestamp()) - before_time}", 0.1,
'#000000')]
for i, label in enumerate(flag_label):
if x_len <= 10:
screen.blit(label, ((x_len / 7) * BANNER_SIZE + font_size, BANNER_SIZE / 4 + (i * font_size)))
else:
screen.blit(label, ((x_len / 8) * BANNER_SIZE, BANNER_SIZE / 4 + (i * font_size)))
pygame.display.flip()
clicked_square = exclude
exclude = valid_surrounding(board, exclude)
exclude.append(clicked_square)
for pair in exclude:
if board[pair[1]][pair[0]] == 'm':
board[pair[1]][pair[0]] = 0
new_spot = [random.randint(0, x - 1), random.randint(0, y - 1)]
while board[new_spot[1]][new_spot[0]] == 'm' or new_spot == pair:
new_spot = [random.randint(0, x - 1), random.randint(0, y - 1)]
board[new_spot[1]][new_spot[0]] = 'm'
return board
def make_completed_board(board):
for y, row in enumerate(board):
for x, space in enumerate(row):
if space != 'm':
continue
surrounding = valid_surrounding(board, [x, y])
for tile in surrounding:
if board[tile[1]][tile[0]] == 'm':
continue
board[tile[1]][tile[0]] += 1
return board
if tile_num == 0:
can_chord = True
chordable = []
for coordinate in valid:
if tiles[coordinate[1]][coordinate[0]].color[1] == '#BEBEBE':
continue
if tiles[coordinate[1]][coordinate[0]].flagged and not can_chord:
continue
if board[coordinate[1]][coordinate[0]] == 'm' and can_chord:
continue
chordable.append(coordinate)
return {'tiles': chordable, 'can_chord': can_chord}
if length == len(empty_tiles):
run = False
nearby_nums = []
for space in empty_tiles:
surrounding = valid_surrounding(board, space)
for coordinate in surrounding:
if coordinate in nearby_nums or coordinate in empty_tiles:
continue
nearby_nums.append(coordinate)
return {
'empty': empty_tiles,
'nums': nearby_nums
}
def mouse_pos(tiles):
for y in tiles:
for tile in y:
if tile.hitbox.collidepoint(pygame.mouse.get_pos()):
coordinate = [int(tile.x / TILE_SIZE), int((tile.y - BANNER_SIZE) / TILE_SIZE)]
return coordinate
if __name__ == '__main__':
settings = [18, 14, 40]
while True:
settings = main(settings)
if not settings:
break