import pygame import random pygame.init() # Ukuran layar WIDTH, HEIGHT = 800, 500 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Tangkap Bintang") # Warna SKY = (135, 206, 235) WHITE = (255, 255, 255) BLUE = (30, 100, 220) YELLOW = (255, 215, 0) GREEN = (34, 139, 34) RED = (220, 50, 50) BLACK = (20, 20, 20) clock = pygame.time.Clock() font = pygame.font.SysFont(None, 36) # Pemain player = pygame.Rect(350, 440, 100, 25) player_speed = 7 # Bintang star = pygame.Rect(random.randint(20, 780), 0, 20, 20) star_speed = 5 score = 0 lives = 3 running = True while running: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Kontrol pemain keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player.left > 0: player.x -= player_speed if keys[pygame.K_RIGHT] and player.right < WIDTH: player.x += player_speed # Bintang jatuh star.y += star_speed # Jika tertangkap if player.colliderect(star): score += 1 star.x = random.randint(20, WIDTH - 40) star.y = 0 # Jika bintang terlewat if star.top > HEIGHT: lives -= 1 star.x = random.randint(20, WIDTH - 40) star.y = 0 # Game over if lives <= 0: screen.fill(SKY) game_over = font.render("GAME OVER!", True, RED) result = font.render(f"Skor akhir: {score}", True, BLACK) screen.blit(game_over, (300, 200)) screen.blit(result, (310, 250)) pygame.display.flip() pygame.time.wait(2000) running = False continue # Gambar layar screen.fill(SKY) pygame.draw.rect(screen, BLUE, player) pygame.draw.circle(screen, YELLOW, star.center, 12) score_text = font.render(f"Skor: {score}", True, BLACK) lives_text = font.render(f"Nyawa: {lives}", True, BLACK) screen.blit(score_text, (20, 20)) screen.blit(lives_text, (650, 20)) pygame.display.flip() pygame.quit()