How do I get turtle to move this square right 36 times? - python

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

Related

Cannot change dot color despite turtle's colormode()

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

How do I make this a 6 sized snowflake instead of 8?

Not originally my code:
import turtle
wn = turtle.Screen()
wn.bgcolor('Black')
wn.setup( width = 250, height = 250)
turtle = turtle.Turtle()
def snowflake (size, pensize, x, y):
""" function that draws a snowflake """
turtle.speed(100)
turtle.penup()
turtle.goto(x, y)
turtle.forward(10*size)
turtle.left(45)
turtle.pendown()
turtle.color('white')
for x in range (8):
branch(size)
turtle.left(45)
def branch (size):
for z in range (3):
for z in range (3):
turtle.forward(10.0*size/3)
turtle.backward(10.0*size/3)
turtle.right(45)
turtle.left(90)
turtle.backward(10.0*size/3)
turtle.left(45)
turtle.right(90)
turtle.forward(10.0*size)
snowflake(8, 6, 0, 0)
wn.exitonclick()
If it's not originally your code, your should provide proper blame, I mean credit for it. The issue with this code is that the base angle 45 is being used to generate the 8-sided snowflake as well as the angle of the small branches. So when we go to use 60 for a 6-sided snowflake, it's hard to know which 45's (or 90's) to replace and which to keep. The author of the original code didn't help any by starting the branch logic in the snowflake() function, before calling the branch() function. So let's tease out the two different uses of 45 degrees, make the code more explicit, and switch just the sides to 60 degrees:
from turtle import Screen, Turtle
SIDES = 6
BRANCH_ANGLE = 45
def snowflake(size, x, y):
""" function that draws a snowflake """
turtle.penup()
turtle.goto(x, y)
turtle.forward(size)
turtle.left(BRANCH_ANGLE)
turtle.pendown()
for _ in range(SIDES):
branch(size)
turtle.left(BRANCH_ANGLE)
def branch(size):
for _ in range(3):
for _ in range(3):
turtle.forward(size / 3)
turtle.backward(size / 3)
turtle.right(BRANCH_ANGLE)
turtle.left(2 * BRANCH_ANGLE)
turtle.backward(size / 3)
turtle.left(BRANCH_ANGLE)
turtle.right(BRANCH_ANGLE + 360 / SIDES)
turtle.forward(size)
screen = Screen()
screen.bgcolor('black')
screen.setup(width=250, height=250)
turtle = Turtle()
turtle.color('white')
turtle.speed('fastest')
snowflake(80, 0, 0)
turtle.hideturtle()
screen.exitonclick()

How to draw a square inside a square?

pink square is the taskI am trying to draw a square inside a square using turtle graphics. I couldn't draw exactly like the original one.
Here is my code
import turtle
win = turtle.Screen()
tess = turtle.Turtle()
for two in range(12):
for _ in range(4):
tess.forward(20)
tess.left(90)
tess.penup()
tess.goto(-7,-4)
tess.pendown()
for _ in range(4):
tess.forward(35)
tess.left(90)
tess.penup()
tess.goto(-14,-8)
tess.pendown()
for _ in range(4):
tess.forward(50)
tess.left(90)
tess.penup()
tess.goto(-21,-16)
tess.pendown()
for _ in range(4):
tess.forward(70)
tess.left(90)
A good example for better living through stamping instead of drawing:
from turtle import Screen, Turtle
CURSOR_SIZE = 20
screen = Screen()
screen.bgcolor('lightgreen')
turtle = Turtle()
turtle.shape('square')
turtle.color('pink', 'lightgreen')
for size in range(100, 0, -20):
turtle.shapesize(size / CURSOR_SIZE, outline=3)
turtle.stamp()
screen.exitonclick()
I think, my code solution your problem.
from turtle import *
def draw_square(a,color,x,y):
penup()
goto(x,y)
setheading(90)
backward(a//2)
setheading(0)
backward(a//2)
pendown()
pencolor(color)
for _ in range(4):
forward(a)
left(90)
draw_square(20,"pink",0,0)
draw_square(50,"pink",0,0)
draw_square(70,"pink",0,0)
draw_square(90,"pink",0,0)
draw_square(110,"pink",0,0)

Multiple Turtles, multiple random movements

I have an assignment where I have to make a game in python with turtles.
I am able to generate a grid like I want to, and I am able to generate multiple different turtles. my
- I have no clue where to start in terms of figuring out how to have each of my three different turtles each move throughout the board randomly, while staying on the grid line.
- the "game" must stop after two of the turtles have hit the edge of the screen, I do not know how to do this, either.
here is what I have so far:
import turtle
import random
turtle.pensize(2)
turtle.penup()
turtle.goto(-160,-160)
turtle.pendown()
turtle.color("green")
turtle.speed(0)
for i in range(4):
turtle.forward(320)
turtle.left(90)
for y in range(-160,160,80):
for x in range(-160,160,80):
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
for k in range(4):
turtle.forward(40)
turtle.left(90)
turtle.end_fill()
for y in range(-120,160,80):
for x in range(-120,160,80):
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
for k in range(4):
turtle.forward(40)
turtle.left(90)
billy = turtle.Turtle()
billy.shape("turtle")
billy.color("red")
billy.penup()
billy.left(90)
rick = turtle.Turtle()
rick.shape("turtle")
rick.color("orange")
rick.penup()
mike = turtle.Turtle()
mike.shape("turtle")
mike.color("purple")
mike.penup()
turtle.done()
strong guidance would be VERY helpful.
You could take advantage of object orientation. Here is my version of your turtle script:
import turtle
import random
# I added randint.
from random import randint
# Let's define a class so we can create turtles
class MyTurtle():
# Here's the constructor. Where we give some initial values.
def __init__(self, name, color):
self.name = name
self.color = color
self.turtle = turtle.Turtle()
self.turtle.shape("turtle")
self.turtle.color(self.color)
self.turtle.speed(1)
self.turtle.pd()
# We need to know if this turtle is off the board.
def off_board(self):
x = self.turtle.xcor()
y = self.turtle.ycor()
return x < -160 or 160 < x or y < -160 or 160 < y
# calling this will move the turtle in a random direction.
def move(self):
turn = randint(0,2)
if turn == 1:
self.turtle.lt(45)
elif turn == 2:
self.turtle.rt(45)
self.turtle.forward(5)
# Put your code for drawing the grid
# Let's create some turtles and start moving them.
turtles = []
turtles.append( MyTurtle('billy', 'red'))
turtles.append( MyTurtle('chris', 'blue'))
turtles.append( MyTurtle('lilly', 'pink'))
turtles.append( MyTurtle('kevin', 'green'))
ok = True
while ok:
for t in turtles:
t.move()
if t.off_board():
print ("Turtle %s wins!!!" % (t.name))
ok = False
turtle.done()
Come on! Just one upvote for this awesome turtle script?

Drawing a indian flag chakra in python turtlee

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

Categories

Resources