Turtle.screen().screensize() Not Outputting The Right Screensize
I have written some code to place dots all around the screen randomly; however, it does not cover the entire screen: import turtle import random t = turtle.Turtle() color = ['red
Solution 1:
"Screen" refers to the turtle's logical boundaries (scrollable area) which may not be the same as the window size.
Call turtle.setup(width, height)
to set your window size, then use the turtle.window_width()
and turtle.window_height()
functions to access its size.
You could also make sure the screensize
matches the window size, then use it as you are doing. Set the screen size with turtle.screensize(width, height)
.
Additionally, your random number selection is out of bounds. Use
random.randint(0, width) - width// 2
to shift the range to be centered on 0.
Putting it together:
import turtle
import random
turtle.setup(480, 320)
color = ["red", "green", "blue", "pink", "yellow", "purple"]
t = turtle.Turtle()
t.speed("fastest")
for _ in range(0, 100):
t.color(random.choice(color))
t.dot(4)
w = turtle.window_width()
h = turtle.window_height()
t.setposition(random.randint(0, w) - w // 2, random.randint(0, h) - h // 2)
turtle.exitonclick()
Post a Comment for "Turtle.screen().screensize() Not Outputting The Right Screensize"