如何让图像移动到 window 中的特定点?

How do I get an image to move until it gets to a specific point in the window?

现在我在 Python 中有一些代码使用 Zelle 图形,其中有一个机器人图像和一些向上滚动的文本。当用户点击 window 时,程序结束。

我想要做的是让机器人图像的碎片从 window 的相对两侧进入(头部从顶部向下移动,眼睛从底部向上移动,并且耳朵从左右移动)。一旦它们聚集在一起形成完整的图像,它们就会停止移动。

之后,我希望文本从左侧进入,并在到达屏幕中央、图像下方时停止。在用户单击 window 之前,我不希望动画开始。

到目前为止我的代码是这样的:

    from graphics import *
    from random import randint
    from time import sleep
    screen=GraphWin("Logo",500,700);
    screen.setBackground("#b3e2bf");
    #---------logo-----------

    robotHead=Image(Point(250,250),"robotHead.png");
    robotHead.draw(screen);

    robotEyes=Image(Point(250,310),"robotEyes.png");
    robotEyes.draw(screen);

    robotLeftEar=Image(Point(150,290),"robotLeftEar.png");
    robotLeftEar.draw(screen);

    robotRightEar=Image(Point(350,290),"robotRightEar.png");
    robotRightEar.draw(screen);


    #--------credits-----------
    programmer=Point(250,515);
    lineOne=Text(programmer,"Programmer Name");
    lineOne.draw(screen);

    className=Point(250,535);
    lineTwo=Text(className,"CSC 211");
    lineTwo.draw(screen);

    date=Point(250,555);
    lineThree=Text(date,"November 30th, 2017");
    lineThree.draw(screen);

    copyrightName=Point(250,575);
    lineFour=Text(copyrightName,"Copyright Line");
    lineFour.draw(screen);


    while screen.checkMouse()==None:
        robotHead.move(0,-1);
        robotEyes.move(0,-1);
        robotLeftEar.move(0,-1);
        robotRightEar.move(0,-1);
        lineOne.move(0,-1);
        lineTwo.move(0,-1);
        lineThree.move(0,-1);
        lineFour.move(0,-1);
        sleep(0.1);
    screen.close();

您可以使用 robotHead.anchor.getY()robotHead.anchor.getX() 来检查当前位置,只有当它不在预期位置时才移动它。

您还可以使用带有 True/False 的变量来控制必须移动的元素(甚至是要在屏幕上显示的元素)。一开始你应该有 moveHead = TruemoveLineOne = False。当 head 处于预期位置时,您可以更改 moveHead = FalsemoveLineOne = True.

顺便说一句:graphics 有函数 update(frames_per_second) 来控制动画的速度,你不需要 sleep()。除了 update() 执行一些 graphics 正确工作可能需要的功能。 (graphics 文档:Controlling Display Updates (Advanced)

速度 25-30 FPS 足以让人眼看到流畅的动画(它可能比 60 FPS 使用更少的 CPU 功率)。

简单示例

from graphics import *
from random import randint
from time import sleep

screen = GraphWin("Logo", 500, 700)

# --- objects ---

robot_head = Image(Point(250, 250), "robotHead.png")
robot_head.draw(screen)

programmer = Point(250, 515)
line_one = Text(programmer, "Programmer Name") 
#line_one.draw(screen) # don't show at start

# --- control objects ---

move_robot_head = True # move head at start
move_line_one = False  # don't move text at start

# --- mainloop ---

while not screen.checkMouse(): # while screen.checkMouse() is None:

    if move_robot_head: # if move_robot_head == True:
        # check if head is on destination position
        if robot_head.anchor.getY() <= 100:
            # stop head
            move_robot_head = False

            # show text
            line_one.draw(screen)

            # move text 
            move_line_one = True
        else:
            # move head
            robot_head.move(0, -10)

    if move_line_one: # if move_line_one == True:
        # check if head is on destination position
        if line_one.anchor.getY() <= 150:
            # stop text
            move_programmer = False
        else:
            # move text
            line_one.move(0, -10)

    # control speed of animation        
    update(30) # 30 FPS (frames per second)

# --- end ---

screen.close()