Jumat, 17 April 2026

Python Game - Submarine 4

 




import tkinter as tk

import random

import math


# Window setup

WIDTH, HEIGHT = 800, 600

DRONE_SPEED = 5

MISSILE_SPEED = 10

PROPELLER_RADIUS = 15

ENEMY_COUNT = 5


class UnderseaDroneGame:

    def __init__(self, root):

        self.root = root

        self.root.title("Undersea Drone Animation")

        self.canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="deep sky blue")

        self.canvas.pack()


        # Drone position

        self.drone_x = WIDTH // 2

        self.drone_y = HEIGHT // 2

        self.drone_angle = 0  # For propeller rotation


        # Lists for missiles and enemies

        self.missiles = []

        self.enemies = []


        # Create enemies

        self.spawn_enemies()


        # Key bindings

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


        # Start animation loop

        self.animate()


    def spawn_enemies(self):

        """Spawn ENEMY_COUNT submarines at random positions."""

        for _ in range(ENEMY_COUNT):

            x = random.randint(50, WIDTH - 50)

            y = random.randint(50, HEIGHT - 50)

            self.enemies.append({"x": x, "y": y, "alive": True})


    def key_press(self, event):

        """Handle movement and firing."""

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

            self.drone_y = max(20, self.drone_y - DRONE_SPEED)

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

            self.drone_y = min(HEIGHT - 20, self.drone_y + DRONE_SPEED)

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

            self.drone_x = max(20, self.drone_x - DRONE_SPEED)

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

            self.drone_x = min(WIDTH - 20, self.drone_x + DRONE_SPEED)

        elif event.keysym == "space":

            self.fire_missile()


    def fire_missile(self):

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

        missile_x = self.drone_x

        missile_y = self.drone_y

        self.missiles.append({"x": missile_x, "y": missile_y})


    def move_missiles(self):

        """Move missiles upward and check collisions."""

        for missile in self.missiles[:]:

            missile["y"] -= MISSILE_SPEED

            if missile["y"] < 0:

                self.missiles.remove(missile)

            else:

                self.check_collision(missile)


    def check_collision(self, missile):

        """Check if missile hits an enemy."""

        for enemy in self.enemies:

            if enemy["alive"]:

                dist = math.hypot(missile["x"] - enemy["x"], missile["y"] - enemy["y"])

                if dist < 20:  # Hit radius

                    enemy["alive"] = False

                    if missile in self.missiles:

                        self.missiles.remove(missile)

                    break


    def draw_drone(self):

        """Draw the drone body and rotating propeller."""

        # Drone body

        self.canvas.create_oval(self.drone_x - 20, self.drone_y - 10,

                                self.drone_x + 20, self.drone_y + 10,

                                fill="gray", outline="black")


        # Propeller center

        prop_x = self.drone_x

        prop_y = self.drone_y - 15

        self.canvas.create_oval(prop_x - 5, prop_y - 5,

                                prop_x + 5, prop_y + 5,

                                fill="black")


        # Rotating spokes

        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", width=2)


    def draw_missiles(self):

        """Draw all missiles."""

        for missile in self.missiles:

            self.canvas.create_rectangle(missile["x"] - 2, missile["y"] - 5,

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

                                         fill="red")


    def draw_enemies(self):

        """Draw all alive enemies."""

        for enemy in self.enemies:

            if enemy["alive"]:

                self.canvas.create_rectangle(enemy["x"] - 15, enemy["y"] - 8,

                                             enemy["x"] + 15, enemy["y"] + 8,

                                             fill="darkred", outline="black")


    def animate(self):

        """Main animation loop."""

        self.canvas.delete("all")


        # Rotate propeller

        self.drone_angle = (self.drone_angle + 15) % 360


        # Move missiles

        self.move_missiles()


        # Draw everything

        self.draw_drone()

        self.draw_missiles()

        self.draw_enemies()


        # Repeat

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



if __name__ == "__main__":

    root = tk.Tk()

    game = UnderseaDroneGame(root)

    root.mainloop()

Tidak ada komentar: