I’m trying to make it so that If the arrow reaches the screen boundary it will go up and turn around continuing the code so that I can create a piece of art. I’ve been looking for a post to answer my problem but I can’t find anything that’s specific to this problem.
JavaScript
x
38
38
1
import random
2
from turtle import Screen, Turtle
3
4
dots = Turtle()
5
screen = Screen()
6
screen.colormode(255)
7
dots.speed("fastest")
8
dots.penup()
9
dots.goto(-350, -350)
10
11
# setting x and y-axis:
12
dots.xcor()
13
dots.ycor()
14
WIDTH, HEIGHT = 800, 800
15
screen.setup(WIDTH, HEIGHT)
16
17
color_list = [(250, 246, 243), (211, 154, 98), (53, 107, 131), (235, 240, 244), (177, 78, 33),
18
(198, 142, 35), (116, 155, 171), (124, 79, 98), (123, 175, 157), (226, 197, 130), (190, 88, 109),
19
(12, 50, 64),
20
(56, 39, 19), (41, 168, 128), (50, 126, 121), (199, 123, 143), (166, 21, 30), (224, 93, 79)]
21
22
random_color = random.choice(color_list)
23
24
for _ in range(100):
25
new_random_color = random.choice(color_list)
26
dots.dot(20, new_random_color)
27
dots.fd(40)
28
29
x, y = dots.position
30
31
if x < WIDTH:
32
if y < HEIGHT:
33
dots.undo() # undo error
34
dots.left(180) # turn around
35
dots.forward(10) # redo movement but in new direction
36
37
screen.exitonclick()
38
Thanks!
Advertisement
Answer
I think what’s missing is just to relocate pointer to starting X axis with Y axis + your interval. Easier way would be to follow similar logic. Allocate your pointer in the beginning with setheading() method then indicate what path to follow.
or
you need to indicate one more goto() method like
new_y = dots.ycor + (interval what you want eg: 50) dots.goto(-350, new_y) and create for loop depending how many rows you need. Personally I’ve done this as follows:
JavaScript
1
24
24
1
dots = Turtle()
2
screen = Screen()
3
screen.colormode(255)
4
dots.speed("fastest")
5
dots.penup()
6
dots.setheading(225)
7
dots.forward(300)
8
dots.setheading(0)
9
10
random_color = random.choice(color_list)
11
num_of_dots = 100
12
13
for dot_count in range(1, num_of_dots + 1):
14
dots.penup()
15
dots.dot(20, random.choice(color_list))
16
dots.forward(50)
17
18
if dot_count % 10 == 0:
19
dots.setheading(90)
20
dots.forward(50)
21
dots.setheading(180)
22
dots.forward(500)
23
dots.setheading(0)
24