@@ -69,6 +69,10 @@ def __init__(self,bx,by):
69
69
self .blocks_in_pile = by
70
70
# Score settings
71
71
self .score = 0
72
+ # Remember the current speed
73
+ self .speed = 1
74
+ # The score level threshold
75
+ self .score_level = constants .SCORE_LEVEL
72
76
73
77
def apply_action (self ):
74
78
"""
@@ -109,7 +113,16 @@ def pause(self):
109
113
for ev in pygame .event .get ():
110
114
if ev .type == pygame .KEYDOWN and ev .key == pygame .K_p :
111
115
return
112
-
116
+
117
+ def set_move_timer (self ):
118
+ """
119
+ Setup the move timer to the
120
+ """
121
+ # Setup the time to fire the move event. Minimal allowed value is 1
122
+ speed = math .floor (constants .MOVE_TICK / self .speed )
123
+ speed = max (1 ,speed )
124
+ pygame .time .set_timer (constants .TIMER_MOVE_EVENT ,speed )
125
+
113
126
def run (self ):
114
127
# Initialize the game (pygame, fonts)
115
128
pygame .init ()
@@ -118,7 +131,7 @@ def run(self):
118
131
self .screen = pygame .display .set_mode ((self .resx ,self .resy ))
119
132
pygame .display .set_caption ("Tetris" )
120
133
# Setup the time to fire the move event every given time
121
- pygame . time . set_timer ( constants . TIMER_MOVE_EVENT , constants . MOVE_TICK )
134
+ self . set_move_timer ( )
122
135
# Control variables for the game. The done signal is used
123
136
# to control the main loop (it is set by the quit action), the game_over signal
124
137
# is set by the game logic and it is also used for the detection of "game over" drawing.
@@ -127,7 +140,7 @@ def run(self):
127
140
self .game_over = False
128
141
self .new_block = True
129
142
# Print the initial score
130
- self .print_score ()
143
+ self .print_status_line ()
131
144
while not (self .done ) and not (self .game_over ):
132
145
# Get the block and run the game logic
133
146
self .get_block ()
@@ -140,11 +153,11 @@ def run(self):
140
153
pygame .font .quit ()
141
154
pygame .display .quit ()
142
155
143
- def print_score (self ):
156
+ def print_status_line (self ):
144
157
"""
145
- Print the current score.
158
+ Print the current state line
146
159
"""
147
- string = ["SCORE: {0}" .format (self .score )]
160
+ string = ["SCORE: {0} SPEED: {1}x " .format (self .score , self . speed )]
148
161
self .print_text (string ,constants .POINT_MARGIN ,constants .POINT_MARGIN )
149
162
150
163
def print_game_over (self ):
@@ -255,6 +268,12 @@ def detect_line(self):
255
268
self .remove_line (tmp_y )
256
269
# Update the score.
257
270
self .score += self .blocks_in_line * constants .POINT_VALUE
271
+ # Check if we need to speed up the game. If yes, change control variables
272
+ if self .score > self .score_level :
273
+ self .score_level *= constants .SCORE_LEVEL_RATIO
274
+ self .speed *= constants .GAME_SPEEDUP_RATIO
275
+ # Change the game speed
276
+ self .set_move_timer ()
258
277
259
278
def remove_line (self ,y ):
260
279
"""
@@ -294,7 +313,7 @@ def draw_board(self):
294
313
pygame .draw .rect (self .screen ,constants .WHITE ,self .board_left )
295
314
pygame .draw .rect (self .screen ,constants .WHITE ,self .board_right )
296
315
# Update the score
297
- self .print_score ()
316
+ self .print_status_line ()
298
317
299
318
def get_block (self ):
300
319
"""
0 commit comments