2017-11-23 1 views
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(); 
+0

당신이 이미지 위치'robotHead.anchor.getY()'를 확인하고 예상 위치에없는 경우에만 이동할 수 있습니다. 변수를 사용하여 이동할 파트를 제어 할 수 있습니다. 'move_className = False'입니다. head가 예상 위치에 있다면'move_className = True'를 설정할 수 있습니다. – furas

+0

'Python'은 줄 끝 부분에';'가 필요 없습니다. – furas

+0

BTW : [PEP 8 스타일 가이드 - 파이썬 코드] (https://www.python.org/dev/peps/pep-0008/)를 읽으십시오.) -'robot_head'와 같은 소문자 이름을 선호하고,','와''주위에 공백을 추가하십시오. – furas

답변

0

당신은 현재의 위치를 ​​확인하고 예상 위치에없는 경우에만 이동하는 robotHead.anchor.getY()robotHead.anchor.getX()를 사용할 수 있습니다.

True/False과 함께 변수를 사용하여 이동할 요소 (또는 화면에 표시 할 요소)를 제어 할 수도 있습니다. 처음에는 moveHead = TruemoveLineOne = False이 있어야합니다. head이 예상 위치에있는 경우 moveHead = FalsemoveLineOne = True을 변경할 수 있습니다.

BTW : 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() 
+0

감사합니다. – user8993750

관련 문제