python 中的海龟迷宫。我不知道如何避免乌龟穿墙和作弊
Turtle Maze in python. I do not know how to avoid the turtle passing walls and cheat
我是编码的完全初学者,我们在学校开始学习 python。我的小表弟发现了这一点,并让我做一个游戏……因为我不是专家,所以我决定做一个简单的游戏,一个迷宫。
我编写了一些函数来从由 1 和 0 组成的 2 个列表生成迷宫。
然后我尝试为一只必须穿过迷宫才能逃脱的乌龟制作移动部分。
事实上,因为你可以穿过墙壁,所以作弊太容易了。
我需要你的帮助来解决这个问题。
我在想撤消错误的动作。但我愿意接受任何解决方案
表弟今天玩了游戏,玩到最终版很热情。
我将 post 我的代码加上一些注释,以便您更好地理解。
P.S。我很抱歉我的英语不好:)
非常感谢
"""
.---.
| | __.....__ __ __ ___ __.....__
| | .-'' '. | |/ `.' `. .-'' '.
.| .-,.--. .| | | / .-''"'-. `. | .-. .-. ' / .-''"'-. `.
.' |_ | .-. | .' |_ | |/ /________\ \| | | | | | __ / /________\ \
.' | _ _ | | | | .' || || || | | | | | .:--.'. .--------.| |
'--. .-' | ' / | | | | |'--. .-'| |\ .-------------'| | | | | |/ | \ | |____ |\ .-------------'
| | .' | .' | | | '- | | | | \ '-.____...---.| | | | | |`" __ | | / / \ '-.____...---.
| | / | / | | | | | | | `. .' |__| |__| |__| .'.''| | .' / `. .'
| '.'| `'. | | | | '.''---' `''-...... -' / / | |_ / /___ `''-...... -'
| / ' .'| '/|_| | / \ \._,\ '/| |
`'-' `-' `--' `'-' `--' `" |_________|
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, 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, 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]]
# 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()
真的很抱歉让你失望了,但我认为使用 turtle 是不可能的。如果你想让乌龟与墙壁碰撞,你需要实施碰撞检测,为此你需要墙壁的坐标,而你没有。也许你可以使用 pygame 模块制作贪吃蛇游戏。
概念:
如果用对齐的小海龟对象替换绘制线条的像素,则这是可能的,每个小海龟对象构成线条的一个像素。
例如,如果每个小乌龟对象都存储在一个名为 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)
实施:
好的,下面是我对您的代码所做的更改。
- 我定义了一个
pixels
列表来存储构成墙壁的每只海龟:
pixels = list()
- 在您的
mark_horizontal_line
和 mark_line_vertical
函数中,每条线将由 2 个形状为 'square'
大小为 1
的海龟对象组成,而不是绘制墙壁像素乘 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)
- 我定义了一个
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
- 最后,将
,
、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, 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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 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]]
# 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()
输出:
观察我如何不断尝试“作弊”,但墙壁不允许我。
我喜欢。 ASCII 艺术很酷。是的,这是可行的,但是您必须将乌龟位置除以所使用的任何间距,以获得关卡数据中的位置,然后比较碰撞。
我已经弄明白了,但是你的一个挂在墙上的列表旋转了 90 度,所以它让我伤脑筋。我将 post 目前为止的所有内容,以便您查看。累了,我还要再过一天才能搞定运行。去喝点茶,看几个 youtube,然后就昏倒了。明天可能再看一眼。
turtle.cfg
试图用它调整大小 window。有一种方法可以做到,但无论出于何种原因,它都不是。
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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 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()
我是编码的完全初学者,我们在学校开始学习 python。我的小表弟发现了这一点,并让我做一个游戏……因为我不是专家,所以我决定做一个简单的游戏,一个迷宫。 我编写了一些函数来从由 1 和 0 组成的 2 个列表生成迷宫。 然后我尝试为一只必须穿过迷宫才能逃脱的乌龟制作移动部分。
事实上,因为你可以穿过墙壁,所以作弊太容易了。 我需要你的帮助来解决这个问题。 我在想撤消错误的动作。但我愿意接受任何解决方案
表弟今天玩了游戏,玩到最终版很热情。 我将 post 我的代码加上一些注释,以便您更好地理解。
P.S。我很抱歉我的英语不好:) 非常感谢
"""
.---.
| | __.....__ __ __ ___ __.....__
| | .-'' '. | |/ `.' `. .-'' '.
.| .-,.--. .| | | / .-''"'-. `. | .-. .-. ' / .-''"'-. `.
.' |_ | .-. | .' |_ | |/ /________\ \| | | | | | __ / /________\ \
.' | _ _ | | | | .' || || || | | | | | .:--.'. .--------.| |
'--. .-' | ' / | | | | |'--. .-'| |\ .-------------'| | | | | |/ | \ | |____ |\ .-------------'
| | .' | .' | | | '- | | | | \ '-.____...---.| | | | | |`" __ | | / / \ '-.____...---.
| | / | / | | | | | | | `. .' |__| |__| |__| .'.''| | .' / `. .'
| '.'| `'. | | | | '.''---' `''-...... -' / / | |_ / /___ `''-...... -'
| / ' .'| '/|_| | / \ \._,\ '/| |
`'-' `-' `--' `'-' `--' `" |_________|
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, 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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()
真的很抱歉让你失望了,但我认为使用 turtle 是不可能的。如果你想让乌龟与墙壁碰撞,你需要实施碰撞检测,为此你需要墙壁的坐标,而你没有。也许你可以使用 pygame 模块制作贪吃蛇游戏。
概念:
如果用对齐的小海龟对象替换绘制线条的像素,则这是可能的,每个小海龟对象构成线条的一个像素。
例如,如果每个小乌龟对象都存储在一个名为 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)
实施:
好的,下面是我对您的代码所做的更改。
- 我定义了一个
pixels
列表来存储构成墙壁的每只海龟:
pixels = list()
- 在您的
mark_horizontal_line
和mark_line_vertical
函数中,每条线将由 2 个形状为'square'
大小为1
的海龟对象组成,而不是绘制墙壁像素乘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)
- 我定义了一个
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
- 最后,将
,
、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, 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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, 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()
输出:
观察我如何不断尝试“作弊”,但墙壁不允许我。
我喜欢。 ASCII 艺术很酷。是的,这是可行的,但是您必须将乌龟位置除以所使用的任何间距,以获得关卡数据中的位置,然后比较碰撞。
我已经弄明白了,但是你的一个挂在墙上的列表旋转了 90 度,所以它让我伤脑筋。我将 post 目前为止的所有内容,以便您查看。累了,我还要再过一天才能搞定运行。去喝点茶,看几个 youtube,然后就昏倒了。明天可能再看一眼。
turtle.cfg
试图用它调整大小 window。有一种方法可以做到,但无论出于何种原因,它都不是。
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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 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()