将乌龟放在正方形内,无输出,无错误

时间:2019-04-14 12:43:13

标签: python python-3.x turtle-graphics

我希望乌龟不走人

窗口弹出,没有错误,但加载继续,如果我单击窗口,它将关闭。

  • xy是乌龟的位置。
  • D是要显示天气情况还是乌龟是否在正方形内并且在行上(?)。
  • a什么都不是

    1. 方向更改
    2. 速度变化
    3. 位置检查和更改
    4. 如果乌龟在一条直线上,它会沿着边界前进。
import turtle
import msvcrt

turtle.setup(100, 100, 0, 0)
t = turtle.Turtle()
D = 0
a = 1

while True:
    if msvcrt.kbhit():
        c = msvcrt.getch().decode('UTF - 8')
        if c == 'j': # turn left
            t.left(10)
        if c == 'k':
            t.right(10)
        if c == 'a':# speed change
            a += 1
            if a > 10: # speed limitation
                a = 10
        if c == 'z':
            a += -1
            if a < 0:
                a = 0
    #---------------------------------------
    x = t.xcor() # turtle's location
    y = t.ycor()
    if x > 100: # if go out of square, go back to the square
        t.setx(100)
    if x < -100:
        t.setx(-100)
    if y > 100:
        t.sety(100)
    if y < -100:
        t.sety(-100)
    #---------------------------------------
    x = t.xcor() # turtle's location
    y = t.ycor()
    h = t.heading() # turtle's direction
    #---------------------------------------
    if - 100 < x < 100 and -100 < y < 100:
        D = 0 # if it's in the square, D = 0
    elif x == 100 and -100 < y < 100: # if it's in ~
        if 0 <= d <= 90: # direction changes
            t.setheading(90) # direction change to 90 degrees
            D = 1 # D = 1
        elif 270 <= d < 360:
            t.setheading(270)
            D = 2
    elif x == -100 and -100 < y < 100:
        if 90 <= d < 180:
            t.setheading(90)
            D = 2
        elif 180 <= d <= 270:
            t.setheading(270)
            D = 1
    elif y == 100 and -100 < x < 100:
        if 0 <= d < 90:
            t.setheading(0)
            D = 2
        elif 90 <= d <= 180:
            t.setheading(180)
            D = 1
    elif y == -100 and -100 < x < 100:
        if 180 <= d < 270:
            t.setheading(180)
            D = 2
        elif 270 <= d < 360:
            t.setheading(0)
            D = 1
    elif D == 1:
        t.left(90)
    elif D == 2:
        t.right(90)

1 个答案:

答案 0 :(得分:1)

此代码是一团糟。首先,通过将窗口大小设置为100 x 100,然后在两个方向上将海龟在-100到100之间移动,您只能看到1/4的游戏场!当turtle具有内置的键盘事件时,为什么使用msvcrt模块?据我所知,您的代码检查并更正了乌龟的位置,但从未实际移动它!而且您有一个while True:循环,在像乌龟这样的事件驱动环境中没有任何地方。

让我们从一个可以通过键盘操作的正方形内的简单运动示例重新开始:

from turtle import Screen, Turtle

def turn_left():
    turtle.left(10)

def turn_right():
    turtle.right(10)

def speed_up():
    speed = turtle.speed() + 1

    if speed > 10: # speed limitation
        speed = 10

    turtle.speed(speed)

def slow_down():
    speed = turtle.speed() - 1

    if speed < 1: # speed limitation
        speed = 1

    turtle.speed(speed)

def move():
    # there are two different senses of 'speed' in play, we'll exploit
    #  both!  I.e. as we move faster, we'll draw faster and vice versa
    turtle.forward(turtle.speed())

    # if we go out of square, go back into the square
    if not -100 < turtle.xcor() < 100:
        turtle.undo()
        turtle.setheading(180 - turtle.heading())
    elif not -100 < turtle.ycor() < 100:
        turtle.undo()
        turtle.setheading(360 - turtle.heading())

    screen.ontimer(move, 100)

screen = Screen()
screen.setup(300, 300)

# show turtle's boundary
boundary = Turtle('square', visible=False)
boundary.color('pink')
boundary.shapesize(10)
boundary.stamp()

turtle = Turtle()

move()

screen.onkey(turn_left, 'j')
screen.onkey(turn_right, 'k')
screen.onkey(speed_up, 'a')
screen.onkey(slow_down, 'z')

screen.listen()
screen.mainloop()

enter image description here