# data[].attributes.body
我是林教授，以下是我的代码
import turtle

# -----------------------------------------------------------------

# 功能模块区，主要放置函数

def draw(p,img,x,y):

#绘制图像

global origin_x,origin_y,size

p.shape(img)

p.goto(origin_x+x*size,origin_y-y*size)

p.stamp()

def move_up():

global grid_y,grid_x,grid,result

if result !=2:

return

grid_y -= 1

if grid[grid_y][grid_x]==1:

grid_y += 1

return

elif grid_y < 0:

grid_y +=1

return

update()

def move_down():

global grid_y,grid_x,grid,height,result

if result !=2:

return

grid_y += 1

if grid[grid_y][grid_x]==1:

grid_y -= 1

return

elif grid_y > height-1:

grid_y -=1

return

update()

def move_left():

global grid_y,grid_x,grid,width,result

if result !=2:

return

grid_x -= 1

if grid[grid_y][grid_x]==1:

grid_x += 1

return

elif grid_x > width-1:

grid_x +=1

return

update()

def move_right():

global grid_y,grid_x,grid,width,result

if result !=2:

return

grid_x += 1

if grid[grid_y][grid_x]==1:

grid_x -= 1

return

elif grid_x <0:

grid_x -=1

return

update()

def update():

'''

更新地图信息

'''

global grid,grid_x,grid_y

if grid[grid_y][grid_x] == 2:

grid[grid_y][grid_x] = 3

elif grid[grid_y][grid_x] == 3:

grid[grid_y][grid_x]= 4

update_result()

def update_result():

'''

更新游戏状态

'''

global grid,grid_x,grid_y,result

if grid[grid_y][grid_x] == 4:

result = 0

else:

result = 1

for i in grid:

if 2 in i:

result = 2

break

def next_level():

global result,level_num,grid_x,grid_y,width,height,origin_x,origin_y,size

if result ==1:

level_num +=1

result=2

grid = copy.deepcopy(level_store[level_num-1])

grid_x = position_store[level_num-1][0]

grid_y = position_store[level_num-1][1]

width = len(grid[0])

height = len(grid)

origin_x = 0 - size * (width - 1) / 2

origin_y = 0 + size * (height - 1) / 2

# ------------------------------------------------------------

# 基本信息区，放置游戏相关的一些信息

''' 地图二维列表信息  0: 空  1：墙壁  2：通道  3：目标  4：窟窿  '''

level_store =[[[1, 1, 1, 1, 1, 1],

[1, 2, 2, 2, 2, 1],

[1, 2, 1, 2, 2, 1],

[1, 3, 1, 2, 2, 1],

[1, 3, 2, 2, 2, 1],

[0, 1, 1, 1, 1, 1]],

[[2, 2, 2, 2, 2, 2],

[2, 2, 2, 2, 2, 2],

[2, 1, 1, 2, 2, 2],

[2, 3, 2, 2, 3, 2],

[2, 2, 2, 2, 2, 2],

[2, 2, 2, 2, 2, 2]]]

position_store = [[1,3], [4,1]]

level_num = 1

grid = copy.deepcopy(level_store[level_num-1])

size = 70

width = len(grid[0])

height= len(grid)

origin_x = 0 - size * (width - 1) / 2

origin_y = 0 + size * (height - 1) / 2

# 角色初始位置

grid_x = position_store[level_num-1][0]

grid_y = position_store[level_num-1][1]

# 游戏状态

'''  result

0: 游戏结束    1：过关    2：游戏进行中'''

result = 2

# ------------------------------------------------------------

# 操作事件区，放置键鼠操作相关的内容

turtle.onkey(move_up,'Up')

turtle.onkey(move_down,'Down')

turtle.onkey(move_left,'Left')

turtle.onkey(move_right,'Right')

turtle.onkey(next_level,'Return')

turtle.listen()

# ------------------------------------------------------------

# 场景素材区，添加游戏画面相关的素材

# 添加素材

tile_shapes = ['空.gif', '墙壁.gif', '通道.gif', '目标.gif','窟窿.gif']

for i in tile_shapes:

turtle.addshape(i)

turtle.addshape('编程猫.gif')

turtle.addshape('遮罩.gif')

result_shapes = ['失败.gif', '成功.gif', '空.gif']

for i in result_shapes:

turtle.addshape(i)

# ------------------------------------------------------------

# 画面绘制区，绘制场景用的画笔，绘制场景的代码

p = turtle.Pen()

p.penup()

turtle.tracer(False)

p.hideturtle()

p.goto(origin_x, origin_y)

while True:

p.clear()

for i in range(len(grid)):

for j in range(len(grid[i])):

draw(p,tile_shapes[grid[i][j]],j,i)

#绘制编程猫

draw(p,'编程猫.gif',grid_x,grid_y)

draw(p,'遮罩.gif',grid_x,grid_y)

draw(p, result_shapes[result], (width-1)/2, (height-1)/2)

turtle.update()

turtle.done()