Sabtu, 25 April 2026

Python Animation 4 Wheels with rectangle - Bing.com - Copilot

 


PROMPT :

python tkinter four wheels with circle aligned and 4 stokes rotate moving forward and above there is rectangle attached to the wheels

================================================


import tkinter as tk

import math


# --- CONFIGURATION ---

WHEEL_RADIUS = 30

SPOKES = 4

WHEEL_SPACING = 100

BODY_WIDTH = 350

BODY_HEIGHT = 50

MOVE_SPEED = 4  # pixels per frame

ROTATE_SPEED = 5  # degrees per frame

FPS = 30


class MovingVehicle:

    def __init__(self, canvas, start_x, start_y):

        self.canvas = canvas

        self.start_x = start_x

        self.start_y = start_y

        self.angle = 0  # rotation angle for spokes


        # Create wheels

        self.wheels = []

        for i in range(4):

            cx = start_x + i * WHEEL_SPACING

            cy = start_y

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

                                            cx + WHEEL_RADIUS, cy + WHEEL_RADIUS,

                                            outline="black", width=2)

            self.wheels.append(wheel)


        # Create spokes for each wheel

        self.spokes = []

        for i in range(4):

            cx = start_x + i * WHEEL_SPACING

            cy = start_y

            wheel_spokes = []

            for _ in range(SPOKES):

                line = self.canvas.create_line(cx, cy, cx, cy - WHEEL_RADIUS, fill="black", width=2)

                wheel_spokes.append(line)

            self.spokes.append(wheel_spokes)


        # Create body rectangle above wheels

        body_top = start_y - WHEEL_RADIUS - BODY_HEIGHT

        body_left = start_x - WHEEL_RADIUS

        self.body = self.canvas.create_rectangle(body_left, body_top,

                                                 body_left + BODY_WIDTH, body_top + BODY_HEIGHT,

                                                 fill="blue")


    def update(self):

        # Move everything forward

        self.canvas.move(self.body, MOVE_SPEED, 0)

        for wheel in self.wheels:

            self.canvas.move(wheel, MOVE_SPEED, 0)

        for wheel_spokes in self.spokes:

            for spoke in wheel_spokes:

                self.canvas.move(spoke, MOVE_SPEED, 0)


        # Rotate spokes

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

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

            # Get wheel center

            coords = self.canvas.coords(self.wheels[i])

            cx = (coords[0] + coords[2]) / 2

            cy = (coords[1] + coords[3]) / 2

            for j, spoke in enumerate(wheel_spokes):

                angle_deg = self.angle + (360 / SPOKES) * j

                rad = math.radians(angle_deg)

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

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

                self.canvas.coords(spoke, cx, cy, x_end, y_end)


# --- MAIN PROGRAM ---

root = tk.Tk()

root.title("Four Wheels Vehicle Animation")


canvas = tk.Canvas(root, width=800, height=300, bg="white")

canvas.pack()


vehicle = MovingVehicle(canvas, 100, 200)


def animate():

    vehicle.update()

    root.after(int(1000 / FPS), animate)


animate()

root.mainloop()

Tidak ada komentar: