1

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()
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Okamiram
  • 43
  • 5
  • Tip: [don't subclass turtle](https://stackoverflow.com/questions/9441331/how-to-create-a-subclass-in-python-that-is-inherited-from-turtle-module/71003069#71003069). Example: [real time turtle](https://stackoverflow.com/questions/47879608/how-to-bind-several-key-presses-together-in-turtle-graphics/70979967#70979967) for handling keypresses and continuous movement (don't change positions in handlers and run your own update loop). – ggorlen Sep 14 '22 at 13:16
  • Does this answer your question? [How to bind several key presses together in turtle graphics?](https://stackoverflow.com/questions/47879608/how-to-bind-several-key-presses-together-in-turtle-graphics) – ggorlen Sep 14 '22 at 13:17

2 Answers2

0

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

codester_09
  • 5,622
  • 2
  • 5
  • 27
  • If you check my code, the statement screen.update() is at the bottom of the code but it does not do the trick. – Okamiram Sep 14 '22 at 22:21
0

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()
furas
  • 134,197
  • 12
  • 106
  • 148
  • Please, I have added in my original code a screen.update() statement after the statements for moving the paddle position, yet it does not work. Removing screen.tracer() does not affect the outcome. – Okamiram Sep 14 '22 at 20:53
  • where did change code? I don't see `screen.update()` after `goto()`. And when I remove `screen.tracer()` then it works even without `screen.update()` after `goto()` - but it may need to change key repeating setting in settings - or it need different method to work with keys. `on_press` may need only to set variable with direction and you may need loop which will check direction and move paddle. – furas Sep 14 '22 at 21:07
  • I added differnt code which use `onkeypress` to increase `speed` and `onkeyrelease` to decrease `speed`, and `ontimer` to check speed and move paddle. – furas Sep 14 '22 at 21:43
  • Furas, Thanks very much for your answer. in re-wrote the code again and somehow it worked. I noticed that this time I did not write the screen.tracer(0) as you suggested and it worked. Thanks again. – Okamiram Sep 14 '22 at 22:20