Skip to content

Commit 9eedc4f

Browse files
committed
Speed up of the game based on gathered score was implemented.
1 parent 470cc6a commit 9eedc4f

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

constants.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
MOVE_TICK = 1000
5353
# Allocated number for the move dowon event
5454
TIMER_MOVE_EVENT = USEREVENT+1
55+
# Speed up ratio of the game (integer values)
56+
GAME_SPEEDUP_RATIO = 1.5
57+
# Score LEVEL - first threshold of the score
58+
SCORE_LEVEL = 2000
59+
# Score level ratio
60+
SCORE_LEVEL_RATIO = 2
5561

5662
# Configuration of score
5763
# Number of points for one building block

tetris.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def __init__(self,bx,by):
6969
self.blocks_in_pile = by
7070
# Score settings
7171
self.score = 0
72+
# Remember the current speed
73+
self.speed = 1
74+
# The score level threshold
75+
self.score_level = constants.SCORE_LEVEL
7276

7377
def apply_action(self):
7478
"""
@@ -109,7 +113,16 @@ def pause(self):
109113
for ev in pygame.event.get():
110114
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_p:
111115
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+
113126
def run(self):
114127
# Initialize the game (pygame, fonts)
115128
pygame.init()
@@ -118,7 +131,7 @@ def run(self):
118131
self.screen = pygame.display.set_mode((self.resx,self.resy))
119132
pygame.display.set_caption("Tetris")
120133
# 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()
122135
# Control variables for the game. The done signal is used
123136
# to control the main loop (it is set by the quit action), the game_over signal
124137
# 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):
127140
self.game_over = False
128141
self.new_block = True
129142
# Print the initial score
130-
self.print_score()
143+
self.print_status_line()
131144
while not(self.done) and not(self.game_over):
132145
# Get the block and run the game logic
133146
self.get_block()
@@ -140,11 +153,11 @@ def run(self):
140153
pygame.font.quit()
141154
pygame.display.quit()
142155

143-
def print_score(self):
156+
def print_status_line(self):
144157
"""
145-
Print the current score.
158+
Print the current state line
146159
"""
147-
string = ["SCORE: {0}".format(self.score)]
160+
string = ["SCORE: {0} SPEED: {1}x".format(self.score,self.speed)]
148161
self.print_text(string,constants.POINT_MARGIN,constants.POINT_MARGIN)
149162

150163
def print_game_over(self):
@@ -255,6 +268,12 @@ def detect_line(self):
255268
self.remove_line(tmp_y)
256269
# Update the score.
257270
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()
258277

259278
def remove_line(self,y):
260279
"""
@@ -294,7 +313,7 @@ def draw_board(self):
294313
pygame.draw.rect(self.screen,constants.WHITE,self.board_left)
295314
pygame.draw.rect(self.screen,constants.WHITE,self.board_right)
296315
# Update the score
297-
self.print_score()
316+
self.print_status_line()
298317

299318
def get_block(self):
300319
"""

0 commit comments

Comments
 (0)