I am drawing the Indian flag using turtle and python. I have got the rectangles and colours down so far, but am struggling to make the chakra in the middle.
It has 24 spokes, and is surrounded by a closed circle. Any tips on how to accomplish this?
This is my code now:
import turtle
def drawRectangle (t, w, h, c):
t.fillcolor(c)
t.begin_fill()
for i in range(2):
t.forward(h)
t.left(90)
t.forward(w)
t.left(90)
t.end_fill()
def main ():
wn = turtle.Screen()
chloe = turtle.Turtle()
drawRectangle(chloe,50,200, "chartreuse3")
chloe.up()
chloe.goto(0,-100)
chloe.down()
drawRectangle(chloe,50,200, "orange1")
chloe.up()
chloe.goto(100,-25)
chloe.down()
chloe.pencolor("blue4")
for i in range(24):
chloe.forward(20)
chloe.backward(20)
chloe.left(15)
chloe.up()
chloe.goto(300,300)
main()
You can use the circle to draw the circle:
chloe.pencolor("blue4")
# draw the spokes
for i in range(24):
chloe.forward(20)
chloe.backward(20)
chloe.left(15)
# raise pen
chloe.up()
# head down
chloe.setheading(270)
# go forward 20
chloe.forward(20)
# reset heading
chloe.setheading(0)
# pen down
chloe.down()
# draw the circle
chloe.circle(20)
import turtle
def drawRectangle (t, l, b, c):
t.fillcolor(c)
t.begin_fill()
for i in range(2):
t.forward(b)
t.left(90)
t.forward(l)
t.left(90)
t.end_fill()
def main ():
wn = turtle.Screen()
india = turtle.Turtle()
drawRectangle(india,50,200, "orange1")
india.up()
india.goto(0,-100)
india.down()
drawRectangle(india,50,200, "green")
india.up()
india.goto(100,-20)
india.down()
india.pencolor("blue3")
for i in range(24):
india.forward(20)
india.backward(20)
india.left(15)
india.up()
india.goto(400,400)
main()
Related
I am studying Python Turtle. I know that the color mode should be 255
in order to use RGB mode. I also know that the correct format is
turtle.dot(int,""). But I get the error:
TurtleGraphicsError: bad color arguments: None
How can I implement RGB?
from turtle import Turtle, Screen
import turtle as t
import random
tim = t.Turtle()
t.colormode(255)
# t.colormode(255)
def rndmclr():
r=random.randrange(255)
g=random.randrange(255)
b=random.randrange(255)
t.pencolor((r,g,b))
t.speed("fastest")
def tendots():
for i in range(10):
t.dot(10,rndmclr())
t.penup()
t.fd(30)
t.pendown()
t.fd(0)
for i in range(10):
tendots()
t.left(90)
t.penup()
t.fd(10)
t.pendown()
t.left(90)
tendots()
t.right(90)
t.penup()
t.fd(10)
t.pendown()
t.right(90)
screen = Screen()
screen.exitonclick()
Your primary flaw is here:
def rndmclr():
r=random.randrange(255)
g=random.randrange(255)
b=random.randrange(255)
t.pencolor((r,g,b))
# ...
t.dot(10,rndmclr())
The rndmclr() sets the random color when it should return the color as it's being used as an argument to dot() where it is currently returning None as the color to use. There are other flaws in your program, most significanly the double import of turtle that has you using both the object-oriented API and the functional API which only leads to trouble -- pick one and stick with it:
from turtle import Turtle, Screen
from random import randrange
def rndmclr():
r = randrange(255)
g = randrange(255)
b = randrange(255)
return (r, g, b)
def tendots():
for _ in range(10):
turtle.dot(10, rndmclr())
turtle.penup()
turtle.forward(30)
turtle.pendown()
screen = Screen()
screen.colormode(255)
turtle = Turtle()
turtle.speed('fastest')
for _ in range(10):
tendots()
turtle.left(90)
turtle.penup()
turtle.forward(10)
turtle.pendown()
turtle.left(90)
tendots()
turtle.right(90)
turtle.penup()
turtle.forward(10)
turtle.pendown()
turtle.right(90)
screen.exitonclick()
The fillcolor() is not working at all in this function -- I can not figure out why. It worked in all of my other functions:
from turtle import Turtle
from random import randint
t = Turtle()
def rocks():
for i in range(5):
t.penup()
t.goto(randint(-300,0), randint(-200,0))
for x in range(40):
t.pendown()
t.fillcolor("gray")
t.fillcolor()
t.begin_fill()
t.forward(5)
t.left(25)
t.right(27)
t.forward(5)
t.right(20)
t.end_fill()
t.speed("fastest")
rocks()
The problem is that you have the begin_fill() & end_fill() inside your loop which means they are trying to fill short line segments. You need them around your loop to fill the entire shape:
from turtle import Turtle, Screen
from random import randint
def rocks(t):
t.fillcolor('gray')
for _ in range(5):
t.penup()
t.goto(randint(-300, 0), randint(-200, 0))
t.begin_fill()
for _ in range(15):
t.pendown()
t.forward(5)
t.right(2)
t.forward(5)
t.right(22)
t.end_fill()
screen = Screen()
turtle = Turtle()
turtle.speed('fastest')
rocks(turtle)
turtle.hideturtle()
screen.exitonclick()
I am trying to move the square to right and draw 36 squares to make a circle out of it:
def draw_art(x,y):
print("Started the op")
window = turtle.Screen()
window.bgcolor("blue")
print("start the drwaing")
c =0
brad = turtle.Turtle()
brad.shape("turtle")
brad.color("green")
brad.speed(3)
print("enter loop")
for i in range(1,37):
draw_square(x,y)
brad.right(10)
window.exitonclick()
draw_art(200,90)
This might get you closer to what you're trying to do. It defines the missing draw_square() function and then draws 36 squares in a circle:
from turtle import Screen, Turtle
def draw_square(turtle):
for _ in range(4):
turtle.forward(50)
turtle.right(90)
def draw_art(turtle, x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for _ in range(36):
draw_square(turtle)
turtle.right(10)
print("Started the app")
window = Screen()
window.bgcolor('blue')
brad = Turtle()
brad.shape('turtle')
brad.color('green')
brad.speed('fastest') # because I have no patience
print("Start the drawing")
draw_art(brad, 200, 90)
brad.hideturtle()
window.exitonclick()
I can't figure out how to calculate the coordinates for the dots inside the circle. Right now my program is supposed to calculate the dots inside the circle within the square and divide that by the total amount of dots.
import turtle
import random
t=turtle.Turtle()
insideCircleCount = 0
def square():
for y in range(4):
t.forward(200)
t.left(90)
def circle():
t.penup()
t.setpos(100,0)
t.pendown()
t.circle(100)
def randomDot():
z=int(input("iterations:"))
for y in range(z):
t.penup()
t.pensize(1)
x=random.randint(0,200)
y=random.randint(0,200)
t.goto(x,y)
t.pendown()
t.dot()
insideCircle(x,y)
def insideCircle(x,y):
if ((x*x + y*y)<100):
insideCircleCount+=1
#main
square()
circle()
randomDot()
print('Dots inside circle account for ', insideCircleCount)
import turtle
import random
t=turtle.Turtle()
insideCircleCount = 0
def square():
for y in range(4):
t.forward(200)
t.left(90)
def circle():
t.penup()
t.setpos(100,0)
t.pendown()
t.circle(100)
def randomDot():
z=int(input("iterations:"))
for y in range(z):
t.penup()
t.pensize(1)
x=random.randint(0,200)
y=random.randint(0,200)
t.goto(x,y)
t.pendown()
t.dot()
insideCircle(x,y)
def insideCircle(x,y):
# Here the circle has a offset with center at (100,100)
#(x – h)^2 + (y – k)^2 = r2
if (((x-100)**2 + (y-100)**2) < 100**2):
# you were trying to access a local variable before assignment
#this fixes that
global insideCircleCount
insideCircleCount+=1
#main
square()
circle()
randomDot()
print('Dots inside circle account for ', insideCircleCount)
turtle.mainloop() #stops the window from closing
I am new to Python and I am struggling with a lesson in our book where we have to change the colors in a bar graph. I am not sure what I am doing wrong. There is no error message the colors are just printing black.
import turtle
tess = turtle.Turtle()
def draw_bar(t, height):
t.begin_fill()
t.left(90)
t.forward(height)
t.write(" "+ str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.penup()
t.left(90)
t.end_fill()
t.forward(10)
t.pendown()
tess.pensize(3)
if xs is 48:
tess.color("blue")
if xs is 117:
tess.color("yellow")
wn = turtle.Screen()
wn.bgcolor("lightgreen")
xs = [48,117,200,240,160,260,220]
for a in xs:
draw_bar(tess, a)
wn.mainloop()
This is what I have so far.
Thanks for the help!
I think you need to have a color assigned before you begin_fill(). Because the computer is like: "Well, I'll fill the box, but I don't have a color", so it uses the default of black.
The if statement you provided doesn't make sense, so I moved it up in the function and modified it where you can examine the height within the function. Instead of using if/else, I'd recommend dictionaries.
This new code works:
import turtle
tess = turtle.Turtle()
def draw_bar(t, height):
# Select Color
if height is 48:
color = "blue"
elif height is 117:
color = "yellow"
else:
color = "black"
# Assign color
t.color(color)
# The rest of your code
t.begin_fill()
t.left(90)
t.forward(height)
t.write(" "+ str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.penup()
t.left(90)
t.end_fill()
t.forward(10)
t.pendown()
tess.pensize(3)
wn = turtle.Screen()
wn.bgcolor("lightgreen")
xs = [48,117,200,240,160,260,220]
for a in xs:
draw_bar(tess, a)
wn.mainloop()