mirage.py

Created by joss-blancot

Created on January 28, 2025

4.2 KB


import kandinsky as k
import math
import random
import ion
import time

# Dimensions de l'écran
SCREEN_WIDTH = 320
SCREEN_HEIGHT = 240

# Avion
PLANE_SIZE = 10 # Taille du triangle
plane_x = SCREEN_WIDTH // 2
plane_y = SCREEN_HEIGHT // 2
plane_angle = 0 # Angle en degrés
plane_speed = 2 # Vitesse constante

# Couleurs
BACKGROUND_COLOR = k.color(135, 206, 235) # Bleu ciel
PLANE_COLOR = k.color(0, 128, 255) # Bleu clair
MISSILE_COLOR = k.color(255, 0, 0) # Rouge pour les missiles

# Missiles
missiles = []

# Générer un missile aléatoire
def generate_missile():
    side = random.choice(["top", "bottom", "left", "right"])
    if side == "top":
        x, y = random.randint(0, SCREEN_WIDTH), 0
    elif side == "bottom":
        x, y = random.randint(0, SCREEN_WIDTH), SCREEN_HEIGHT
    elif side == "left":
        x, y = 0, random.randint(0, SCREEN_HEIGHT)
    else:
        x, y = SCREEN_WIDTH, random.randint(0, SCREEN_HEIGHT)
    target_x, target_y = plane_x, plane_y
    angle = math.atan2(target_y - y, target_x - x)
    speed = random.randint(2, 4)
    return [x, y, angle, speed]

# Dessiner l'avion
def draw_plane(x, y, angle):
    # Points du triangle
    p1 = (
        x + PLANE_SIZE * math.cos(math.radians(angle)),
        y + PLANE_SIZE * math.sin(math.radians(angle))
    )
    p2 = (
        x + PLANE_SIZE * 0.5 * math.cos(math.radians(angle + 120)),
        y + PLANE_SIZE * 0.5 * math.sin(math.radians(angle + 120))
    )
    p3 = (
        x + PLANE_SIZE * 0.5 * math.cos(math.radians(angle - 120)),
        y + PLANE_SIZE * 0.5 * math.sin(math.radians(angle - 120))
    )
    k.fill_triangle(int(p1[0]), int(p1[1]), int(p2[0]), int(p2[1]), int(p3[0]), int(p3[1]), PLANE_COLOR)

# Dessiner les missiles
def draw_missiles():
    for missile in missiles:
        x, y = missile[0], missile[1]
        k.fill_rect(int(x), int(y), 10, 5, MISSILE_COLOR)

# Déplacer les missiles
def move_missiles():
    for missile in missiles:
        missile[0] += missile[3] * math.cos(missile[2])
        missile[1] += missile[3] * math.sin(missile[2])
    # Supprimer les missiles hors écran
    missiles[:] = [m for m in missiles if 0 <= m[0] <= SCREEN_WIDTH and 0 <= m[1] <= SCREEN_HEIGHT]

# Vérifier les collisions
def check_collisions():
    for missile in missiles:
        if math.hypot(missile[0] - plane_x, missile[1] - plane_y) < PLANE_SIZE:
            return True
    return False

# Fonction principale
def main():
    global plane_x, plane_y, plane_angle, missiles

    missile_timer = 0

    while True:
        # Effacer l'écran
        k.fill_rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, BACKGROUND_COLOR)

        # Générer un missile toutes les 30 itérations
        missile_timer += 1
        if missile_timer >= 30:
            missile_timer = 0
            missiles.append(generate_missile())

        # Déplacer les missiles
        move_missiles()

        # Vérifier les collisions
        if check_collisions():
            k.draw_string("Game Over", 100, 100, k.color(255, 255, 255), BACKGROUND_COLOR)
            time.sleep(2)
            return

        # Gérer les contrôles pour tourner l'avion
        if ion.keypad.is_pressed(ion.keypad.Key.LEFT):
            plane_angle = (plane_angle - 5) % 360
        if ion.keypad.is_pressed(ion.keypad.Key.RIGHT):
            plane_angle = (plane_angle + 5) % 360

        # Déplacer automatiquement l'avion dans la direction actuelle
        plane_x += plane_speed * math.cos(math.radians(plane_angle))
        plane_y += plane_speed * math.sin(math.radians(plane_angle))

        # Empêcher l'avion de sortir de l'écran (rebond)
        if plane_x < 0:
            plane_x = 0
            plane_angle = (plane_angle + 180) % 360
        elif plane_x > SCREEN_WIDTH:
            plane_x = SCREEN_WIDTH
            plane_angle = (plane_angle + 180) % 360
        if plane_y < 0:
            plane_y = 0
            plane_angle = (plane_angle + 180) % 360
        elif plane_y > SCREEN_HEIGHT:
            plane_y = SCREEN_HEIGHT
            plane_angle = (plane_angle + 180) % 360

        # Dessiner l'avion et les missiles
        draw_plane(plane_x, plane_y, plane_angle)
        draw_missiles()

        # Pause pour ralentir le jeu
        time.sleep(0.05)

# Lancer le jeu
main()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.