Make turtle go to a random place on the screen with randint - python

How to make the cursor go to a random place on the screen with randint() using Python turtle graphics:
stars = Turtle()
drawings = [Trees, Moon, BlueSky, NightSky, Sun, Sunset, stars]
def penup():
for x in drawings:
x.penup()
penup()
def Stars():
for x in range(5):
penup()
stars.goto((randint(-100,0), randint(0,100)))
pendown()
stars.left(36)
stars.forward(10)
stars.left(36)
penup()
Stars()
I'm trying to make a program that draws stars at the top of the screen, with random spots to where it goes in the night sky. Could someone help with this randint() function? It's not going to a random position and it's acting weird.

Your code is a bit of a mess. Having both stars and Stars is risky coding. You use your penup() function as if it's turtle's penup() function, which it isn't. Your code doesn't even draw one star, let alone many. And the star you're attempting to draw looks more like a pentagon than a star.
Here's a rework of your code that does what you describe:
from turtle import Turtle, Screen
from random import randint
def stars():
for _ in range(20):
star.penup()
star.goto(randint(-200, 200), randint(100, 200))
star.pendown()
for _ in range(5):
star.left(144)
star.forward(10)
screen = Screen()
star = Turtle()
star.speed('fastest') # because I have no patience
stars()
star.hideturtle()
screen.exitonclick()

Related

How to draw dot inside shape turtle python?

I am new with Turtle python library and I am trying to draw turtle dot inside the shape (square), which should look like on the picture below. The problem is that when I am trying to do this the shape covers the dot and I see only the shape (square).
enter image description here
My code:
def add_dot_square():
obj = Turtle()
obj.penup()
obj.shape("square")
obj.shapesize(1.5, 1.5)
obj.color("orange")
obj.goto(0, 0)
obj.dot(20, "red")
Turtle's can't appear behind things they draw, only other turtles (and even that's tricky.) Instead of the turtle being the square, have the turtle draw or stamp the square, and then place the dot atop it:
from turtle import Screen, Turtle
def add_dot_square(obj):
obj.penup()
obj.shape('square')
obj.shapesize(1.5)
obj.color('orange')
obj.goto(0, 0)
obj.stamp()
obj.dot(20, 'red')
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
add_dot_square(turtle)
screen.exitonclick()
I still ha[v]e a problem when I want to for example move this
square and dot
Let's rearrange the code a bit and add some motion:
from turtle import Screen, Turtle
def add_dot_square(obj):
obj.clear()
obj.stamp()
obj.dot(20, 'red')
screen = Screen()
screen.tracer(False)
turtle = Turtle()
turtle.hideturtle()
turtle.shape('square')
turtle.shapesize(1.5)
turtle.color('orange')
turtle.penup()
for _ in range(360):
turtle.circle(100, extent=1)
add_dot_square(turtle)
screen.update()
screen.exitonclick()

Can you make the turtle the center of a circle in python?

I am making a tic tac toe game and when the user presses 'o' a circle is printed but the circle is always on the left of the turtle. i would like the turtle to be in the center of a box and draw the circle around itself.
You have to move turtle on your own - using left,right,forward, penup, pendowm.
Example
import turtle
radius = 100
# move
turtle.penup()
turtle.right(90)
turtle.forward(radius)
turtle.left(90)
turtle.pendown()
# circle
turtle.circle(radius)
# move back
turtle.penup()
turtle.right(-90)
turtle.forward(radius)
turtle.left(-90)
turtle.pendown()
Result:
There are a couple of ways to draw a circle centered around a Python turtle without moving the turtle. The first is the dot() method. It takes a diameter, rather than a radius, and optionally allows you to specify the color at the same time:
import turtle
RADIUS = 100
turtle.dot(RADIUS * 2)
turtle.dot(RADIUS * 1.6, turtle.bgcolor()) # "unfill" the circle
turtle.done()
Another way to do it is via stamping, that is, make the turtle cursor itself a circle, size it, and then call stamp():
import turtle
RADIUS = 100
CURSOR_RADIUS = 10
turtle.hideturtle()
turtle.shape('circle')
turtle.fillcolor(turtle.bgcolor())
turtle.shapesize(RADIUS / CURSOR_RADIUS, outline=RADIUS/5)
turtle.stamp()
turtle.done()
Both of the above have the side effect of overwritting anything that the circle surrounds, which doesn't seem like a problem for tic-tac-toe. To avoid this, you can, of course, temporarily shift the turtle's position, as #furas suggests:
import turtle
RADIUS = 100
turtle.width(RADIUS/5)
turtle.penup()
turtle.sety(turtle.ycor() - RADIUS)
turtle.pendown()
turtle.circle(RADIUS)
turtle.penup()
turtle.sety(turtle.ycor() + RADIUS)
turtle.pendown()
turtle.done()

Make Turtle Bigger?

I have been trying to make a size difference but am unable to. Do I add or edit a line? Help is very much appriciated. Here is my code.
import turtle
#first turtle
my_turtle1 = turtle.Turtle()
my_turtle1.shape("square")
my_turtle1.screen.bgcolor("white")
my_turtle1.color('black')
my_turtle1.setheading(90)
#second turtle
my_turtle2 = turtle.Turtle()
my_turtle2.setposition(50,0)
my_turtle2.shape("square")
my_turtle2.color('black')
my_turtle2.setheading(90)
#3rd turtle
my_turtle3 = turtle.Turtle()
my_turtle3.setposition(100,0)
my_turtle3.shape("square")
my_turtle3.color('black')
my_turtle3.setheading(90)
#4th turtle
my_turtle4 = turtle.Turtle()
my_turtle4.setposition(150,0)
my_turtle4.shape("square")
my_turtle4.color('black')
my_turtle4.setheading(90)
turtle.done()
You can do this:
import turtle
#first turtle
my_turtle1 = turtle.Turtle()
my_turtle1.turtlesize(90, 90, 1) # pass, stretch width, stretch_len, outline, to this function like in the docs
turtle.done()
Link to docs: https://docs.python.org/3.3/library/turtle.html?highlight=turtle#turtle.turtlesize
Not sure what you are trying to say by making turtle bigger. Is it the size of the turtle or the shape.
For making the turtle bigger-use turtlesize()
For the shape,use shapesize()
I hope you find it helpful : )

