Minggu, 12 April 2026

Python Animation - Drone 4

 




import tkinter as tk

import math

import time


class QuadcopterAnimation:

    def __init__(self, root):

        self.root = root

        self.root.title("Quadcopter Flight Animation")

        

        # Canvas setup

        self.canvas = tk.Canvas(root, width=600, height=500, bg='white')

        self.canvas.pack()

        

        # Start Button

        self.btn = tk.Button(root, text="Start Flight", command=self.start_animation)

        self.btn.pack()

        

        # Initial position

        self.x, self.y = 300, 400

        self.angle = 0

        self.flying = False


    def draw_drone(self, x, y, angle):

        self.canvas.delete("drone")

        

        # Body

        self.canvas.create_rectangle(x-40, y-10, x+40, y+10, fill='blue', tags="drone")

        

        # Propellers (simple lines rotating)

        prop_coords = [

            (x-30, y-15), (x+30, y-15),

            (x-30, y+15), (x+30, y+15)

        ]

        

        for px, py in prop_coords:

            length = 15

            dx = length * math.cos(math.radians(angle))

            dy = length * math.sin(math.radians(angle))

            self.canvas.create_line(px-dx, py-dy, px+dx, py+dy, width=3, fill='black', tags="drone")

            # Spin propeller faster

            self.canvas.create_line(px-dy, py+dx, px+dy, py-dx, width=3, fill='black', tags="drone")


    def animate(self):

        if self.y > 50:

            self.y -= 2  # Move up

            self.angle += 20 # Rotate props

            self.draw_drone(self.x, self.y, self.angle)

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

        else:

            self.flying = False


    def start_animation(self):

        if not self.flying:

            self.flying = True

            self.x, self.y = 300, 400

            self.animate()


if __name__ == "__main__":

    root = tk.Tk()

    app = QuadcopterAnimation(root)

    root.mainloop()

Tidak ada komentar: