0

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

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

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

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

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

1 回答 1

0

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

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

顺便说一句:graphics具有update(frames_per_second)控制动画速度的功能,您不需要sleep(). 除了update()执行一些graphics可能需要正确工作的功能。(graphics文档:控制显示更新(高级)

速度 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()
于 2017-11-23T11:31:29.723 回答