Okamiram
Okamiram

Reputation: 103

Turtle Object does not move to new position goto(x, y)

Good morning,

I have created a turtle object (a paddle) which I intent to move from left to right from a predetermine position. I am using the turtle.goto(x, y) to achieve this however it is not working. I have added a print statement in the functions go_left / go_right, just to see if the code was working and yes the new x position is being set but the paddle does not move. I have not a clue on why this is happening. Any Ideas ?. Thanks for the help.

from turtle import Screen, Turtle

screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor('black')
screen.title('Testing Paddle')
screen.tracer(0)

# Creating a paddle shape and registering it to the turtle shape custom list
screen.register_shape("paddle", ((0, 0), (15, 0), (15, 80), (0, 80)))

class Paddle(Turtle):
    def __init__(self, position):
        super().__init__()
        self.shape('paddle')
        self.color('magenta')
        self.penup()
        self.goto(position)

    def go_left(self):
        new_x = self.xcor() - 20
        self.goto(new_x, self.ycor())
        print(new_x, self.ycor())

    def go_right(self):
        new_x = self.xcor() + 20
        self.goto(new_x, self.ycor())
        print(new_x, self.ycor())

paddle = Paddle((-40, -240))
screen.listen()

game_is_on = True

screen.onkey(fun=paddle.go_left, key='Left')
screen.onkey(fun=paddle.go_right, key='Right')

screen.update()

screen.mainloop()

Upvotes: 1

Views: 562

Answers (2)

furas
furas

Reputation: 142983

You set

screen.tracer(0)

so now it doesn't update screen automatically and you have to do it on your own.

You have to use

screen.update()

after changing position. (After every goto(), etc.)


EDIT:

Turtle has one problem: after pressing key system makes longer delay before it starts treats it as repeated press - and paddle moves with some delay after pressing button. This need different method to work with keys. It needs to use onkeypress to increase speed (without moving paddle) and onkeyrelease to decrease speed. And code has to use ontimer to repeat code which will check current speed and move paddel.

from turtle import Screen, Turtle

screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor('black')
screen.title('Testing Paddle')
screen.tracer(0)

# Creating a paddle shape and registering it to the turtle shape custom list
screen.register_shape("paddle", ((0, 0), (15, 0), (15, 80), (0, 80)))

class Paddle(Turtle):
    def __init__(self, position, color='magenta'):
        super().__init__()
        self.shape('paddle')
        self.color(color)
        self.penup()
        self.goto(position)
        self.speed = 0
        
    def on_press_left(self):
        self.speed -= 20
        
    def on_press_right(self):
        self.speed += 20

    def on_release_left(self):
        self.speed += 20
        
    def on_release_right(self):
        self.speed -= 20

    def update(self):
        if self.speed != 0:
            new_x = self.xcor() + self.speed
            
            # pad width: 80
            
            if new_x < -300:
                new_x = -300
            if new_x > 300 - 80:  
                new_x = 300 - 80
                
            self.goto(new_x, self.ycor())

# ---

def update():
    enemy.speed = -paddle.speed  # moves enemy in different direction
    
    paddle.update()
    enemy.update()
    
    screen.update()
    screen.ontimer(fun=update, t=25)  # repeat after 25ms (0.025s)

# --- main ---

game_is_on = True

paddle = Paddle((-40, -240))
enemy  = Paddle((-40, 240+15), 'red')
screen.listen()

screen.onkeypress(fun=paddle.on_press_left, key='Left')
screen.onkeypress(fun=paddle.on_press_right, key='Right')

screen.onkeyrelease(fun=paddle.on_release_left, key='Left')
screen.onkeyrelease(fun=paddle.on_release_right, key='Right')

screen.ontimer(fun=update, t=25)  # execute after 25ms (0.025s)

screen.mainloop()

Upvotes: 0

Sharim09
Sharim09

Reputation: 6224

You have to update your screen too to show the turtle's new position

add this line in the last line of the functions screen.update()

Modified Code.

from turtle import Screen, Turtle

screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor('black')
screen.title('Testing Paddle')
screen.tracer(0)

# Creating a paddle shape and registering it to the turtle shape custom list
screen.register_shape("paddle", ((0, 0), (15, 0), (15, 80), (0, 80)))

class Paddle(Turtle):
    def __init__(self, position):
        super().__init__()
        self.shape('paddle')
        self.color('magenta')
        self.penup()
        self.goto(position)

    def go_left(self):
        new_x = self.xcor() - 20
        self.goto(new_x, self.ycor())
        print(new_x, self.ycor())
        screen.update() # edit


    def go_right(self):
        new_x = self.xcor() + 20
        self.goto(new_x, self.ycor())
        print(new_x, self.ycor())
        screen.update() # edit

paddle = Paddle((-40, -240))
screen.listen()

game_is_on = True

screen.onkey(fun=paddle.go_left, key='Left')
screen.onkey(fun=paddle.go_right, key='Right')

screen.update()

screen.mainloop()

Upvotes: 0

Related Questions