Python Animation Two Wheels with rectangle and text and controlled by keyboard arrow right and left from Gemini AI
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
import tkinter as tk
import math
# Setup main window
root = tk.Tk()
root.title("Animated Wheels")
canvas = tk.Canvas(root, width=800, height=300, bg='white')
canvas.pack()
# Initial Positions
car_x = 100
car_y = 150
wheel_radius = 20
spokes = 4
angle_offset = 0
# --- Functions to Draw and Move ---
def draw_car(x, y, angle):
canvas.delete("car") # Clear previous frame
# Draw Brown Rectangle
canvas.create_rectangle(x-50, y-30, x+50, y+10, fill='brown', tags="car")
canvas.create_text(x, y-10, text="hello", fill="white", font=('Arial', 12),
tags="car")
# Draw Wheels and Spokes
for wheel_x in [x-30, x+30]:
canvas.create_oval(wheel_x-wheel_radius, y+10-wheel_radius,
wheel_x+wheel_radius, y+10+wheel_radius,
fill='black', outline='black', tags="car")
# Draw Spokes
for i in range(spokes):
a = angle + (i * 2 * math.pi / spokes)
# Spoke end points
x1 = wheel_x
y1 = y + 10
x2 = wheel_x + wheel_radius * math.cos(a)
y2 = y + 10 + wheel_radius * math.sin(a)
canvas.create_line(x1, y1, x2, y2, fill='white', width=2, tags="car")
def move(event):
global car_x, angle_offset
if event.keysym == 'Right':
car_x += 10
angle_offset += 0.2
elif event.keysym == 'Left':
car_x -= 10
angle_offset -= 0.2
draw_car(car_x, car_y, angle_offset)
# Bind keys
root.bind('<Left>', move)
root.bind('<Right>', move)
# Initial Draw
draw_car(car_x, car_y, angle_offset)
root.mainloop()

Tidak ada komentar:
Posting Komentar