[英]Draw Co-ordinates using Turtle
I'm trying to draw a set of co-ordinates using Python Turtle
, however,I'm stuck and have no clue how to move on.我正在尝试使用 Python
Turtle
绘制一组坐标,但是,我被卡住了,不知道如何继续。
def generate_map(x_range, y_range, locations):
generated_map = []
for x in range(locations):
generated_map.append([random.randint(x_range, y_range), random.randint(x_range, y_range)])
return generated_map
new_generated_map = generate_map(-20,20,10)
print("The co-ordinates are:",new_generated_map)
def print_map(speed, color, thickness, selected_map):
print("printing map")
turtle.speed(3)
turtle.pencolor("black")
turtle.pensize(4)
for locations in (new_generated_map):
turtle.pendown()
I know that I have to set up a loop function but I'm not sure how to write it out, still new to programming.我知道我必须设置一个循环 function 但我不知道如何写出来,仍然是编程新手。
Add this to the loop in print_map
:将此添加到
print_map
的循环中:
turtle.goto(locations)
You can use turtle.setpos()
or turtle.goto()
, with the coordinates as the arguments:您可以使用
turtle.setpos()
或turtle.goto()
,坐标为 arguments:
def print_map(speed, color, thickness, selected_map):
print("printing map")
turtle.speed(3)
turtle.pencolor("black")
turtle.pensize(4)
for locations in (new_generated_map):
turtle.setpos(locations)
turtle.pendown()
A few things to note:需要注意的几点:
turtle.penup()
in your for
loop, you only need to call the turtle.pendown()
method once, outside of the for
loop, as it saves efficiency.for
循环中没有turtle.penup()
,你只需要在for
循环之外调用一次turtle.pendown()
方法,因为它节省了效率。 If there is no turtle.penup()
call in your code at all, then the turtle.pendown()
can be removed entirely.turtle.penup()
调用,则可以完全删除turtle.pendown()
。new_generated_map
list in brackets in order to loop through the list.new_generated_map
列表括在括号中即可循环遍历列表。 So the improved function would be:所以改进后的 function 将是:
def print_map(speed, color, thickness, selected_map):
print("printing map")
turtle.speed(3)
turtle.pencolor("black")
turtle.pensize(4)
for locations in new_generated_map:
turtle.setpos(locations)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.