Minggu, 12 April 2026

Python Animation - Drone 10

 





import tkinter as tk

import math


# =========================

# Quadcopter Animation App

# =========================

class QuadcopterApp:

    def __init__(self, root):

        self.root = root

        self.root.title("Quadcopter Animation")


        # Canvas setup

        self.canvas = tk.Canvas(root, width=800, height=600, bg="skyblue")

        self.canvas.pack()


        # Start button

        self.start_btn = tk.Button(root, text="Start Animation", command=self.start_animation)

        self.start_btn.pack(pady=10)


        # Quadcopter state

        self.qx, self.qy = 400, 300  # center position

        self.prop_angle = 0

        self.running = False

        self.missiles = []  # list of active missiles


        # Bind keys

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


    def start_animation(self):

        """Start the animation loop."""

        if not self.running:

            self.running = True

            self.animate()


    def key_press(self, event):

        """Handle movement and missile launch."""

        if not self.running:

            return

        step = 10

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

            self.qy -= step

        elif event.keysym.lower() == "s":

            self.qy += step

        elif event.keysym.lower() == "a":

            self.qx -= step

        elif event.keysym.lower() == "d":

            self.qx += step

        elif event.keysym == "space":

            self.launch_missile()


    def launch_missile(self):

        """Create a missile starting from quadcopter center."""

        missile = {

            "x": self.qx,

            "y": self.qy - 20,

            "id": None

        }

        self.missiles.append(missile)


    def draw_quadcopter(self):

        """Draw the quadcopter body and rotating propellers."""

        # Body

        body_size = 40

        self.canvas.create_rectangle(

            self.qx - body_size/2, self.qy - body_size/2,

            self.qx + body_size/2, self.qy + body_size/2,

            fill="gray", outline="black"

        )


        # Arm length

        arm_len = 50

        prop_radius = 10


        # Propeller positions (relative to center)

        offsets = [

            (-arm_len, -arm_len),

            (arm_len, -arm_len),

            (-arm_len, arm_len),

            (arm_len, arm_len)

        ]


        for dx, dy in offsets:

            cx = self.qx + dx

            cy = self.qy + dy

            # Draw propeller hub

            self.canvas.create_oval(cx - prop_radius, cy - prop_radius,

                                    cx + prop_radius, cy + prop_radius,

                                    fill="black")

            # Draw rotating blades

            blade_len = 20

            angle_rad = math.radians(self.prop_angle)

            x1 = cx + blade_len * math.cos(angle_rad)

            y1 = cy + blade_len * math.sin(angle_rad)

            x2 = cx - blade_len * math.cos(angle_rad)

            y2 = cy - blade_len * math.sin(angle_rad)

            self.canvas.create_line(x1, y1, x2, y2, fill="white", width=2)


            # Second blade (perpendicular)

            angle_rad2 = angle_rad + math.pi / 2

            x3 = cx + blade_len * math.cos(angle_rad2)

            y3 = cy + blade_len * math.sin(angle_rad2)

            x4 = cx - blade_len * math.cos(angle_rad2)

            y4 = cy - blade_len * math.sin(angle_rad2)

            self.canvas.create_line(x3, y3, x4, y4, fill="white", width=2)


    def draw_missiles(self):

        """Draw and update missiles."""

        missile_speed = 15

        for missile in self.missiles[:]:

            missile["y"] -= missile_speed

            if missile["y"] < 0:

                self.missiles.remove(missile)

                continue

            missile["id"] = self.canvas.create_rectangle(

                missile["x"] - 2, missile["y"] - 10,

                missile["x"] + 2, missile["y"],

                fill="red"

            )


    def animate(self):

        """Main animation loop."""

        if not self.running:

            return


        self.canvas.delete("all")  # Clear frame


        # Update propeller rotation

        self.prop_angle = (self.prop_angle + 20) % 360


        # Draw quadcopter

        self.draw_quadcopter()


        # Draw missiles

        self.draw_missiles()


        # Schedule next frame

        self.root.after(50, self.animate)



# =========================

# Run the App

# =========================

if __name__ == "__main__":

    root = tk.Tk()

    app = QuadcopterApp(root)

    root.mainloop()

Tidak ada komentar: