Set the maximum distance that a turtle can go - python

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()

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()

How do I make a turtle object go behind another turtle object in python?

I want to be able to easily make a square go behind a group of lines without lagging the program or causing any major bugs. I wanted to do all of this using the turtle module as I am currently making a game using that module. I have attempted this several times, but it was never working the way I wanted it to. I'm also using Python 3.8.
Here is my attempt at this problem I've been having:
import turtle
#The Setup
wn = turtle.Screen()
global WIDTH
global HEIGHT
WIDTH = 800
HEIGHT = 650
wn.setup(width = WIDTH, height = HEIGHT)
turtle.speed(0)
wn.title("Game by iAmInfernal")
wn.bgcolor('white')
wn.tracer(0)
#I create a turtle object
t = turtle. Turtle()
#I create a player object
player = turtle.Turtle()
t.stamp()
down = 0
player.shape('square')
player.color('red')
player.penup()
player.speed(0)
player.stamp()
#I make a function that will make 150 lines with a space of 10 units
def makeLines():
global down
for item in range(150):
t.penup()
t.goto(-400, 300 - down)
t.stamp()
t.pendown()
t.hideturtle()
t.pensize(3)
t.forward(800)
down += 10
while True:
makeLines()
wn.update()
If you execute this snippet of code you will be able to see that there are many lines with a red square in the center. However as many times I tried, the red square never went behind those group of lines. How exactly do I make the red square go behind those group of lines?
Turtles can't appear behind the things they draw, whether via drawing or stamping. However, turtles can appear behind other turtles, so one workaround might be to make the lines themselves be turtles:
from turtle import Screen, Turtle
WIDTH, HEIGHT = 800, 650
SPACING = 10
CURSOR_SIZE = 20
def makeLines():
down = 0
for _ in range(HEIGHT // SPACING):
turtle = prototype.clone()
turtle.sety(HEIGHT/2 - down)
turtle.showturtle()
down += SPACING
screen = Screen()
screen.setup(width=WIDTH, height=HEIGHT)
screen.tracer(0)
player = Turtle()
player.shape('square')
player.color('red')
player.penup()
player.stamp()
prototype = Turtle()
prototype.hideturtle()
prototype.shape('square')
prototype.shapesize(2 / CURSOR_SIZE, WIDTH / CURSOR_SIZE, 1)
prototype.penup()
makeLines()
screen.update()
screen.mainloop()
The problem then becomes controlling the layering of turtles where the basic rule of thumb is "the last thing that moved is on top".

Circle shape in turtle with smaller radius

This is my first question on StackOverflow, so bear with me. I am trying to have my turtle's shape be a small circle. However, the default circle is too big for my project. Is there a way to make it smaller? My company won't let me load a gif file to be the shape of a turtle.
What I've tried so far is with a gif file where I do:
import turtle
screen = turtle.Screen()
screen.register_shape('circle1', 'circle.gif')
t = turtle.Turtle()
t.shape('circle1')
t.forward(10)
This works but uses a gif file, which my company doesn't allow. Is there a way I can do this without a gif?
Working with the set of cursors that Python provides, including 'circle', you can adjust the size of the cursor using the shapesize() method (aka turtlesize):
from turtle import Screen, Turtle
screen = Screen()
turtle = Turtle()
turtle.shape('circle')
turtle.shapesize(0.5) # make the cursor half the default size
turtle.forward(100)
screen.exitonclick()
Above, we resized it relative to it's default size. If we want to size it to a specific pixel size, we start with the knowledge that the provided cursors are based on a 20px by 20px square, and adjust accordingly:
from turtle import Screen, Turtle
CURSOR_SIZE = 20
screen = Screen()
turtle = Turtle()
turtle.shape('circle')
turtle.shapesize(15 / CURSOR_SIZE) # make the cursor 15 pixels diameter
turtle.color('black', 'white') # make it an open circle this time
turtle.forward(100)
screen.exitonclick()
What you can do is input the polynomial corresponding to the size of the circle you want.
register_shape takes in more than just a file name, it can also take in coordinates.
i.e. screen.register_shape(name, coordinates) is another way of using that function.
If you want to make the circle smaller, draw the circle with the corresponding size and append it to a list. Then convert that list into a tuple and that can be your new shape.
In general you can draw any shape, append the coordinates and use that.
Here is an example for a circle:
Specific Example
import turtle
def drawCircle(radius,iterations, t): # returns tuples
coords = []
for i in range(iterations):
t.forward(radius)
t.right(360.0/iterations)
coords.append((t.xcor(), t.ycor()))
return tuple(coords)
t = turtle.Turtle()
screen = turtle.Screen()
screen.register_shape('myCircle', drawCircle(1, 72, t))
t.shape('myCircle')
General Example
import turtle
def drawShape(t):
coords = []
while # turtle move
coords.append((t.xcor(), t.ycor()))
return tuple(coords)
t = turtle.Turtle()
screen = turtle.Screen()
screen.register_shape('myShape', drawShape(t))
t.shape('myShape')
Note that when you do have a custom shape, then turtle will automatically fill it in for you. So this won't be a circle, but a filled-in circle instead.

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

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()

Creating multiple colored turtles in one window in editor on Python 3.4

So far I have this and it makes two circles but one is off-screen. I want to center it and have them separate from each other. Right now it does two loops but I want it to do one small circle then go on to make a larger one around the first in the middle of the screen. Both need to be diff. colors.
def sun_and_earth():
import turtle #allows me to use the turtles library
turtle.Turtle()
turtle.Screen() #creates turtle screen
turtle.window_height()
turtle.window_width()
turtle.bgcolor('grey') #makes background color
turtle.color("red", "green")
turtle.circle(2, 360) #draws a (size, radius) circle
turtle.circle(218, 360)
turtle.exitonclick() #exits out of turtle window on click of window
I think you may have some misunderstanding with regard to some of the functions in the turtle library. Firstly, turtle.window_height() and turtle.window_width() return the height and width of the window, so (as these values are not being assigned) those two lines do nothing. Similarly, turtle.Screen() returns an object, so again that line does nothing.
In order to centre your circles, you need to change where the turtle starts by using the turtle.setpos() function. This will change the x and y coordinates of where your turtle is. If you start the turtle one radius down, this will effectively centre the circle at (0, 0), because the center of the circle is (from the documentation) one radius to the left.
Remember to take your pen off the page when you are moving so that you don't draw lines between the two points by accident, and to put the pen back down again when you want to draw again.
Try this code:
import turtle
turtle.Turtle()
turtle.bgcolor('grey')
# decide what your small circle will look like
smallColour = "red"
smallRadius = 5
# draw the small circle
turtle.color(smallColour)
turtle.penup()
turtle.setpos(0, -smallRadius)
turtle.pendown()
turtle.circle(smallRadius)
# decide what your large circle will look like
largeColour = "white"
largeRadius = 100
# draw the large circle
turtle.color(largeColour)
turtle.penup()
print(turtle.pos())
turtle.setpos(0, -largeRadius)
print(turtle.pos())
turtle.pendown()
turtle.circle(largeRadius)
turtle.penup()
turtle.setpos(0, 0)
I hope this helps, but I think that you have a few misunderstandings about the use of the turtle, it might be a good idea to look at a tutorial or maybe take a look at the documentation
Best of luck

Categories

Resources