I am new to turtle and I am trying to draw two hearts with an arrow. But after I finished two hearts, it does not draw anymore. I already have the "pendown" and I don't know why it does not draw.
I have my codes here. Thank you for your help. ♡♡
# draw a big heart
turtle.pencolor('pink')
turtle.fillcolor('pink')
turtle.begin_fill()
turtle.left(90)
turtle.circle(100, 200)
turtle.left(20)
turtle.forward(285)
turtle.left(100)
turtle.forward(285)
turtle.left(20)
turtle.circle(100, 200)
turtle.end_fill()
turtle.penup()
turtle.left(90)
turtle.forward(150)
turtle.pendown
#draw a small heart
turtle.pencolor('red')
turtle.fillcolor('red')
turtle.begin_fill()
turtle.left(90)
turtle.circle(80, 200)
turtle.left(20)
turtle.forward(228)
turtle.left(100)
turtle.forward(228)
turtle.left(20)
turtle.circle(80, 200)
turtle.end_fill()
#draw an arrow
turtle.pencolor('black')
turtle.pensize(5)
turtle.penup()
turtle.right(80)
turtle.forward(400)
turtle.right(180)
turtle.pendown
turtle.forward(600)
I don't know what turtle is but you write turtle.penup() but turtle.pendown. So this may be the problem
Related
I a currently trying to animate this bicycle in PYTHON TURTLE. I am struggling if anyone can teach me how to animate it with code, I will be grateful for that. I am adding more details as I am only trying to make the bicycle wheels move from one place to another like the bicycle wheels animation and the bicycle moves while the wheels are also moving thank you a lot!
import turtle
turtle.delay(10)
turtle.pensize(20)
# Move the turtle to the right position of the left tyre
turtle.penup()
turtle.setposition(-87.5, -50)
turtle.setheading(90)
# Draw the left tyre
turtle.begin_fill()
turtle.circle(62.5)
turtle.end_fill()
# Move the turtle to the right position of the right tyre
turtle.penup()
turtle.setposition(212.5, -50)
turtle.setheading(90)
# Draw the right tyre
turtle.begin_fill()
turtle.circle(62.5)
turtle.end_fill()
# Draw the handle
turtle.penup()
turtle.setposition(137.5, 0)
turtle.pendown()
turtle.setposition(75, 150)
# Draw the straight part of the handle
turtle.setheading(0)
turtle.setposition(125, 150)
# Draw the circular part of the handle
turtle.penup()
turtle.setposition(125, 100)
turtle.pendown()
turtle.circle(25, 180)
# Draw the seat tube
turtle.penup()
turtle.setposition(12.5, -50)
turtle.pendown()
turtle.setposition(-62.5, 137.5)
# Draw the saddle
turtle.setheading(180)
turtle.setposition(-100, 137.5)
# Draw the top tube
turtle.penup()
turtle.setposition(100, 75)
turtle.pendown()
turtle.setposition(-37.5, 75)
# Draw the down tube
turtle.penup()
turtle.setposition(100, 75)
turtle.pendown()
turtle.setposition(12.5, -50)
# Draw the seat stay
turtle.penup()
turtle.setposition(-37.5, 75)
turtle.pendown()
turtle.setposition(-150, -50)
# Draw the chain stay
turtle.penup()
turtle.setposition(12.5, -50)
turtle.pendown()
turtle.setposition(-150, -50)
Here is the code I came with for the animation of two wheels and some kind of handlebar :
import turtle
import time
def moving_wheel(turtle):
turtle.fillcolor('orange')
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
def moving_handle(turtle):
pos = turtle.pos()
turtle.left(90)
turtle.forward(70)
turtle.left(120)
turtle.forward(110)
turtle.penup()
turtle.goto(pos)
turtle.right(210)
turtle.pendown()
if __name__ == "__main__":
screen = turtle.Screen()
screen.setup(600, 600)
screen.bgcolor('green')
screen.tracer(0)
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t3 = turtle.Turtle()
# set a turtle object color
t1.color('red')
t2.color('red')
t3.color('red')
# set turtle object speed
t1.speed(0)
t2.speed(0)
t3.speed(0)
t1.width(2)
t2.width(2)
t3.width(1.5)
t1.hideturtle()
t2.hideturtle()
t3.hideturtle()
# turtle object in air
t1.penup()
t2.penup()
t3.penup()
# set initial position
t1.goto(-250, 0)
t2.goto(-150,0)
t3.goto(-150,20)
t3.pendown()
# move turtle object to surface
t1.pendown()
t2.pendown()
# infinite loop
while True:
# clear turtle work
t1.clear()
t2.clear()
t3.clear()
# call function to draw ball
moving_wheel(t1)
moving_wheel(t2)
moving_handle(t3)
# update screen
screen.update()
# forward motion by turtle object
t1.forward(0.1)
t2.forward(0.1)
t3.forward(0.1)
You can modify the code for the handle part, I'm not an artist haha
I am learning Python turtle. I copied this code from internet but I am getting this error NoneType object has no attribute.
I think error is in 2nd last line in turtle.write() function.
This is code:
import turtle
import random
# sets background
bg = turtle.Screen()
bg.bgcolor("black")
# Bottom Line 1
turtle.penup()
turtle.goto(-170,-180)
turtle.color("white")
turtle.pendown()
turtle.forward(350)
# Mid Line 2
turtle.penup()
turtle.goto(-160,-150)
turtle.color("white")
turtle.pendown()
turtle.forward(300)
# First Line 3
turtle.penup()
turtle.goto(-150,-120)
turtle.color("white")
turtle.pendown()
turtle.forward(250)
# Cake
turtle.penup()
turtle.goto(-100,-100)
turtle.color("white")
turtle.begin_fill()
turtle.pendown()
turtle.forward(140)
turtle.left(90)
turtle.forward(95)
turtle.left(90)
turtle.forward(140)
turtle.left(90)
turtle.forward(95)
turtle.end_fill()
# Candles
turtle.penup()
turtle.goto(-90,0)
turtle.color("red")
turtle.left(180)
turtle.pendown()
turtle.forward(20)
turtle.penup()
turtle.goto(-60,0)
turtle.color("blue")
turtle.pendown()
turtle.forward(20)
turtle.penup()
turtle.goto(-30,0)
turtle.color("yellow")
turtle.pendown()
turtle.forward(20)
turtle.penup()
turtle.goto(0,0)
turtle.color("green")
turtle.pendown()
turtle.forward(20)
turtle.penup()
turtle.goto(30,0)
turtle.color("purple")
turtle.pendown()
turtle.forward(20)
# Decoration
colors = ["red", "orange", "yellow", "green", "blue", "purple", "black"]
turtle.penup()
turtle.goto(-40,-50)
turtle.pendown()
for each_color in colors:
angle = 360 / len(colors)
turtle.color(each_color)
turtle.circle(10)
turtle.right(angle)
turtle.forward(10)
# Happy Birthday message
turtle.penup()
turtle.goto(-150, 50)
turtle.color("pink")
turtle.pendown()
turtle.write("Happy Birthday PUNNU!", None, None, "24pt bold")
turtle.color("black")
Here is the documentation for turtle.write().
You are supplying NoneType objects incorrectly. To set the font and leave the move and align parameters as their default values, change your second to last line to turtle.write("Happy Birthday PUNNU!", font=("Arial", 24, "bold"))
from turtle import *
o=Turtle()
d=Screen()
o.begin_fill()
o.width("2")
o.color("yellow")
extent1=150**2*3.14
print(extent1)
o.circle(radius=150,extent=extent1,steps=10)
o.end_fill()
#jasonharper's comment is spot-on (+1). To make the filled star appear the same on both architectures, draw (then fill) just the perimeter of the star you want:
from turtle import Screen, Turtle
screen = Screen()
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
turtle.color("yellow")
turtle.begin_fill()
for _ in range(11):
turtle.forward(64)
turtle.right(98.2)
turtle.forward(64)
turtle.left(130.91)
turtle.end_fill()
turtle.hideturtle()
screen.exitonclick()
I'm new to this but wanted to try Python with turtle. But I have a problem with my code when I tell the cursor to move to underneath the letter H. It just throws cursor at the start where the H is. I wanna write:
HAPPY
BDAY
in that order. Here is the code i have so far.
import turtle
frame = turtle.Screen().bgcolor("Red")
hi = turtle.Turtle()
hi.color("Black")
hi.width (3)
hi.speed (3)
# H
hi.left(90)
hi.forward(70)
hi.penup()
hi.goto(0, 35)
hi.pendown()
hi.right(90)
hi.forward(30)
hi.penup()
hi.goto(30, 70)
hi.pendown()
hi.right(90)
hi.forward(70)
# A
hi.penup()
hi.goto(40, 0)
hi.pendown()
hi.right(200)
hi.forward(75)
hi.right(140)
hi.forward(75)
hi.penup()
hi.goto(50, 35)
hi.pendown()
hi.right(-70)
hi.forward(31)
# P
hi.penup()
hi.goto(105, 70)
hi.pendown()
hi.right(90)
hi.forward(70)
hi.penup()
hi.goto(105,35)
hi.pendown()
hi.right(275)
hi.circle(18, 200)
# PP
hi.penup()
hi.goto(140, 70)
hi.pendown()
hi.right(285)
hi.forward(70)
hi.penup()
hi.goto(140,35)
hi.pendown()
hi.right(275)
hi.circle(18, 200)
# Y
hi.penup()
hi.goto(170, 70)
hi.pendown()
hi.right(255)
hi.forward(40)
hi.right(30)
hi.forward(35)
hi.penup()
hi.goto(190, 35)
hi.pendown()
hi.right(215)
hi.forward(40)
#B
hi.penup()
hi.goto(200, 70)
hi.pendown()
hi.forward(70)
turtle.mainloop()
https://imgur.com/QFx5nub the white dots are where I want the word Bday to start but the turtle is in the position where the arrow is.
You can only eyeball and guess so far. For text like this, it helps to have a fixed size box that each letter is going to fit into, and a constant location where the cursor is going to be in that box when letter drawing begins. It also helps to use relative positioning, like forward() rather than absolute positioning like goto(), when trying to debug your letters:
from turtle import Screen, Turtle
screen = Screen()
screen.bgcolor('red')
turtle = Turtle()
turtle.width(3)
# letters are in a box 30 wide by 70 tall with 10px between
# the pen starts at the lower left of each letter box
# H
turtle.left(90)
turtle.forward(70)
turtle.backward(35)
turtle.right(90)
turtle.forward(30)
turtle.left(90)
turtle.forward(35)
turtle.backward(70)
turtle.penup()
turtle.right(90)
turtle.forward(10)
turtle.pendown()
# A
turtle.left(78) # 77.9 degrees is rise 70 over run 15
turtle.forward(74)
turtle.right(156)
turtle.forward(37)
turtle.right(102)
turtle.forward(16)
turtle.backward(16)
turtle.left(102)
turtle.forward(37)
turtle.penup()
turtle.left(78)
turtle.forward(10)
turtle.pendown()
# P
turtle.left(90)
turtle.forward(70)
turtle.backward(35)
turtle.right(90)
turtle.forward(17.5)
turtle.circle(17.5, 180)
turtle.forward(17.5)
turtle.penup()
turtle.left(90)
turtle.forward(70)
turtle.left(90)
turtle.forward(40)
turtle.pendown()
# PP
turtle.left(90)
turtle.forward(70)
turtle.backward(35)
turtle.right(90)
turtle.forward(17.5)
turtle.circle(17.5, 180)
turtle.forward(17.5)
turtle.penup()
turtle.left(90)
turtle.forward(70)
turtle.left(90)
turtle.forward(40)
turtle.pendown()
# Y
turtle.penup()
turtle.forward(15)
turtle.pendown()
turtle.left(90)
turtle.forward(35)
turtle.left(23) # 90 - 67 as 66.8 degrees is rise 35 over run 15
turtle.forward(38)
turtle.backward(38)
turtle.right(46)
turtle.forward(38)
turtle.backward(38)
turtle.left(23)
turtle.backward(35)
turtle.penup()
turtle.right(90)
turtle.goto(0, -75) # 5px between lines of text
turtle.pendown()
# B
turtle.forward(17.5)
turtle.circle(17.5, 180)
turtle.forward(17.5)
turtle.right(180)
turtle.forward(17.5)
turtle.circle(17.5, 180)
turtle.forward(17.5)
turtle.left(90)
turtle.forward(70)
turtle.penup()
turtle.left(90)
turtle.forward(40)
turtle.pendown()
screen.mainloop()
Now you're in position to draw "D" in a 70x30 box with the cursor in the lower left corner pointing right.
This code draws a few letters using turtle graphics:
import turtle
turtle.speed(1)
myWin = turtle.Screen()
turtle.left(90)
turtle.forward(260)
turtle.left(55)
turtle.forward(-60)
turtle.right(55)
turtle.forward(-60)
turtle.right(55)
turtle.forward(-60)
turtle.left(100)
turtle.forward(-60)
turtle.right(45)
turtle.forward(-60)
turtle.right(55)
turtle.forward(-50)
turtle.right(35)
turtle.forward(100)
turtle.left(90)
turtle.forward(260)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(50)
turtle.forward(-200)
turtle.right(130)
turtle.forward(50)
turtle.left(45)
turtle.forward(260)
myWin.exitonclick()
The problem is it starts drawing in the middle of the screen and I want it to start at the far left side. Is there any way to change the initial position
Note that the turtle interface is a coordinate grid.
so you can use the goto(x, y) function
turtle.penup()
turtle.goto(300,200)
turtle.pendown()
Use turtle.penup to pick up the pen and move to the position you want, and then do turtle.pendown