Jumat, 17 April 2026

Python Game - Submarine 2

 



import tkinter as tk

import math


# --- CONFIGURATION ---

WINDOW_WIDTH = 800

WINDOW_HEIGHT = 600

SUBMARINE_SPEED = 5

PROPELLER_RADIUS = 20

PROPELLER_BLADES = 4

ROTATION_SPEED = 10  # degrees per frame


class SubmarineApp:

    def __init__(self, root):

        self.root = root

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


        # Create canvas

        self.canvas = tk.Canvas(root, width=WINDOW_WIDTH, height=WINDOW_HEIGHT, bg="lightblue")

        self.canvas.pack()


        # Submarine position

        self.sub_x = WINDOW_WIDTH // 2

        self.sub_y = WINDOW_HEIGHT // 2

        self.angle = 0  # propeller rotation angle


        # Draw submarine parts

        self.draw_submarine()


        # Bind keys for movement

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


        # Start animation loop

        self.animate()


    def draw_submarine(self):

        """Draws the submarine body, periscope, and propeller."""

        self.canvas.delete("all")


        # Submarine body

        body_width = 120

        body_height = 50

        self.canvas.create_oval(

            self.sub_x - body_width//2, self.sub_y - body_height//2,

            self.sub_x + body_width//2, self.sub_y + body_height//2,

            fill="yellow", outline="black"

        )


        # Periscope

        self.canvas.create_rectangle(

            self.sub_x - 5, self.sub_y - body_height//2 - 30,

            self.sub_x + 5, self.sub_y - body_height//2,

            fill="gray", outline="black"

        )

        self.canvas.create_rectangle(

            self.sub_x - 15, self.sub_y - body_height//2 - 40,

            self.sub_x + 15, self.sub_y - body_height//2 - 30,

            fill="gray", outline="black"

        )


        # Propeller center position (rear of submarine)

        prop_x = self.sub_x - body_width//2 - PROPELLER_RADIUS

        prop_y = self.sub_y


        # Draw propeller blades

        for i in range(PROPELLER_BLADES):

            angle_deg = self.angle + (360 / PROPELLER_BLADES) * i

            angle_rad = math.radians(angle_deg)

            x_end = prop_x + PROPELLER_RADIUS * math.cos(angle_rad)

            y_end = prop_y + PROPELLER_RADIUS * math.sin(angle_rad)

            self.canvas.create_line(prop_x, prop_y, x_end, y_end, width=3, fill="black")


        # Propeller hub

        self.canvas.create_oval(

            prop_x - 5, prop_y - 5,

            prop_x + 5, prop_y + 5,

            fill="black"

        )


    def animate(self):

        """Updates the propeller rotation and redraws the submarine."""

        self.angle = (self.angle + ROTATION_SPEED) % 360

        self.draw_submarine()

        self.root.after(50, self.animate)  # ~20 FPS


    def on_key_press(self, event):

        """Handles movement with W, A, S, D keys."""

        key = event.keysym.lower()

        if key == "w":

            self.sub_y -= SUBMARINE_SPEED

        elif key == "s":

            self.sub_y += SUBMARINE_SPEED

        elif key == "a":

            self.sub_x -= SUBMARINE_SPEED

        elif key == "d":

            self.sub_x += SUBMARINE_SPEED


        # Keep submarine inside window

        self.sub_x = max(60, min(WINDOW_WIDTH - 60, self.sub_x))

        self.sub_y = max(60, min(WINDOW_HEIGHT - 60, self.sub_y))


# --- RUN PROGRAM ---

if __name__ == "__main__":

    root = tk.Tk()

    app = SubmarineApp(root)

    root.mainloop()

Tidak ada komentar: