while running: clock.tick(60) frame += 1
def update_rect(self): self.rect.y = TRACK_Y[self.track]
pygame.display.flip()
# Spawn obstacles if frame % random.randint(40, 70) == 0: track = random.randint(0, 2) obstacles.append(Obstacle(track, WIDTH)) Subway Surfers For Linux
show_score(score, screen) show_coins(total_coins, screen)
# Spawn coins (more frequent than obstacles) if frame % random.randint(25, 45) == 0: track = random.randint(0, 2) coins.append(Coin(track, WIDTH))
for obs in obstacles: obs.draw(screen) for coin in coins: coin.draw(screen) player.draw(screen) while running: clock
# Update coins & collection for coin in coins[:]: coin.update(speed) if coin.off_screen(): coins.remove(coin) elif player.rect.colliderect(coin.rect) and player.track == coin.track: coins.remove(coin) total_coins += 1 score += 10 # extra points for coins
def off_screen(self): return self.x + COIN_SIZE < 0 def show_score(score, surf): text = font.render(f"Score: score", True, BLACK) surf.blit(text, (10, 10))
def draw(self, surf): pygame.draw.rect(surf, RED, self.rect) pygame.draw.rect(surf, BLACK, self.rect, 2) 70) == 0: track = random.randint(0
screen.blit(game_over_text, (WIDTH//2 - 80, HEIGHT//2 - 60)) screen.blit(score_text, (WIDTH//2 - 60, HEIGHT//2 - 10)) screen.blit(coin_text, (WIDTH//2 - 60, HEIGHT//2 + 20)) screen.blit(restart_text, (WIDTH//2 - 180, HEIGHT//2 + 70)) pygame.display.flip()
def game_over_screen(final_score, final_coins): screen.fill(WHITE) game_over_text = font.render("GAME OVER", True, RED) score_text = font.render(f"Score: final_score", True, BLACK) coin_text = font.render(f"Coins: final_coins", True, YELLOW) restart_text = font.render("Press R to restart or Q to quit", True, BLACK)
# Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: player.move_up() if event.key == pygame.K_DOWN: player.move_down()
# Draw everything screen.fill(WHITE)
def move_up(self): if self.track > 0: self.track -= 1 self.update_rect()