python turtle turn code example

Example 1: how to make a screen in turtle

def turtle_triangle():
    window = turtle.Screen()
    window.bgcolor("red")
    
    brad = turtle.Turtle()
    
    brad.shape("turtle")
    brad.color("yellow")
    brad.speed(1)

    for _ in range(3):
        brad.right(60)
        brad.forward(200)
        brad.right(60)

    window.exitonclick()

Example 2: python turtle movement

import turtle

wn = turtle.Screen()
wn.setup(width=700,height=400)
wn.title("Python Turtle Movement")

def playerUp():
  player.sety(player.ycor()+10)
def playerDown():
  player.sety(player.ycor()-10)
def playerRight():
  player.setx(player.xcor()+10)
def playerLeft():
  player.setx(player.xcor()-10)

player = turtle.Turtle()
player.speed(0) #this will make your player created instantly
player.shape("square") #set player shape
player.color("red") #set player color
player.penup() #prevent drawing lines
player.goto(0,0) #set player location

wn.onkeypress(playerUp, "w") #function, key
wn.onkeypress(playerDown, "s")
wn.onkeypress(playerRight, "d")
wn.onkeypress(playerLeft, "a")

#update the window
while True:
  wn.update()

Example 3: how to import turtle in python

import turtle # imports it
whateverYouWantToCallIt = turtle.Turtle() # adds it to the project
#code
whateverYouWantToCallIt.forward(10) # moves whateverYouWantToCallIt forward
whateverYouWantToCallIt.color("purple") # color
whateverYouWantToCallIt.left(90) # turns him 90 degrees
whateverYouWantToCallIt.right(90) # turns him 90 degrees the other direction

Example 4: how to hide turtle in python

>>> turtle.hideturtle()

Example 5: turtle write

turtle.write(arg, move=False, align="left", font=("Arial", 8, "normal"))

Example 6: turtle with python

import turtle
#creating a square with turtle
t = turtle.Turtle()
t.forward(100)
t.color('blue')
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)