PROMPT :
python tkinter create game submarine with ellipse and 4 stokes rotating animation when press key "w" go up "A" go left "s" go down "d" go right and if space key pressed can launch missiles to the top and on the top there are rectangles enemies moving random if missile hit the enemies disappear and score added
Got it ✅ — you want a Python Tkinter submarine game with:
- Submarine drawn as an ellipse with 4 rotating strokes (propeller effect)
- W/A/S/D keys to move
- Space to fire missiles upward
- Enemies (rectangles) moving randomly at the top
- Collision detection to remove enemies and add score
Here’s a complete runnable Tkinter game implementing your requirements:
import tkinter as tk import random import math # ========================= # Game Configuration # ========================= WINDOW_WIDTH = 800 WINDOW_HEIGHT = 600 SUBMARINE_WIDTH = 60 SUBMARINE_HEIGHT = 30 MISSILE_WIDTH = 4 MISSILE_HEIGHT = 10 ENEMY_WIDTH = 40 ENEMY_HEIGHT = 20 ENEMY_COUNT = 5 MISSILE_SPEED = 8 ENEMY_SPEED = 2 SUBMARINE_SPEED = 5 PROP_ROTATE_SPEED = 10 # degrees per frame # ========================= # Main Game Class # ========================= 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="lightblue") self.canvas.pack() # Game state self.score = 0 self.missiles = [] self.enemies = [] self.prop_angle = 0 # Create submarine self.submarine_x = WINDOW_WIDTH // 2 self.submarine_y = WINDOW_HEIGHT - 100 self.submarine_body = None self.prop_lines = [] # Create score text self.score_text = self.canvas.create_text(10, 10, anchor="nw", font=("Arial", 16), text="Score: 0") # Create enemies self.create_enemies() # Bind keys self.root.bind("<KeyPress>", self.key_press) # Start game loop self.update_game() def create_enemies(self): for _ in range(ENEMY_COUNT): x = random.randint(0, WINDOW_WIDTH - ENEMY_WIDTH) y = random.randint(20, 100) dx = random.choice([-ENEMY_SPEED, ENEMY_SPEED]) enemy = { "id": self.canvas.create_rectangle(x, y, x + ENEMY_WIDTH, y + ENEMY_HEIGHT, fill="red"), "dx": dx } self.enemies.append(enemy) def draw_submarine(self): # Remove old submarine parts if self.submarine_body: self.canvas.delete(self.submarine_body) for line in self.prop_lines: self.canvas.delete(line) self.prop_lines.clear() # Draw body (ellipse) self.submarine_body = self.canvas.create_oval( self.submarine_x - SUBMARINE_WIDTH // 2, self.submarine_y - SUBMARINE_HEIGHT // 2, self.submarine_x + SUBMARINE_WIDTH // 2, self.submarine_y + SUBMARINE_HEIGHT // 2, fill="yellow" ) # Draw rotating propeller (4 strokes) cx = self.submarine_x - SUBMARINE_WIDTH // 2 - 10 cy = self.submarine_y for i in range(4): angle = math.radians(self.prop_angle + i * 90) x1 = cx + math.cos(angle) * 10 y1 = cy + math.sin(angle) * 10 x2 = cx + math.cos(angle) * 20 y2 = cy + math.sin(angle) * 20 line = self.canvas.create_line(x1, y1, x2, y2, width=2, fill="black") self.prop_lines.append(line) def key_press(self, event): key = event.keysym.lower() if key == "w" and self.submarine_y - SUBMARINE_HEIGHT // 2 > 0: self.submarine_y -= SUBMARINE_SPEED elif key == "s" and self.submarine_y + SUBMARINE_HEIGHT // 2 < WINDOW_HEIGHT: self.submarine_y += SUBMARINE_SPEED elif key == "a" and self.submarine_x - SUBMARINE_WIDTH // 2 > 0: self.submarine_x -= SUBMARINE_SPEED elif key == "d" and self.submarine_x + SUBMARINE_WIDTH // 2 < WINDOW_WIDTH: self.submarine_x += SUBMARINE_SPEED elif key == "space": self.fire_missile() def fire_missile(self): missile = self.canvas.create_rectangle( self.submarine_x - MISSILE_WIDTH // 2, self.submarine_y - SUBMARINE_HEIGHT // 2 - MISSILE_HEIGHT, self.submarine_x + MISSILE_WIDTH // 2, self.submarine_y - SUBMARINE_HEIGHT // 2, 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) def move_enemies(self): for enemy in self.enemies: self.canvas.move(enemy["id"], enemy["dx"], 0) x1, y1, x2, y2 = self.canvas.coords(enemy["id"]) if x1 <= 0 or x2 >= WINDOW_WIDTH: enemy["dx"] *= -1 def check_collisions(self): for missile in self.missiles[:]: 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: # Collision detected self.canvas.delete(missile) self.canvas.delete(enemy["id"]) self.missiles.remove(missile) self.enemies.remove(enemy) self.score += 10 self.canvas.itemconfig(self.score_text, text=f"Score: {self.score}") break def update_game(self): self.prop_angle = (self.prop_angle + PROP_ROTATE_SPEED) % 360 self.draw_submarine() self.move_missiles() self.move_enemies() self.check_collisions() # Respawn enemies if all destroyed if not self.enemies: self.create_enemies() self.root.after(50, self.update_game) # ========================= # Run Game # ========================= if __name__ == "__main__": root = tk.Tk() game = SubmarineGame(root) root.mainloop()

Tidak ada komentar:
Posting Komentar