def main():
square(0,0,50,'red')
def square(x,y,width,color):
turtle.penup()
turtle.goto(0,0)
turtle.fillcolor(color)
turtle.pendown()
turtle.begin_fill()
for number in range(5):
for count in range(4):
turtle.forward(width)
turtle.left(90)
turtle.end_fill()
x = x+50
turtle.goto(x,y)
turtle.showturtle()
Call the main function
main()
This gives me one row of 5 squares. How do i code an outer loop to draw 4 more
of rows of such 5 squares - a 5 by 5 checkerboard?
I simplified your "square" function to only draw a single square. Then I added a separate function which included a nested loop that calls the square function.
Trying to keep it simple and have only one responsibility for each function:
import turtle
def main():
board(5, 5, 50)
input("Hit enter to close")
def square(x,y,width,color):
turtle.penup()
turtle.fillcolor(color)
turtle.goto(x, y)
turtle.pendown()
turtle.begin_fill()
for side in range(4):
turtle.forward(width)
turtle.left(90)
turtle.end_fill()
def board(rows, columns, square_width):
turtle.showturtle()
for row in range(rows):
for column in range(columns):
color = "red" if (row + column)%2 == 1 else "white"
square(row*square_width, column*square_width, square_width, color)
turtle.hideturtle()
You already have code to draw a row of squares, that's the loop you have right now.
You want that code to be run 5 times, so just wrap it in another loop, making sure to modify variables as necessary. Something along the lines of this:
for i in range(5):
for number in range(5):
for count in range(4):
turtle.forward(width)
turtle.left(90)
turtle.end_fill()
x += width
turtle.goto(x,y)
x -= 5*width
y += width
Also a note on style, square() is ignoring most of its parameters. You hardcode your turtle to go to (0,0), regardless of the values of x and y. Because your later gotos use these values, your code will break if you set them to anything other than 0. Your line x = x+50 also ignores the value of width, even though the line the turtle draws uses it. Again, if you set it to anything other than 50, this will break.
Related
I'm trying to create a rhombus tessellation pattern with the turtle graphics on python that looks like this image:
http://www.supercoloring.com/sites/default/files/styles/coloring_medium/public/cif/2015/01/tessellation-with-rhombus-coloring-pages.png
I thought about creating a hexagon pattern first and then dividing the hexagons into thirds. I'm not sure how I can create the hexagon pattern recursively. So far, I'm only alternating the angles of the turtles as I run the program and I don't have a definite strategy. Any advice on how to approach this?
So far, I created 3 hexagons in the center with 3 turtles and used for loops to draw the hexagons around the 3 hexagons. However, when I loop the program, the turtles trace back the same path as before and it takes a while for it to draw the others.
Here is my code so far:
import turtle
t = turtle.Turtle()
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t1.left(120)
t2.left(240)
for i in (t, t1, t2):
i.speed(0)
def hex():
for b in (t, t1, t2):
for i in range(6):
b.forward(100)
b.right(60)
for i in range(3):
t.left(120)
hex()
def rep():
for i in range(3):
for a in (t, t1, t2):
a.forward(100)
a.right(60)
for i in range(6):
a.forward(100)
a.left(60)
rep()
def rep2():
for a in (t, t1, t2):
for i in range(3):
a.left(120)
a.forward(100)
a.right(60)
rep()
a.right(120)
a.forward(100)
a.right(60)
rep()
rep2()
There are many of drawing this. I would draw based the rhombus shape because it will allow you to fill them with different colors. To be able to fill each rhombus, it needs to be drawn individually. The figure contains three different rhombus shapes (they are the same rhombus in different orientation).
I will draw first row and second row. After that it is repetition of the first and second row. Here is code:
def draw_rhombus(x,y,degree,size,tilt,color):
turtle.up()
turtle.goto(x,y)
turtle.seth(tilt)
turtle.down()
turtle.pencolor('dark gray')
turtle.fillcolor(color)
turtle.begin_fill()
turtle.fd(size)
turtle.left(degree)
turtle.fd(size)
turtle.left(180-degree)
turtle.fd(size)
turtle.left(degree)
turtle.fd(size)
turtle.left(180-degree)
turtle.end_fill()
def draw_rhombus_1(x,y,size):
draw_rhombus(x,y,120,size,0,'red')
def draw_rhombus_2(x,y,size):
draw_rhombus(x,y,60,size,0,'green')
def draw_rhombus_3(x,y,size):
draw_rhombus(x,y,60,size,60,'blue')
def rt_row_1(startx,starty,size,n):
x = startx
y = starty
for i in range(n):
draw_rhombus_1(x,y,size)
x += size
draw_rhombus_3(x,y,size)
draw_rhombus_2(x,y,size)
x += 2*size
def rt_row_2(startx,starty,size,n):
x = startx
y = starty
for i in range(n):
draw_rhombus_2(x,y,size)
x += 2*size
draw_rhombus_1(x,y,size)
x += size
draw_rhombus_3(x,y,size)
size = 80
x = -400
y = -400
for i in range(800//int(round(size*math.sqrt(3)))):
rt_row_1(x,y,size,800//(size*3))
rt_row_2(x-size/2,y+size*math.sqrt(3)/2,size,800//(size*3))
y += size*math.sqrt(3)
First, let's simplify your three turtle, three function hexagonal tessellation to a single turtle, single recursive function solution:
from turtle import Screen, Turtle
OUTER_RADIUS = 100
INNER_RADIUS = 3**0.5 * OUTER_RADIUS / 2
SIDES = 6
EXTENT = 360 / SIDES
def tessellation(depth):
turtle.right(EXTENT/2)
for _ in range(SIDES):
turtle.circle(OUTER_RADIUS, EXTENT, 1)
if depth:
heading = turtle.heading()
turtle.right(90)
tessellation(depth - 1)
turtle.setheading(heading)
screen = Screen()
turtle = Turtle(visible=False)
screen.tracer(False) # because I have no patience
turtle.penup()
turtle.goto(-OUTER_RADIUS / 2, -INNER_RADIUS)
turtle.pendown()
tessellation(2)
screen.tracer(True)
screen.exitonclick()
(Increase the depth argument to fill the window.) The tessellation you really want is four (not thirds) of these patterns overlaid atop each other. Keeping our initial code the same:
screen = Screen()
turtle = Turtle(visible=False)
screen.tracer(False) # because I have no patience
turtle.penup()
turtle.color('blue')
turtle.goto(OUTER_RADIUS / 4, -1 * INNER_RADIUS / 2)
turtle.pendown()
turtle.setheading(0)
tessellation(2)
turtle.penup()
turtle.color('red')
turtle.goto(-OUTER_RADIUS / 2, -2 * INNER_RADIUS / 2)
turtle.pendown()
turtle.setheading(0)
tessellation(2)
turtle.penup()
turtle.color('yellow')
turtle.goto(OUTER_RADIUS / 4, -3 * INNER_RADIUS / 2)
turtle.pendown()
turtle.setheading(0)
tessellation(2)
turtle.penup()
turtle.color('green')
turtle.goto(-OUTER_RADIUS / 2, -4 * INNER_RADIUS / 2)
turtle.pendown()
turtle.setheading(0)
tessellation(2)
screen.tracer(True)
screen.exitonclick()
I've been trying to code this, but it's not working and I'm confused.
Use recursion to draw the “H-Tree” fractal.
The H-Tree fractal is defined as follows:
Begin with the letter H. The Three lines of the H are all of the same length, as shown in the first image. This is an H-Tree of order 0.
The letter H has four endpoints. Draw an H centered at each of the four endpoints, as shown in the second image. These H’s are half the size of the previous H. This is an
H-Tree of order 1.
Repeat step 2 to create an H-Tree fractal of higher orders, as shown in images 3 and 4.
Your function definition MUST have this format:
def h_tree(order, center, size):
Where:
order is is the order of the fractal, as described above
center is the center point of the H-Tree, in the format [x, y]
size is the length of each of the lines in the H
Here's my code:
import turtle
#reverse method
def reverse_string(string):
if len(string) == 0:
return string
else:
return reverse_string(string[1:]) + string[0]
#fractal method
def h_tree(order, center, size):
if order == 0:
draw_turtle([0,0], 300)
else:
get_endpoints(center, size)
h_tree(order-1, ep1, size)
h_tree(order-1, ep2, size)
h_tree(order-1, ep3, size)
h_tree(order-1, ep4, size)
def draw_turtle(center, size):
turtle.showturtle()
turtle.penup()
turtle.goto(center)
turtle.pendown()
turtle.forward(size/2)
turtle.left(90)
turtle.forward(size/2)
turtle.right(180)
turtle.forward(size)
turtle.penup()
turtle.goto(center)
turtle.right(90)
turtle.pendown()
turtle.forward(size/2)
turtle.right(90)
turtle.forward(size/2)
turtle.right(180)
turtle.forward(size)
turtle.done()
def get_endpoints(center, size):
ep1 = center[0] + size/2
ep2 = center[1] + size/2
ep3 = center[0] + size/2
ep4 = center[1] + size/2
return [ep1, ep2, ep3, ep4]
'''
The animate function needs to take in an x and y as arguments
because of how we are calling it from the main function.
Since it is being called when the mouse is clicked, it is
required to take the x,y location of the mouse at the time
of the click. You do not need to use the x,y for anything, so
just leave them there as parameters but you do not need to use
them inside your function.
'''
'''
def animate(x, y):
return
'''
def main():
# Q1 - call the recursive reverse_string() function
print(reverse_string("desserts"))
print(reverse_string("flow"))
print(reverse_string("abcdefg"))
# Q2 - call the recursive H-Tree fractal function
turtle.speed(0)
turtle.hideturtle()
h_tree(2, [0, 0], 300)
turtle.done()
'''
# Q3 - when the mouse is clicked in the turtle window,
# call the animate() function to display a spinning star
turtle.onscreenclick(animate)
'''
main()
You have a logic that's basically sound, though not the way I'd do it, but needs some patching. Mostly because you didn't follow through on your own logic. You know that h_tree() takes a position (pair of numbers) as its second argument but you only passed a single number (if even that, as #SRTHellKitty notes, you forgot to save the result of get_endpoints()). You also forgot to reduce size by half on the recursive call. You miscalculated two of the end points. And you didn't leave the turtle in its original orientation at the end of draw_turtle() which would have left you with half the H figures rotated, should you get it working.
Below is the repaired code -- I've pulled out your code for Q1 since it wasn't related to the problems you are asking about:
import turtle
def h_tree(order, center, size):
draw_turtle(center, size)
if order > 0:
ep1, ep2, ep3, ep4 = get_endpoints(center, size)
h_tree(order - 1, (ep1, ep2), size / 2)
h_tree(order - 1, (ep1, ep4), size / 2)
h_tree(order - 1, (ep3, ep2), size / 2)
h_tree(order - 1, (ep3, ep4), size / 2)
def draw_turtle(center, size):
turtle.penup()
turtle.goto(center)
turtle.pendown()
turtle.forward(size / 2) # right side of H
turtle.left(90)
turtle.forward(size / 2)
turtle.right(180)
turtle.forward(size)
turtle.penup()
turtle.goto(center)
turtle.pendown()
turtle.right(90) # left side of H
turtle.forward(size / 2)
turtle.right(90)
turtle.forward(size / 2)
turtle.right(180)
turtle.forward(size)
turtle.right(90) # return turtle to original orientation
def get_endpoints(center, size):
ep1 = center[0] + size / 2
ep2 = center[1] + size / 2
ep3 = center[0] - size / 2
ep4 = center[1] - size / 2
return ep1, ep2, ep3, ep4
def main():
turtle.speed('fastest')
h_tree(2, (0, 0), 300)
turtle.hideturtle()
turtle.done()
main()
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.
QUESTION: Implement the following pseudo-code to draw a checkered flag to the screen.
1. Ask the user for the size of the checkered flag (n).
2. Draw an n x n grid to the screen.
3. For i = 0,2,4,...,62:
4. row = i // n
5. offset = row % 2
6. col = (i % n) + offset
Please copy and paste the link see the output:
I implemented the pseudocode, but I need some help. I can able to draw the n*n grid; moreover, I am keep getting this error: NameError: name 'row' is not defined
My program:
from turtle import*
def size():
size = eval(input("Please enter the size of the checkered flag: "))
return size
def draw(n):
wn = Screen()
wn.setworldcoordinates(-1,-1,10,10)
pen = Turtle()
for i in range(0,n+1):
pen.up()
pen.goto(0,i)
pen.down()
pen.forward(n)
pen.left(90)
for i in range(0,n+1):
pen.up()
pen.goto(i,0)
pen.down()
pen.forward(n)
def findGrid(n):
for i in range(0,63):
row = i // n
offset = row % 2
col = (i % n) + offset
return row, col
def fillSquare(x,y):
pen = Turtle()
pen.hideturtle()
pen.speed(10)
pen.up()
pen.goto(x,y)
pen.fillcolor("black")
pen.begin_fill()
def main():
x = size()
y = draw(x)
row, col = findGrid(x) #I think the problem is here.
f = fillSquare(row, col)
main()
The code you posted in your question doesn't draw squares because you don't have any turtle operations after pen.begin_fill().
You can draw a filled square like so:
turtle.begin_fill()
for i in range(4):
turtle.forward(1)
turtle.right(90)
turtle.end_fill()
There's actually a mistake in the pseudocode. The offset calculation offset = row % 2 is only valid when n, the number of rows, is an even number. The pseudocode fails to calculate checkered square locations when n is odd.
To make the code work for even and odd values of n, you can evaluate the offset as follows:
offset = ~(n % 2) & (row % 2)
I have implemented these changes in the code below. I have also modified the structure of your program by defining the turtle outside the drawing functions and passing it in as an argument. This lets us set the turtle's speed and visibility settings just once instead of having to do so within each drawing function.
from turtle import*
# Ask the user for the size of the checkered flag (n).
def getSize():
size = eval(input('Please enter the size of the checkered flag: '))
return size
# Draw an n x n grid to the screen.
def drawGrid(turtle, n):
for i in range(0, n+1):
turtle.up()
turtle.goto(0, i)
turtle.down()
turtle.forward(n)
turtle.left(90)
for i in range(0, n+1):
turtle.up()
turtle.goto(i, 0)
turtle.down()
turtle.forward(n)
# Fill the square in the given row and column.
def fillSquare(turtle, row, col):
turtle.up()
turtle.goto(col, row)
turtle.begin_fill()
for i in range(4):
turtle.forward(1)
turtle.right(90)
turtle.end_fill()
def main():
# Get the user's input.
n = getSize()
# Set up the drawing coordinates.
screen = Screen()
screen.setworldcoordinates(-1, -1, 10, 10)
# Make a turtle object for use in drawing. Maximize its speed.
turtle = Turtle()
turtle.speed('fastest')
turtle.hideturtle()
# Draw the checkered flag.
drawGrid(turtle, n)
for i in range(0, n*n, 2):
row = i // n
offset = ~(n % 2) & (row % 2)
col = i % n + offset
fillSquare(turtle, row, col)
print('Hit Enter to quit.')
input()
main()
This is a situation where I believe stamping makes things simpler than drawing in Python turtle:
from turtle import Turtle, Screen
CURSOR_SIZE = 20
def getSize():
""" Ask user for the size of the checkered flag. """
return int(input('Please enter the size of the checkered flag: '))
cells = getSize()
screen = Screen()
size = min(screen.window_width() - 10, screen.window_height() - 30) / cells
offset = (cells % 2) * size/2 + size/2 # properly center odd & even cells
turtle = Turtle('square', visible=False)
turtle.shapesize(size / CURSOR_SIZE)
turtle.speed('fastest')
turtle.color('black')
turtle.penup()
for row in range(-cells // 2, cells // 2):
parity = row % 2 # properly color cells
turtle.goto(-cells // 2 * size + offset, row * size + offset)
for column in range(cells):
turtle.fillcolor(['white', 'black'][parity == column % 2])
turtle.stamp()
turtle.forward(size)
screen.exitonclick()
Stamping also makes the program faster as we're working with larger chunks of the drawing.
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()