1 回答
TA贡献2016条经验 获得超9个赞
假设你想要的是:
然后我们需要将方形光标的大小从默认大小 20 调整为图块大小 24:
from turtle import Screen, Turtle
TILE_SIZE = 24
CURSOR_SIZE = 20
class Pen(Turtle):
def __init__(self):
super().__init__()
self.shape('square')
self.shapesize(TILE_SIZE / CURSOR_SIZE)
self.color('grey')
self.penup()
self.speed('fastest')
def setup_maze(level):
''' Conversion from the list to the map in turtle. '''
maze_height, maze_width = len(level), len(level[0])
for y in range(maze_height):
for x in range(maze_width):
# get the character at each x,y coordinate
character = level[y][x]
# check if it is a wall
if character == 'X':
# calculate the screen x, y coordinates
screen_x = (x - maze_width) * TILE_SIZE
screen_y = (maze_width - y) * TILE_SIZE
pen.goto(screen_x, screen_y)
pen.stamp()
screen = Screen()
screen.setup(700, 700)
screen.title("PIZZA RUNNERS")
maze = []
with open("map01.txt") as file:
for line in file:
maze.append(line.strip())
pen = Pen()
setup_maze(maze)
screen.mainloop()
如果您正在寻找更像这样的东西:
然后更改行:
self.color('grey')
在上面的代码中:
self.color('black', 'grey')
最后,如果你愿意:
然后我们需要对上面的代码做一些小修改:
class Pen(Turtle):
def __init__(self):
super().__init__()
self.shape('square')
self.shapesize(TILE_SIZE / CURSOR_SIZE)
self.pencolor('black')
self.penup()
self.speed('fastest')
def setup_maze(level):
''' Conversion from the list to the map in turtle. '''
maze_height, maze_width = len(level), len(level[0])
for y in range(maze_height):
for x in range(maze_width):
# get the character at each x,y coordinate
character = level[y][x]
# check if it is a wall or a path
pen.fillcolor(['white', 'grey'][character == 'X'])
# calculate the screen x, y coordinates
screen_x = (x - maze_width) * TILE_SIZE
screen_y = (maze_width - y) * TILE_SIZE
pen.goto(screen_x, screen_y)
pen.stamp()
添加回答
举报