Rabu, 22 April 2026

Python - Animation Wheels WIth Text 2

 




import tkinter as tk

import math


# Configuration

WIDTH, HEIGHT = 800, 300

WHEEL_RADIUS = 30

SPOKE_LEN = WHEEL_RADIUS - 2

RECT_WIDTH, RECT_HEIGHT = 150, 60

SPEED = 5

ANGLE_STEP = 0.2


# Initialize Tkinter

root = tk.Tk()

root.title("Animated Wagon")

canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg='white')

canvas.pack()


# Initial Positions

rect_x = 50

rect_y = HEIGHT - 100

angle = 0


def draw_spokes(x, y, angle):

    """Draws 4 rotating spokes for a wheel."""

    spokes = []

    for i in range(4):

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

        x1 = x + SPOKE_LEN * math.cos(a)

        y1 = y + SPOKE_LEN * math.sin(a)

        x2 = x - SPOKE_LEN * math.cos(a)

        y2 = y - SPOKE_LEN * math.sin(a)

        spokes.append(canvas.create_line(x1, y1, x2, y2, fill="white", width=2))

    return spokes


def animate():

    global rect_x, angle

    

    # Update positions

    rect_x += SPEED

    angle += ANGLE_STEP

    

    # Wrap around screen

    if rect_x > WIDTH:

        rect_x = -RECT_WIDTH

        

    # Clear and Redraw Everything

    canvas.delete("all")

    

    # Draw Brown Rectangle

    canvas.create_rectangle(rect_x, rect_y, rect_x + RECT_WIDTH, rect_y + RECT_HEIGHT, fill="#8B4513", outline="")

    canvas.create_text(rect_x + RECT_WIDTH/2, rect_y + RECT_HEIGHT/2, text="hello", fill="white", font=('Arial', 16))

    

    # Wheel Centers

    wh1_x, wh2_x = rect_x + 30, rect_x + RECT_WIDTH - 30

    wh_y = rect_y + RECT_HEIGHT + 10

    

    # Draw Wheels (circles)

    for cx in [wh1_x, wh2_x]:

        canvas.create_oval(cx-WHEEL_RADIUS, wh_y-WHEEL_RADIUS, cx+WHEEL_RADIUS, wh_y+WHEEL_RADIUS, fill="black", outline="white")

        draw_spokes(cx, wh_y, angle)

        

    root.after(30, animate)


# Start animation

animate()

root.mainloop()

Tidak ada komentar: