How do I teleport the turtle in Python?

Question:

I am making a game in Python 3.6.4.

When the player touches the red circle, the player should teleport back to the start.

Question 1: How do I move the turtle to the start if it hits the red circle?

My code:

import turtle

wn = turtle.Screen()
wn.bgcolor("black")

player = turtle.Turtle()
player.color("white")
player.speed(0)
player.penup()
player.setposition(100, -110)

enemy = turtle.Turtle()
enemy.color("red")
enemy.shape("circle")
enemy.penup()
enemy.setposition(100, -100)

if player.pos(100, -100):
    player.pos(100, -110)

The error I get is:

Traceback (most recent call last):
File "C:UsersruffiAppDataLocalProgramsPythonPython36-32behg.p", line 18, in <module>
if player.pos(100, -100):
TypeError: pos() takes 1 positional argument but 3 were given

Question 2: How do I fix this error?

Asked By: The Weird

||

Answers:

It seems that you are not familiar with the founction pos().This kind of arguments are not satisfied requirement.I suggest you read the helping document agau

Answered By: David

Turtle.pos() returns the turtle’s position; it doesn’t test being present at a given position (as attempted by your if line) or set it (as attempted by your last line). The former job is performed by Vec2D comparison, while the latter is Turtle.goto().

Separately, your code contains no provision for user input, so there is no way to “react” to the player arriving anywhere; perhaps you already know that.

Answered By: Davis Herring

In your if statement, you wrote player.pos(100, -100). Turtle.pos()returns the Turtle’s position. To check if this position is equal to (100, -100), simple do: if player.pos()==(100,-100). To make change the player’s position, use Turtle.goto(x,y). The following code should be what you are trying to do:

if player.pos()==(100,-100):
    player.goto(100, -110)

Hope this helps!

Answered By: Alexander Zhang

The approach I would take to solve both issues is don’t do the position comparisons yourself, use the distance() method that turtle provides:

if player.distance(enemy) < 10:  # if the centers are within 10 pixels

Here’s a complete example that lets you click on the screen to move the player around but if the player gets too close to the heart of the enemy, it will teleport back to the start where you can once again move by clicking on the screen:

from turtle import Turtle, Screen

wn = Screen()
wn.setup(500, 500)
wn.bgcolor('black')

player = Turtle(visible=False)
player.speed('fastest')
player.shapesize(2)
player.color('white')
player.penup()
player.setposition(200, -200)
player.showturtle()

enemy = Turtle('circle', visible=False)
enemy.shapesize(2)
enemy.color('red')
enemy.penup()
enemy.setposition(-200, 200)
enemy.showturtle()

def click_handler(x, y):
    wn.onclick(None)  # disable handler while in handler
    player.setheading(player.towards(x, y))  # head towards new location
    player.setposition(x, y)

    if player.distance(enemy) < 10:
        player.hideturtle()  # teleport
        player.setposition(200, -200)
        player.showturtle()

    wn.onclick(click_handler)

wn.onclick(click_handler)

wn.mainloop()
Answered By: cdlane

idk I’m looking at this for info on how to make a heart shaped object to use for my wiener dog eating contest convention for next week instructions unclear.

Answered By: user21235621