Kamis, 23 April 2026

Python Animation Two Wheels with rectangle and text and controlled by keyboard arrow right and left from and launch missile Bing.com Copilot

  Python Animation Two Wheels with rectangle and text and controlled by keyboard arrow right and left from and launch missile Bing.com Copilot

Prompt :

python tkinter create two circles wheels animation with 4 stokes and create a brown rectangle attach with them with text ="hello" if keyboard arrow press right the wheel rotate and move to right and if keyboard arrow press left the wheel rotate to left and move left and if space key pressed can launch missiles to the top





import tkinter as tk

import math


# --- CONFIG ---

WHEEL_RADIUS = 30

SPOKES = 4

BODY_WIDTH = 150

BODY_HEIGHT = 50

MOVE_SPEED = 5

ROTATE_SPEED = 10  # degrees per move

MISSILE_SPEED = 10


class VehicleAnimation:

    def __init__(self, root):

        self.root = root

        self.root.title("Vehicle Animation with Wheels and Missiles")


        self.canvas = tk.Canvas(root, width=800, height=400, bg="skyblue")

        self.canvas.pack()


        # Initial position

        self.x = 200

        self.y = 300

        self.angle = 0  # wheel rotation angle


        # Store missiles

        self.missiles = []


        # Draw vehicle

        self.draw_vehicle()


        # Bind keys

        self.root.bind("<Right>", self.move_right)

        self.root.bind("<Left>", self.move_left)

        self.root.bind("<space>", self.fire_missile)


        # Animation loop

        self.update_animation()


    def draw_vehicle(self):

        self.canvas.delete("all")


        # Draw wheels

        self.wheel1_center = (self.x, self.y)

        self.wheel2_center = (self.x + BODY_WIDTH - 2*WHEEL_RADIUS, self.y)


        self.draw_wheel(*self.wheel1_center, self.angle)

        self.draw_wheel(*self.wheel2_center, self.angle)


        # Draw body

        body_x1 = self.x - WHEEL_RADIUS

        body_y1 = self.y - WHEEL_RADIUS - BODY_HEIGHT

        body_x2 = body_x1 + BODY_WIDTH

        body_y2 = body_y1 + BODY_HEIGHT

        self.canvas.create_rectangle(body_x1, body_y1, body_x2, body_y2, 


fill="brown")


        # Draw text

        self.canvas.create_text((body_x1 + body_x2) / 2, (body_y1 + body_y2) / 2,

                                text="hello", fill="white", font=("Arial", 16, "bold"))


        # Draw missiles

        for mx, my in self.missiles:

            self.canvas.create_rectangle(mx - 2, my - 10, mx + 2, my, fill="red")


    def draw_wheel(self, cx, cy, angle):

        # Wheel circle

        self.canvas.create_oval(cx - WHEEL_RADIUS, cy - WHEEL_RADIUS,

                                cx + WHEEL_RADIUS, cy + WHEEL_RADIUS, fill="black")


        # Spokes

        for i in range(SPOKES):

            theta = math.radians(angle + i * (360 / SPOKES))

            x_end = cx + WHEEL_RADIUS * math.cos(theta)

            y_end = cy + WHEEL_RADIUS * math.sin(theta)

            self.canvas.create_line(cx, cy, x_end, y_end, fill="white", width=2)


    def move_right(self, event):

        self.x += MOVE_SPEED

        self.angle -= ROTATE_SPEED  # rotate clockwise


    def move_left(self, event):

        self.x -= MOVE_SPEED

        self.angle += ROTATE_SPEED  # rotate counter-clockwise


    def fire_missile(self, event):

        # Missile starts from top center of the body

        missile_x = self.x + BODY_WIDTH / 2 - WHEEL_RADIUS

        missile_y = self.y - WHEEL_RADIUS - BODY_HEIGHT

        self.missiles.append([missile_x, missile_y])


    def update_animation(self):

        # Move missiles upward

        new_missiles = []

        for mx, my in self.missiles:

            my -= MISSILE_SPEED

            if my > 0:

                new_missiles.append([mx, my])

        self.missiles = new_missiles


        # Redraw everything

        self.draw_vehicle()


        # Schedule next frame

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



if __name__ == "__main__":

    root = tk.Tk()

    app = VehicleAnimation(root)

    root.mainloop()

Tidak ada komentar: