gpt4 book ai didi

python - 让 turtle 保持在一个正方形内,没有输出,也没有错误

转载 作者:行者123 更新时间:2023-12-01 08:02:20 28 4
gpt4 key购买 nike

我希望 turtle 不要出格

窗口出现并且没有错误,但加载仍然存在,如果我单击该窗口,它就会关闭。

  • x, y 是 turtle 的位置。
  • D 是显示天气或 turtle 是否在方 block 内且在线(?)。
  • a 没什么

    1. 方向改变
    2. 速度变化
    3. 位置检查和更改
    4. 如果 turtle 在一条线上,它就会沿着边界移动。
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)

最佳答案

这段代码一团糟。首先,通过将窗口大小设置为 100 x 100,然后在两个维度上在 -100 和 100 之间移动 turtle ,只有 1/4 的游戏区域可见!为什么使用 msvcrt当turtle内置了完美的键盘事件时模块?据我所知,您的代码检查并纠正了 turtle 的位置,但从未真正移动它!你有一个 while True:循环在像 turtle 这样的事件驱动环境中没有地位。

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

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

关于python - 让 turtle 保持在一个正方形内,没有输出,也没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55675395/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com