I have to make a change to this specific code, which produces a square grid of circles, I have to change the code to make a triangle grid of circles.
import turtle
window = turtle.Screen()
my_boi = turtle.Turtle()
my_boi.speed(0)
for y in range(-200,200,50):
for x in range(-200,200,50):
my_boi.penup()
my_boi.setposition(x,y)
my_boi.pendown()
my_boi.circle(20)
window.exitonclick()
I'm sure there is a smarter approach, but this is one way to do it:
import turtle
window = turtle.Screen()
my_boi = turtle.Turtle()
my_boi.speed(0)
for (i,y) in enumerate(range(-200,200,50)):
for x in range(-200+(25*i),200-(25*i),50):
my_boi.penup()
my_boi.setposition(x,y)
my_boi.pendown()
my_boi.circle(20)
window.exitonclick()
turtle.done()
In the second for-loop the range is iteratively decreased by 1/2 of the circle diameter in each side.
I'd simplify things somewhat:
from turtle import Screen, Turtle
window = Screen()
my_boi = Turtle()
my_boi.speed('fastest')
my_boi.penup()
for y in range(1, 9):
my_boi.setposition(-25 * y + 25, 50 * y - 250)
for x in range(y):
my_boi.pendown()
my_boi.circle(20)
my_boi.penup()
my_boi.forward(50)
my_boi.hideturtle()
window.exitonclick()
Only the starting position of each row has to be calculated and placed via setposition(). The column positions can be a simple forward() statement.
I know this is an old post, but I was looking for the answer and managed to figure it out, so to help everyone else out, All you need to do is change the stop range in the nested loop to be y. I switched the x and y variables because I wanted the triangle to be flat, but if you need it the other way that also works.
import turtle
window = turtle.Screen()
my_boi = turtle.Turtle()
my_boi.speed(0)
for x in range(-200,200,50):
for y in range(-200,x,50):
my_boi.penup()
my_boi.setposition(x,y)
my_boi.pendown()
my_boi.circle(20)
window.exitonclick()
Related
Been using turtle library to make a billiard and cue ball trajectory. I have implemented the ball trajectory physics, so when it hits the walls (or limited distance specified) it reflects to the appropriate direction. But I faced a problem where the ball goes to a direction I didn't specify then according to the heading given it rotates a bit to that direction. I debugged the code and found no issues, and tried to find where the coordinates the cue ball is trying to initially go to, knowing that if the loop of ball movement changes, the initial value always is to go to that direction and breaks the point of having a heading.
import turtle
from math import *
t = turtle.Turtle()
width=200
# White ball - Moving ball
t.color("black")
t.shape("circle")
t.penup()
t.goto(90,-80)
# White ball - Permanent
ball=turtle.Turtle()
ball.speed(0)
ball.penup()
ball.goto(90,-80)
ball.color("black")
ball.shape("circle")
angleString="Enter the hit force's angle (0-360) in degrees: "
angle=int(input(angleString))
# Moving Ball
t.color("yellow")
# Ball movement mechanic
step=20
t.penup()
LIMIT_X=width-8
LIMIT_Y=(width/2)-8
t.setheading(angle)
t.pendown()
x=-LIMIT_X
y=LIMIT_Y
t.dx=step*cos(angle)
t.dy=step*sin(angle)
for x in range(50):
t.speed(1)
print(x,t.pos(), t.heading())
x+=t.dx
y+=t.dy
t.goto(x,y)
# Check vertical borders
if abs(x)>LIMIT_X:
t.dx=-t.dx
# Check horizontal borders
if abs(y)>LIMIT_Y:
t.dy=-t.dy
turtle.done()
showcasing an example of angle 120
I see two problems with your code. First, you have two different x variables in play at the same time:
x=-LIMIT_X
y=LIMIT_Y
t.dx=step*cos(angle)
t.dy=step*sin(angle)
for x in range(50):
t.speed(1)
print(x,t.pos(), t.heading())
x+=t.dx
y+=t.dy
t.goto(x,y)
I'm guessing the iteration x variable is only meant for the debugging print() statement, not to position the ball, and should be renamed.
Second, angle is in degrees but the trigonometric funtions from the math module assume radians, so these calculations make no sense:
t.dx=step*cos(angle)
t.dy=step*sin(angle)
Use the radians() function from math to convert the angle. (Turtle can work with either, but you need to tell it if you want to use radians instead of degrees.)
Additionally, this statement does nothing:
t.setheading(angle)
as you move your ball with goto(). The heading would only matter if you were moving your ball with forward().
My rework of your code fixing the above along with other changes:
from turtle import Screen, Turtle
from math import sin, cos, radians
WIDTH = 200
LIMIT_X = WIDTH - 8
LIMIT_Y = WIDTH/2 - 8
STEP = 20
ANGLE_STRING = "Enter the hit force's angle (0-360) in degrees: "
CURSOR_SIZE = 20
angle = int(input(ANGLE_STRING))
screen = Screen()
# Billard table (roughly)
table = Turtle()
table.hideturtle()
table.shape('square')
table.shapesize(WIDTH / CURSOR_SIZE + 1, WIDTH*2 / CURSOR_SIZE + 1)
table.color('green')
table.stamp()
# Black ball - Permanent
permanent = Turtle()
permanent.shape('circle')
permanent.color('black')
permanent.penup()
permanent.goto(90, -80)
# Yellow ball - Moving ball
moving = permanent.clone()
moving.color('yellow')
moving.pendown()
x = -LIMIT_X
y = LIMIT_Y
moving.dx = STEP * cos(radians(angle))
moving.dy = STEP * sin(radians(angle))
for z in range(50):
moving.speed('slowest')
print(z, moving.pos(), moving.heading())
x += moving.dx
y += moving.dy
moving.goto(x, y)
# Check vertical borders
if abs(x) > LIMIT_X:
moving.dx = -moving.dx
# Check horizontal borders
if abs(y) > LIMIT_Y:
moving.dy = -moving.dy
screen.mainloop()
I'm not saying it does what you want, it's simply less buggy. Perhaps basing your motion on setheading() and forward() instead of goto() might get you the result you desire. You'll have to rethink your wall reflection logic, of course.
I have a homework to draw a spiral(from inside to outside) in python with turtle, but I cant think of a way to do that, beside what I did its need to be like this:
I tried to do it like that, but its not working properly.
import turtle
turtle.shape ('turtle')
d = 20 #Distance
a = 1 #StartingAngle
x = 200 #Num of loops
for i in range (x):
turtle.left(a)
turtle.forward(d)
a = a + 5
You are increasing the wrong variable in your loop.
A spiral is like a "circunference whose radius increases over time".
You should increase the d variable over time, and not the angle a. The amount of the increase of each loop will be essential to determine the appearance of your spiral, but you can obtain a good value for the increase with some calculation or by trial and error.
import turtle
from math import sin,cos,pi
t=turtle.Turtle()
t.speed(0)
n=50 #number of spirals
d=10 #distance between 2 spirals
r=0 #radius
x,y = 0, 0
cur_r = r
for i in range(n):
for a in range(1,360, 4):
r = cur_r + d*a/360.0
a *= pi/180.0
y = r*sin(a)
x = r*cos(a)
turtle.goto(x,y)
cur_r += d
import turtle
i=0
while i<4:
turtle.speed(0)
turtle.pensize(1+i)
turtle.forward(1+i)
t.color("blue")
turtle.bgcolor("pink")
t.lt(3)
i+=0.003
If you are using program other than IDLE(such as pycharm), add
turtle.mainloop()
at the end of the code
import turtle as t
for i in range(360):
t.circle(i,20)
Source: Explained here
def drawTri(a):
b = (a*math.tan(45))
c = (a/math.cos(45))
t.forward(a)
t.left(135)
t.forward(c)
t.left(135)
t.forward(b)
import turtle
def drawTri(a):
hyp = a * 2**0.5
s = turtle.Screen()
t = turtle.Turtle()
t.forward(a)
t.left(135)
t.forward(hyp)
t.left(135)
t.forward(a)
The problem here is close to that described in Basic trigonometry isn't working correctly in python
The turtle module uses degrees for angles, the math module uses radians
To calculate the cosine of 45 degrees you can use
math.cos(math.radians(45))
i made a relay simple way of making a right angle triangle i also added some other helpful things to know about python turtle that is you didn't already know them they should be helpful(i know you already have an answer but i just think that this is a much simpler way)
import turtle
t = turtle
f = t.forward
r = t.right
t.color('blue','yellow')
t.begin_fill()
f(70)
r(135)
f(100)
r(135)
f(70)
r(135)
t.end_fill()
t.penup()
t.setposition(-50,30)
t.pendown()
t.color('blue','yellow')
t.begin_fill()
f(70)
r(135)
f(100)
r(135)
f(70)
r(135)
t.end_fill()
there are two triangles there and its the extra stuff that makes it so bulky
Who needs angles?
def drawTri(a):
x, y = turtle.position()
turtle.setx(x + a)
turtle.sety(y + a)
turtle.goto(x, y)
I am a beginner at Python and I'm new to Stack Exchange. I'm trying to write a program that has 5 turtles moving within a square. I've got code that does what I want, but it's tedious and I'd like to initialize all my turtles using classes instead of doing it one by one. I just want them to start out at random coordinates and with a random heading.
The problems with my code:
Only one turtle is shown on screen. Two are defined in the code below.
The turtle's heading and coordinates aren't being initialized.
Here's the code that I've tried:
import numpy as np
from turtle import *
# setting up screen
reset()
screensize(550)
Screen().bgcolor('black')
tracer(0)
# drawing box
t0 = Turtle()
t0.penup()
t0.goto(-256,-256)
t0.color('cyan')
t0.pendown()
for i in range(4):
t0.forward(512)
t0.left(90)
t0.ht()
# parameters
velocity = 5
iterations = 200
boxsize = 512
ranheader = np.random.random()*360
ranx = np.random.random()*boxsize
rany = np.random.random()*boxsize
class turtle_agents(Turtle):
def _init_(self):
self.up()
self.seth(ranheader)
self.setpos(ranx,rany)
self.velocity = velocity
self.down()
# turtle
t1 = turtle_agents()
t1.color('green')
t2 = turtle_agents()
t2.color('blue')
# turtle movement
for turtle in turtles():
for i in range(iterations):
turtle.forward(velocity)
if turtle.xcor() >= 256:
turtle.goto(-256,t0.ycor())
elif turtle.xcor() <= -256:
turtle.goto(256,t0.ycor())
elif turtle.ycor() >= 256:
turtle.goto(t0.xcor(),-256)
elif turtle.ycor() <= -256:
turtle.goto(t0.xcor(),256)
update()
exitonclick()
only one turtle shown on screen. Two are defined in the code below.
the turtle's heading and coordinates aren't being initialized.
I believe the problem is that you defined the random position and heading once, outside the turtle creation loop so they all start in the same place, move in the same direction at the same speed. I.e. they're right on top of each other.
We don't need #BlivetWidget's explicit List to fix the problem since, as you discovered, turtles are already maintained in a list which we can get via the screen's turtles() method. Below is my rework of your code to fix various issues:
from turtle import Screen, Turtle
from random import randrange, randint
# parameters
COLORS = ['green', 'blue', 'red', 'orange', 'white']
ITERATIONS = 500
VELOCITY = 5
BOX_SIZE = 512
# setting up screen
screen = Screen()
screen.setup(BOX_SIZE + 50, BOX_SIZE + 50)
screen.bgcolor('black')
screen.tracer(False)
# drawing box
turtle = Turtle()
turtle.hideturtle()
turtle.color('cyan')
turtle.penup()
turtle.goto(-BOX_SIZE/2, -BOX_SIZE/2)
turtle.pendown()
for _ in range(4):
turtle.forward(BOX_SIZE)
turtle.left(90)
# turtle
for color in COLORS:
angle = randrange(360)
x = randint(-BOX_SIZE/2, BOX_SIZE/2)
y = randint(-BOX_SIZE/2, BOX_SIZE/2)
turtle = Turtle()
turtle.color(color)
turtle.setheading(angle)
turtle.penup()
turtle.setposition(x, y)
turtle.pendown()
# turtle movement
for _ in range(ITERATIONS):
for turtle in screen.turtles():
turtle.forward(VELOCITY)
x, y = turtle.position()
if x >= BOX_SIZE/2:
turtle.penup()
turtle.setx(-BOX_SIZE/2)
turtle.pendown()
elif x <= -BOX_SIZE/2:
turtle.penup()
turtle.setx(BOX_SIZE/2)
turtle.pendown()
elif y >= BOX_SIZE/2:
turtle.penup()
turtle.sety(-BOX_SIZE/2)
turtle.pendown()
elif y <= -BOX_SIZE/2:
turtle.penup()
turtle.sety(BOX_SIZE/2)
turtle.pendown()
screen.update()
screen.exitonclick()
I agree with #BlivetWidget that "you don't need to create a class just to move them to your starting positions". I use a simple loop above.
You should consider storing your turtles in a list, as the turtles are already objects and you don't need to create a class just to move them to your starting positions. Lists in Python are incredibly powerful because they can store arbitrary data types. Here, I will create 5 turtles and move them so you can tell them apart:
import turtle
num_turtles = 5
my_turtles = [turtle.Turtle() for i in range(num_turtles)]
for i, turt in enumerate(my_turtles):
turt.forward(50 * i)
You want to do the same thing, just replace my turt.forward() line with whatever you want the turtles to do. In your case, go to a random position within your square.
I am trying to fill the color in these squares:
Right now the turtle only fills the corners of theses squares, not the entire square.
Here is my code:
import turtle
import time
import random
print ("This program draws shapes based on the number you enter in a uniform pattern.")
num_str = input("Enter the side number of the shape you want to draw: ")
if num_str.isdigit():
squares = int(num_str)
angle = 180 - 180*(squares-2)/squares
turtle.up
x = 0
y = 0
turtle.setpos(x,y)
numshapes = 8
for x in range(numshapes):
turtle.color(random.random(),random.random(), random.random())
x += 5
y += 5
turtle.forward(x)
turtle.left(y)
for i in range(squares):
turtle.begin_fill()
turtle.down()
turtle.forward(40)
turtle.left(angle)
turtle.forward(40)
print (turtle.pos())
turtle.up()
turtle.end_fill()
time.sleep(11)
turtle.bye()
I've tried moving around turtle.begin_fill() and end_fill() in numerous locations with no luckā¦ Using Python 3.2.3, thanks.
I haven't really used turtle, but it looks like this may be what you want to do. Correct me if I've assumed the wrong functionality for these calls:
turtle.begin_fill() # Begin the fill process.
turtle.down() # "Pen" down?
for i in range(squares): # For each edge of the shape
turtle.forward(40) # Move forward 40 units
turtle.left(angle) # Turn ready for the next edge
turtle.up() # Pen up
turtle.end_fill() # End fill.
You're drawing a series of triangles, using begin_fill() and end_fill() for each one. What you can probably do is move your calls to begin_fill() and end_fill() outside the inner loop, so you draw a full square and then ask for it to be filled.
Use fill
t.begin_fill()
t.color("red")
for x in range(4):
t.fd(100)
t.rt(90)
t.end_fill()
Along with moving begin_fill() and end_fill() outside the loop, as several folks have mentioned, you've other issues with your code. For example, this is a no-op:
turtle.up
I.e. it doesn't do anything. (Missing parentheses.) This test:
if num_str.isdigit():
Doesn't do much for you as there is no else clause to handle the error. (I.e. when it isn't a number, the next statement simply uses the string as a number and fails.) This calculation seems a bit too complicated:
angle = 180 - 180*(squares-2)/squares
And finally there should be a cleaner way to exit the program. Let's address all these issues:
from turtle import Screen, Turtle
from random import random
NUMBER_SHAPES = 8
print("This program draws shapes based on the number you enter in a uniform pattern.")
num_str = ""
while not num_str.isdigit():
num_str = input("Enter the side number of the shape you want to draw: ")
sides = int(num_str)
angle = 360 / sides
delta_distance = 0
delta_angle = 0
screen = Screen()
turtle = Turtle()
for x in range(NUMBER_SHAPES):
turtle.color(random(), random(), random())
turtle.penup()
delta_distance += 5
turtle.forward(delta_distance)
delta_angle += 5
turtle.left(delta_angle)
turtle.pendown()
turtle.begin_fill()
for _ in range(sides):
turtle.forward(40)
turtle.left(angle)
turtle.forward(40)
turtle.end_fill()
screen.exitonclick()