Skip to content

Commit e19c506

Browse files
committed
Pysweeper version 0.1
1 parent 18ac1e9 commit e19c506

File tree

6 files changed

+183
-90
lines changed

6 files changed

+183
-90
lines changed

nuitka.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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
1+
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

pysweeper.exe

-4 MB
Binary file not shown.

pysweeper.py

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from sys import exit
22

3-
from utilities import Board, Button, Smiley, pygame, screen, sec_to_time
3+
from utilities import Board, Smiley, Timer, pygame, screen
44

55

66
class Pysweeper:
@@ -16,6 +16,29 @@ def __init__(self, width: int, height: int, bombs: int) -> None:
1616
+ 32*self._width//2),
1717
y=(self._board._top_offset - 64))
1818
self._time = 0
19+
self._timer_0 = Timer(x=(self._board._left_offset + 32
20+
+ 32*self._width//2),
21+
y=(self._board._top_offset - 64))
22+
self._timer_1 = Timer(x=(self._board._left_offset + 64
23+
+ 32*self._width//2),
24+
y=(self._board._top_offset - 64))
25+
self._timer_2 = Timer(x=(self._board._left_offset + 96
26+
+ 32*self._width//2),
27+
y=(self._board._top_offset - 64))
28+
self._timer = [self._timer_0, self._timer_1, self._timer_2]
29+
self._score_0 = Timer(x=(self._board._left_offset - 128
30+
+ 32*self._width//2),
31+
y=(self._board._top_offset - 64))
32+
self._score_1 = Timer(x=(self._board._left_offset - 96
33+
+ 32*self._width//2),
34+
y=(self._board._top_offset - 64))
35+
self._score_2 = Timer(x=(self._board._left_offset - 64
36+
+ 32*self._width//2),
37+
y=(self._board._top_offset - 64))
38+
self._score = [self._score_0, self._score_1, self._score_2]
39+
self._score[0].set_number(self._bombs // 100)
40+
self._score[1].set_number(self._bombs // 10)
41+
self._score[2].set_number(self._bombs % 10)
1942

2043
def run(self) -> None:
2144
pygame.init()
@@ -33,22 +56,27 @@ def run(self) -> None:
3356
clock = pygame.time.Clock()
3457

3558
while True:
36-
screen.fill((255, 255, 255))
59+
screen.fill((170, 170, 170))
3760

3861
self._board.draw()
3962
self._smiley.draw(screen)
63+
for timer in self._timer:
64+
timer.draw(screen)
65+
for score in self._score:
66+
score.draw(screen)
4067

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

45-
text_font = pygame.font.Font("fonts/minecraft_regular.ttf", 16)
46-
4772
for event in pygame.event.get():
4873
if event.type == pygame.QUIT:
4974
pygame.quit()
5075
exit()
5176
if event.type == pygame.MOUSEBUTTONDOWN:
77+
self._smiley.set_in_awe()
78+
if event.type == pygame.MOUSEBUTTONUP:
79+
self._smiley.set_reset()
5280
if mouse_x < self._width and \
5381
mouse_x >= 0 and \
5482
mouse_y < self._height and \
@@ -65,64 +93,44 @@ def run(self) -> None:
6593
else:
6694
self._flags += 1
6795
block.flag()
96+
self._score[0].set_number((self._bombs - self._flags) // 100)
97+
self._score[1].set_number((self._bombs - self._flags) // 10)
98+
self._score[2].set_number((self._bombs - self._flags) % 10)
99+
68100
elif event.button == 2:
69101
if block.is_question_mark():
70102
self._question_marks -= 1
71103
block.unquestion_mark()
72104
else:
73105
self._question_marks += 1
74106
block.question_mark()
107+
if mouse_pos[0] < self._smiley.get_position()[0] + 64 and \
108+
mouse_pos[0] >= self._smiley.get_position()[0] and \
109+
mouse_pos[1] < self._smiley.get_position()[1] + 64 and \
110+
mouse_pos[1] >= self._smiley.get_position()[1]:
111+
if event.button == 1:
112+
self.__init__(self._width,
113+
self._height,
114+
self._bombs)
115+
play_sound = True
116+
117+
self._board.check_win()
75118

76-
fps_counter = text_font.render(f"FPS: {int(clock.get_fps())}",
77-
True,
78-
(0, 0, 0))
79-
screen.blit(fps_counter, (8 + self._board._left_offset, 40))
80-
81-
num_flags = text_font.render(f"Bombs: {self._bombs - self._flags}",
82-
True,
83-
(0, 0, 0))
84-
screen.blit(num_flags, (40 + self._board._left_offset
85-
+ fps_counter.get_width(), 40))
86-
87-
num_clicks = text_font.render(f"Clicks: {self._clicks}",
88-
True,
89-
(0, 0, 0))
90-
screen.blit(num_clicks, (72 + self._board._left_offset
91-
+ fps_counter.get_width()
92-
+ num_flags.get_width(), 40))
93-
94-
time = text_font.render(f"Time: {sec_to_time(self._time)}",
95-
True,
96-
(0, 0, 0))
97-
screen.blit(time, (104 + self._board._left_offset
98-
+ fps_counter.get_width()
99-
+ num_flags.get_width()
100-
+ num_clicks.get_width(), 40))
119+
if self._board._game_over == "WIN":
120+
self._smiley.set_cool()
101121

102122
if self._board._game_over == "LOSE":
103123
if play_sound:
104124
pygame.mixer.music.load("sounds/explosion.mp3")
105125
pygame.mixer.music.play()
106126
play_sound = False
107-
dim_light = pygame.Surface((32*self._width, 32*self._height))
108-
dim_light.set_alpha(100)
109-
dim_light.fill((0, 0, 0))
110-
screen.blit(dim_light, (self._board._left_offset,
111-
self._board._top_offset))
112-
button = Button(x=((32*self._width - 128)/2
113-
+ self._board._left_offset),
114-
y=((32*self._height - 64)/2
115-
+ self._board._top_offset),
116-
width=128,
117-
height=64,
118-
text="Restart")
119-
button.draw()
120-
if button.is_clicked(mouse_pos):
121-
self.__init__(self._width, self._height, self._bombs)
122-
play_sound = True
127+
self._smiley.set_dead()
123128

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

127135
pygame.display.update()
128136
clock.tick(60)

sprites.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ctypes
22
import os
3+
from typing import Tuple
34

45
user32 = ctypes.windll.user32
56
screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
@@ -18,6 +19,31 @@
1819

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

22+
23+
def img_outline(img: pygame.Surface,
24+
color: Tuple[int, int, int],
25+
loc: Tuple[int, int],
26+
screen: pygame.Surface) -> None:
27+
mask = pygame.mask.from_surface(img)
28+
mask_outline = mask.outline()
29+
mask_surf = pygame.Surface(img.get_size())
30+
for pixel in mask_outline:
31+
mask_surf.set_at(pixel, color)
32+
mask_surf.set_colorkey((0, 0, 0))
33+
screen.blit(mask_surf, (loc[0] - 1, loc[1]))
34+
screen.blit(mask_surf, (loc[0] + 1, loc[1]))
35+
screen.blit(mask_surf, (loc[0], loc[1] - 1))
36+
screen.blit(mask_surf, (loc[0], loc[1] + 1))
37+
38+
39+
def blit_sprite(sprite: pygame.Surface,
40+
outline_color: Tuple[int, int, int],
41+
location: Tuple[int, int],
42+
screen: pygame.Surface) -> None:
43+
img_outline(sprite, outline_color, location, screen)
44+
screen.blit(sprite, location)
45+
46+
2147
bomb_explode_sprite = pygame.image.load("textures/bomb_explode.png")
2248
bomb_no_sprite = pygame.image.load("textures/bomb_no.png")
2349
bomb_sprite = pygame.image.load("textures/bomb.png")
@@ -52,6 +78,8 @@
5278
smiley_yeah_sprite = pygame.image.load("textures/smiley_yeah.png")
5379
smiley_sprite = pygame.image.load("textures/smiley.png")
5480

81+
tile = pygame.image.load("textures/tile.png")
82+
5583

5684
class Sprites:
5785
def __init__(self):
@@ -83,7 +111,8 @@ def __init__(self):
83111
"smiley_rip": smiley_rip_sprite,
84112
"smiley_wow": smiley_wow_sprite,
85113
"smiley_yeah": smiley_yeah_sprite,
86-
"smiley": smiley_sprite
114+
"smiley": smiley_sprite,
115+
"tile": tile
87116
}
88117

89118
for i, sprite in self.sprites.items():

textures/tile.png

204 Bytes
Loading

0 commit comments

Comments
 (0)