Skip to content

Commit

Permalink
Pysweeper version 0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Chubercik committed Mar 16, 2022
1 parent 18ac1e9 commit e19c506
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 90 deletions.
2 changes: 1 addition & 1 deletion nuitka.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
python -m nuitka --mingw64 --onefile --plugin-enable=numpy --include-data-file=../pysweeper/dlls/libmpg123-0.dll=libmpg123-0.dll --include-data-dir=../pysweeper/textures=textures --include-data-dir=../pysweeper/fonts=fonts --include-data-dir=../pysweeper/sounds=sounds --windows-icon-from-ico=../pysweeper/textures/bomb.png --windows-company-name=chubercik --windows-product-name=pysweeper --windows-product-version=0.1 --windows-disable-console main.py -o pysweeper.exe
python -m nuitka --mingw64 --onefile --include-data-file=../pysweeper/dlls/libmpg123-0.dll=libmpg123-0.dll --include-data-dir=../pysweeper/textures=textures --include-data-dir=../pysweeper/fonts=fonts --include-data-dir=../pysweeper/sounds=sounds --windows-icon-from-ico=../pysweeper/textures/bomb.png --windows-company-name=chubercik --windows-product-name=pysweeper --windows-file-version=0.1 --windows-product-version=0.1 --windows-file-description="a Minesweeper clone written in Python" --windows-disable-console main.py -o pysweeper.exe
Binary file modified pysweeper.exe
Binary file not shown.
98 changes: 53 additions & 45 deletions pysweeper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from sys import exit

from utilities import Board, Button, Smiley, pygame, screen, sec_to_time
from utilities import Board, Smiley, Timer, pygame, screen


