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()