import tkinter as tk
import math
# --- CONFIG ---
CANVAS_WIDTH = 800
CANVAS_HEIGHT = 600
DRONE_WIDTH = 100
DRONE_HEIGHT = 40
PROPELLER_RADIUS = 15
MISSILE_SPEED = 10
DRONE_SPEED = 5
ENEMY_WIDTH = 60
ENEMY_HEIGHT = 40
class UnderseaGame:
def __init__(self, root):
self.root = root
self.root.title("Undersea Drone Animation")
self.canvas = tk.Canvas(root, width=CANVAS_WIDTH, height=CANVAS_HEIGHT, bg="lightblue")
self.canvas.pack()
# Drone position
self.drone_x = CANVAS_WIDTH // 2
self.drone_y = CANVAS_HEIGHT // 2
self.drone_angle = 0 # For propeller rotation
# Missile list
self.missiles = []
# Enemy
self.enemy = self.canvas.create_rectangle(
600, 200, 600 + ENEMY_WIDTH, 200 + ENEMY_HEIGHT, fill="red"
)
# Bind keys
self.root.bind("<KeyPress>", self.key_press)
# Start animation
self.animate()
def draw_drone(self):
# Clear old drone
self.canvas.delete("drone")
# Drone body
x1 = self.drone_x - DRONE_WIDTH // 2
y1 = self.drone_y - DRONE_HEIGHT // 2
x2 = self.drone_x + DRONE_WIDTH // 2
y2 = self.drone_y + DRONE_HEIGHT // 2
self.canvas.create_rectangle(x1, y1, x2, y2, fill="gray", tags="drone")
# Propeller center (left side of drone)
prop_x = x1 - PROPELLER_RADIUS - 5
prop_y = self.drone_y
# Draw propeller circle
self.canvas.create_oval(
prop_x - PROPELLER_RADIUS, prop_y - PROPELLER_RADIUS,
prop_x + PROPELLER_RADIUS, prop_y + PROPELLER_RADIUS,
outline="black", fill="silver", tags="drone"
)
# Draw 4 spokes (rotating)
for i in range(4):
angle = math.radians(self.drone_angle + i * 90)
x_end = prop_x + PROPELLER_RADIUS * math.cos(angle)
y_end = prop_y + PROPELLER_RADIUS * math.sin(angle)
self.canvas.create_line(prop_x, prop_y, x_end, y_end, fill="black", tags="drone")
def draw_missiles(self):
self.canvas.delete("missile")
for missile in self.missiles:
x, y = missile["pos"]
self.canvas.create_rectangle(
x - 3, y - 3, x + 3, y + 3, fill="yellow", tags="missile"
)
def move_missiles(self):
for missile in self.missiles[:]:
missile["pos"] = (missile["pos"][0] + MISSILE_SPEED, missile["pos"][1])
# Remove if out of bounds
if missile["pos"][0] > CANVAS_WIDTH:
self.missiles.remove(missile)
else:
self.check_collision(missile)
def check_collision(self, missile):
if self.enemy:
ex1, ey1, ex2, ey2 = self.canvas.coords(self.enemy)
mx, my = missile["pos"]
if ex1 <= mx <= ex2 and ey1 <= my <= ey2:
# Hit enemy
self.canvas.delete(self.enemy)
self.enemy = None
self.missiles.remove(missile)
def key_press(self, event):
if event.keysym.lower() == "w":
self.drone_y = max(DRONE_HEIGHT // 2, self.drone_y - DRONE_SPEED)
elif event.keysym.lower() == "s":
self.drone_y = min(CANVAS_HEIGHT - DRONE_HEIGHT // 2, self.drone_y + DRONE_SPEED)
elif event.keysym.lower() == "a":
self.drone_x = max(DRONE_WIDTH // 2, self.drone_x - DRONE_SPEED)
elif event.keysym.lower() == "d":
self.drone_x = min(CANVAS_WIDTH - DRONE_WIDTH // 2, self.drone_x + DRONE_SPEED)
elif event.keysym.lower() == "space":
# Launch missile from front of drone
missile_x = self.drone_x + DRONE_WIDTH // 2
missile_y = self.drone_y
self.missiles.append({"pos": (missile_x, missile_y)})
def animate(self):
# Rotate propeller
self.drone_angle = (self.drone_angle + 15) % 360
# Move missiles
self.move_missiles()
# Redraw everything
self.draw_drone()
self.draw_missiles()
# Repeat
self.root.after(50, self.animate)
# --- RUN GAME ---
if __name__ == "__main__":
root = tk.Tk()
game = UnderseaGame(root)
root.mainloop()






