2016-07-24 4 views
0

저는 현재 Charles Dierbach의 저서 '파이썬을 이용한 컴퓨터 과학 개론'을 배우고 있습니다.거북이를 캔버스에서 튀기는 방법

거북이가 캔버스에서 튀어 나오려고하지만 작동하지 않습니다. 나는 다른 변화를 시도했지만이

여기에 내 코드를 수정할 수있는하지 않은 : 나는 당신의 코드에서 최소한의 예를 추출했습니다

#Drunkard walk PYTHON 

from turtle import * 
from random import * 

#draw a house 
def house(t): 
    pu() 
    goto(270,100) 
    pd() 
    pensize(5) 
    for i in range(4): 
     fd(100) 
     right(90) 
    setheading(120) 
    fd(100) 
    setheading(240) 
    fd(100) 
    pu() 
    goto(200,0) 
    pd() 
    setheading(90) 
    fd(70) 
    setheading(0) 
    fd(40) 
    setheading(270) 
    fd(70) 
    pu() 


#make roads 
def road(t): 
    pu() 
    goto(80, 280) 
    pd() 
    begin_fill() 
    color('black') 
    setheading(270) 
    fd(250) 
    setheading(180) 
    fd(100) 
    setheading(90) 
    fd(250) 
    setheading(0) 
    fd(100) 
    end_fill() 
    pu() 
    goto(80,25) 
    pd() 
    begin_fill() 
    color('white') 
    setheading(270) 
    for i in range(4): 
     fd(70) 
     right(90) 
     fd(100) 
     right(90) 
    end_fill() 
    pu() 
    goto(80, -45) 
    pd() 
    begin_fill() 
    color('black') 
    setheading(270) 
    fd(240) 
    setheading(180) 
    fd(100) 
    setheading(90) 
    fd(240) 
    setheading(0) 
    fd(100) 
    end_fill() 

    #this is my code to keep turtle on canvas 
def isInScreen(window, t): 
    xmin=-299 
    xmax=299 
    ymin=-299 
    ymax=299 

    xTcor = t.xcor() 
    yTcor = t.ycor() 

    if xTcor<xmin or xTcor>xmax: 
     new_heading = (180 - t.heading()) 
     return new_heading 
    if yTcor<ymin or yTcor>ymax: 
     new_heading = (360 - t.heading()) 
     return new_heading 
    #house coord 
    if (170<=xTcor<=200 or 200<=xTcor<=270)and yTcor==0: 
     new_heading = (360 - t.heading()) 
     return new_heading 
    if xTcor==170 and 0<=yTcor<=100: 
     new_heading = (180 - t.heading()) 
     return new_heading 
    if (170<=xTcor<=200 or 200<=xTcor<=270) and yTcor==100: 
     new_heading = (360 - t.heading()) 
     return new_heading 
    if xTcor==270 and 0<=yTcor<=100: 
     new_heading = (180 - t.heading()) 
     return new_heading 
    if 170<=xTcor<=271 and 100<=yTcor<=150: 
     new_heading = (360 - t.heading()) 
     return new_heading 
    if 200<=xTcor<=240 and yTcor ==0: 
     new_heading = 0 
     return new_heading 
    return 100 




#################MAIN#################### 
setup(600,600) 
window=Screen() 
window.title("Drunkard walk") 
window.bgcolor("grey") 


#get the turtle and change the shape 
t=getturtle() 
t.shape('turtle') 
shapesize(2,1.2,1.2) 

pu() 

#change coords and make the ouer roads 
goto(290,290) 
pd() 
setheading(270) 
pensize(10) 

for i in range(4): 
    fd(580) 
    right(90) 


shape('circle') 
house(t) 
goto(80,0) 
road(t) 
penup() 
goto(-250,-260) 
shapesize(1,1,1) 
walking = True 
while walking: 
    pendown() 
    fd(10) 
    color = choice(["black", "red", "yellow", "blue", "white", "green"]) 
    fillcolor(color) 
    ch = randrange(2) 
    if ch == 0: 
     left(90) 
    else: 
     right(90) 
    setheading(isInScreen(window, t)) 


mainloop() 
exitonclick() 

답변

0

하고이를 화면에 대해 수신 거부를 받았습니다. 주요 변경 사항은 다음과 같습니다

거북이가 실제로 벽에 도달하지 않은 경우 벽에서 반사한다 거북이 아무것도하지 말아야 할 경우 결정 1) 일상 -하지만 당신은

에 관계없이 제목 새를 반환

2) 거북이 그래픽을 사용할 때 무한 루프를 생성하면 안됩니다. 이렇게하면 이벤트 처리기가 실행되지 않습니다 (예 : exitonclick()이 코드에 도달하지 못해 코드에서 작동하지 않음). 제공된 거북이 기능을 사용하여 이벤트 프레임 워크 :

import turtle 

CANVAS_WIDTH, CANVAS_HEIGHT = 600, 600 

def headIntoCanvas(t): 
    """ keep turtle on canvas """ 
    xmin, xmax = -CANVAS_WIDTH // 2, CANVAS_WIDTH // 2 
    ymin, ymax = -CANVAS_HEIGHT // 2, CANVAS_HEIGHT // 2 

    xTcor, yTcor = t.position() 

    old_heading = t.heading() 

    if not xmin < xTcor < xmax: 
     new_heading = (180 - old_heading) 
     return new_heading # bounce off wall 

    if not ymin < yTcor < ymax: 
     new_heading = (360 - old_heading) 
     return new_heading # bounce off floor/ceiling 

    return old_heading # stay the course 

def motion(): 
    """ make the turtle march around """ 
    t.forward(5) 
    t.setheading(headIntoCanvas(t)) 
    turtle.ontimer(motion, 25) # again! 

turtle.setup(CANVAS_WIDTH, CANVAS_HEIGHT) 

# get the turtle and change the shape 
t = turtle.Turtle() 
t.shape('circle') 
t.shapesize(1, 1, 1) 
t.speed("fastest") 

t.penup() 
t.goto(- CANVAS_WIDTH // 3, - CANVAS_HEIGHT // 3) 
t.pendown() 

t.setheading(120) 

motion() 

turtle.exitonclick() 

enter image description here

+0

도와 주셔서 감사합니다. – user6277136

+0

도와 주셔서 감사합니다.하지만 술주정 뱅이가 무작위로 오른쪽이나 왼쪽으로 걸어가는 술주정 뱅이를 만들려고합니다. 무한 루프를 사용하여이를 수행 할 수있는 방법이 있습니까? 덕분에 @cdlane – user6277136

+0

@ user6277136, 모션 함수에 술취한 보행 코드를 다시 추가하면 거북이가 움직이는 방식을 제어합니다. – cdlane

관련 문제