Sabtu, 25 April 2026

Python Submarine Game - Submarine Shooting Random Moving Enemies (Bing.com - Copilot AI)

 





PROMPT :

python tkinter create submarine with one circle wheel animation with 4 stokes rotate when for Movement press key W → up A → left S → down D → right, when space key is presss, launch missile to top and above there are 20 enemies of small rectangles with moving to a random positions



Got it ✅ — you want a Python Tkinter game where:

  • submarine moves with WASD keys.
  • It has one rotating wheel with 4 spokes that spins when moving.
  • Spacebar launches a missile upward.
  • 20 enemies (small rectangles) move randomly at the top of the screen.

Here’s a complete runnable Tkinter program implementing this:

import tkinter as tk

import random

import math


# Game settings

WINDOW_WIDTH = 800

WINDOW_HEIGHT = 600

SUBMARINE_SPEED = 5

WHEEL_RADIUS = 15

MISSILE_SPEED = 8

ENEMY_COUNT = 20

ENEMY_SPEED = 2


class SubmarineGame:

    def __init__(self, root):

        self.root = root

        self.root.title("Submarine Game")

        self.canvas = tk.Canvas(root, width=WINDOW_WIDTH, height=WINDOW_HEIGHT, 


bg="skyblue")

        self.canvas.pack()


        # Submarine position

        self.sub_x = WINDOW_WIDTH // 2

        self.sub_y = WINDOW_HEIGHT - 80

        self.wheel_angle = 0

        self.moving = False


        # Create submarine body

        self.submarine_body = self.canvas.create_rectangle(

            self.sub_x - 40, self.sub_y - 20,

            self.sub_x + 40, self.sub_y + 20,

            fill="yellow"

        )


        # Create wheel

        self.wheel_center = (self.sub_x, self.sub_y + 25)

        self.wheel = self.canvas.create_oval(

            self.wheel_center[0] - WHEEL_RADIUS, self.wheel_center[1] - WHEEL_RADIUS,

            self.wheel_center[0] + WHEEL_RADIUS, self.wheel_center[1] + WHEEL_RADIUS,

            fill="gray"

        )

        self.spokes = []

        for _ in range(4):

            self.spokes.append(self.canvas.create_line(0, 0, 0, 0, fill="black", width=2))


        # Missiles

        self.missiles = []


        # Enemies

        self.enemies = []

        for _ in range(ENEMY_COUNT):

            x = random.randint(20, WINDOW_WIDTH - 20)

            y = random.randint(20, 100)

            rect = self.canvas.create_rectangle(x - 10, y - 10, x + 10, y + 10, fill="red")

            self.enemies.append({"id": rect, "dx": random.choice([-ENEMY_SPEED, ENEMY_SPEED]),

                                 "dy": random.choice([-ENEMY_SPEED, ENEMY_SPEED])})


        # Key bindings

        self.keys = set()

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

        self.root.bind("<KeyRelease>", self.key_release)


        # Start game loop

        self.update_game()


    def key_press(self, event):

        self.keys.add(event.keysym)


    def key_release(self, event):

        if event.keysym in self.keys:

            self.keys.remove(event.keysym)


    def move_submarine(self):

        dx = dy = 0

        self.moving = False

        if "w" in self.keys or "W" in self.keys:

            dy = -SUBMARINE_SPEED

            self.moving = True

        if "s" in self.keys or "S" in self.keys:

            dy = SUBMARINE_SPEED

            self.moving = True

        if "a" in self.keys or "A" in self.keys:

            dx = -SUBMARINE_SPEED

            self.moving = True

        if "d" in self.keys or "D" in self.keys:

            dx = SUBMARINE_SPEED

            self.moving = True


        # Move submarine and wheel

        self.canvas.move(self.submarine_body, dx, dy)

        self.canvas.move(self.wheel, dx, dy)

        for spoke in self.spokes:

            self.canvas.move(spoke, dx, dy)


        # Update wheel center

        self.wheel_center = (

            self.wheel_center[0] + dx,

            self.wheel_center[1] + dy

        )


        # Rotate wheel if moving

        if self.moving:

            self.wheel_angle += 0.2

            self.draw_spokes()


        # Fire missile

        if "space" in self.keys:

            self.fire_missile()


    def draw_spokes(self):

        for i, spoke in enumerate(self.spokes):

            angle = self.wheel_angle + (math.pi / 2) * i

            x1 = self.wheel_center[0] + math.cos(angle) * WHEEL_RADIUS

            y1 = self.wheel_center[1] + math.sin(angle) * WHEEL_RADIUS

            x2 = self.wheel_center[0] - math.cos(angle) * WHEEL_RADIUS

            y2 = self.wheel_center[1] - math.sin(angle) * WHEEL_RADIUS

            self.canvas.coords(spoke, x1, y1, x2, y2)


    def fire_missile(self):

        if not self.missiles or self.canvas.coords(self.missiles[-1])[1] < self.sub_y - 50:

            missile = self.canvas.create_rectangle(

                self.wheel_center[0] - 2, self.sub_y - 25,

                self.wheel_center[0] + 2, self.sub_y - 15,

                fill="black"

            )

            self.missiles.append(missile)


    def move_missiles(self):

        for missile in self.missiles[:]:

            self.canvas.move(missile, 0, -MISSILE_SPEED)

            x1, y1, x2, y2 = self.canvas.coords(missile)

            if y2 < 0:

                self.canvas.delete(missile)

                self.missiles.remove(missile)

            else:

                self.check_missile_collision(missile)


    def check_missile_collision(self, missile):

        mx1, my1, mx2, my2 = self.canvas.coords(missile)

        for enemy in self.enemies[:]:

            ex1, ey1, ex2, ey2 = self.canvas.coords(enemy["id"])

            if mx1 < ex2 and mx2 > ex1 and my1 < ey2 and my2 > ey1:

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

                self.enemies.remove(enemy)

                self.canvas.delete(missile)

                if missile in self.missiles:

                    self.missiles.remove(missile)

                break


    def move_enemies(self):

        for enemy in self.enemies:

            self.canvas.move(enemy["id"], enemy["dx"], enemy["dy"])

            x1, y1, x2, y2 = self.canvas.coords(enemy["id"])

            if x1 <= 0 or x2 >= WINDOW_WIDTH:

                enemy["dx"] *= -1

            if y1 <= 0 or y2 >= 150:

                enemy["dy"] *= -1


    def update_game(self):

        self.move_submarine()

        self.move_missiles()

        self.move_enemies()

        self.root.after(30, self.update_game)


