猫史档案馆


潘泊言

潘泊言

Lv.1

获赞:100收藏:24浏览:1013作品收藏:17
回复帖子评论
上一页2 页 / 共 2下一页

黑人盗墓,黑人抬棺 中回复

抽一个吧!中奖会获得我做的系统代码哟!

2020-04-21T16:19:57 点赞:0

【🤩快来推荐!】帮你的合作者拿编辑器! 中回复

我推荐萌萌小猪猪

链接:https://shequ.codemao.cn/user/7083772

TA在“灯光跑酷”帮助我了做代码

TA很想有权限

我相信TA以后会继续做出很好的作品的!

2020-04-24T19:05:12 点赞:0

哇快看不是P的 中回复

NO

2020-04-30T10:39:35 点赞:0

box3圈地之王代码! 中回复

const SECONDS_PER_TICK = 1000 / 128; 
const ROUND_TIME = 200 * SECONDS_PER_TICK;//每局游戏时长
const MIN_PLAYERS = 2;//玩家数量下限
const GRID_X = 128;
const GRID_Z = 128;

const TEAMS = [
    { r: 1, g: 0, b: 0, v: voxels.id('red_light'),      n: '红队',       score: 0, },
    { r: 0, g: 1, b: 0, v: voxels.id('green_light'),    n: '绿队',     score: 0, },
    { r: 0, g: 0, b: 1, v: voxels.id('blue_light'),     n: '蓝队',      score: 0, },
    { r: 1, g: 0, b: 1, v: voxels.id('purple'),         n: '紫队',   score: 0, },
    { r: 0, g: 1, b: 1, v: voxels.id('indigo_light'),   n: '靛蓝队',      score: 0, },
    { r: 1, g: 1, b: 0, v: voxels.id('yellow_light'),   n: '黄队',    score: 0, },
];

// 辅助函数,用于随机打乱一个数组
function shuffle (array) {
    for (let i = 1; i < array.length; ++i) {
        const temp = array[i];
        const x = (Math.random() * (i + 1)) | 0;
        array[i] = array[x];
        array[x] = temp;
    }
    return array;
}

async function startGame () {
    // 将地面初始化,全部变成不锈钢
    for (let j = 0; j < GRID_Z; ++j) {
        for (let i = 0; i < GRID_X; ++i) {
            voxels.setVoxel(i, 8, j, 'stainless_steel');
        }
    }
    
    // 等待玩家进入
    while (world.querySelectorAll('player').length < MIN_PLAYERS) {
        world.say('等待其他玩家加入...');
        await sleep(1000);
        world.say('2个玩家立即开始');
        await world.nextPlayerJoin();
    }

    // 足够玩家加入后,就开始倒计时
    for (let i = 3; i > 0; --i) {
        world.say(i + '...');
        await sleep(1000);
    }
    world.say('去圈地吧!看谁是圈地之王?');
    
    // 每局都随机打乱队伍
    const players = shuffle(world.querySelectorAll('player'));
    shuffle(TEAMS);
    
    // 设置玩家的颜色和初始位置
    function setTeam (p, t) {
        p.player.color.r = t.r;
        p.player.color.g = t.g;
        p.player.color.b = t.b;
        p.position.x = Math.random() * GRID_X;
        p.position.y = 30;
        p.position.z = Math.random() * GRID_Z;
    }

    // 获得初始团队
    const teams = players.map((p, i) => {
        const t = TEAMS[i % TEAMS.length]
        setTeam(p, t);
        return t;
    })
    
    //当玩家接触到地面任意方块时,更新该方块的颜色
    const contactHandler = world.onVoxelContact(({ entity, x, y, z }) => {
        const idx = players.indexOf(entity);
        if (idx < 0) {
            return;
        }
        const t = teams[idx];
        voxels.setVoxelId(x, y, z, t.v);
    });
    
    // 新玩家加入,会被随机分配到一个队伍
    const joinHandler = world.onPlayerJoin(({entity}) => {
        const t = TEAMS[(Math.random() * teams.length) | 0];
        setTeam(entity, t);
        teams.push(t);
        players.push(entity)
    });
    
    // 每局游戏会在计时到达时结束。
    const endTick = world.currentTick + ROUND_TIME;
    
    // 检测游戏是否进行中
    function gameInProgress () {
        let numActivePlayers = 0;
        for (let i = 0; i < players.length; ++i) {
            if (!players[i].destroyed) {
                numActivePlayers += 1;
            }
        }
        return world.currentTick < endTick && numActivePlayers >= MIN_PLAYERS;
    }
    
    // 通过深度优先搜索,将圈起来的地自动填色
    const visited = new Uint8Array(GRID_X * GRID_Z)
    const toVisit = []
    function mark (i, j, team) {
        const idx = i + j * GRID_X;
        if (i < 0 || i >= GRID_X ||
            j < 0 || j >= GRID_Z ||
            visited[idx] ||
            voxels.getVoxel(i, 8, j) === team) {
            return;
        }
        visited[idx] = true;
        toVisit.push(i, j);
    }
    function fillHoles (team) {
        visited.fill(0);
        for (let i = 0; i < Math.max(GRID_X, GRID_Z); ++i) {
            mark(i, 0, team);//更正了,将8改成了0
            mark(0, i, team);
            mark(GRID_X - 1, i, team);
            mark(i, GRID_Z - 1, team);
        }
        
        while (toVisit.length > 0) {
            const z = toVisit.pop() | 0;
            const x = toVisit.pop() | 0;
            mark(x - 1, z, team);
            mark(x + 1, z, team);
            mark(x, z - 1, team);
            mark(x, z + 1, team);
        }
        
        let score = 0;
        for (let j = 0; j < GRID_Z; ++j) {
            for (let i = 0; i < GRID_X; ++i) {
                const idx = i + j * GRID_X;
                if (!visited[idx]) {
                    voxels.setVoxelId(i, 8, j, team);
                    score += 1;
                }
            }
        }
        return score;
    }
    
    // 当游戏运行时
    while (gameInProgress()) {
        // 等待 1 Tick
        await world.nextTick();
        
        // 计算各队伍的分数
        for (let i = 0; i < TEAMS.length; ++i) {
            const c = TEAMS[i].score = fillHoles(TEAMS[i].v);
            
            // 如果有团队圈完全场,那么直接胜出
            if (c >= GRID_X * GRID_Z) {
                break;
            }
        }
    }
    
    // 清除事件处理器
    contactHandler.cancel();
    joinHandler.cancel();

    // 重置玩家颜色
    for (let i = 0; i < players.length; ++i) {
        const c = players[i].player.color;
        c.r = c.g = c.b = 1;
    }
    world.say('本局游戏结束!');
    await sleep(5000);
    
    // 宣布获胜队伍
    TEAMS.sort((a, b) => b.score - a.score);
    world.say(`恭喜 ${TEAMS[0].n} 成为圈地之王!!`);
    await sleep(10000);
}

