Oct-23-2024, 01:29 PM
I've tried several methods but haven't been able to get it right at all.
Every 5 seconds the score (starting at 0) goes up by 1. Every 30 seconds, the score gets doubled, so in the first 30 seconds, the score will be 6, and since that will be 30 seconds, the 6 gets doubled to 12. However in my case, the score goes to 11. My other attempts have made the score go to 10, but I can never get it to to correctly double.
Every 5 seconds the score (starting at 0) goes up by 1. Every 30 seconds, the score gets doubled, so in the first 30 seconds, the score will be 6, and since that will be 30 seconds, the 6 gets doubled to 12. However in my case, the score goes to 11. My other attempts have made the score go to 10, but I can never get it to to correctly double.
import pygame import sys pygame.init() screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption('game test 1') white = (255, 255, 255) black = (0, 0, 0) clock = pygame.time.Clock() font = pygame.font.SysFont(None, 48) score = 0 last_score_increase = 0 last_score_double = 0 def display_time(time_ms): # get mm:ss seconds = int((time_ms / 1000) % 60) minutes = int((time_ms / (1000 * 60)) % 60) return f"{minutes:02}:{seconds:02}" def game_loop(): global score, last_score_increase, last_score_double start_time = pygame.time.get_ticks() running = True while running: current_time = pygame.time.get_ticks() - start_time for event in pygame.event.get(): if event.type == pygame.QUIT: return False, score if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: return current_time, score if event.key == pygame.K_r: return "restart", score if current_time - last_score_increase >= 5000: score += 1 last_score_increase = current_time if current_time >= 30000 and current_time - last_score_double >= 30000: score *= 2 last_score_double = current_time screen.fill(white) score_text = font.render(f'Score: {score}', True, black) screen.blit(score_text, (10, 10)) pygame.display.flip() clock.tick(60) return False, score def end_screen(elapsed_time, score): screen.fill(white) time_text = font.render(f"Time: {display_time(elapsed_time)} Score: {score}", True, black) screen.blit(time_text, (screen_width // 2 - time_text.get_width() // 2, screen_height // 2 - time_text.get_height() // 2)) restart_text = font.render("r to restart gane", True, black) screen.blit(restart_text, (screen_width // 2 - restart_text.get_width() // 2, screen_height // 2 + 50)) pygame.display.flip() waiting = True while waiting: for event in pygame.event.get(): if event.type == pygame.QUIT: return False if event.type == pygame.KEYDOWN: if event.key == pygame.K_r: return True return False # the game loop while True: result, final_score = game_loop() if result == "restart": score = 0 continue elif result is False: break if end_screen(result, final_score): score = 0 continue else: break pygame.quit() sys.exit()Edit: Nevermind I managed to solve it.