MacOS Playgrounds 学习编程二 第四十一关 数组9-生成地形

我们过了这么多关卡,每一个砖块、海水都是系统设置好的,我们能不能自己给自己做一个?

现在机会来了。

我们把原始的地形看一下。

现在我们要做的是,造一个高低不一的岛屿。

代码如下:

var heights: [Int] = [9,7,5,3,1]

//var heights: [Int] = [randomInt(from: 0, to: 10)]

let allCoordinates = world.allPossibleCoordinates

var index = 0

for coordinate in allCoordinates {

    if index == heights.count {

        index = 0

    }

    for index in 0...heights[index] {

        world.place(Block(), at: coordinate)

        //world.place(Block(), at: coordinate)// 放置一块砖。


    }

    index = index + 1// 使索引递增。

}

执行后:

有的数字我们自己试试修改一下
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容