11from 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
66class 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 )
0 commit comments