import pygame
import random
import sys

pygame.init()
window = pygame.display.set_mode((500, 600))
pygame.display.set_caption("Proyek 1: Catch the Apple")
clock = pygame.time.Clock()

# Variabel Objek
player = pygame.Rect(200, 500, 100, 20) # Keranjang
apple = pygame.Rect(random.randint(0, 450), -50, 30, 30) # Apel
score = 0

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Input Keyboard (Kiri/Kanan)
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player.left > 0:
        player.x -= 7
    if keys[pygame.K_RIGHT] and player.right < 500:
        player.x += 7

    # Logika Apel Jatuh (Simulasi gravitasi sederhana)
    apple.y += 5
    if apple.y > 600: # Jika apel terlewat
        apple.y = -50
        apple.x = random.randint(0, 450)
        score -= 1 # Kurangi skor

    # Logika Tabrakan (Menangkap Apel)
    if player.colliderect(apple):
        score += 10
        apple.y = -50
        apple.x = random.randint(0, 450)

    # Rendering (Menggambar)
    window.fill((30, 30, 30)) # Background gelap
    pygame.draw.rect(window, (0, 150, 255), player) # Gambar Keranjang
    pygame.draw.rect(window, (255, 50, 50), apple)  # Gambar Apel
    
    # Render Teks Skor
    font = pygame.font.SysFont(None, 36)
    score_text = font.render(f"Skor: {score}", True, (255, 255, 255))
    window.blit(score_text, (10, 10))

    pygame.display.update()
    clock.tick(60)

pygame.quit()
sys.exit()