【发布时间】: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, 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,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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]]
# 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