Answers for "python turtle set position"

4

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()
Posted by: Guest on February-18-2020
2

get position of turtle python

import turtle
t = turtle.Turtle()

turtles_position = t.pos() # Will return a tuple that includes both X and Y
# coordinates of the turtle named t respectively, E.G (200,600).

turtles_positionX = t.xcor() # Will return a float value of the turtle's
# X position.

turtles_positionY = t.ycor() # Will return a float value of the turtle's
# Y position.

###############################################################
# Make sure to round the last 2 method's numbers as without   #
# rounding you can get numbers like 299.99999999999994.       #
# (You can round with the round(integer) function)			  #
###############################################################
Posted by: Guest on September-13-2021

Python Answers by Framework

Browse Popular Code Answers by Language