# Run the game

if __name__ == "__main__":

    root = tk.Tk()

    game = SubmarineGame(root)

    root.mainloop()


Python Submarine Game - Shooting Enemies which moving random position (Gemini AI)

 





PROMPT :

python tkinter create submarine with one circle wheel animation with 4 stokes rotate when for Movement press key W → up A → left S → down D → right, when space key is presss, launch missile to top and above there are 20 enemies of small rectangles with moving to a random positions



This Python script uses tkinter to create a 2D game environment. It features a submarine that moves with WASD, a propeller with 4 strokes that rotates, a space-key missile launcher, and 20 small, randomly moving enemy rectangles.
python
import tkinter as tk
import random
import math

# Configuration
WIDTH = 800
HEIGHT = 600
SUB_SPEED = 15
MISSILE_SPEED = 10
ENEMY_COUNT = 20

class SubmarineGame:
    def __init__(self, root):
        self.root = root
        self.root.title("Submarine Defender")
        
        self.canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="#0077be") # Ocean blue
        self.canvas.pack()
        
        # Initial positions
        self.sub_x = WIDTH // 2
        self.sub_y = HEIGHT - 100
        self.angle = 0
        
        self.create_submarine()
        self.create_enemies()
        
        # Key bindings
        self.root.bind("<KeyPress-w>", self.move_up)
        self.root.bind("<KeyPress-a>", self.move_left)
        self.root.bind("<KeyPress-s>", self.move_down)
        self.root.bind("<KeyPress-d>", self.move_right)
        self.root.bind("<space>", self.launch_missile)
        
        # Start animations
        self.animate_propeller()
        self.animate_enemies()

    def create_submarine(self):
        # Submarine Body (Oval)
        self.sub_body = self.canvas.create_oval(self.sub_x-50, self.sub_y-20, 
                                               self.sub_x+50, self.sub_y+20, 
                                               fill="gray", outline="black", tags="submarine")
        # Submarine Tower
        self.sub_tower = self.canvas.create_rectangle(self.sub_x-10, self.sub_y-35, 
                                                     self.sub_x+10, self.sub_y-20, 
                                                     fill="gray", outline="black", tags="submarine")
        # Propeller center
        self.prop_center = self.canvas.create_oval(self.sub_x-55, self.sub_y-5, 
                                                  self.sub_x-45, self.sub_y+5, 
                                                  fill="black", tags="submarine")
        
        # 4 Propeller Strokes (Initial drawn)
        self.prop_strokes = []
        for i in range(4):
            stroke = self.canvas.create_line(self.sub_x-50, self.sub_y, 
                                            self.sub_x-50, self.sub_y, 
                                            width=3, fill="black", tags="submarine")
            self.prop_strokes.append(stroke)

    def rotate_propeller(self):
        self.angle += 20
        length = 15
        for i in range(4):
            a = math.radians(self.angle + (i * 90))
            x2 = (self.sub_x - 50) + length * math.cos(a)
            y2 = self.sub_y + length * math.sin(a)
            self.canvas.coords(self.prop_strokes[i], self.sub_x-50, self.sub_y, x2, y2)

    def animate_propeller(self):
        self.rotate_propeller()
        self.root.after(50, self.animate_propeller)

    def move_submarine(self, dx, dy):
        self.sub_x += dx
        self.sub_y += dy
        self.canvas.move("submarine", dx, dy)

    def move_up(self, event): self.move_submarine(0, -SUB_SPEED)
    def move_down(self, event): self.move_submarine(0, SUB_SPEED)
    def move_left(self, event): self.move_submarine(-SUB_SPEED, 0)
    def move_right(self, event): self.move_submarine(SUB_SPEED, 0)

    def launch_missile(self, event):
        m = self.canvas.create_rectangle(self.sub_x-2, self.sub_y-40, 
                                        self.sub_x+2, self.sub_y-20, 
                                        fill="red", outline="yellow")
        self.animate_missile(m)

    def animate_missile(self, missile):
        self.canvas.move(missile, 0, -MISSILE_SPEED)
        pos = self.canvas.coords(missile)
        
        # Check collision with enemies
        hit = False
        for enemy in self.enemies:
            e_pos = self.canvas.coords(enemy)
            if pos[0] < e_pos[2] and pos[2] > e_pos[0] and pos[1] < e_pos[3] and pos[3] > e_pos[1]:
                self.canvas.delete(enemy)
                self.enemies.remove(enemy)
                hit = True
                break
        
        if pos[1] > 0 and not hit:
            self.root.after(20, lambda: self.animate_missile(missile))
        else:
            self.canvas.delete(missile)

    def create_enemies(self):
        self.enemies = []
        for _ in range(ENEMY_COUNT):
            x = random.randint(50, WIDTH-50)
            y = random.randint(50, HEIGHT//2)
            enemy = self.canvas.create_rectangle(x, y, x+20, y+20, 
                                                fill="darkred", outline="white")
            self.enemies.append(enemy)

    def animate_enemies(self):
        for enemy in self.enemies:
            dx = random.randint(-5, 5)
            dy = random.randint(-2, 2)
            self.canvas.move(enemy, dx, dy)
        self.root.after(100, self.animate_enemies)

if __name__ == "__main__":
    root = tk.Tk()
    game = SubmarineGame(root)
    root.mainloop()
Key Features of the Script:
  • Submarine Creation: Uses canvas.create_oval and create_rectangle to form a submarine.
  • Propeller Animation: Four lines (create_line) are updated every 50ms using math.cos/sin to simulate rotation.
  • WASD Movement: Key bindings (<KeyPress-w>) update the submarine's coordinates and move all "submarine" tagged items.
  • Missile Launcher: Pressing the space key launches a rectangle. The animate_missile function moves it upward.
  • Enemies: 20 small red rectangles are created at random positions, updating their positions randomly every 100ms.
  • Collision Detection: Simple boundary check to delete enemies upon missile hit