@@ -24,53 +24,53 @@ def __init__(self,
2424 height : int ,
2525 bombs : int ,
2626 jr_reveal : bool = False ) -> None :
27- self ._width = width
28- self ._height = height
29- self ._clicks = 0
30- self ._flags = 0
31- self ._question_marks = 0
32- self ._board = Board (width , height , bombs )
33- self ._bombs = self ._board . _bomb_count
34- self ._smiley = Smiley (x = (self ._board . _left_offset - 32
35- + 32 * self ._width // 2 ),
36- y = (self ._board . _top_offset - 64 ))
37- self ._time : float = 0
38- self ._timer_0 = Timer (x = (self ._board . _left_offset + 32
39- + 32 * self ._width // 2 ),
40- y = (self ._board . _top_offset - 64 ))
41- self ._timer_1 = Timer (x = (self ._board . _left_offset + 64
42- + 32 * self ._width // 2 ),
43- y = (self ._board . _top_offset - 64 ))
44- self ._timer_2 = Timer (x = (self ._board . _left_offset + 96
45- + 32 * self ._width // 2 ),
46- y = (self ._board . _top_offset - 64 ))
47- self ._timer = [self ._timer_0 , self ._timer_1 , self ._timer_2 ]
48- self ._score_0 = Timer (x = (self ._board . _left_offset - 128
49- + 32 * self ._width // 2 ),
50- y = (self ._board . _top_offset - 64 ))
51- self ._score_1 = Timer (x = (self ._board . _left_offset - 96
52- + 32 * self ._width // 2 ),
53- y = (self ._board . _top_offset - 64 ))
54- self ._score_2 = Timer (x = (self ._board . _left_offset - 64
55- + 32 * self ._width // 2 ),
56- y = (self ._board . _top_offset - 64 ))
57- self ._score = [self ._score_0 , self ._score_1 , self ._score_2 ]
58- self ._score [0 ].set_number (self ._bombs // 100 )
59- self ._score [1 ].set_number ((self ._bombs % 100 ) // 10 )
60- self ._score [2 ].set_number (self ._bombs % 10 )
61- self ._first_move = True
62- self ._play_sound = True
63- self ._new_highscore = True
64- self ._jr_reveal = jr_reveal
27+ self .width = width
28+ self .height = height
29+ self .clicks = 0
30+ self .flags = 0
31+ self .question_marks = 0
32+ self .board = Board (width , height , bombs )
33+ self .bombs = self .board . bomb_count
34+ self .smiley = Smiley (x = (self .board . left_offset - 32
35+ + 32 * self .width // 2 ),
36+ y = (self .board . top_offset - 64 ))
37+ self .time : float = 0
38+ self .timer_0 = Timer (x = (self .board . left_offset + 32
39+ + 32 * self .width // 2 ),
40+ y = (self .board . top_offset - 64 ))
41+ self .timer_1 = Timer (x = (self .board . left_offset + 64
42+ + 32 * self .width // 2 ),
43+ y = (self .board . top_offset - 64 ))
44+ self .timer_2 = Timer (x = (self .board . left_offset + 96
45+ + 32 * self .width // 2 ),
46+ y = (self .board . top_offset - 64 ))
47+ self .timer = [self .timer_0 , self .timer_1 , self .timer_2 ]
48+ self .score_0 = Timer (x = (self .board . left_offset - 128
49+ + 32 * self .width // 2 ),
50+ y = (self .board . top_offset - 64 ))
51+ self .score_1 = Timer (x = (self .board . left_offset - 96
52+ + 32 * self .width // 2 ),
53+ y = (self .board . top_offset - 64 ))
54+ self .score_2 = Timer (x = (self .board . left_offset - 64
55+ + 32 * self .width // 2 ),
56+ y = (self .board . top_offset - 64 ))
57+ self .score = [self .score_0 , self .score_1 , self .score_2 ]
58+ self .score [0 ].number = (self .bombs // 100 )
59+ self .score [1 ].number = ((self .bombs % 100 ) // 10 )
60+ self .score [2 ].number = (self .bombs % 10 )
61+ self .first_move = True
62+ self .play_sound = True
63+ self .new_highscore = True
64+ self .jr_reveal = jr_reveal
6565
6666 def run (self ) -> None :
6767 pg .init ()
6868 pg .mixer .init ()
6969
7070 pg .display .set_caption ("pysweeper" )
7171 pg .display .set_icon (pg .image .load (load_file ("textures/bomb.png" )))
72- pg .display .set_mode (size = (32 * self ._width + 200 ,
73- 32 * self ._height + 200 ),
72+ pg .display .set_mode (size = (32 * self .width + 200 ,
73+ 32 * self .height + 200 ),
7474 flags = pg .RESIZABLE ,
7575 vsync = True )
7676
@@ -85,21 +85,21 @@ def run(self) -> None:
8585 while True :
8686 screen .fill ((170 , 170 , 170 ))
8787
88- self ._board .draw ()
89- self ._smiley .draw (screen )
90- for timer in self ._timer :
88+ self .board .draw ()
89+ self .smiley .draw (screen )
90+ for timer in self .timer :
9191 timer .draw (screen )
92- for score in self ._score :
92+ for score in self .score :
9393 score .draw (screen )
9494
9595 mouse_pos = pg .mouse .get_pos ()
96- mouse_x = (mouse_pos [0 ] - self ._board . _left_offset ) // 32
97- mouse_y = (mouse_pos [1 ] - self ._board . _top_offset ) // 32
96+ mouse_x = (mouse_pos [0 ] - self .board . left_offset ) // 32
97+ mouse_y = (mouse_pos [1 ] - self .board . top_offset ) // 32
9898
9999 if cheat and sys_name == "Windows" and \
100- mouse_x < self ._width and mouse_x >= 0 and \
101- mouse_y < self ._height and mouse_y >= 0 and \
102- not self ._board . _board [mouse_y ][mouse_x ].is_bomb () :
100+ mouse_x < self .width and mouse_x >= 0 and \
101+ mouse_y < self .height and mouse_y >= 0 and \
102+ not self .board . board [mouse_y ][mouse_x ].is_bomb :
103103 win32gui .SetPixel (dc , 0 , 0 , white )
104104 elif sys_name == "Windows" :
105105 win32gui .SetPixel (dc , 0 , 0 , temp )
@@ -110,55 +110,55 @@ def run(self) -> None:
110110 exit ()
111111
112112 if event .type == pg .MOUSEBUTTONDOWN :
113- self ._smiley .set_in_awe ()
113+ self .smiley .set_in_awe ()
114114
115115 if event .type == pg .MOUSEBUTTONUP :
116- self ._smiley .set_reset ()
117- if mouse_x < self ._width and \
116+ self .smiley .set_reset ()
117+ if mouse_x < self .width and \
118118 mouse_x >= 0 and \
119- mouse_y < self ._height and \
119+ mouse_y < self .height and \
120120 mouse_y >= 0 and \
121- self ._board . _game_over is None :
122- block = self ._board . _board [mouse_y ][mouse_x ]
123- if event .button == 1 and not block .is_revealed () \
124- and not block .is_flagged () :
125- self ._clicks += 1
126- if self ._first_move :
127- if block .is_bomb () :
128- self ._board .move_bomb (mouse_x , mouse_y )
129- self ._first_move = False
130- self ._board .reveal (mouse_x , mouse_y )
131- elif event .button == 1 and block .is_revealed () \
132- and self ._jr_reveal :
133- self ._clicks += 1
121+ self .board . game_over is None :
122+ block = self .board . board [mouse_y ][mouse_x ]
123+ if event .button == 1 and not block .is_revealed \
124+ and not block .is_flagged :
125+ self .clicks += 1
126+ if self .first_move :
127+ if block .is_bomb :
128+ self .board .move_bomb (mouse_x , mouse_y )
129+ self .first_move = False
130+ self .board .reveal (mouse_x , mouse_y )
131+ elif event .button == 1 and block .is_revealed \
132+ and self .jr_reveal :
133+ self .clicks += 1
134134 for i , j in itertools .product (range (- 1 , 2 ), range (- 1 , 2 )):
135135 if (
136- 0 <= mouse_x + i < self ._width
137- and 0 <= mouse_y + j < self ._height
138- and not self ._board . _board [mouse_y + j ][mouse_x + i ].is_revealed ()
139- and not self ._board . _board [mouse_y + j ][mouse_x + i ].is_flagged ()
136+ 0 <= mouse_x + i < self .width
137+ and 0 <= mouse_y + j < self .height
138+ and not self .board . board [mouse_y + j ][mouse_x + i ].is_revealed
139+ and not self .board . board [mouse_y + j ][mouse_x + i ].is_flagged
140140 ):
141- self ._board .reveal (mouse_x + i , mouse_y + j )
142- elif event .button == 3 and not block .is_revealed () :
143- if block .is_flagged () :
144- self ._flags -= 1
145- block .unflag ()
141+ self .board .reveal (mouse_x + i , mouse_y + j )
142+ elif event .button == 3 and not block .is_revealed :
143+ if block .is_flagged :
144+ self .flags -= 1
145+ block .is_flagged = False
146146 else :
147- self ._flags += 1
148- block .flag ()
147+ self .flags += 1
148+ block .is_flagged = True
149149 self .update_score ()
150150 elif event .button == 2 :
151- if not block .is_flagged () :
152- if block .is_question_mark () :
153- self ._question_marks -= 1
154- block .unquestion_mark ()
151+ if not block .is_flagged :
152+ if block .is_question_mark :
153+ self .question_marks -= 1
154+ block .is_question_mark = False
155155 else :
156- self ._question_marks += 1
157- block .question_mark ()
158- if mouse_pos [0 ] < self ._smiley .get_position ()[0 ] + 64 and \
159- mouse_pos [0 ] >= self ._smiley .get_position ()[0 ] and \
160- mouse_pos [1 ] < self ._smiley .get_position ()[1 ] + 64 and \
161- mouse_pos [1 ] >= self ._smiley .get_position ()[1 ] and \
156+ self .question_marks += 1
157+ block .is_question_mark = True
158+ if mouse_pos [0 ] < self .smiley .get_position ()[0 ] + 64 and \
159+ mouse_pos [0 ] >= self .smiley .get_position ()[0 ] and \
160+ mouse_pos [1 ] < self .smiley .get_position ()[1 ] + 64 and \
161+ mouse_pos [1 ] >= self .smiley .get_position ()[1 ] and \
162162 event .button == 1 :
163163 self .restart ()
164164
@@ -185,106 +185,106 @@ def run(self) -> None:
185185 text = font .render ("" .join (input_arr ), True , (0 , 0 , 0 ))
186186 screen .blit (text , (50 , 750 ))
187187
188- self ._board .check_win ()
188+ self .board .check_win ()
189189
190- if self ._board . _game_over == "WIN" :
191- self ._smiley .set_cool ()
192- if self ._new_highscore :
190+ if self .board . game_over == "WIN" :
191+ self .smiley .set_cool ()
192+ if self .new_highscore :
193193 highscores = read_json (load_file ("data/data.json" ))
194194 score_id = len (highscores )
195195 summary = {
196- "width" : self ._width ,
197- "height" : self ._height ,
198- "bombs" : self ._bombs ,
199- "clicks" : self ._clicks ,
200- "time" : round (self ._time , 3 )
196+ "width" : self .width ,
197+ "height" : self .height ,
198+ "bombs" : self .bombs ,
199+ "clicks" : self .clicks ,
200+ "time" : round (self .time , 3 )
201201 }
202202 highscores [score_id ] = summary
203203 write_json (load_file ("data/data.json" ), highscores )
204- self ._new_highscore = False
204+ self .new_highscore = False
205205
206- if self ._board . _game_over == "LOSE" :
207- if self ._play_sound :
206+ if self .board . game_over == "LOSE" :
207+ if self .play_sound :
208208 pg .mixer .music .load (load_file ("sounds/explosion.mp3" ))
209209 pg .mixer .music .play ()
210- self ._play_sound = False
211- self ._smiley .set_dead ()
210+ self .play_sound = False
211+ self .smiley .set_dead ()
212212
213- if self ._board . _game_over is None and not self ._first_move :
214- self ._time += 1 / 60
215- self ._timer [0 ].set_number (int (self ._time ) // 100 )
216- self ._timer [1 ].set_number ((int (self ._time ) % 100 ) // 10 )
217- self ._timer [2 ].set_number (int (self ._time ) % 10 )
213+ if self .board . game_over is None and not self .first_move :
214+ self .time += 1 / 60
215+ self .timer [0 ].number = (int (self .time ) // 100 )
216+ self .timer [1 ].number = ((int (self .time ) % 100 ) // 10 )
217+ self .timer [2 ].number = (int (self .time ) % 10 )
218218
219219 clock .tick (60 )
220220
221221 pg .display .update ()
222222
223223 def restart (self ) -> None :
224- self ._clicks = 0
225- self ._flags = 0
226- self ._question_marks = 0
227- temp_left_offset = self ._board . _left_offset
228- temp_top_offset = self ._board . _top_offset
229- self ._board = Board (self ._width , self ._height , self ._bombs )
230- self ._board . _left_offset = temp_left_offset
231- self ._board . _top_offset = temp_top_offset
232- self ._board .window_resize ()
233- self ._smiley = Smiley (x = (self ._board . _left_offset - 32
234- + 32 * self ._width // 2 ),
235- y = (self ._board . _top_offset - 64 ))
236- self ._time = 0
224+ self .clicks = 0
225+ self .flags = 0
226+ self .question_marks = 0
227+ temp_left_offset = self .board . left_offset
228+ temp_top_offset = self .board . top_offset
229+ self .board = Board (self .width , self .height , self .bombs )
230+ self .board . left_offset = temp_left_offset
231+ self .board . top_offset = temp_top_offset
232+ self .board .window_resize ()
233+ self .smiley = Smiley (x = (self .board . left_offset - 32
234+ + 32 * self .width // 2 ),
235+ y = (self .board . top_offset - 64 ))
236+ self .time = 0
237237 self .set_digit_displays ()
238- self ._score [0 ].set_number (self ._bombs // 100 )
239- self ._score [1 ].set_number ((self ._bombs % 100 ) // 10 )
240- self ._score [2 ].set_number (self ._bombs % 10 )
241- self ._first_move = True
242- self ._new_highscore = True
243- self ._play_sound = True
238+ self .score [0 ].number = (self .bombs // 100 )
239+ self .score [1 ].number = ((self .bombs % 100 ) // 10 )
240+ self .score [2 ].number = (self .bombs % 10 )
241+ self .first_move = True
242+ self .new_highscore = True
243+ self .play_sound = True
244244
245245 def resize (self ) -> None :
246- self ._board . _left_offset = (pg .display .get_surface ().get_width () -
247- 32 * self ._width )// 2
248- self ._board . _top_offset = (pg .display .get_surface ().get_height () -
249- 32 * self ._height )// 2
250- self ._smiley = Smiley (x = (self ._board . _left_offset - 32
251- + 32 * self ._width // 2 ),
252- y = (self ._board . _top_offset - 64 ))
246+ self .board . left_offset = (pg .display .get_surface ().get_width () -
247+ 32 * self .width )// 2
248+ self .board . top_offset = (pg .display .get_surface ().get_height () -
249+ 32 * self .height )// 2
250+ self .smiley = Smiley (x = (self .board . left_offset - 32
251+ + 32 * self .width // 2 ),
252+ y = (self .board . top_offset - 64 ))
253253 self .set_digit_displays ()
254254 self .update_score ()
255- self ._board .window_resize ()
255+ self .board .window_resize ()
256256
257257 def set_digit_displays (self ):
258- self ._timer_0 = Timer (x = (self ._board . _left_offset
259- + 32 + 32 * self ._width // 2 ),
260- y = self ._board . _top_offset - 64 )
258+ self .timer_0 = Timer (x = (self .board . left_offset
259+ + 32 + 32 * self .width // 2 ),
260+ y = self .board . top_offset - 64 )
261261
262- self ._timer_1 = Timer (x = (self ._board . _left_offset
263- + 64 + 32 * self ._width // 2 ),
264- y = self ._board . _top_offset - 64 )
262+ self .timer_1 = Timer (x = (self .board . left_offset
263+ + 64 + 32 * self .width // 2 ),
264+ y = self .board . top_offset - 64 )
265265
266- self ._timer_2 = Timer (x = (self ._board . _left_offset
267- + 96 + 32 * self ._width // 2 ),
268- y = self ._board . _top_offset - 64 )
269- self ._timer = [self ._timer_0 , self ._timer_1 , self ._timer_2 ]
266+ self .timer_2 = Timer (x = (self .board . left_offset
267+ + 96 + 32 * self .width // 2 ),
268+ y = self .board . top_offset - 64 )
269+ self .timer = [self .timer_0 , self .timer_1 , self .timer_2 ]
270270
271- self ._score_0 = Timer (x = (self ._board . _left_offset
272- - 128 + 32 * self ._width // 2 ),
273- y = self ._board . _top_offset - 64 )
271+ self .score_0 = Timer (x = (self .board . left_offset
272+ - 128 + 32 * self .width // 2 ),
273+ y = self .board . top_offset - 64 )
274274
275- self ._score_1 = Timer (x = (self ._board . _left_offset
276- - 96 + 32 * self ._width // 2 ),
277- y = self ._board . _top_offset - 64 )
275+ self .score_1 = Timer (x = (self .board . left_offset
276+ - 96 + 32 * self .width // 2 ),
277+ y = self .board . top_offset - 64 )
278278
279- self ._score_2 = Timer (x = (self ._board . _left_offset
280- - 64 + 32 * self ._width // 2 ),
281- y = self ._board . _top_offset - 64 )
282- self ._score = [self ._score_0 , self ._score_1 , self ._score_2 ]
279+ self .score_2 = Timer (x = (self .board . left_offset
280+ - 64 + 32 * self .width // 2 ),
281+ y = self .board . top_offset - 64 )
282+ self .score = [self .score_0 , self .score_1 , self .score_2 ]
283283
284284 def update_score (self ):
285- self ._score [0 ].set_number ((((self ._bombs - self ._flags )) // 100 ))
286- self ._score [1 ].set_number (((((self ._bombs - self ._flags )) % 100 ) // 10 ))
287- self ._score [2 ].set_number ((((self ._bombs - self ._flags )) % 10 ))
285+ self .score [0 ].number = ((((self .bombs - self .flags )) // 100 ))
286+ self .score [1 ].number = (((((self .bombs - self .flags )) % 100 ) // 10 ))
287+ self .score [2 ].number = ((((self .bombs - self .flags )) % 10 ))
288288
289289
290290def main ():
0 commit comments