Creating Bricks with Turtle Python - python

i'm very new to programming and in here i'm supposed to create a pyramid shape of bricks where the edge of the pyramid is filled with one color and the inside of it is filled with random colors, but i can't seem to figure it out how to move the bricks to create a new row, column and the random colors specified only to inside bricks of the pyramid. Does anyone have a suggestion? Thanks in advance. This is my code and it's still working for one brick only:
import turtle
import math
import random
bottom_brick = 10
top_brick = 1
brick_length = 35
brick_width = 25
from turtle import *
from turtle import Screen
screen = Screen()
screen.bgcolor('white')
turtle.speed('fastest')
penup ()
goto(0, -100)
pendown()
fillcolor('#BC4A3C')
begin_fill()
#bottom_brick and top_brick are inputs from the user
#to indicate how many bricks are used for the bottom layer and the top layer
# brick_length and brick_width are also input from user
for i in range (0, bottom_brick):
turtle.setposition(0.5 * (i % 2), i)
for j in range(i, bottom_brick):
forward(brick_length)
left(90)
forward(brick_width)
left(90)
forward(brick_length)
left(90)
forward(brick_width)
left(90)
end_fill()
done()

This will get you most of the way there with regards to making sure your bricks are laid out correctly:
import turtle
bottom_brick = 10
top_brick = 1
brick_length = 35
brick_width = 25
turtle.Screen().bgcolor('white')
turtle.speed('fastest')
turtle.penup()
turtle.goto(0, -100)
turtle.pendown()
#bottom_brick and top_brick are inputs from the user
#to indicate how many bricks are used for the bottom layer and the top layer
# brick_length and brick_width are also input from user
for i in range(bottom_brick - top_brick + 2):
for j in range(bottom_brick - i + 1):
turtle.setposition((j + i / 2) * brick_length, i * brick_width)
turtle.fillcolor('#BC4A3C')
turtle.begin_fill()
turtle.forward(brick_length)
turtle.left(90)
turtle.forward(brick_width)
turtle.left(90)
turtle.forward(brick_length)
turtle.left(90)
turtle.forward(brick_width)
turtle.left(90)
turtle.end_fill()
turtle.done()
Note that the fill color needs to be set inside the loop, and that's the spot where you'll want to randomize the color so that each brick is different. You'll also need to be mindful of when the pen is up and when it's down in order to avoid extra lines showing up.

This seems like an opportunity for better living through stamping instead of drawing:
from turtle import Screen, Turtle
from random import random
top_brick = 1
bottom_brick = 10
brick_length = 35
brick_width = 25
CURSOR_SIZE = 20
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
turtle.shape('square')
turtle.shapesize(brick_width / CURSOR_SIZE, brick_length / CURSOR_SIZE)
turtle.penup()
turtle.sety((bottom_brick - top_brick + 1) * brick_width / -2)
for row in range(bottom_brick, top_brick - 1, -1):
turtle.goto(-brick_length * (row / 2), turtle.ycor() + brick_width)
for column in range(row):
if top_brick < row < bottom_brick and 0 < column < row - 1:
turtle.fillcolor(random(), random(), random())
else:
turtle.fillcolor('#BC4A3C')
turtle.stamp()
turtle.forward(brick_length)
screen.exitonclick()
Plus an example of how you might handle the brick coloring issue.

Related

Python turtle drawing equal cells in a rectangle

