from pyxel import *

game_over = False
landed = False
resolution_x = 1024
resolution_y = 512
anim_frame = 0
thrusting = False
explosion_timer = 0
explosion_x = 0
explosion_y = 0
shake_timer = 0
particles = []
stars = []
for i in range(1000):
    stars.append((rndi(0, resolution_x), rndi(0, resolution_y)))
x = rndi(50, resolution_x-50)
landing_pad = rndi(50, resolution_y-50)
y = 50
dx = 0
dy = 0
fuel = 200


def reset():
    global x,y,dx,dy,fuel,landing_pad,game_over,landed
    global particles, explosion_timer
    x = rndi(50, resolution_y-50)
    y = 50
    dx = 0
    dy = 0
    fuel = 100
    landing_pad = rndi(50, resolution_y-50)
    game_over = False
    landed = False
    explosion_timer = 0
    particles = []


def draw_centered_text(y, txt, color):
    text_width = len(txt) * 4
    text((resolution_x - text_width) // 2, y, txt, color)
    
    
def start_explosion():
    global explosion_timer, explosion_x, explosion_y, shake_timer
    explosion_timer = 30
    shake_timer = 25
    explosion_x = x
    explosion_y = y
    play(1,1)


def spawn_thruster_particles():
    for i in range(3):
        particles.append({
            "x": x + 4 + rndi(-2,2),
            "y": y,
            "dx": rndf(-0.4,0.4),
            "dy": rndf(0.8,1.5),
            "life": rndi(6,12),
            "col": rndi(8,10)
        })


def update_particles():
    for p in particles:
        p["x"] += p["dx"]
        p["y"] += p["dy"]
        p["life"] -= 1
    particles[:] = [p for p in particles if p["life"] > 0]


def draw_particles():
    for p in particles:
        pset(p["x"], p["y"], p["col"])


def draw_explosion():
    if explosion_timer <= 0:
        return
    r = (30 - explosion_timer) * 3
    circ(explosion_x, explosion_y, r, 10)
    circ(explosion_x, explosion_y, r-4, 9)
    circ(explosion_x, explosion_y, r-8, 7)


def set_sounds():
    sound(0).set("c2b1a1g1f1","n","7531","f",8)  # düse
    sound(1).set("f3d3c3a2","n","7777","f",20)  # explosion
    sound(2).set("c3e3g3c4","t","7531","n",18)  # Erfolg!


def update():
    global x,y,dx,dy,fuel,game_over,landed
    global anim_frame, explosion_timer, shake_timer
    global thrusting
    thrusting = False
    if game_over or landed:
        if explosion_timer > 0:
            explosion_timer -= 1
        if shake_timer > 0:
            shake_timer -= 1
        update_particles()
        if btnp(KEY_R):
            reset()
        return
    if fuel > 0:
        if btn(KEY_RIGHT):
            dx += 0.1
            fuel -= 1
            thrusting = True
        if btn(KEY_LEFT):
            dx -= 0.1
            fuel -= 1
            thrusting = True
        if btn(KEY_UP):
            dy -= 0.15
            fuel -= 1
            thrusting = True
        if thrusting:
            anim_frame = 1
            spawn_thruster_particles()
            play(0,0)
        else:
            anim_frame = 0
    dy += 0.05
    x += dx
    y += dy
    update_particles()
    if y >= resolution_y-50:
        if abs(dy) < 1.5 and landing_pad < x < landing_pad + 20:
            landed = True
            play(2,2)
        else:
            game_over = True
            start_explosion()
            

def draw():
    global shake_timer
    cls(0)
    cam_x = 0
    cam_y = 0
    if shake_timer > 0:
        cam_x = rndi(-4,4)
        cam_y = rndi(-4,4)
    camera(cam_x, cam_y)
    for star in stars:
        pset(star[0], star[1], 7)
    rect(0, resolution_y-50, resolution_x, 50, 5)
    rect(landing_pad, resolution_y-50, 20, 4, 7)
    draw_particles()
    if not game_over:
        text(50, 10, "Fuel:", 11)
        rect(80, 10, fuel*2, 3, 11)
        blt(x-4, y-16, 0, 0, 16, 16, 16, 0)
    draw_explosion()
    camera()
    if game_over:
        draw_centered_text(60, "Game Over", 14)
        draw_centered_text(70, "Press R to restart", 3)
    if landed:
        draw_centered_text(80, "Successfully Landed!", 4)
        draw_centered_text(90, "Press R to restart", 14)


init(resolution_x, resolution_y, title = "Alien Lander",fps=30)
load("alien1_1.pyxres")
set_sounds()
run(update, draw)