简体   繁体   English

使用 Turtle 绘制坐标

[英]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:需要注意的几点:

  1. As there is no 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()
  2. There is no need to enclose the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM