Reputation: 3
I'm creating a Snake game. When I lower the cooldown of the Timer I want the snake to move faster.
It's actually working, but only if I change the number of the game speed before starting the project. If I start the project and change the timer in game, by a function, then var for the timer gets lower (printed it in different situations) but the speed doesn't change.
game_speed = 100
SCRREEN_UPDATE = pygame.USEREVENT
pygame.time.set_timer(SCRREEN_UPDATE, game_speed)
while go:
events = pygame.event.get()
if not game_over:
screen.fill((0,90,10))
snake.draw_grass()
for event in events:
main_game.Quit(event)
if event.type == SCRREEN_UPDATE:
main_game.update()
self.list.append(self.user_input)
self.user_input = int(self.user_input)
if isinstance(self.user_input,str):
self.list = [0]
self.user_input = "0"
game_speed -= int(self.list[-1])
self.list = [0]
self.user_input = "0"
print(game_speed)
return game_speed
Upvotes: 0
Views: 58
Reputation: 211176
The timer event and the game_speed
variable are not tied. The timer interval doesn't magically change when you change the value of game_speed
. You have to restart the timer with the new interval. So after changing game_speed
, pygame.time.set_timer
needs to be called again:
game_speed -= int(self.list[-1])
pygame.time.set_timer(SCRREEN_UPDATE, game_speed)
Upvotes: 2