'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.
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.
Solution 1:[1]
You can use turtle.setpos() or turtle.goto(), with the coordinates as the 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:
- As there is no
turtle.penup()in yourforloop, you only need to call theturtle.pendown()method once, outside of theforloop, as it saves efficiency. If there is noturtle.penup()call in your code at all, then theturtle.pendown()can be removed entirely. - There is no need to enclose the
new_generated_maplist in brackets in order to loop through the list.
So the improved function would be:
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ann Zen |
