第一关
# 当在程序中多次执行一段相同的代码时,我们可以创建一个函数表示这一段代码,在需要时调用代码即可
#例如在这一关中,我们通过def命令定义一个函数,函数的名称为get_item,这里函数的名称大家可以使用字母和数字的组合,但是数字不能作为第一个字符
#函数的名称就代表了函数内部的代码,可以通过函数名称进行调用
#定义函数
def get_item():
Dev.step(4) #函数内的代码,需要缩进四格
Dev.step(-4)
get_item()
Spaceship.step(3)
get_item() #可以多次调用同一个函数
Spaceship.step(2)
get_item()
Spaceship.step(4)
get_item()
Spaceship.step(2)
get_item()
Spaceship.step(3)
get_item()
#尝试调用函数,继续完成后面的代码
第二关
# 那么定义函数时要注意函数的格式
# 补充完成下面的代码,完成函数的定义
#定义函数
def get_item():
Dev.step(-5)
Dev.step(5)
get_item()
Spaceship.step(2)
get_item()
Spaceship.step(3)
Spaceship.turnLeft()
Spaceship.step(1)
get_item()
Spaceship.turnRight()
Spaceship.step(2)
get_item()
Spaceship.step(4)
Spaceship.turnLeft()
Spaceship.step()
get_item()
第三关
# 找到地图中重复的部分,定义函数,根据需要完成函数的调用
# 定义函数时,函数的名称可以自行定义,函数的名称可以由数字、字母和下划线( _ )组成,但是数字不能开头
# 例如函数的名字可以为:func、main_1、get666、F4、Fa2c等
# 注意定义函数时,别忘记关键字def以及函数名后的括号和冒号
def func():
#注意函数内部的代码需要缩进4格
Dev.step(-4)
Dev.step(4)
func()
Spaceship.step(2)
Spaceship.turnLeft()
Spaceship.step()
func()
Spaceship.turnLeft()
for i in range(2):
Spaceship.turnLeft()
Spaceship.step(2)
func()
Spaceship.step(3)
Spaceship.turnLeft()
Spaceship.step()
func()
第四关
# 找到地图中重复的部分,定义函数,根据需要完成函数的调用
def func():
Dev.step(3)
Dev.step(-3)
func()
Spaceship.step(3)
Spaceship.turnLeft()
Spaceship.step(3)
Spaceship.turnRight()
func()
Spaceship.step(2)
Dev.turnRight()
Spaceship.turnRight()
func()
Spaceship.step(3)
func()
第五关
# 找到地图中重复的部分,定义函数,根据需要完成函数的调用
def func():
for i in range(4):
Dev.step(2)
Dev.turnLeft()
Spaceship.step(2)
func()
Spaceship.turnRight()
Spaceship.step(4)
Dev.turnRight()
func()
Spaceship.turnRight()
Spaceship.step(3)
Spaceship.turnLeft()
Spaceship.step(2)
Dev.turnRight()
func()
第六关
# 找到地图中重复的部分,定义函数,根据需要完成函数的调用
def func():
Dev.turnLeft()
Dev.step(2)
Dev.turnLeft()
for j in range(4):
Dev.step()
Dev.turnRight()
Dev.step()
Dev.turnRight()
Dev.step(-2)
Dev.turnRight()
Dev.step(2)
func()
Dev.step(5)
func()
Dev.step(4)
func()
Dev.step(5)
func()
Dev.step(2)
第七关
# 找到地图中重复的部分,定义函数,根据需要完成函数的调用
def func():
Dev.step(3)
Dev.turnLeft()
Dev.step(2)
Dev.turnRight()
Spaceship.step(2)
Dev.step(-3)
func()
Spaceship.step(3)
func()
Spaceship.step(2)
func()
Spaceship.step(4)
func()
第八关
# 找到地图中重复的部分,定义函数,根据需要完成函数的调用
def func():
Dev.step(3)
Dev.turnRight()
Dev.step(2)
Dev.turnLeft()
Spaceship.step(2)
Dev.step(-3)
func()
Spaceship.step()
Spaceship.turnRight()
Spaceship.step()
Dev.turnRight()
func()
Spaceship.turnRight()
Spaceship.step(3)
Dev.turnRight()
func()
Spaceship.step()
第九关
# 找到地图中重复的部分,定义函数,根据需要完成函数的调用
def func():
Dev.step(4)
Dev.turnRight()
Dev.step(2)
Dev.turnLeft()
Spaceship.step(2)
Spaceship.turnLeft()
Spaceship.step()
Dev.step(-3)
func()
Spaceship.turnLeft()
for i in range(2):
Spaceship.turnLeft()
Spaceship.step(2)
func()
Spaceship.turnLeft()
for i in range(2):
Spaceship.turnLeft()
Spaceship.step(1+2*i)
func()
Spaceship.turnRight()
Spaceship.step(2)
func()