// 无限循环游戏
async function gameLoop () {
    while (true) {
        await startGame();    
    }
}
gameLoop();

2020-05-05T18:21:34 点赞:0

【有奖活动】一起来用方言来为Kitten打CALL!! 中回复

使KITTEN

1

KITTEN

使KITTEN

KITTEN

TA

2020-05-21T19:54:56 点赞:3

滚动的天空工作室招人了 中回复

你叫我进0星工作室,不可能!(我被别人说过0星,他们不进,我很气,所以这样说)

2020-05-22T09:38:44 点赞:0

找个师父,我太菜了 中回复

2020-05-22T09:40:16 点赞:0

[box3]技巧分享:如何替换世界中指定的方块? 中回复

emotion_编程猫_翻白眼好...好,我学费了emotion_泪

2020-06-08T09:11:59 点赞:0

谁有图?? 中回复

https://box3create.codemao.cn/games/cd1d61c68c98af80d170

https://box3create.codemao.cn/games/cd1d61c68c98af80d170

https://box3create.codemao.cn/games/cd1d61c68c98af80d170

重要的链接发三遍

2020-06-08T22:05:29 点赞:0

【html代码】:下载代码 中回复

!!

2020-06-24T17:05:44 点赞:0

[水作] Box3客户端 中回复

emotion_编程猫_加油emotion_猪头

2020-06-27T19:45:11 点赞:0

[水作] Box3客户端 中回复

emotion_编程猫_加油emotion_猪头

2020-06-27T19:45:16 点赞:0

【Python作品分享】新的作品 中回复

额~

2020-07-06T11:14:13 点赞:0

【Python作品分享】新的作品 中回复

牛!

2020-07-06T11:14:28 点赞:0

源码精灵的音乐在哪里下载的啊? 中回复

好奇,lz有

2020-07-13T12:25:49 点赞:0

我想要权限 中回复

2020-07-25T10:17:14 点赞:1

我想要权限 中回复

按申请编辑

 

2020-07-25T10:18:37 点赞:0

【咸鱼报刊】简易数据库(1)认识数据库 中回复

emotion_编程猫_搓头

2020-10-13T19:21:58 点赞:0

【编程一小时】 pyinstaller的使用教程 中回复

收藏emotion_编程猫_点赞

2020-12-10T17:37:40 点赞:0

Javascript教程 0.What is Javascript 和 html??? 中回复

2021-03-13T13:37:04 点赞:0

【智编云】正式公测!域名免费注册!主机免费申请! 中回复

我要云主机,我要域名panboyan.tk

2021-05-23T22:53:24 点赞:0

box3图形化链接谁知道? 中回复

某某喵:有人做了图形化,我不做了,就是玩~

2021-05-26T22:10:09 点赞:0