Reputation: 13
For a class project, I'm trying to make a Rogue-like game with Python (3.7.3) Turtle. I want to know if it is possible for the canvas screen to center on my turtle "player" module so it doesn't go outside of the user's field of view and the user doesn't have to use a scroll bar.
I've tried looking up a number of different solutions but none seem to actually be about my question. I've also tried looking at the turtle section of the Python website and I don't know what will help.
# Draw border
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color('black')
border_pen.penup()
border_pen.setposition(-600,-600)
border_pen.pendown()
border_pen.pensize(3)
for side in range(4):
border_pen.fd(600)
border_pen.lt(90)
border_pen.hideturtle()
Above is just the code I used to make the canvas itself. As you can see, it's rather big (it needs to be). I didn't think of this to be a problem at first, but now I see that it is a rather difficult one. My module, as I've said, can move off the screen.
Upvotes: 0
Views: 5734
Reputation: 41872
Below is an example of manipulating the scrolling at the tkinter level to obtain a desired behavior. In this case, I'm keeping the turtle stationary at the center of the screen but using the arrow keys to move the landscape beneath it.
The code between ### Generate a landscape ...
and ### Finished generating a ...
is from a SO question I answered earlier and is there to make an interesting fractal landscape to explore:
from turtle import Turtle, Screen
from random import random
MAGNIFICATION = 10
def move_left():
canvas.xview_scroll(-1, "units")
turtle.setx(turtle.xcor() - MAGNIFICATION)
def move_right():
canvas.xview_scroll(1, "units")
turtle.setx(turtle.xcor() + MAGNIFICATION)
def move_up():
canvas.yview_scroll(-1, "units")
turtle.sety(turtle.ycor() + MAGNIFICATION)
def move_down():
canvas.yview_scroll(1, "units")
turtle.sety(turtle.ycor() - MAGNIFICATION)
screen = Screen()
width, height = screen.screensize()
screen.screensize(width * MAGNIFICATION, height * MAGNIFICATION)
canvas = screen.getcanvas()
canvas.config(xscrollincrement=str(MAGNIFICATION))
canvas.config(yscrollincrement=str(MAGNIFICATION))
# turtle initialization
turtle = Turtle("turtle", visible=False)
turtle.width(MAGNIFICATION)
turtle.resizemode('auto')
### Generate a landscape to explore
screen.tracer(False)
RULES = {'x':'x+yf+', 'y':'-fx-y', 'f':'f', '+':'+', '-':'-'}
sub_string = string = "fx"
LEVEL = 13
for _ in range(LEVEL):
turtle.pencolor(random(), random(), random())
for character in sub_string:
if character == '+':
turtle.right(90)
elif character == '-':
turtle.left(90)
elif character == 'f':
turtle.forward(5 * MAGNIFICATION)
screen.update()
full_string = "".join(RULES[character] for character in string)
sub_string = full_string[len(string):]
string = full_string
screen.tracer(True)
### Finished generating a landscape to explore
turtle.penup()
turtle.home()
turtle.setheading(90)
turtle.color('dark green', 'light green')
turtle.showturtle()
screen.onkey(move_left, "Left")
screen.onkey(move_right, "Right")
screen.onkey(move_up, "Up")
screen.onkey(move_down, "Down")
screen.listen()
screen.mainloop()
The scroll bars reflect the movement about the entire space. Unfortunately, the scroll bars are still active and will throw things askew (use the arrow keys instead), and it needs work near the landscape edges, but this is simply an example to show that pretty much anything is possible if you take the time to explore the tkinter/Tk underpinnings.
Upvotes: 1