Array
(
    [query] => Array
        (
            [match] => Array
                (
                    [text] => Array
                        (
                            [query] => Tomorrow - 地形生成(1)
                        )

                )

        )

    [highlight] => Array
        (
            [fields] => Array
                (
                    [text] => stdClass Object
                        (
                        )

                )

            [pre_tags] => #em#
            [post_tags] => #/em#
        )

    [size] => 8
    [from] => 0
)
Array
(
)
Array
(
    [query] => Array
        (
            [match] => Array
                (
                    [text] => Array
                        (
                            [query] => Tomorrow - 地形生成(1)
                            [operator] => or
                            [minimum_should_match] => 2<55%
                        )

                )

        )

    [highlight] => Array
        (
            [fields] => Array
                (
                    [text] => stdClass Object
                        (
                        )

                )

            [pre_tags] => #em#
            [post_tags] => #/em#
        )

    [size] => 8
    [from] => 0
)
Array
(
)
Array
(
    [query] => Array
        (
            [match] => Array
                (
                    [text] => Array
                        (
                            [query] => Tomorrow - 地形生成(1)
                        )

                )

        )

    [highlight] => Array
        (
            [fields] => Array
                (
                    [text] => stdClass Object
                        (
                        )

                )

            [pre_tags] => #em#
            [post_tags] => #/em#
        )

    [size] => 8
    [from] => 8
)
Array
(
)
Array
(
    [query] => Array
        (
            [match] => Array
                (
                    [text] => Array
                        (
                            [query] => Tomorrow - 地形生成(1)
                            [operator] => or
                            [minimum_should_match] => 2<45%
                        )

                )

        )

    [highlight] => Array
        (
            [fields] => Array
                (
                    [text] => stdClass Object
                        (
                        )

                )

            [pre_tags] => #em#
            [post_tags] => #/em#
        )

    [size] => 8
    [from] => 8
)
Array
(
)
Array
(
    [query] => Array
        (
            [match] => Array
                (
                    [title] => Array
                        (
                            [query] => Tomorrow - 地形生成(1)
                        )

                )

        )

    [highlight] => Array
        (
            [fields] => Array
                (
                    [title] => stdClass Object
                        (
                        )

                )

            [pre_tags] => #em#
            [post_tags] => #/em#
        )

    [from] => 0
)
Array
(
)
Array
(
    [query] => Array
        (
            [bool] => Array
                (
                    [must] => Array
                        (
                            [0] => Array
                                (
                                    [match] => Array
                                        (
                                            [title] => Array
                                                (
                                                    [query] => Tomorrow - 地形生成(1)
                                                )

                                        )

                                )

                        )

                    [must_not] => Array
                        (
                            [0] => Array
                                (
                                    [term] => Array
                                        (
                                            [cate1] => 电子书籍
                                        )

                                )

                        )

                )

        )

    [highlight] => Array
        (
            [fields] => Array
                (
                    [title] => stdClass Object
                        (
                        )

                )

            [pre_tags] => #em#
            [post_tags] => #/em#
        )

    [size] => 5
    [from] => 0
)
Array
(
)
Tomorrow - 地形生成(1) - 爱码网
tesla-173

沙盒游戏-地形生成(1)

原理很简单,请不要喷。


效果展示

种子输入框 种子输入框

种子为12345的地形 种子为12345的地形

种子为23456的地形 种子为23456的地形


代码展示

globalvar map

random_set_seed(real(get_string("Seed: ","")))//获取种子

for(i=0;i<=room_width/32;i+=1)
{
    for(j=0;j<=room_height/32;j+=1)
    {
        map[i,j]=0;//初始化数组
    }
}

for(i=0;i<=room_width/32;i+=1)
{
    for(j=0;j<=room_height/32;j+=1)
    {
        if(random_range(0,100)>=65) map[i,j]=1;//按概率填充房间
    }
}

for(n=1;n<=room_height/32;n+=1)
{
    for(i=0;i<=room_width/32;i+=1)
    {
        for(j=0;j<=room_height/32-1;j+=1)
        {
            if(map[i,j+1]==0&&map[i,j]==1)
            {
                map[i,j+1]=map[i,j];
                map[i,j]=0;//方块重力处理
            }
        }
    }
}

for(i=0;i<=room_width/32;i+=1)
{
    for(j=0;j<=room_height/32;j+=1)
    {
       if(map[i,j]==1) instance_create(i*32,j*32,obj_block)//在房间中按地图创建物体
    }
}

代码讲解

使用的坐标系

0 1 2 3 4 5 6 7 8 9 ...  
1  
2  
3  
4  
5  
6  
7  
8  
9  
...  

随机数

random_set_seed():置随机数种子
random_range:生产随机数

方块重力处理

map[i,j]==1是确保正在处理的位置有方块
map[i,j+1]==0是确保正在处理的位置下面一格没有方块

如果符合条件的话

map[i,j+1]=map[i,j]//清空当前格
map[i,j]=0;//填充下面格

(即当前方块下落)

注意一下,这里没有用map[i,j+1]=1,让日后二次开发有更多方块。

放置方块

其实它的本质就是创建对象。

instance_create(x,y,obj)的用法自己看F1.

结语

这是我做的第一个记录,希望我的沙盒游戏也能坚持做下去!

说明

本文原作者Tesla,首发于Tesla-173的个人博客

Tesla本人转载至Gamemaker开发者之家

未经授权禁止转载。

如需转载请联系2230229670@qq.com。

 

PS:刚才在Gamemaker开发者之家(https://gamemake.org/thread-2148.htm)发了,看到一个回帖:

摸魔抹莫  发表于 2020-04-18 07:10:21
 0  3

已阅读,楼主的代码思路以及讲解较清晰,但以下几点希望改进: ①下述用法

real(get_string(...))
GameMaker Language

如果输入有误,导致输入的字符串不能被转换成实数,则会引发错误。推荐自己实现real()函数以做到可控错误处理。 ②二维数组map[][]中存储的值都是逻辑值(布尔值),这种情况下用true和false来代替1和0能使代码更具有可读性。在判断时也无需写成==0或者==1,直接写或者用逻辑取反符号!更合理。 希望楼主继续进步!

 

我觉得还是应该解释一下。
第一点我之后是准备自己写编辑框,只允许检测0-9的按键。
第二点是因为沙盒游戏不仅存在有(1)和没有(2)两种方块,使用1是因为现在暂时没有更多方块,以后会采用类似方块id的形式存储地图,所以要用real型的数组。正因如此必须使用表达式来判断。

沙盒游戏-地形生成(1)

分类:

技术点:

相关文章: