import tkinter as tk
import math
import time
# =========================
# Drone Animation Class
# =========================
class DroneApp:
def __init__(self, root):
self.root = root
self.root.title("Drone Animation with Missile Launch")
# Canvas setup
self.canvas = tk.Canvas(root, width=800, height=600, bg="skyblue")
self.canvas.pack()
# Drone properties
self.drone_width = 100
self.drone_height = 40
self.x = 400
self.y = 300
self.propeller_radius = 15
self.spoke_count = 4
self.angle = 0 # rotation angle for propellers
# Movement speed
self.speed = 5
# Missile properties
self.missiles = [] # list of (id, x, y)
self.missile_speed = 10
# Key bindings
self.root.bind("<KeyPress>", self.key_press)
# Start animation loop
self.animate()
def draw_drone(self):
# Clear canvas
self.canvas.delete("all")
# Draw drone body
left = self.x - self.drone_width / 2
top = self.y - self.drone_height / 2
right = self.x + self.drone_width / 2
bottom = self.y + self.drone_height / 2
self.canvas.create_rectangle(left, top, right, bottom, fill="gray", outline="black", width=2)
# Propeller positions (corners)
offsets = [
(-self.drone_width / 2, -self.drone_height / 2), # top-left
(self.drone_width / 2, -self.drone_height / 2), # top-right
(-self.drone_width / 2, self.drone_height / 2), # bottom-left
(self.drone_width / 2, self.drone_height / 2) # bottom-right
]
for dx, dy in offsets:
cx = self.x + dx
cy = self.y + dy
self.draw_propeller(cx, cy)
# Draw missiles
for missile in self.missiles:
self.canvas.create_oval(missile[0] - 3, missile[1] - 3,
missile[0] + 3, missile[1] + 3,
fill="red")
def draw_propeller(self, cx, cy):
# Draw propeller circle
self.canvas.create_oval(cx - self.propeller_radius, cy - self.propeller_radius,
cx + self.propeller_radius, cy + self.propeller_radius,
outline="black", width=2)
# Draw spokes
for i in range(self.spoke_count):
angle_rad = math.radians(self.angle + (360 / self.spoke_count) * i)
x_end = cx + self.propeller_radius * math.cos(angle_rad)
y_end = cy + self.propeller_radius * math.sin(angle_rad)
self.canvas.create_line(cx, cy, x_end, y_end, fill="black", width=1)
def key_press(self, event):
key = event.keysym.lower()
if key == "w":
self.y -= self.speed
elif key == "s":
self.y += self.speed
elif key == "a":
self.x -= self.speed
elif key == "d":
self.x += self.speed
elif key == "space":
self.launch_missile()
def launch_missile(self):
# Missile starts from center front of drone
missile_x = self.x + self.drone_width / 2
missile_y = self.y
self.missiles.append([missile_x, missile_y])
def update_missiles(self):
# Move missiles forward
for missile in self.missiles:
missile[0] += self.missile_speed
# Remove missiles that go off screen
self.missiles = [m for m in self.missiles if m[0] < 800]
def animate(self):
# Rotate propellers
self.angle = (self.angle + 15) % 360
# Update missiles
self.update_missiles()
# Redraw drone
self.draw_drone()
# Schedule next frame
self.root.after(50, self.animate)
# =========================
# Run the Application
# =========================
if __name__ == "__main__":
root = tk.Tk()
app = DroneApp(root)
root.mainloop()

Tidak ada komentar:
Posting Komentar