Set the maximum distance that a turtle can go

I want to set a maximum distance that a turtle can travel. Using the code below, I want the first turtle who moves distance x forward to stop all the turtles:
for i in range(130):
alex.forward(randint(5,10))
tess.forward(randint(5,10))
tim.forward(randint(5,10))
duck.forward(randint(5,10))
dog.forward(randint(5,10))
You need just a bit more infrastructure than you currently have. To make it easier we'll need to work with individual elements of a list of turtles instead of individual variables for each turtle. Then we can test if a turtle has crossed the finish line by doing:
any(turtle.xcor() > 300 for turtle in turtles)
Here's an minimalist, example implementation:
from turtle import Screen, Turtle
from random import randint
COLORS = ["red", "green", "blue", "cyan", "magenta"]
for index, color in enumerate(COLORS):
turtle = Turtle('turtle')
turtle.color(color)
turtle.penup()
turtle.setposition(-300, -60 + index * 30)
screen = Screen()
turtles = screen.turtles()
while True:
for turtle in turtles:
turtle.forward(randint(5, 15))
if any(turtle.xcor() > 300 for turtle in turtles):
break
screen.mainloop()

Turtle graphic begin_fill() function does not work correctly on MAC

I am trying to draw a "Yellow" star by using module turtle. When I run my program on Windows OS, it works correctly. However, when I run it on macOS, the graphic is wrong.
Result on macOS
Result on Windows OS
import turtle
# Setup a screen and a turtle
win = turtle.Screen()
bob = turtle.Turtle()
# set the background color for the flag
win.bgcolor("red")
# Draw a star
# change the turtle color to yellow
bob.color("yellow")
# to center we have to go backward for half of a side length
bob.penup()
bob.back(100)
bob.pendown()
bob.begin_fill()
for i in range(5):
bob.forward(200)
bob.right(144)
bob.end_fill()
win.exitonclick()
This is not a turtle problem, but an issue with the underlying tkinter library. The fill on the two operating systems is different when there are crossing lines involved. The solution is to draw the star without crossing lines:
from turtle import Screen, Turtle
win = Screen()
win.bgcolor("red")
bob = Turtle()
bob.color("yellow")
bob.penup()
bob.goto(24.5, 33.1)
bob.pendown()
bob.begin_fill()
for i in range(5):
bob.forward(80)
bob.right(144)
bob.forward(80)
bob.left(72)
bob.end_fill()
bob.hideturtle()
win.exitonclick()
This should look the same on both implementations:

Categories

Resources