我们学会制造地形了,现在我们再进一步,

原始的代码如下:

有很多我们可以发挥的空间。我先写一点点上去,试着运行一下。
代码如下:
let allCoordinates = world.allPossibleCoordinates
var heights: [Int] = []
for i in 0...11 {
let heightsramndomInt = randomInt(from: 0, to: 9)
heights.append(heightsramndomInt)// 将随机数附加到高度中。
}
var index = 0
for coordinate in allCoordinates {
if index == heights.count {
index = 0
}
// currentHeight 储存当前索引处的高度。
var currentHeight = heights[index]
if currentHeight == 0 {
// 当 currentHeight 等于 0 时,执行一些有趣的操作。
} else {
for i in 1...currentHeight {
world.place(Block(), at: coordinate)
}
if currentHeight > 10 {
// 试点不一样的东西,如放置角色。
} else if coordinate.column >= 3 && coordinate.column < 6 {
// 试点不一样的东西,如放置水。
}
// 添加更多规则来自定你的世界。
}
index += 1
}
//很多都没有添加上,可以自己写一下。

通过 let heightsramndomInt = randomInt(from: 0, to: 9),我们可以创造一个每次都不一样的岛屿了。 randomInt(from: 0, to: 9),就是生成一个0-9之间的一个随机数。