I'm trying to use turtle in order to draw a rectangle, and then 32 equal cells inside it. But I somehow can't get it right, I don't know why.
Here is the code I've written:
import turtle, tkinter, datetime, time
turtle.setx(-400)
turtle.sety(200)
turtle.pendown()
turtle.pencolor('#00807c')
for i in range (0,4):
x = 800
if i%2 == 0:
turtle.fd(x)
else:
turtle.fd(x/2)
turtle.right(90)
def cells(position):
for i in range (0,4):
x = 100
turtle.fd(x)
turtle.right(90)
if turtle.pos() == position:
turtle.fd(x)
position = turtle.pos()
for j in range(0, 8):
cells(turtle.pos())
turtle.done()
The result is weird, it only draws three or four cells and then the program ends.
I'd be grateful if somebody could possibly help me with this problem. Thanks.
I've rewritten your code and I don't understand why you're using a function. I've used 3 loops:
loop 3(4):
loop 2(8):
loop 1(4):
The first loop repeats himself 4 times and draws the sides of 1 square
The second loop runs the first loop 8 times, so it draws 8 squares next to each other
The third loop runs the second loop 4 times, so it draws 4 lines of 8 squares.
And that makes a field of 32 cells.
My code:
import turtle, tkinter, datetime, time
turtle.penup()
turtle.hideturtle()
turtle.setx(-400)
turtle.sety(200)
turtle.pendown()
turtle.pencolor('#00807c')
turtle.speed(0)
turtle.pendown()
for i in range (4):
x = 800
if i%2 == 0:
turtle.fd(x)
else:
turtle.fd(x/2)
turtle.right(90)
for w in range (4):
for i in range (8):
for i in range (4):
x = 100
turtle.fd(x)
turtle.right(90)
turtle.forward(x)
turtle.penup()
turtle.goto(-400,turtle.ycor()-100)
turtle.pendown()
turtle.done()
PS: I've also changed a few things like:
I hid the turtle
I changed the speed (to maximum)
I added a turtle.penup()-command before I moved the turtle in the beginning, so you don't see a black line.
Kind regards
spyrolix
You can create two functions, to simplify the logic: One to draw a square, and one to position the turtle at the place to draw a square. Then, using a little bit of index arithmetic in two nested loops (one for rows, and one for columns), use the indices values, and the side length of a square to draw at the correct location:
Maybe like this:
import turtle, tkinter
def draw_square(side):
"""draws a square of side=side starting at the current turtle location
"""
turtle.pendown()
turtle.setheading(0)
for _ in range(4):
turtle.forward(side)
turtle.left(90)
turtle.penup()
def draw_grid(rows, cols):
"""positions the turtle at the correct location,
in order to draw a grid of squares
"""
for jdx in range(rows):
for idx in range(cols):
turtle.penup()
turtle.goto(startx + side*idx, starty + side*jdx)
draw_square(side)
turtle.pencolor('#00807c')
side = 20
startx, starty = 0, 0 # this can be changed,
# other locations used are relative to this starting point
turtle.penup()
turtle.goto(startx, starty)
rows, cols = 4, 8 # this can be changed
draw_grid(rows, cols)
turtle.goto(startx, starty) # return to the starting point
turtle.done()
This is a situation where I would switch from drawing to stamping to simplify the code and speed it up:
from turtle import Screen, Turtle
WIDTH, HEIGHT = 800, 400
SQUARE = 100
CURSOR_SIZE = 20
def cells(t):
x, y = t.position()
for dy in range(HEIGHT // SQUARE):
turtle.goto(x, y + dy * SQUARE)
for dx in range(WIDTH // SQUARE):
turtle.stamp()
turtle.forward(SQUARE)
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
turtle.shape('square')
turtle.shapesize(HEIGHT / CURSOR_SIZE, WIDTH / CURSOR_SIZE)
turtle.color('#00807c', 'white')
turtle.speed('fastest')
turtle.penup()
turtle.stamp() # draw outer perimeter
turtle.shapesize(SQUARE / CURSOR_SIZE)
turtle.goto(SQUARE/2 - WIDTH/2, SQUARE/2 - HEIGHT/2)
cells(turtle) # draw inner squares
screen.exitonclick()
I'd also remove magic numbers from the body of the code and declare them at the start to make them easier to adjust.

Different lines of color in a hexagon

I was just wondering on how to get different colors on each line of the hexagon. Below I have the desired output and input.
Output Right Now - Link to output right now
Output I want - Link to output I want
import turtle as trtl
colors = ["#9c2921", "#cf8e04","#f5d905",]
#--------------------
num_sides = int(input("Enter the number of sides(Enter 6 to get the output of the real image): "))
if num_sides == 6:
print("Desired artwork is displayed")
side_length = 25
circumradius = side_length
angle = 360/len(colors)
trtl.width(10)
for color in colors:
trtl.color(color)
trtl.pensize(10)
for move_turtle in range(1):
trtl.penup()
trtl.sety(-circumradius)
trtl.pendown()
trtl.circle(circumradius, steps = num_sides)
circumradius *= 2
trtl.hideturtle()
Oddly, your program looks like code I wrote in response to your previous question that has neither been accepted nor upvoted. Moving on:
Given this circle() and a fat pen based approach for drawing the hexagons, I believe this is about the best you can do:
import turtle
from itertools import cycle
COLORS = ["#9c2921", "#f5d905", "#cf8e04",]
NUM_SIDES = 6
SIDE_LENGTH = 50
PEN_WIDTH = 25
circumradius = SIDE_LENGTH
turtle.width(PEN_WIDTH)
color = cycle(COLORS)
for _ in range(4):
turtle.penup()
turtle.sety(-circumradius)
turtle.pendown()
for _ in range(NUM_SIDES):
turtle.color(next(color))
turtle.circle(circumradius, extent=360/NUM_SIDES, steps=1)
circumradius += PEN_WIDTH*2
turtle.hideturtle()
turtle.done()
To get closer to the target image, you'd need to draw the individual segments of the hexagon (circle) as trapezoids.
import turtle
from itertools import cycle
COLORS = ["#9c2921", "#f5d905", "#cf8e04",]
NUM_SIDES = 6
SIDE_LENGTH = 50
PEN_WIDTH = 30
circumradius = SIDE_LENGTH
turtle.width(1)
turtle.speed('fastest') # because I have no patience
color = cycle(COLORS)
for _ in range(4):
turtle.penup()
turtle.sety(-circumradius)
turtle.pendown()
for _ in range(NUM_SIDES):
turtle.color(next(color))
turtle.circle(circumradius, extent=360/NUM_SIDES, steps=1)
turtle.right(90)
turtle.begin_fill()
turtle.forward(PEN_WIDTH/2)
turtle.right(120)
turtle.forward(circumradius + PEN_WIDTH/2)
turtle.right(120)
turtle.forward(PEN_WIDTH/2)
turtle.end_fill()
turtle.begin_fill()
turtle.forward(PEN_WIDTH/2)
turtle.right(60)
turtle.forward(circumradius - PEN_WIDTH/2)
turtle.right(60)
turtle.forward(PEN_WIDTH/2)
turtle.end_fill()
turtle.left(90)
circumradius += PEN_WIDTH*2
turtle.hideturtle()
turtle.done()

Recursive rainbow-colored circles around each other

I'm trying to change the color and # of circles shown on the screen. So far, I've figured out how to make all of them different colors in a recursive pattern, but I need help finding out how to add more. Attached is what I have versus what I need to achieve.
my code
import turtle
import colorsys
def draw_circle(x,y,r,color):
turtle.seth(0)
turtle.up()
turtle.goto(x,y-r)
turtle.down()
turtle.fillcolor(color)
turtle.begin_fill()
turtle.circle(r)
turtle.end_fill()
def draw_recursive_circles(x,y,r,color,n):
if n == 0:
return
draw_circle(x,y,r,color)
colors = ['red','orange','yellow','green','blue','purple']
i = 0
for angle in range(30,360,60):
turtle.up()
turtle.goto(x,y)
turtle.seth(angle)
turtle.fd(r*2)
draw_recursive_circles(turtle.xcor(),turtle.ycor(),r/3,colors[i],n-1)
i += 1
turtle.tracer(0)
turtle.hideturtle()
turtle.speed(0)
draw_recursive_circles(0,0,100,'red',5)
turtle.update()
What I need to achieve
What I have so far
You import colorsys but never use it -- this is a clue that you're supposed to generate colors based on angles and not a fixed list of colors. The reason for the import is that turtle's RGB-based colors are the wrong model for our needs, so we want a more appropriate model, like HSV (where we only really care about H/hue), and have it convert those values to RGB.
The number of satellites is determined by your range call:
for angle in range(30,360,60):
Which for this drawing should be more like:
for angle in range(0, 360, 30):
As there are twelve satellites and 360 / 30 is 12. Finally, we need to do proper accounting such that whenever we change a position or heading, in order to do recursive drawing, we need to restore the original values on exit. Below is my simplified example solution to this problem:
from turtle import Screen, Turtle
from colorsys import hsv_to_rgb
def draw_circle(radius):
y = turtle.ycor() # save position & heading
heading = turtle.heading()
turtle.fillcolor(hsv_to_rgb(heading / 360, 1.0, 1.0))
turtle.sety(y - radius)
turtle.setheading(0)
turtle.begin_fill()
turtle.circle(radius)
turtle.end_fill()
turtle.sety(y) # restore position & heading
turtle.setheading(heading)
def draw_recursive_circles(radius, n):
if n == 0:
return
draw_circle(radius)
if n > 1:
heading = turtle.heading() # save heading
for angle in range(0, 360, 30):
turtle.setheading(angle)
turtle.forward(radius * 2)
draw_recursive_circles(radius / 5, n - 1)
turtle.backward(radius * 2)
turtle.setheading(heading) # restore heading
screen = Screen()
screen.tracer(False)
turtle = Turtle(visible=False)
turtle.penup()
draw_recursive_circles(150, 4)
screen.update()
screen.tracer(True)
screen.exitonclick()
I've intentionally kept the pen up to simplifiy my example so only filled portions of the circles are shown. Putting back the surrounding outlines I leave as an exercise for you.
The center circle is not the right color. Fixing this is a simple matter of setting the turtle's heading prior to the initial call to draw_recursive_circles()

How to get python to generate random size?

I need to make my python code draw my stars in different sizes at random. I've tried every method I know but unable to figure it out...still trying to completely understand loops.
import turtle
import random
for n in range(60):
turtle.penup()
turtle.goto(random.randint(-400, 400), random.randint(-400, 400))
turtle.pendown()
red_amount = random.randint( 0, 100) / 100.0
blue_amount = random.randint(0 , 100) / 100.0
green_amount = random.randint( 0, 100) / 100.0
turtle.pencolor((red_amount, green_amount, blue_amount))
turtle.pensize(random.randint(1, 10))
for i in range(6):
turtle.begin_fill()
turtle.forward(50)
turtle.right(144)
turtle.end_fill()
All it does currently is draw stars at the same size.
You've added code to randomize the color, starting position and pen width but there's none to change the size of the stars. The size is controlled by this statement:
turtle.forward(50)
but you don't want to simply replace the 50 with another call to the random module as your stars will come out irregular. You need to compute a random size before the loop and then use that size in the forward() call in the loop:
import turtle
import random
for n in range(60):
turtle.penup()
turtle.goto(random.randint(-400, 400), random.randint(-400, 400))
turtle.pendown()
red_amount = random.random()
blue_amount = random.random()
green_amount = random.random()
turtle.color(red_amount, green_amount, blue_amount)
turtle.pensize(random.randint(1, 10))
size = random.randint(25, 100)
turtle.begin_fill()
for i in range(5):
turtle.forward(size)
turtle.right(144)
turtle.end_fill()
turtle.done()
I've also made a few style changes to your code while I was at it.

Turtle in python, do not want line to print

I am writing this code to print polygons. If the number is two or less, it is supposed to print nothing according to my professor but I cant figure this out. Can somebody tell me what I need to write in my function for 2 or less side numbers to not print anything?
import turtle
import math
turtle.shape("turtle")
turtle.speed(3)
from turtle import *
pensize (5)
pencolor ("purple")
def polygon(num_side, length):
for i in range(num_side):
turtle.forward(length)
turtle.left(360/num_side)
print(polygon(2, 100)) #nothing?
turtle.penup()
turtle.goto(50, 0)
turtle.pendown()
print(polygon(3,100)) #triangle
turtle.penup()
turtle.goto(50, 0)
turtle.pendown()
print(polygon(4, 100)) #square
turtle.penup()
turtle.goto(50, 0)
turtle.pendown()
print(polygon(5, 100)) #pentagon
turtle.penup()
turtle.goto(50, 0)
turtle.pendown()
print(polygon(6, 100)) #hexagon
turtle.penup()
turtle.goto(50, 0)
turtle.pendown()
print(polygon(7, 100)) #heptagon
You need to exit if the number of sides is less than three and only output if n is more than 2. Thus, the if will take care of this.
import turtle
import math
turtle.shape("turtle")
turtle.speed(3)
turtle.pensize (5)
turtle.pencolor ("purple")
def polygon(num_side, length):
if num_side > 2:
for i in range(num_side):
turtle.forward(length)
turtle.left(360/num_side)
turtle.pendown()
for n in range(10):
polygon(n, 100)

Categories