Skip to content Skip to sidebar Skip to footer

Entire Screen Not Moving With The Turtle

I am making a game in python using the turtle module in python. I want the screen to move with the turtle. Is there any way to do it? Thanks

Solution 1:

The problem is that the screen isn't moving with the turtle ... Does anyone know why?

I know why. First, @martineau is correct, as usual +1, about passing the wrong range of values to yview_moveto(). But there's another piece to this puzzle: tkinter and turtle do not use the same coordinate system! You need to correct for the coordinate system difference, then turn the value into a percentage.

Here's a stripped down example based on your code and desired behavior. It keeps the ball in the middle of the window but you can tell from the numbers, and the vertical scroll bar, it's falling. Tap the the up arrow to slow it down -- tap it again to stop the motion. Tap it once more to start rising. Or use the down arrow to reverse your movement again:

from turtle import Screen, Turtle

WIDTH, DEPTH = 300, 10_000
CURSOR_SIZE = 20

vy = -10

def rise():
    global vy
    vy += 5

def fall():
    global vy
    vy -= 5

def move():
    global vy

    if abs(ball.ycor()) < DEPTH/2:
        ball.forward(vy)

        canvas.yview_moveto((DEPTH/2 - ball.ycor() - WIDTH/2 + CURSOR_SIZE) / DEPTH)

        screen.update()
        screen.ontimer(move)

screen = Screen()
screen.setup(WIDTH, WIDTH)  # visible window
screen.screensize(WIDTH, DEPTH)  # area window peeps into
screen.tracer(False)

canvas = screen.getcanvas()

marker = Turtle()
marker.hideturtle()
marker.penup()
marker.setx(-WIDTH/4)  # so we can see we're moving

for y in range(-DEPTH//2, DEPTH//2, 100):
    marker.sety(y)
    marker.write(y, align='right')

ball = Turtle('circle')
ball.speed('fastest')
ball.setheading(90)
ball.penup()

screen.onkey(rise, 'Up')
screen.onkey(fall, 'Down')
screen.listen()

move()

screen.mainloop()

Post a Comment for "Entire Screen Not Moving With The Turtle"