简体   繁体   English

如何防止 escaping 开箱即用的字符?

[英]How to prevent the character from escaping out of the box?

So, I've been trying to make a maze using python, and it's been going well, except that I can't stop the character from getting out of the maze's box: This is my code:所以,我一直在尝试使用 python 制作迷宫,并且进展顺利,除了我无法阻止角色走出迷宫的盒子:这是我的代码:

import turtle

wn = turtle.Screen()
wn.setup(width=1000, height=700)
wn.bgcolor('white')
wn.title('test')
wn.tracer(0)

box_1 = turtle.Turtle()
box_1.shape('square')
box_1.penup()
box_1.color('black')
box_1.goto(-485, 0)
box_1.shapesize(stretch_len=1.5, stretch_wid=35)

box_2 = turtle.Turtle()
box_2.shape('square')
box_2.penup()
box_2.color('black')
box_2.goto(-500, -95)
box_2.shapesize(stretch_len=35, stretch_wid=1.5)

box_3 = turtle.Turtle()
box_3.shape('square')
box_3.penup()
box_3.color('black')
box_3.goto(-325, 0)
box_3.shapesize(stretch_len=1.5, stretch_wid=10)

box_4 = turtle.Turtle()
box_4.shape('square')
box_4.penup()
box_4.color('black')
box_4.goto(-145, -180)
box_4.shapesize(stretch_len=1.5, stretch_wid=10)

box_4 = turtle.Turtle()
box_4.shape('square')
box_4.penup()
box_4.color('black')
box_4.goto(-240, -265)
box_4.shapesize(stretch_len=10, stretch_wid=1.5)

box_4 = turtle.Turtle()
box_4.shape('square')
box_4.penup()
box_4.color('black')
box_4.goto(-345, -220)
box_4.shapesize(stretch_len=1.5, stretch_wid=6)

box_4 = turtle.Turtle()
box_4.shape('square')
box_4.penup()
box_4.color('black')
box_4.goto(-190, 90)
box_4.shapesize(stretch_len=15, stretch_wid=1.5)

character = turtle.Turtle()
character.shape('square')
character.speed(0)
character.penup()
character.color('yellow')
character.goto(-485, 335)
character.shapesize(stretch_wid=1, stretch_len=1)


def character_up():
    y = character.ycor()
    y += 10
    character.sety(y)


def character_down():
    y = character.ycor()
    y -= 10
    character.sety(y)


def character_right():
    x = character.xcor()
    x += 10
    character.setx(x)


def character_left():
    x = character.xcor()
    x -= 10
    character.setx(x)


wn.listen()
wn.onkeypress(character_up, "Up")
wn.onkeypress(character_down, "Down")
wn.onkeypress(character_right, "Right")
wn.onkeypress(character_left, "Left")


while True:
    wn.update()
    x = character.xcor()
    y = character.ycor()


You can try this out yourself and you'll see that you can just easily get the character out of the maze, but I want to prevent it.您可以自己尝试一下,您会发现您可以轻松地将角色从迷宫中取出,但我想阻止它。 Can someone help me?有人能帮我吗?

I lack the ability to run this code at the moment, but I could give the following points:我目前缺乏运行此代码的能力,但我可以给出以下几点:

  1. Don't reuse the box_4 variable.不要重复使用box_4变量。 Set box_5 , box_6 , and box_7 .设置box_5box_6box_7

  2. In your character_[up/down/right/left] functions, check if the X value is above or below a certain value, and don't accept the move if so.在你的character_[up/down/right/left]函数中,检查 X 值是高于还是低于某个值,如果是,则不接受移动。 For example:例如:

def character_up():
    y = character.ycor()
    if (y < 100) { // Only do this if the Y variable is within bounds
        y += 10
    }
    character.sety(y)


def character_down():
    y = character.ycor()
    if (y > -100) {
        y -= 10
    }
    character.sety(y)


def character_right():
    x = character.xcor()
    if (x < 100) {
        x += 10
    }
    character.setx(x)


def character_left():
    x = character.xcor()
    if (x > -100) {
        x -= 10
    }
    character.setx(x)

