【问题标题】:Turtle Maze in python. I do not know how to avoid the turtle passing walls and cheat蟒蛇中的海龟迷宫。我不知道如何避免乌龟过墙作弊
【发布时间】:2021-04-27 23:29:17
【问题描述】:

我是编码的初学者,在学校我们开始学习 python。我的小表弟发现了,并让我做一个游戏......因为我不是专家,所以我决定做一个简单的游戏,一个迷宫。 我编写了一些函数来从由 1 和 0 组成的 2 个列表生成迷宫。 然后我尝试为一只必须穿过迷宫才能逃脱的海龟制作运动部分。

事实上,因为你可以穿过墙壁,所以作弊太容易了。 我需要你的帮助来解决这个问题。 我想撤消错误的动作。但我愿意接受任何解决方案

我表弟今天玩了游戏,非常热衷于玩最终版。 我会将我的代码与一些 cmets 一起发布,以便您更好地理解。

附:我很抱歉我的英语不好:) 非常感谢

"""
                                     .---.
                                     |   |      __.....__      __  __   ___                             __.....__
                                     |   |  .-''         '.   |  |/  `.'   `.                       .-''         '.
     .|             .-,.--.      .|  |   | /     .-''"'-.  `. |   .-.  .-.   '                     /     .-''"'-.  `.
   .' |_            |  .-. |   .' |_ |   |/     /________\   \|  |  |  |  |  |    __              /     /________\   \
 .'     |   _    _  | |  | | .'     ||   ||                  ||  |  |  |  |  | .:--.'.  .--------.|                  |
'--.  .-'  | '  / | | |  | |'--.  .-'|   |\    .-------------'|  |  |  |  |  |/ |   \ | |____    |\    .-------------'
   |  |   .' | .' | | |  '-    |  |  |   | \    '-.____...---.|  |  |  |  |  |`" __ | |     /   /  \    '-.____...---.
   |  |   /  | /  | | |        |  |  |   |  `.             .' |__|  |__|  |__| .'.''| |   .'   /    `.             .'
   |  '.'|   `'.  | | |        |  '.''---'    `''-...... -'                   / /   | |_ /    /___    `''-...... -'
   |   / '   .'|  '/|_|        |   /                                          \ \._,\ '/|         |
   `'-'   `-'  `--'            `'-'                                            `--'  `" |_________|

to my little cousin
"""



import turtle

turtle.title("TurtleMaze")





#here i define some funcions that mark the walls of the maze by the usage of some lists.

def mark_horizontal_line(lo, x, y):
    line_horizontal = turtle.Turtle()
    line_horizontal.speed(0)
    line_horizontal.hideturtle()
    line_horizontal.up()
    line_horizontal.setposition(x, y)
    for n in lo:
        if n == 1:
            line_horizontal.down()
            line_horizontal.forward(21)
            line_horizontal.up()
        if n == 0:
            line_horizontal.forward(21)


def mark_horizontal_lines(lo, x, y):
    step_y = y
    for n in lo:
        mark_horizontal_line(n, x, step_y)
        step_y -= 21



def mark_line_vertical(lv, x, y):
    line_v = turtle.Turtle()
    line_v.hideturtle()
    line_v.speed(0)
    line_v.up()
    line_v.setposition(x, y)
    line_v.right(90)
    for n in lv:
        if n == 1:
            line_v.down()
            line_v.forward(21)
            line_v.up()
        if n == 0:
            line_v.forward(21)


def create_lines_vertical(lv, x, y):
    step_dx = x
    for n in lv:
        mark_line_vertical(n, step_dx, y)
        step_dx += 21


# this function marcks the entire maze by attaching all the others above

def mark_maze(lo, lv, x, y):
    mark_horizontal_lines(lo, x, y)
    create_lines_vertical(lv, x, y)

# i create a finestra = window

finestra = turtle.Screen()
finestra.screensize(600, 400)
finestra.title("TurtleMaze")

# these are the lists that contain the info to mark the lines

lo = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
      [0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
      [0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1],
      [0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0],
      [0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1],
      [0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0],
      [0, 0, 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]




# i call the function to generate the maze

mark_maze(lo, lv, -200, 300)



# now the movement section

starting_pos = (63, -162)

red_line = turtle.Turtle()
red_line.hideturtle()
red_line.speed(0)
red_line.up()
red_line.setposition(starting_pos)
red_line.color("red")
red_line.up()
red_line.down()


def dx():
    red_line.speed(5)
    red_line.forward(11)
    red_line.speed(0)

def sx():

    red_line.right(180)
    red_line.speed(5)
    red_line.forward(11)
    red_line.speed(0)
    red_line.right(-180)



def down():

    red_line.right(90)
    red_line.speed(5)
    red_line.forward(11)
    red_line.speed(0)
    red_line.right(-90)



def up():

    red_line.right(-90)
    red_line.speed(5)
    red_line.forward(11)
    red_line.speed(0)
    red_line.right(90)

# to make the turtle go back i made some separated commands to keep it as easy as possible

def undo_sx():
    red_line.undo()
    red_line.undo()
    red_line.undo()



def undo_dx():
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    


def undo_down():
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()


def undo_up():
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()


def close():
    finestra.bye()


finestra.onkey(dx, "Right")
finestra.onkey(sx, "Left")
finestra.onkey(up, "Up")
finestra.onkey(down, "Down")
finestra.onkey(undo_dx, "d")
finestra.onkey(undo_sx, "a")
finestra.onkey(undo_down, "s")
finestra.onkey(undo_up, "w")
finestra.onkey(close, "q")

finestra.listen()
finestra.mainloop()

【问题讨论】:

  • 您好,我目前正在做一些可以帮助您的事情,我可能会在今晚或明天发布。
  • 所以在考虑了大约一个小时后,我得出结论,以你绘制迷宫的方式,你无法检查你周围是否有墙。如果你愿意,你必须定义一个列表,告诉你的玩家是否在列表的索引处,他可以向下、向左、向右或向上移动。显然,你不能使用两个大小不同的列表。
  • mh,我真的无法弄清楚,因为我不知道......你能给我举个例子吗?

标签: python python-3.x turtle-graphics python-turtle


【解决方案1】:

概念:

如果你用对齐的小海龟对象替换绘制线的像素,这是可能的,每个小海龟对象构成线条的一个像素。

例如,如果每个小海龟对象都存储在一个名为lines 的列表中:

def dx():
    red_line.speed(5)
    red_line.forward(11)
    for line in lines:
        if red_line.distance(line) < 5: # If the player came within 5 units of any pixel in the lines list...
            red_line.undo() # then undo the previous move
            break
    red_line.speed(0)

实施:

好的,以下是我对您的代码所做的更改。

  1. 我定义了一个pixels 列表来存储构成墙壁的每只海龟:
pixels = list()
  1. 在您的 mark_horizontal_linemark_line_vertical 函数中,每条线将由 2 个形状为 'square' 和大小为 1 像素 x 10.5 像素的海龟对象组成,而不是绘制墙壁 (一半墙的大小)

所以在mark_horizontal_line函数中,来自

            line_horizontal.down()
            line_horizontal.forward(21)
            line_horizontal.up()

            for i in range(2):
                line_horizontal.forward(21 / 4)
                pixel = line_horizontal.clone()
                pixel.showturtle()
                pixel.shape("square")
                pixel.shapesize(1 / 20, 10.5 / 20)
                pixels.append(pixel)
                line_horizontal.forward(21 / 4)

而在mark_line_vertical函数中,来自:

            line_v.down()
            line_v.forward(21)
            line_v.up()

            for i in range(2):
                line_v.forward(21 / 4)
                pixel = line_v.clone()
                pixel.showturtle()
                pixel.shape("square")
                pixel.shapesize(1 / 20, 10.5 / 20)
                pixels.append(pixel)
                line_v.forward(21 / 4)
  1. 我定义了一个forward() 函数,该函数将使red_line 乌龟向前移动,但如果它在pixels 列表的任何像素的5 像素内自动撤消它的移动:
def forward(turt):
    for i in range(11):
        turt.forward(1)
        if any(pixel.distance(turt) < 5 for pixel in pixels):
            for i in range(i):
                turt.undo()
            break
  1. 最后,将, and函数中的red_line.forward(11)替换为forward(red_line),调用定义的函数:
def dx():
    red_line.speed(5)
    forward(red_line) # Function call
    red_line.speed(0)

def sx():

    red_line.right(180)
    red_line.speed(5)
    forward(red_line) # Function call
    red_line.speed(0)
    red_line.right(-180)

def down():
    red_line.right(90)
    red_line.speed(5)
    forward(red_line) # Function call
    red_line.speed(0)
    red_line.right(-90)

def up():
    red_line.right(-90)
    red_line.speed(5)
    forward(red_line) # Function call
    red_line.speed(0)
    red_line.right(90)

注意:如果您不想等待迷宫被绘制出来,您可以关闭tracer() 调用mark_maze()

turtle.tracer(0)
mark_maze(lo, lv, -200, 300)
turtle.update()
turtle.tracer(1)

总共:

import turtle

turtle.title("TurtleMaze")





#here i define some funcions that mark the walls of the maze by the usage of some lists.
pixels = list()

def mark_horizontal_line(lo, x, y):
    line_horizontal = turtle.Turtle()
    line_horizontal.speed(0)
    line_horizontal.hideturtle()
    line_horizontal.up()
    line_horizontal.setposition(x, y)
    for n in lo:
        if n == 1:
            for i in range(2):
                line_horizontal.forward(21 / 4)
                pixel = line_horizontal.clone()
                pixel.showturtle()
                pixel.shape("square")
                pixel.shapesize(1 / 20, 10.5 / 20)
                pixels.append(pixel)
                line_horizontal.forward(21 / 4)
                
        if n == 0:
            line_horizontal.forward(21)


def mark_horizontal_lines(lo, x, y):
    step_y = y
    for n in lo:
        mark_horizontal_line(n, x, step_y)
        step_y -= 21



def mark_line_vertical(lv, x, y):
    line_v = turtle.Turtle()
    line_v.hideturtle()
    line_v.speed(0)
    line_v.up()
    line_v.setposition(x, y)
    line_v.right(90)
    for n in lv:
        if n == 1:
            for i in range(2):
                line_v.forward(21 / 4)
                pixel = line_v.clone()
                pixel.showturtle()
                pixel.shape("square")
                pixel.shapesize(1 / 20, 10.5 / 20)
                pixels.append(pixel)
                line_v.forward(21 / 4)
        if n == 0:
            line_v.forward(21)


def create_lines_vertical(lv, x, y):
    step_dx = x
    for n in lv:
        mark_line_vertical(n, step_dx, y)
        step_dx += 21


# this function marcks the entire maze by attaching all the others above

def mark_maze(lo, lv, x, y):
    mark_horizontal_lines(lo, x, y)
    create_lines_vertical(lv, x, y)

# i create a finestra = window

finestra = turtle.Screen()
finestra.screensize(600, 400)
finestra.title("TurtleMaze")

# these are the lists that contain the info to mark the lines

lo = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
      [0, 0, 1, 1, 0, 0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1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1],
      [0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0],
      [1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
      [0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
      [0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0],
      [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]




# i call the function to generate the maze

turtle.tracer(0)
mark_maze(lo, lv, -200, 300)
turtle.update()
turtle.tracer(1)

# now the movement section

starting_pos = (63, -162)

red_line = turtle.Turtle()
red_line.hideturtle()
red_line.speed(0)
red_line.up()
red_line.setposition(starting_pos)
red_line.color("red")
red_line.up()
red_line.down()

def forward(turt):
    for i in range(11):
        turt.forward(1)
        if any(pixel.distance(turt) < 5 for pixel in pixels):
            for i in range(i):
                turt.undo()
            break
def dx():
    red_line.speed(5)
    forward(red_line)
    red_line.speed(0)

def sx():

    red_line.right(180)
    red_line.speed(5)
    forward(red_line)
    red_line.speed(0)
    red_line.right(-180)

def down():
    red_line.right(90)
    red_line.speed(5)
    forward(red_line)
    red_line.speed(0)
    red_line.right(-90)

def up():
    red_line.right(-90)
    red_line.speed(5)
    forward(red_line)
    red_line.speed(0)
    red_line.right(90)

# to make the turtle go back i made some separated commands to keep it as easy as possible

def undo_sx():
    red_line.undo()
    red_line.undo()
    red_line.undo()



def undo_dx():
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    


def undo_down():
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()


def undo_up():
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()


def close():
    finestra.bye()


finestra.onkey(dx, "Right")
finestra.onkey(sx, "Left")
finestra.onkey(up, "Up")
finestra.onkey(down, "Down")
finestra.onkey(undo_dx, "d")
finestra.onkey(undo_sx, "a")
finestra.onkey(undo_down, "s")
finestra.onkey(undo_up, "w")
finestra.onkey(close, "q")

finestra.listen()
finestra.mainloop()

输出:

请注意我是如何不断尝试“作弊”的,但墙壁不允许我这样做。

【讨论】:

  • 所以,如果我理解...我应该为每一行创建一个海龟并以某种方式应用您发布的功能...对吗?顺便谢谢你
  • @MarcoMac01 是的,每行可能有多个海龟
  • 我认为放入一个函数太多了。也许对我来说太难了。明天我会尝试做一些测试。要应用这种计算,我应该为每个像素创建一个海龟,然后使用每个海龟之间的间距。我只是在想,我肯定错了。
  • @MarcoMac01 请记住,turtle.shape("square")turtle.shapesize(21 / 20, 1 / 20) 会造成一堵墙。
  • 我应该做很多单行...我认为浪费了太多时间。
【解决方案2】:

我喜欢。 ASCII 艺术很酷。是的,这是可行的,但是您必须将海龟的位置除以使用的任何间距,以在您的关卡数据中获得位置,然后比较碰撞。

我已经找到了它,但你的墙上列表之一旋转了 90 度,所以它破坏了我的大脑。我会发布我目前所拥有的,所以你可以看看它。累了,我还需要一天的时间才能让它运转起来。去喝杯茶,看几个youtube,然后昏倒。明天可能会再看一遍。


turtle.cfg

试图用这个来调整窗口大小。有办法做到这一点,但无论出于何种原因,它都不是。

width = 0.5
height = 0.75
leftright = None
topbottom = None
canvwidth = 700
canvheight = 500
mode = standard
colormode = 1.0
delay = 0
undobuffersize = 1000
shape = classic
pencolor = black
fillcolor = black
resizemode = noresize
visible = True
language = english
exampleturtle = turtle
examplescreen = screen
title = TurtleMaze
using_IDLE = False

#! /usr/bin/python3

##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##  docs.python.org/3/library/t.html

import turtle as t
import math

##  create a finestra = window
finestra = t .Screen()

width = 700
height = 500

half_width = math .floor( width /2 )
half_height = math .floor( height /2 )

finestra .screensize( width, height )
finestra .title('TurtleMaze')

##  t.setheading() or t.seth()
East = 0
North = 90
West = 180
South = 270

red_line = t .Turtle()
red_line .hideturtle()
red_line .up()
red_line .speed('fastest')
red_line .color('red')

line_h = t .Turtle()
line_h .hideturtle()
line_h .seth( East )
line_h .up()
line_h .speed('fastest')

line_v = t .Turtle()
line_v .hideturtle()
line_v .seth( South )
line_v .up()
line_v .speed('fastest')

##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

##  draw walls by referencing lists.

def mark_horizontal_line( row, x, y ):
    line_h .up()
    line_h .setposition( x, y )
    for r in row:
        if r == 0:  line_h .up()
        else:  line_h .down()
        line_h .forward( horiz_spacing )


def create_horiz_lines( lo, x, y ):
    step_y = y
    for n in lo:
        mark_horizontal_line( n, x, step_y )
        step_y -= vert_spacing  ##  go down


def mark_line_vertical( column, x, y ):
    line_v .up()
    line_v .setposition( x, y )
    for c in column:
        if c == 0:  line_v .up()
        else:  line_v .down()
        line_v .forward( vert_spacing )


def create_vert_lines( lv, x, y ):
    step_dx = x
    for n in lv:
        mark_line_vertical( n, step_dx, y )
        step_dx += horiz_spacing  ##  go right

##  generate entire maze by attaching all the others above

def mark_maze( lo, lv, x, y ):
    create_horiz_lines( lo, x, y )
    create_vert_lines( lv, x, y )


##  horizontal segment data:  23 vert x 25 horiz

lo = [  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
        [0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1],
        [0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0],
        [0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1],
        [0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0],
        [0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0],
        [1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0],
        [0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0],
        [0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1],
        [1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0],
        [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0],
        [1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1],
        [0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0],
        [0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0],
        [0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0],
        [0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0],
        [0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1],
        [1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0],
        [0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1],
        [0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0],
        [1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]  ]

##  vertical segment data:  26 vert x 22 horiz   rotated by 90*

lv = [  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1],
        [0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0],
        [0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1],
        [1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0],
        [0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0],
        [1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0],
        [0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0],
        [1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1],
        [0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0],
        [1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0],
        [1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1],
        [1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0],
        [0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1],
        [1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0],
        [0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0],
        [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0],
        [1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1],
        [0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0],
        [1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
        [0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
        [0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]  ]

##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

horiz_spacing = width /len( lo[0] )
vert_spacing = height /len( lv[0] )

horiz_walk = horiz_spacing /3
vert_walk = vert_spacing /3

##  generate maze
mark_maze( lo, lv, -half_width, half_height )

##  now the movement section
starting_pos = ( 0, -half_height )

red_line .setposition( starting_pos )
red_line .seth( North )
red_line .showturtle()
red_line .down()

def up():
    xx = round( (half_width +red_line .xcor()) /horiz_spacing )
    yy = round( (half_height +red_line .ycor() -vert_walk) /vert_spacing )
    red_line .seth( North )
    print( xx, yy )
    if lo[-yy][xx] == 0:
        red_line .forward( vert_walk )
    else:
        finestra .bgcolor( 'orange' )
        red_line .speed( 'slow' )
        red_line .forward( vert_walk )
        red_line .undo()
        finestra .bgcolor( 'white' )


def down():
    xx = round( (half_width +red_line .xcor()) /horiz_spacing )
    yy = round( (half_height +red_line .ycor() +vert_walk) /vert_spacing )
    red_line .seth( South )
    print( xx, yy )
    if lo[-yy][xx] == 0:
        red_line .forward( vert_walk )
    else:
        finestra .bgcolor( 'orange' )
        red_line .speed( 'slow' )
        red_line .forward( vert_walk )
        red_line .undo()
        finestra .bgcolor( 'white' )


def left():
    xx = round( (half_width +red_line .xcor() -horiz_walk) /horiz_spacing )
    yy = round( (half_height +red_line .ycor()) /vert_spacing )
    red_line .seth( West )
    print( xx, yy )
    if lv[xx][yy] == 0:
        red_line .forward( vert_walk )
    else:
        finestra .bgcolor( 'orange' )
        red_line .speed( 'slow' )
        red_line .forward( vert_walk )
        red_line .undo()
        finestra .bgcolor( 'white' )


def right():
    xx = round( (half_width +red_line .xcor() +vert_walk) /horiz_spacing )
    yy = round( (half_height +red_line .ycor()) /vert_spacing )
    red_line .seth( East )
    print( xx, yy )
    if lv[xx][yy] == 0:
        red_line .forward( vert_walk )
    else:
        finestra .bgcolor( 'orange' )
        red_line .speed( 'slow' )
        red_line .forward( vert_walk )
        red_line .undo()
        finestra .bgcolor( 'white' )

def undo():
    red_line .undo()

def quit():
    finestra .bye()

finestra .onkey( up, 'Up' )
finestra .onkey( down, 'Down' )
finestra .onkey( left, 'Left' )
finestra .onkey( right, 'Right' )

finestra .onkey( undo, 'space' )
finestra .onkey( quit, 'q' )

finestra .listen()
finestra .mainloop()

【讨论】:

  • 好的,谢谢。我很高兴看到什么可以解决这个问题。非常感谢你所做的一切
  • 乌龟 git 卡在墙上了,我设法把它扭出来……乌龟最终在墙的另一边。
【解决方案3】:

真的很抱歉让你失望了,但我认为这不可能使用乌龟。如果您希望海龟与墙壁发生碰撞,则需要实现碰撞检测,为此您需要墙壁的坐标,而您没有。也许您可以使用 pygame 模块制作蛇游戏。

【讨论】:

    猜你喜欢
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2014-12-01
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多