Sabtu, 25 April 2026

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

Tidak ada komentar: