如何让 Turtle 画布屏幕与 Turtle 模块一起移动? [英] How do I get the Turtle canvas screen to move with a Turtle module?

查看:70
本文介绍了如何让 Turtle 画布屏幕与 Turtle 模块一起移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个班级项目,我正在尝试使用 Python (3.7.3) Turtle 制作一个类似 Rogue 的游戏.我想知道画布屏幕是否可以以我的海龟播放器"模块为中心,这样它就不会超出用户的视野,用户也不必使用滚动条.

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.

我尝试查找了许多不同的解决方案,但似乎没有一个真正与我的问题有关.我也试过查看Python网站的turtle部分,不知道有什么帮助.

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.

推荐答案

以下是在 tkinter 级别操纵滚动以获得所需行为的示例.在本例中,我将海龟固定在屏幕中央,但使用箭头键移动其下方的景观.

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.

### Generate a Landscape ...### Finished generate a ... 之间的代码来自我之前回答的一个 SO 问题,是否存在制作一个有趣的分形景观来探索:

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

滚动条反映了整个空间的运动.不幸的是,滚动条仍然处于活动状态并且会歪斜(改用箭头键),并且它需要在景观边缘附近工作,但这只是一个示例,表明如果您花时间探索 tkinter/Tk 基础.

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.

这篇关于如何让 Turtle 画布屏幕与 Turtle 模块一起移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