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.
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"))
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
import turtle
#1)draw board
sc= turtle.Screen()
sc.setup(300,300)
import turtle
sc= turtle.Screen()
sc.setup(300,300)
turtle.speed(0)
turtle.pensize(10)
turtle.bgcolor("black")
turtle.pencolor("white")
turtle.penup()
turtle.goto(-150,50)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(-150,-50)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(-50,150)
turtle.setheading(-90)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(50,150)
turtle.pendown()
turtle.forward(300)
turtle.penup()
#2) X's and O's
turtle.pencolor("yellow")
def kreuz(x,y):
turtle.penup()
turtle.goto(x+30,y-45)
turtle.setheading(-45)
turtle.pendown()
turtle.forward(50)
turtle.penup()
turtle.goto(x+60,y-40)
turtle.setheading(-130)
turtle.pendown()
turtle.forward(50)
turtle.penup()
def kreis(x,y):
turtle.penup()
turtle.goto(x+50,y-80)
turtle.setheading(0)
turtle.pendown()
turtle.circle(20)
turtle.penup()
AlleTeile=["","","","","","","","",""]
nächsterZug="X"
def zeichneTeile (AlleTeile):
x = -150
y = 150
for einTeil in AlleTeile:
if einTeil=="X":
kreuz(x,y)
elif einTeil=="O":
kreis(x,y)
else:
print("leeres feld")
x=x+100
if x > 50:
x=-150
y=y-100
zeichneTeile(AlleTeile)
def geklickt(x,y):
global nächsterZug,AlleTeile
senkrecht= (x+150) // 100
waagrecht= (-y+150)// 100
Bereich= senkrecht+waagrecht*3
Bereich=int(Bereich)
print("Du klicktest auf Bereich-Nummer", Bereich)
AlleTeile[Bereich]=nächsterZug
if nächsterZug =="X":
nächsterZug="O"
else:
nächsterZug="X"
zeichneTeile(AlleTeile)
turtle.onscreenclick(geklickt)
turtle.mainloop()
I want to create this tic-tac-toe game using turtle in Python but I am stuck. The problem is that I keep drawing noughts and crosses on the game board after all 9 fields are full with noughts and crosses. How can I code this so that after 9 turns (9 fields) it is no longer possible to keep on drawing?
Since unused squares consist of an empty string in AlleTeile, this is trivial to fix since empty strings are False in a boolean context. At the top of geklickt(), after the global statement:
if all(AlleTeile):
print("All taken!")
return
We can reuse the same trick later in that function to prevent overwritting of existing squares. After the Bereich calculation, we can do something like:
print("Du klicktest auf Bereich-Nummer", Bereich, end="")
if AlleTeile[Bereich]:
print(" -- already taken!")
return
else:
print()
Here's my complete rework with the above changes, some turtle idoms, code cleanup and English translation courtesy of Google (and common sense):
from turtle import Screen, Turtle
# X's and O's
def cross(x, y):
turtle.penup()
turtle.goto(x + 30, y - 35)
turtle.setheading(-45)
turtle.pendown()
turtle.forward(50)
turtle.penup()
turtle.goto(x + 60, y - 30)
turtle.setheading(-130)
turtle.pendown()
turtle.forward(50)
turtle.penup()
def nought(x, y):
turtle.penup()
turtle.goto(x + 50, y - 70)
turtle.setheading(0)
turtle.pendown()
turtle.circle(20)
turtle.penup()
# Playing the game
def drawSquares():
screen.tracer(False) # because this routine is a graphics bottleneck
x, y = -150, 150
for square in AllSquares:
if square == "X":
cross(x, y)
elif square == "O":
nought(x, y)
x += 100
if x > 50:
x = -150
y -= 100
screen.tracer(True)
def clicked(x, y):
global turn
if all(AllSquares):
print("All taken!")
return
vertical = (x + 150) // 100
horizontal = (150 - y) // 100
Area = int(vertical + horizontal * 3)
print("You clicked area number", Area, end="")
if AllSquares[Area]:
print(" -- already taken!")
return
else:
print()
AllSquares[Area] = turn
if turn == "X":
turn = "O"
else:
turn = "X"
drawSquares()
# Draw the board
screen = Screen()
screen.setup(330, 330)
screen.bgcolor("black")
turtle = Turtle(visible=False)
turtle.pensize(10)
turtle.color("white")
turtle.speed('fastest')
turtle.penup()
turtle.goto(-150, 50)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(-150, -50)
turtle.pendown()
turtle.forward(300)
turtle.setheading(-90)
turtle.penup()
turtle.goto(-50, 150)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(50, 150)
turtle.pendown()
turtle.forward(300)
turtle.penup()
# Start the game
turtle.color("yellow")
AllSquares = ["", "", "", "", "", "", "", "", ""]
turn = "X"
drawSquares()
screen.onscreenclick(clicked)
screen.mainloop()
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