在这一关,让我们整体来回顾一下之前学习的内容,把砖块、传送门、阶梯都放到需要的位置上。
我们看看地图
代码如下:
let character = Character()
let Block1 = Block()
let Block2 = Block()
let Block3 = Block()
let Block4 = Block()
let Block5 = Block()
let Block6 = Block()
let Block7 = Block()
let Block8 = Block()
let Block9 = Block()
let Block10 = Block()
let Block11 = Block()
let Portal1 = Portal(color: #colorLiteral(red: 0.3411764705882353, green: 0.6235294117647059, blue: 0.16862745098039217, alpha: 1.0))//系统内的颜色是可以点选的。不用这样子写。
let Portal2 = Portal(color: #colorLiteral(red: 0.9254901960784314, green: 0.23529411764705882, blue: 0.10196078431372549, alpha: 1.0))//系统内的颜色是可以点选的。不用这样子写。
let Portal3 = Portal(color: #colorLiteral(red: 0.9607843137254902, green: 0.7058823529411765, blue: 0.2, alpha: 1.0))//系统内的颜色是可以点选的。不用这样子写。
let Portal4 = Portal(color: #colorLiteral(red: 0.17647058823529413, green: 0.011764705882352941, blue: 0.5607843137254902, alpha: 1.0))//系统内的颜色是可以点选的。不用这样子写。
var GNum = 0
var SNum = 0
world.place(Block1, atColumn: 1, row: 2)
world.place(Block2, atColumn: 1, row: 6)
world.place(Block3, atColumn: 5, row: 3)
world.place(Block4, atColumn: 7, row: 3)
world.place(Block5, atColumn: 7, row: 4)
world.place(Block6, atColumn: 2, row: 7)
world.place(Block7, atColumn: 3, row: 1)
world.place(Block8, atColumn: 3, row: 1)
world.place(Block9, atColumn: 5, row: 5)
world.place(Block10, atColumn: 7, row: 4)
world.place(Block11, atColumn: 3, row: 2)
world.place(Portal1, atStartColumn: 3, startRow: 2, atEndColumn: 2, endRow: 1)
world.place(Portal2, atStartColumn: 3, startRow: 1, atEndColumn: 3, endRow: 6)
world.place(Portal3, atStartColumn: 2, startRow: 7, atEndColumn: 5, endRow: 5)
world.place(Portal4, atStartColumn: 7, startRow: 3, atEndColumn: 7, endRow: 4)
world.place(character, facing: .east, atColumn: 3, row: 0)
while SNum < 6 || GNum<3 {
if character.isOnGem {
character.collectGem()
GNum += 1
}else if character.isOnClosedSwitch {
character.toggleSwitch()
SNum += 1
}else if character.isBlocked && character.isBlockedLeft {
character.turnRight()
}else if character.isBlocked && character.isBlockedRight {
character.turnLeft()
}else{
character.moveForward()
}
}
执行后:
自己想想还有没有更好的方法?可以自己试试写一下。
代码注释自己还得要加上去。