我需要一个物体沿线运动

-3 投票
1 回答
35 浏览
提问于 2025-04-12 18:10

我之前提到过,我需要做一个动画,我选择的动画是让物体(在这个例子中是雪)沿着一条从左到右的锯齿形线路径移动。我想制作一个雪花落在小房子上的动画。同时,我还有一个问题,就是如何用一个命令生成多个星号(*),并且希望它们之间有一定的间隔。如果你有任何改进的建议,也可以分享给我。我需要在一周内完成这个项目。

所以我开始导入tkinter和random这两个库,因为我知道我会用到它们。接着,我定义了房子和雪这两个对象。我知道我需要画一条线,之后会把它变成白色或蓝色,这样就看不见了。我需要帮助来实现每20个像素生成一个星号的功能。以下是我的代码:

import tkinter
import random
Canvas=tkinter.Canvas(width=1000, height=1000)
Canvas.pack()

sx=10
sy=10
x=200
y=300

def barak(x,y):
    Canvas.create_rectangle(x, y, x+300, y+150, fill="brown", outline="red")
    Canvas.create_rectangle(x+80, y+80, x+120, y+120, fill="yellow", outline="red")
    Canvas.create_rectangle(x+200, y+80, x+240, y+150, fill="black", outline="red")
    Canvas.create_polygon(x, y, x+150, y-100, x+300, y, fill="dark red", outline="red")


Canvas.create_line(x+400,y-300, x+450,y-250, x+400,y-200, x+450,y-150, x+400,y-100, x+450,y-50, x+400,y, x+450,y+50, x+400,y+100, x+450,y+150, x+400,y+200, x+450,y+250)

barak(x,y)

def sneh(x,y):
    Canvas.create_text(sx, sy, font="arial 15 ", text="*", fill="green")

sneh(x,y)
    
for i in range(1, 500, 20):
    sneh(x+i,y)


相关问题:

1 个回答

0

你是在找像这样的东西吗?

import tkinter as tk
import random

root = tk.Tk()
root.title("Snow Animation")

canvas = tk.Canvas(root, width=800, height=600, bg="light blue")
canvas.pack()

house_x, house_y = 200, 300

def draw_house(x, y):
    canvas.create_rectangle(x, y, x + 300, y + 150, fill="brown", outline="red")
    canvas.create_rectangle(x + 80, y + 80, x + 120, y + 120, fill="yellow", outline="red")
    canvas.create_rectangle(x + 200, y + 80, x + 240, y + 150, fill="black", outline="red")
    canvas.create_polygon(x, y, x + 150, y - 100, x + 300, y, fill="dark red", outline="red")

def draw_snowflake(x, y):
    canvas.create_text(x, y, font="arial 15 ", text="❄", fill="white")

def animate_snow():
    # Clear previous snowflakes
    canvas.delete("snowflake")

    # Draw snowflakes at random positions above the house
    for _ in range(10):
        x = random.randint(house_x, house_x + 300)
        y = random.randint(house_y - 100, house_y)
        draw_snowflake(x, y)
    
    # Move the snowflakes downwards
    canvas.move("snowflake", 0, 5)

    # Schedule the next animation frame
    root.after(100, animate_snow)

# Draw the house
draw_house(house_x, house_y)

# Start the snow animation
animate_snow()

root.mainloop()

撰写回答