You may have to adjust the bounds ( -100 --> -200 , 100 --> 200 )您可能需要调整边界( -100 --> -200100 --> 200

It seems to me you're trying to find a simple way out of a difficult problem.在我看来,您正试图找到解决难题的简单方法。 You want to draw black lines of a maze, and have your yellow character bound to those lines.您想绘制迷宫的黑线,并将黄色字符绑定到这些线。 Folks often approach this as drawing the walls of the maze and keeping the character from moving onto them.人们经常将其视为绘制迷宫的墙壁并阻止角色移动到它们上面。 You've reversed this in that everything is a wall and you want to draw the path , keeping your character on the path.你已经扭转了这一点,因为一切都是一堵墙,你想绘制路径,让你的角色保持在路径上。 We can do this.我们做得到。

The approach here, is we build the path out of homogenous, small square blocks that are turtles.这里的方法是,我们用海龟的同质小方块构建路径。 We keep a list of those blocks, and anytime the character moves, we validate the move by making sure they are always within a short distance of some block:我们保留了这些块的list ,并且每当角色移动时,我们通过确保它们始终在某个块的短距离内来验证移动:

from turtle import Screen, Turtle

BLOCK_SIZE = 30
CURSOR_SIZE = 20
CHARACTER_SIZE = CURSOR_SIZE

def character_up():
    character.sety(character.ycor() + CHARACTER_SIZE/2)

    if not any(box.distance(character) < BLOCK_SIZE/2 for box in boxes):
        character.undo()

def character_down():
    character.sety(character.ycor() - CHARACTER_SIZE/2)

    if not any(box.distance(character) < BLOCK_SIZE/2 for box in boxes):
        character.undo()

def character_right():
    character.setx(character.xcor() + CHARACTER_SIZE/2)

    if not any(box.distance(character) < BLOCK_SIZE/2 for box in boxes):
        character.undo()

def character_left():
    character.setx(character.xcor() - CHARACTER_SIZE/2)

    if not any(box.distance(character) < BLOCK_SIZE/2 for box in boxes):
        character.undo()

screen = Screen()
screen.setup(width=1000, height=700)
screen.tracer(False)

box = Turtle()
box.shape('square')
box.penup()
box.color('black')
box.shapesize(BLOCK_SIZE / CURSOR_SIZE)

boxes = []

box.goto(-485, 335)
box.setheading(270)
for _ in range(23):
    boxes.append(box.clone())
    box.forward(BLOCK_SIZE)

box.backward(9 * BLOCK_SIZE)
box.setheading(0)
for _ in range(13):
    boxes.append(box.clone())
    box.forward(BLOCK_SIZE)

box.backward(8 * BLOCK_SIZE)
box.setheading(90)
for _ in range(7):
    boxes.append(box.clone())
    box.forward(BLOCK_SIZE)

box.setheading(0)
for _ in range(10):
    boxes.append(box.clone())
    box.forward(BLOCK_SIZE)

box.backward(3 * BLOCK_SIZE)
box.setheading(270)
box.forward(7 * BLOCK_SIZE)
for _ in range(6):
    boxes.append(box.clone())
    box.forward(BLOCK_SIZE)

box.setheading(180)
for _ in range(6):
    boxes.append(box.clone())
    box.forward(BLOCK_SIZE)

box.setheading(90)
for _ in range(4):
    boxes.append(box.clone())
    box.forward(BLOCK_SIZE)

box.hideturtle()
screen.tracer(True)

character = Turtle()
character.shape('square')
character.speed('fastest')
character.color('yellow')
character.penup()
character.goto(-485, 335)
character.shapesize(CHARACTER_SIZE / CURSOR_SIZE)

screen.onkeypress(character_up, 'Up')
screen.onkeypress(character_down, 'Down')
screen.onkeypress(character_right, 'Right')
screen.onkeypress(character_left, 'Left')

screen.listen()
screen.mainloop()

My maze drawing code may seem complicated, but by using relative drawing of the blocks, instead of absolute positions, it actually simplifies the problem, as we start thinking in a block-oriented coordinate system, instead of pixels.我的迷宫绘制代码可能看起来很复杂,但是通过使用块的相对绘制而不是绝对位置,它实际上简化了问题,因为我们开始在面向块的坐标系中思考,而不是像素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM