python - 为什么我的海龟绘图程序在 2 个动作后就死了
问题描述
该代码应该 A)创建一个网格(它确实如此)和 B)在绘制其路径时沿对角线移动并在它撞到墙壁时改变方向并在它返回其原始位置时停止移动。
A部分有效,但B部分在2个动作后完成,认为它回到了原来的位置,即使它肯定不是。
您需要任何建议或其他信息吗?
import turtle as t
from time import sleep
""" HOW TO USE:
- Set the size variables
- Every movement is in 10-step increments
-
"""
""" Size variables """
top_x_border = 100
bottom_x_border = 0
top_y_border = 80
bottom_y_border = 0
# Creating the screen
sc = t.Screen()
sc.title("Kelttiläiset solmut")
sc.bgcolor("white")
sc.setup(width = 0.7, height = 0.8)
sc.tracer(2)
sc.delay(10000)
# Creating the drawer-turtle
drawer = t.Turtle()
change_x = 10
change_y = 10
"""
Functions
"""
""" Methods """
# method to draw y-axis lines
def drawy(steps, val):
# line
drawer.forward(steps)
# set position
drawer.up()
drawer.setpos(val,steps)
drawer.down()
# another line
drawer.backward(steps)
# set position again
drawer.up()
drawer.setpos(val+10,0)
drawer.down()
# method to draw x-axis lines
def drawx(steps, val):
# line
drawer.forward(steps)
# set position
drawer.up()
drawer.setpos(steps,val)
drawer.down()
# another line
drawer.backward(steps)
# set position again
drawer.up()
drawer.setpos(0,val+10)
drawer.down()
# Whole Grid
def grid():
drawer.speed(100)
drawer.left(90)
# y lines
for i in range(top_x_border // 10):
drawy(top_x_border, 10*(i+1))
# set position for x lines
drawer.right(90)
drawer.up()
drawer.setpos(0,0)
drawer.down()
# x lines
for i in range(top_x_border // 10):
drawx(top_x_border, 10*(i+1))
# hide the turtle
drawer.hideturtle()
# actually drawing the thing
def actually_do_stuff():
global change_x
global change_y
global top_x_border
global bottom_x_border
global top_y_border
global bottom_y_border
for i in (1, 101):
# Move the drawer
print("{}x{}".format(drawer.xcor(),drawer.ycor()))
drawer.goto(drawer.xcor()+change_x, drawer.ycor()+change_y)
"""
# Check if back at start
if [ drawer.xcor() == Start["x"] ] and [ drawer.ycor() == Start["x"] ]:
break
"""
print("top/bottom_x_borders: ", top_x_border, bottom_x_border)
print("{}x{}".format(drawer.xcor(),drawer.ycor()))
"""
# Check for collisions with the borders
if [ drawer.xcor() <= top_x_border ] or [ drawer.xcor() >= bottom_x_border ]:
change_x *= -1
elif [ drawer.ycor() <= top_y_border ] or [drawer.ycor() >= bottom_y_border]:
change_y *= -1
"""
# Methods end
""" ACTUAL THINGS HAPPENING """
drawer.color('gray')
grid()
#go to starting position
drawer.penup()
drawer.goto(bottom_x_border, bottom_y_border+10)
drawer.pendown()
#mark starting position
Start = {}
Start["x"] = drawer.xcor()
Start["y"] = drawer.ycor()
# Actually draw what you wanted
drawer.color('red')
actually_do_stuff()
drawer.hideturtle()
sc.exitonclick()
解决方案
它在 2 次移动后停在 (20, 30) 处,因为在actually_do_stuff()
你写for i in (1, 101)
的中,那是 2 次移动,对吗?
我认为应该是for i in range(1, 101)
这样,所以它继续移动 100 次。
推荐阅读
- copy-paste - 在 Python 中复制文件 - 读写 vs Shutil.copy
- kotlin - Kotlin 如何使用扩展单个基类的对象创建缓存
- flutter - 颤动中的盒子阴影没有出现
- mysql - 检查子查询结果是否存在,然后减去它
- python - 找不到 django-rest-auth url
- api - 微服务架构中的授权/认证
- node.js - 在 Node.js 中访问整个应用程序中的变量,而不在所有文件中导入它
- laravel - 如何在laravel中获得2个日期之间的减法总数?
- python - 如何在同一行调用 jinja2 中的 2 个宏?
- json - 如何使用python从字符串列表中删除生成的json旁边的双引号