class Pysweeper:
Expand All @@ -16,6 +16,29 @@ def __init__(self, width: int, height: int, bombs: int) -> None:
+ 32*self._width//2),
y=(self._board._top_offset - 64))
self._time = 0
self._timer_0 = Timer(x=(self._board._left_offset + 32
+ 32*self._width//2),
y=(self._board._top_offset - 64))
self._timer_1 = Timer(x=(self._board._left_offset + 64
+ 32*self._width//2),
y=(self._board._top_offset - 64))
self._timer_2 = Timer(x=(self._board._left_offset + 96
+ 32*self._width//2),
y=(self._board._top_offset - 64))
self._timer = [self._timer_0, self._timer_1, self._timer_2]
self._score_0 = Timer(x=(self._board._left_offset - 128
+ 32*self._width//2),
y=(self._board._top_offset - 64))
self._score_1 = Timer(x=(self._board._left_offset - 96
+ 32*self._width//2),
y=(self._board._top_offset - 64))
self._score_2 = Timer(x=(self._board._left_offset - 64
+ 32*self._width//2),
y=(self._board._top_offset - 64))
self._score = [self._score_0, self._score_1, self._score_2]
self._score[0].set_number(self._bombs // 100)
self._score[1].set_number(self._bombs // 10)
self._score[2].set_number(self._bombs % 10)

def run(self) -> None:
pygame.init()
Expand All @@ -33,22 +56,27 @@ def run(self) -> None:
clock = pygame.time.Clock()

while True:
screen.fill((255, 255, 255))
screen.fill((170, 170, 170))

self._board.draw()
self._smiley.draw(screen)
for timer in self._timer:
timer.draw(screen)
for score in self._score:
score.draw(screen)

mouse_pos = pygame.mouse.get_pos()
mouse_x = (mouse_pos[0] - self._board._left_offset) // 32
mouse_y = (mouse_pos[1] - self._board._top_offset) // 32

text_font = pygame.font.Font("fonts/minecraft_regular.ttf", 16)

for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
self._smiley.set_in_awe()
if event.type == pygame.MOUSEBUTTONUP:
self._smiley.set_reset()
if mouse_x < self._width and \
mouse_x >= 0 and \
mouse_y < self._height and \
Expand All @@ -65,64 +93,44 @@ def run(self) -> None:
else:
self._flags += 1
block.flag()
self._score[0].set_number((self._bombs - self._flags) // 100)
self._score[1].set_number((self._bombs - self._flags) // 10)
self._score[2].set_number((self._bombs - self._flags) % 10)

elif event.button == 2:
if block.is_question_mark():
self._question_marks -= 1
block.unquestion_mark()
else:
self._question_marks += 1
block.question_mark()
if mouse_pos[0] < self._smiley.get_position()[0] + 64 and \
mouse_pos[0] >= self._smiley.get_position()[0] and \
mouse_pos[1] < self._smiley.get_position()[1] + 64 and \
mouse_pos[1] >= self._smiley.get_position()[1]:
if event.button == 1:
self.__init__(self._width,
self._height,
self._bombs)
play_sound = True

self._board.check_win()

fps_counter = text_font.render(f"FPS: {int(clock.get_fps())}",
True,
(0, 0, 0))
screen.blit(fps_counter, (8 + self._board._left_offset, 40))

num_flags = text_font.render(f"Bombs: {self._bombs - self._flags}",
True,
(0, 0, 0))
screen.blit(num_flags, (40 + self._board._left_offset
+ fps_counter.get_width(), 40))

num_clicks = text_font.render(f"Clicks: {self._clicks}",
True,
(0, 0, 0))
screen.blit(num_clicks, (72 + self._board._left_offset
+ fps_counter.get_width()
+ num_flags.get_width(), 40))

time = text_font.render(f"Time: {sec_to_time(self._time)}",
True,
(0, 0, 0))
screen.blit(time, (104 + self._board._left_offset
+ fps_counter.get_width()
+ num_flags.get_width()
+ num_clicks.get_width(), 40))
if self._board._game_over == "WIN":
self._smiley.set_cool()

if self._board._game_over == "LOSE":
if play_sound:
pygame.mixer.music.load("sounds/explosion.mp3")
pygame.mixer.music.play()
play_sound = False
dim_light = pygame.Surface((32*self._width, 32*self._height))
dim_light.set_alpha(100)
dim_light.fill((0, 0, 0))
screen.blit(dim_light, (self._board._left_offset,
self._board._top_offset))
button = Button(x=((32*self._width - 128)/2
+ self._board._left_offset),
y=((32*self._height - 64)/2
+ self._board._top_offset),
width=128,
height=64,
text="Restart")
button.draw()
if button.is_clicked(mouse_pos):
self.__init__(self._width, self._height, self._bombs)
play_sound = True
self._smiley.set_dead()

if self._board._game_over is None:
self._time += 1/60
self._timer[0].set_number(int(self._time) // 100)
self._timer[1].set_number(int(self._time) // 10)
self._timer[2].set_number(int(self._time) % 10)

pygame.display.update()
clock.tick(60)
Expand Down
31 changes: 30 additions & 1 deletion sprites.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ctypes
import os
from typing import Tuple

user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
Expand All @@ -18,6 +19,31 @@

screen = pygame.display.set_mode(flags=pygame.HIDDEN)


def img_outline(img: pygame.Surface,
color: Tuple[int, int, int],
loc: Tuple[int, int],
screen: pygame.Surface) -> None:
mask = pygame.mask.from_surface(img)
mask_outline = mask.outline()
mask_surf = pygame.Surface(img.get_size())
for pixel in mask_outline:
mask_surf.set_at(pixel, color)
mask_surf.set_colorkey((0, 0, 0))
screen.blit(mask_surf, (loc[0] - 1, loc[1]))
screen.blit(mask_surf, (loc[0] + 1, loc[1]))
screen.blit(mask_surf, (loc[0], loc[1] - 1))
screen.blit(mask_surf, (loc[0], loc[1] + 1))


def blit_sprite(sprite: pygame.Surface,
outline_color: Tuple[int, int, int],
location: Tuple[int, int],
screen: pygame.Surface) -> None:
img_outline(sprite, outline_color, location, screen)
screen.blit(sprite, location)


bomb_explode_sprite = pygame.image.load("textures/bomb_explode.png")
bomb_no_sprite = pygame.image.load("textures/bomb_no.png")
bomb_sprite = pygame.image.load("textures/bomb.png")
Expand Down Expand Up @@ -52,6 +78,8 @@
smiley_yeah_sprite = pygame.image.load("textures/smiley_yeah.png")
smiley_sprite = pygame.image.load("textures/smiley.png")

tile = pygame.image.load("textures/tile.png")


class Sprites:
def __init__(self):
Expand Down Expand Up @@ -83,7 +111,8 @@ def __init__(self):
"smiley_rip": smiley_rip_sprite,
"smiley_wow": smiley_wow_sprite,
"smiley_yeah": smiley_yeah_sprite,
"smiley": smiley_sprite
"smiley": smiley_sprite,
"tile": tile
}

for i, sprite in self.sprites.items():
Expand Down
Binary file added textures/tile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e19c506

Please sign in to comment.