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

2024-06-07 04:19:06 发布

您现在位置:Python中文网/ 问答频道 /正文

现在我用Python编写了一些使用Zelle图形的代码,它有一个机器人图像和一些向上滚动的文本。当用户单击窗口时,程序结束。在

我要做的是让机器人图像的片段从窗口的另一侧进入(头部从顶部向下移动,眼睛从底部向上移动,耳朵从左向右移动)。一旦它们聚在一起形成完整的图像,它们就会停止移动。在

在那之后,我希望文本从左侧进入,并在到达屏幕中心图像下方停止。我不想动画开始,直到用户点击窗口。在

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

    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();

Tags: textfrom图像imageimportmovepngscreen
1条回答
网友
1楼 · 发布于 2024-06-07 04:19:06

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

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

顺便说一句:graphicsupdate(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()

相关问题 更多 >