Jumat, 17 April 2026

Python Game - Submarine 1




 import tkinter as tk

import math


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

# Drone Submarine Animation

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

class DroneSubmarine:

    def __init__(self, root):

        self.root = root

        self.root.title("Drone Submarine Periscope Animation")


        # Canvas setup

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

        self.canvas.pack()


        # Submarine position and movement

        self.x = 400

        self.y = 300

        self.speed = 5


        # Propeller rotation

        self.propeller_angle = 0

        self.propeller_speed = 15  # degrees per frame


        # Store missile objects

        self.missiles = []


        # Draw submarine

        self.submarine_body = None

        self.periscope = None

        self.propeller_lines = []


        # Bind keys

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


        # Start animation

        self.animate()


    def draw_submarine(self):

        # Remove old submarine parts

        if self.submarine_body:

            self.canvas.delete(self.submarine_body)

        if self.periscope:

            self.canvas.delete(self.periscope)

        for line in self.propeller_lines:

            self.canvas.delete(line)


        # Draw submarine body (ellipse)

        self.submarine_body = self.canvas.create_oval(

            self.x - 40, self.y - 20, self.x + 40, self.y + 20,

            fill="yellow", outline="black"

        )


        # Draw periscope

        self.periscope = self.canvas.create_rectangle(

            self.x - 5, self.y - 50, self.x + 5, self.y - 20,

            fill="gray", outline="black"

        )


        # Draw propeller (4 strokes)

        self.propeller_lines = []

        for i in range(4):

            angle = math.radians(self.propeller_angle + i * 90)

            x_end = self.x - 50 + math.cos(angle) * 10

            y_end = self.y + math.sin(angle) * 10

            line = self.canvas.create_line(

                self.x - 50, self.y, x_end, y_end,

                fill="black", width=2

            )

            self.propeller_lines.append(line)


    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):

        # Create missile starting from periscope top

        missile = {

            "id": self.canvas.create_rectangle(

                self.x - 2, self.y - 55, self.x + 2, self.y - 45,

                fill="red"

            ),

            "x": self.x,

            "y": self.y - 55

        }

        self.missiles.append(missile)


    def move_missiles(self):

        for missile in self.missiles[:]:

            missile["y"] -= 10  # Move upward

            self.canvas.coords(

                missile["id"],

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

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

            )

            if missile["y"] < 0:

                self.canvas.delete(missile["id"])

                self.missiles.remove(missile)


    def animate(self):

        # Rotate propeller

        self.propeller_angle = (self.propeller_angle + self.propeller_speed) % 360


        # Draw submarine

        self.draw_submarine()


        # Move missiles

        self.move_missiles()


        # Repeat animation

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



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

# Run the Application

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

if __name__ == "__main__":

    root = tk.Tk()

    app = DroneSubmarine(root)

    root.mainloop()

Tidak ada komentar: