Day 44
Drawing dotted lines using the turtle module:
from turtle import Turtle, Screen
maddy = Turtle()
maddy.shape("turtle")
maddy.color("green")
maddy.pencolor("black")
for i in range (50):
maddy.forward(3.5)
maddy.penup()
maddy.forward(3.5)
maddy.pendown()
screen = Screen()
screen.exitonclick()
Drawing shapes:
from turtle import Turtle, Screen
import random
colors = ["red", "green", "blue", "yellow", "cyan", "magenta", "orange", "violet", "brown", "black"]
maddy = Turtle()
maddy.shape("turtle")
maddy.color("green")
draw = True
sides = 2
while draw:
maddy.pencolor(random.choice(colors))
if sides <= 10:
sides += 1
for i in range(sides):
maddy.forward(70)
maddy.right(360 / sides)
else:
draw = False
screen = Screen()
screen.exitonclick()
Random walk:
I tried this code:
from turtle import Turtle, Screen
import random
colors = ["red", "green", "blue", "yellow", "cyan", "magenta", "orange", "violet", "brown", "black"]
walk_sequence = ["forward", "backward", "left", "right"]
walk = random.choice(walk_sequence)
maddy = Turtle()
maddy.shape("turtle")
maddy.color("green")
maddy.pensize(7)
for i in range(50):
maddy.color(random.choice(colors))
maddy.walk(10)
It gave this error:
maddy.walk(10)
^^^^^^^^^^
AttributeError: 'Turtle' object has no attribute 'walk'
This worked:
from turtle import Turtle, Screen
import random
colors = ["red", "green", "blue", "yellow", "cyan", "magenta", "orange", "violet", "brown", "black"]
possible_direction = [0, 90, 180, 270]
maddy = Turtle()
maddy.hideturtle()
maddy.pensize(7)
maddy.speed(0)
for i in range(140):
maddy.color(random.choice(colors))
maddy.setheading(random.choice(possible_direction))
maddy.forward(14)
screen = Screen()
screen.exitonclick()
Drawing a spirograph:
from turtle import Turtle, Screen, colormode
import random
maddy = Turtle()
colormode(255)
maddy.speed(0)
def random_color():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
return (r, g, b)
for i in range (36):
maddy.color(random_color())
maddy.circle(140)
maddy.left(10)
screen = Screen()
screen.exitonclick()
Comments
Post a Comment