简体   繁体   中英

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:

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. Set box_5 , box_6 , and box_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. 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 )

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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