Senin, 13 April 2026

Python Animation - Drone 15

 



import tkinter as tk

import math

import random


# Window setup

WIDTH, HEIGHT = 800, 600

root = tk.Tk()

root.title("Drone Animation with Missile and Enemy")


canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="skyblue")

canvas.pack()


# Drone properties

drone_x, drone_y = WIDTH // 2, HEIGHT // 2

drone_size = 100

propeller_radius = 15

spoke_count = 4

spoke_angle = 0  # For rotation animation


# Missile properties

missiles = []  # List of (x, y, dx, dy, id)

missile_speed = 10


# Enemy properties

enemy_size = 60

enemy_x = random.randint(100, WIDTH - 100)

enemy_y = random.randint(100, HEIGHT - 100)

enemy_id = canvas.create_rectangle(enemy_x, enemy_y,

                                   enemy_x + enemy_size, enemy_y + enemy_size,

                                   fill="red")


# Movement control

keys_pressed = set()

drone_speed = 5



def draw_drone():

    """Draws the drone with rotating propellers."""

    global spoke_angle

    canvas.delete("drone")


    # Drone body

    body_id = canvas.create_rectangle(drone_x - drone_size // 2,

                                      drone_y - drone_size // 4,

                                      drone_x + drone_size // 2,

                                      drone_y + drone_size // 4,

                                      fill="gray", tags="drone")


    # Propeller positions (top-left, top-right, bottom-left, bottom-right)

    offsets = [(-drone_size // 2, -drone_size // 4),

               (drone_size // 2, -drone_size // 4),

               (-drone_size // 2, drone_size // 4),

               (drone_size // 2, drone_size // 4)]


    for ox, oy in offsets:

        cx = drone_x + ox

        cy = drone_y + oy

        canvas.create_oval(cx - propeller_radius, cy - propeller_radius,

                           cx + propeller_radius, cy + propeller_radius,

                           fill="black", tags="drone")


        # Draw rotating spokes

        for i in range(spoke_count):

            angle = spoke_angle + (i * math.pi / (spoke_count / 2))

            x1 = cx + math.cos(angle) * propeller_radius

            y1 = cy + math.sin(angle) * propeller_radius

            x2 = cx - math.cos(angle) * propeller_radius

            y2 = cy - math.sin(angle) * propeller_radius

            canvas.create_line(x1, y1, x2, y2, fill="white", tags="drone")


    spoke_angle += 0.2  # Rotation speed



def move_drone():

    """Moves the drone based on pressed keys."""

    global drone_x, drone_y

    if "w" in keys_pressed and drone_y - drone_speed > 0:

        drone_y -= drone_speed

    if "s" in keys_pressed and drone_y + drone_speed < HEIGHT:

        drone_y += drone_speed

    if "a" in keys_pressed and drone_x - drone_speed > 0:

        drone_x -= drone_speed

    if "d" in keys_pressed and drone_x + drone_speed < WIDTH:

        drone_x += drone_speed



def fire_missile():

    """Launches a missile from the drone's front."""

    mx = drone_x + drone_size // 2

    my = drone_y

    missiles.append([mx, my, missile_speed, 0, None])



def update_missiles():

    """Moves missiles and checks for collisions."""

    global enemy_id

    for missile in missiles[:]:

        missile[0] += missile[2]

        missile[1] += missile[3]


        # Remove if out of bounds

        if missile[0] > WIDTH or missile[0] < 0 or missile[1] > HEIGHT or missile[1] < 0:

            missiles.remove(missile)

            continue


        # Draw missile

        if missile[4] is not None:

            canvas.delete(missile[4])

        missile[4] = canvas.create_rectangle(missile[0] - 5, missile[1] - 2,

                                             missile[0] + 5, missile[1] + 2,

                                             fill="yellow", tags="missile")


        # Collision with enemy

        if enemy_id is not None:

            ex1, ey1, ex2, ey2 = canvas.coords(enemy_id)

            if ex1 <= missile[0] <= ex2 and ey1 <= missile[1] <= ey2:

                canvas.delete(enemy_id)

                enemy_id = None

                missiles.remove(missile)



def game_loop():

    """Main animation loop."""

    move_drone()

    draw_drone()

    update_missiles()

    root.after(30, game_loop)



# Key bindings

def key_press(event):

    keys_pressed.add(event.keysym.lower())

    if event.keysym.lower() == "space":

        fire_missile()



def key_release(event):

    if event.keysym.lower() in keys_pressed:

        keys_pressed.remove(event.keysym.lower())



root.bind("<KeyPress>", key_press)

root.bind("<KeyRelease>", key_release)


# Start game

game_loop()

root.mainloop()


Tidak ada komentar: