import tkinter as tk
import math
# --- Constants ---
WIDTH, HEIGHT = 800, 300
WHEEL_RADIUS = 30
WHEEL_DIST = 100
CAR_Y = 200
SPOKES = 10
# --- Setup ---
root = tk.Tk()
root.title("Moving Vehicle")
canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="white")
canvas.pack()
# --- Vehicle Components ---
# Wheels (Circles)
wheel1 = canvas.create_oval(0, 0, 0, 0, outline="brown", width=3)
wheel2 = canvas.create_oval(0, 0, 0, 0, outline="brown", width=3)
# Spokes
spoke_lines = []
for _ in range(SPOKES * 2):
spoke_lines.append(canvas.create_line(0, 0, 0, 0, fill="brown"))
# Body (Rectangle)
body = canvas.create_rectangle(0, 0, 0, 0, fill="brown", outline="brown")
# Text
text_obj = canvas.create_text(0, 0, text="SMA INFORMATIKA CIAMIS",
fill="white", font=("Arial", 10, "bold"))
# --- Animation ---
x_pos = 50
angle = 0
def draw_vehicle():
global angle
# Update positions
x1 = x_pos
x2 = x_pos + WHEEL_DIST
# Move wheels
canvas.coords(wheel1, x1-WHEEL_RADIUS, CAR_Y-WHEEL_RADIUS,
x1+WHEEL_RADIUS, CAR_Y+WHEEL_RADIUS)
canvas.coords(wheel2, x2-WHEEL_RADIUS, CAR_Y-WHEEL_RADIUS,
x2+WHEEL_RADIUS, CAR_Y+WHEEL_RADIUS)
# Move Body
canvas.coords(body, x1-WHEEL_RADIUS-10, CAR_Y-WHEEL_RADIUS-30,
x2+WHEEL_RADIUS+10, CAR_Y-10)
# Move Text
canvas.coords(text_obj, (x1+x2)/2, CAR_Y-45)
# Rotate Spokes
for i in range(SPOKES):
a = angle + (i * 2 * math.pi / SPOKES)
# Spoke 1
x_s1 = x1 + WHEEL_RADIUS * math.cos(a)
y_s1 = CAR_Y + WHEEL_RADIUS * math.sin(a)
canvas.coords(spoke_lines[i*2], x1, CAR_Y, x_s1, y_s1)
# Spoke 2
x_s2 = x2 + WHEEL_RADIUS * math.cos(a)
y_s2 = CAR_Y + WHEEL_RADIUS * math.sin(a)
canvas.coords(spoke_lines[i*2+1], x2, CAR_Y, x_s2, y_s2)
angle += 0.1 # Rotation speed
def move_vehicle():
global x_pos
x_pos += 2
if x_pos > WIDTH + 100:
x_pos = -100
draw_vehicle()
canvas.after(20, move_vehicle)
# --- Start ---
move_vehicle()
root.mainloop()

Tidak ada komentar:
Posting Komentar