用户:
嗯哼君 渠源查看:15 回复:15 评论:15 创建时间:2020-05-02T23:09:52
先放出成品吧:https://box3.codemao.cn/content/1/9302
根据吉吉喵的《圈地之王》改编而来,不过都是我一个人写的代码
主要的难点有两个地方:①实现尾迹的长短变化;②实现杀喵敌人的变化
先放上源代码吧,很多没用的注释,删去就行:
console.log("Link Start!");
const SECONDS_PER_TICK = 1000 / 128;
const ROUND_TIME = 200 * SECONDS_PER_TICK;//每局游戏时长
const MIN_PLAYERS = 2;//玩家数量下限
const BLACK_TIME = 250 //刷新黑方块的时间,默认一秒钟4个
const GRID_X = 128;
const GRID_Z = 128;
var t;
world.gravity *= 10;
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, },
];
world.onPlayerJoin(({ entity }) => {
entity.player.Len = 1
//console.log(entity.player.name + "的Len初始化为 : " + entity.player.Len)
entity.player.All = []
//console.log(entity.player.name + "的All初始化为 : " + entity.player.Len)
});
// 辅助函数,用于随机打乱一个数组
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 () {
// 将地面初始化,全部变成white
for (let j = 0; j < GRID_Z; ++j) {
for (let i = 0; i < GRID_X; ++i) {
voxels.setVoxel(i, 8, j, 'white');
}
}
// 等待玩家进入
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('游戏开始!');
putb()
// 每局都随机打乱队伍
const players = shuffle(world.querySelectorAll('player'));
// 设置玩家的颜色、分值和初始位置
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;
//新加了一个v的私人变量,用于下面的判断
p.player.v = t.v;
//初始化分数
p.player.Len = 1
//初始化身体
p.player.All = []
}
// 获得初始团队
const teams = players.map((p, i) => {
//console.log("t初始化开始");
t = TEAMS[i % TEAMS.length]
setTeam(p, t);
//console.log("t : " + t);
return t;
})
//console.log("t初始化完毕");
//当玩家接触到地面任意方块时,更新该方块的颜色
const contactHandler = world.onVoxelContact(({voxel, entity, x, y, z }) => {
//获得该玩家的索引idx:从所有的玩家列表中获得第一个(唯一一个)符合条件的玩家)
//调用私人变量时要用entity.player.xxx
const idx = players.indexOf(entity);
if (idx < 0) {
//indexOf返回-1时表明未找到
return;
}
//console.log(entity.player.name + "的颜色为" + entity.player.v);
if (voxel === voxels.id('black')){
//如果染色的方块为black(食物)
entity.player.Len ++ ;
//console.log(entity.player.name + "的Len增加为 : " + entity.player.Len)
//向列表末尾添加染色方块的位置
entity.player.All[entity.player.All.length] = [x, y, z];
}
else if (voxel === voxels.id('white')){
//如果染色的方块为white(空地)
//向列表末尾添加染色方块的位置
entity.player.All[entity.player.All.length] = [x, y, z];
//console.log(entity.player.name + "的列表增加 : " + entity.player.All[0][0] + " " +entity.player.All[0][1] + " " + entity.player.All[0][2])
//将列表第一项填充为white(末尾减少)
//console.log("填充前的列表" + entity.player.All)
voxels.setVoxelId(entity.player.All[0][0], entity.player.All[0][1], entity.player.All[0][2], voxels.id('white'));
//console.log("填充后的方块" + voxels.name(voxels.getVoxel(entity.player.All[0][0], entity.player.All[0][1], entity.player.All[0][2])));
//console.log("填充后的列表" + entity.player.All)
if (entity.player.All.length != 1){
//喵也不知道哪来的BUG
entity.player.All.shift()
}
//第一项:entity.player.All[-(entity.player.Len + 1)]
//console.log("删除后的列表" + entity.player.All)
}
else if (voxel === entity.player.v){
//console.log(entity.player.name + "踩到了自己颜色")
//如果染色的方块为自己的颜色(遗留痕迹)
//向列表末尾添加染色方块的位置
entity.player.All[entity.player.All.length] = [x, y, z];
//console.log(entity.player.name + "的列表增加 : " + entity.player.All[0][0] + " " +entity.player.All[0][1] + " " + entity.player.All[0][2])
//将列表第一项填充为white(末尾减少)
//console.log("填充前的列表" + entity.player.All)
voxels.setVoxelId(entity.player.All[0][0], entity.player.All[0][1], entity.player.All[0][2], voxels.id('white'));
//console.log("填充后的方块" + voxels.name(voxels.getVoxel(entity.player.All[0][0], entity.player.All[0][1], entity.player.All[0][2])));
//console.log("填充后的列表" + entity.player.All)
if (entity.player.All.length != 1){
//喵也不知道哪来的BUG
entity.player.All.shift()
}
//第一项:entity.player.All[-(entity.player.Len + 1)]
//console.log("删除后的列表" + entity.player.All)
}
else{
//如果染色的方块不为black、white或自己的颜色(其他人)
//获取目标(将要被染色)的方块
mubiao = voxels.getVoxel(x,y,z);
//console.log(entity.player.name + "踩到了" + voxels.name(mubiao));
//console.log("踩到的方块:"+ voxel)
//console.log("自己的颜色:" + entity.player.v)
for(i=0; i<TEAMS.length; i++){
//遍历查找该队
if (mubiao === TEAMS[i].v){
//console.log("当前共喵 + players.length + "人")
for(let j=0; j<players.length; j++){
//遍历查找属于这个队伍的玩家
/*console.log("j为" + j)
console.log("players为" + players)
console.log("players[j].isPlayer为" + players[j].isPlayer)
console.log("players[j].player.name为" + players[j].player.name)
console.log("players[j].player.v为" + players[j].player.v)*/
//players[j].cameraMode = 'fps'
if (players[j].player.v == mubiao){
//console.log(players[j].player.name + "与目标方块颜色相匹配")
//如果颜色相匹配
//players[j].player.forceRespawn()
players[j].position.x = Math.random() * GRID_X;
players[j].position.y = 30;
players[j].position.z = Math.random() * GRID_Z;
//GO DIETH!
restart(players[j])
};
};
};
};
//向列表末尾添加染色方块的位置
entity.player.All[entity.player.All.length] = [x, y, z];
//console.log(entity.player.name + "的列表增加 : " + entity.player.All[0][0] + " " +entity.player.All[0][1] + " " + entity.player.All[0][2])
//将列表第一项填充为white(末尾减少)
//console.log("填充前的列表" + entity.player.All)
voxels.setVoxelId(entity.player.All[0][0], entity.player.All[0][1], entity.player.All[0][2], voxels.id('white'));
//console.log("填充后的方块" + voxels.name(voxels.getVoxel(entity.player.All[0][0], entity.player.All[0][1], entity.player.All[0][2])));
//console.log("填充后的列表" + entity.player.All)
if (entity.player.All.length != 1){
//喵也不知道哪来的BUG
entity.player.All.shift()
}
//第一项:entity.player.All[-(entity.player.Len + 1)]
//console.log("删除后的列表" + entity.player.All)
}
const t = teams[idx];
voxels.setVoxelId(x, y, z, t.v);
});
world.onVoxelSeparate(({entity}) => {
//当有玩家停止接触方块(即有可能掉出虚空)
if(entity && entity.isPlayer){
//console.log("entity.position['y']为" + entity.position['y'])
//console.log("entity.position['y'] < 9为" + (entity.position['y'] < 9))
if(entity.position['y'] < 9){
//如果y坐标小于9(玩家是10)
console.log(entity.player.name + "y坐标小于9")
restart(entity);
//GO DEATH!
}
}
});
// 新玩家加入,会被随机分配到一个队伍
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;
//每隔2秒会出现一个方块(食物)
// 检测游戏是否进行中
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;
//返回一布尔值,是否游戏中
}
// 当游戏结束时
while (gameInProgress()) {
// 等待 1 Tick
await world.nextTick();
}
// 计算各队伍的分数
for (let i = 0; i < TEAMS.length; ++i) {
//遍历各队伍计算分数
for (let j = 0; j < players.length; j++){
//遍历各玩家,找出这一个队伍的
//console.log("players[j].player.v为" + players[j].player.v)
//console.log("TEAMS[i].v为" + TEAMS[i].v)
if (players[j].player.v == TEAMS[i].v){
//如果该玩家颜色与该队伍颜色一样
TEAMS[i].score += players[j].player.Len;
console.log(players[j].player.name + "的队伍是" + voxels.name(players[j].player.v) + ",自己的分数是" + players[j].player.Len + "队伍的分数是" + TEAMS[i].score)
//console.log(TEAMS)
//将队伍分数加上该玩家分数
}
}
//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);
// 宣布获胜队伍
/*for(let i=0; i<players.length; i++){
console.log(players[i].player.name + "的队伍是" + voxels.player.name(players[i].v) + ",分数是" + players[i].player.Len)
}*/
TEAMS.sort((a, b) => b.score - a.score);
world.say(`恭喜 ${TEAMS[0].n} 胜利!!`);
await sleep(10000);
for(let i=0;i<TEAMS.length;i++ ){
TEAMS[i].score = 0
}
}
// 无限循环游戏
async function gameLoop () {
while (true) {
await startGame();
}
}
async function putb(){
//刷黑方块
voxels.setVoxelId((Math.random() * GRID_X), 8, (Math.random() * GRID_Z), voxels.id('black'));
await sleep(BLACK_TIME)
putb()
};
async function restart(f_player){
console.log(f_player.player.name + "喵了")
f_player.player.Len = 1;
f_player.player.All = []
for(let ij=0; ij<GRID_X; ij++){
//通过遍历还原所有方块
//骂骂咧咧:console.log("开始还原初始化")
for(let ji=0; ji<GRID_Z; ji++){
//c我电脑要炸了,*龙门粗口*:console.log(ij+ "," +ji)
if(voxels.getVoxel(ij,8,ji) == f_player.player.v){
//如果该方块为该玩家的颜色
//s**t:console.log("开始还原颜色")
voxels.setVoxelId(ij, 8, ji, voxels.id('black'));
};
};
};
}
gameLoop();
建议配合原《圈地之王》代码查看异同之处,下面说一下基本原理:
①将玩家行走过的方块全部添加到一个列表中(All),只要根据他目前的长短(Len),还原指定的方块即可
②当自己触碰到其他颜色的方块时,遍历全方块查找该颜色,替换为black
能看懂最好,看不懂就看不懂吧
当然你要有什么不懂的地方也可以在评论区里问我,我尽可能的回答
以下是几点改进方向,也给想要二次创作的人们提出几点建议:
①可不可以更改一下玩家的视角,更加真实地还原这个游戏
②可不可以加一些障碍(比如地雷、树木等),增加难度与趣味性
③可不可以让玩家尾迹被触碰后,只是让自己的方块还原,而不是让一队的人都跟着陪葬
④可不可以局部代码优化(比如本人就用不惯forEach,基本都是用的for……)
⑤……
(PS:如有意见,还请多多提出啊,我们一起改进交流~)
嗯哼君 渠源
最终精简版:
console.log("Link Start!");//桐老爷专属水印[doge]
const SECONDS_PER_TICK = 1000 / 128;
const ROUND_TIME = 200 * SECONDS_PER_TICK;//每局游戏时长
const MIN_PLAYERS = 2;//玩家数量下限
const BLACK_TIME = 250 //刷新黑方块的时间,默认一秒钟4个
const GRID_X = 128;//两个都是地图边界
const GRID_Z = 128;
var t;
world.gravity *= 10;
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, },
];
world.onPlayerJoin(({ entity }) => {
//初始化各参数
entity.player.Len = 1
//因为刚开始玩家就有一条尾迹,所以说不是0而是1
entity.player.All = []
//因为尾迹的添加是在触碰后再添加的,所以说目前为空列表
});
// 辅助函数,用于随机打乱一个数组(未魔改)
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 () {
// 将地面初始化,全部变成white
for (let j = 0; j < GRID_Z; ++j) {
for (let i = 0; i < GRID_X; ++i) {
voxels.setVoxel(i, 8, j, 'white');
}
}
// 等待玩家进入(未魔改)
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('游戏开始!');
putb()
//putb()即"put black",用于随机放置黑色方块
// 每局都随机打乱队伍(喵)
const players = shuffle(world.querySelectorAll('player'));
// 设置玩家的颜色、分值和初始位置
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;
//新加了一个v的私人变量(储存他的颜色),用于下面的队伍判断
p.player.v = t.v;
//初始化分数(似乎有点多余了?但也不敢随意删去hhhc)
p.player.Len = 1
//初始化身体(似乎有点多余了?但也不敢随意删去hhhc)
p.player.All = []
}
// 获得初始团队(未魔改……吧)
const teams = players.map((p, i) => {
t = TEAMS[i % TEAMS.length]
setTeam(p, t);
return t;
})
//当玩家接触到地面任意方块时,更新该方块的颜色
const contactHandler = world.onVoxelContact(({voxel, entity, x, y, z }) => {
//获得该玩家的索引idx:从所有的玩家列表中获得第一个(唯一一个)符合条件的玩家)
//调用私人变量时要用entity.player.xxx
const idx = players.indexOf(entity);
if (idx < 0) {
//indexOf返回-1时表明未找到
return;
}
if (voxel === voxels.id('black')){
//如果染色的方块为black(食物)
entity.player.Len ++ ;
//长度增加
entity.player.All[entity.player.All.length] = [x, y, z];
//向列表末尾添加染色方块的位置
//因为他会让玩家变长,所以说不用删除末尾的方块(举个例子:大家都在退步,你不退步就是进步)
}
else if (voxel === voxels.id('white')){
//如果染色的方块为white(空地)
//向列表末尾添加染色方块的位置
entity.player.All[entity.player.All.length] = [x, y, z];
//将列表第一项填充为white(末尾减少)
voxels.setVoxelId(entity.player.All[0][0], entity.player.All[0][1], entity.player.All[0][2], voxels.id('white'));
if (entity.player.All.length != 1){
//喵也不知道哪来的BUG
entity.player.All.shift()
}
//之前用的第一项,看不懂忽略即可:entity.player.All[-(entity.player.Len + 1)]
}
else if (voxel === entity.player.v){
//如果染色的方块为自己的颜色(遗留痕迹/尾迹)
//向列表末尾添加染色方块的位置
entity.player.All[entity.player.All.length] = [x, y, z];
//将列表第一项填充为white(尾迹减少)
voxels.setVoxelId(entity.player.All[0][0], entity.player.All[0][1], entity.player.All[0][2], voxels.id('white'));
if (entity.player.All.length != 1){
//同上,喵也不知道哪来的BUG
entity.player.All.shift()
}
//同上,第一项:entity.player.All[-(entity.player.Len + 1)]
else{
//如果染色的方块不为black、white或自己的颜色(其他人)
//获取目标(将要被染色)的方块
mubiao = voxels.getVoxel(x,y,z);
for(i=0; i<TEAMS.length; i++){
//遍历查找该队
if (mubiao === TEAMS[i].v){
for(let j=0; j<players.length; j++){
//遍历查找属于这个队伍的玩家
if (players[j].player.v == mubiao){
//如果颜色相匹配(与上方的player.v相对应)
players[j].position.x = Math.random() * GRID_X;
players[j].position.y = 30;
players[j].position.z = Math.random() * GRID_Z;
//GO DIETH!
restart(players[j])
};
};
};
};
//向列表末尾添加染色方块的位置
entity.player.All[entity.player.All.length] = [x, y, z];
//将列表第一项填充为white(末尾减少)
if (entity.player.All.length != 1){
//喵也不知道哪来的BUG×3
entity.player.All.shift()
}
}
const t = teams[idx];
voxels.setVoxelId(x, y, z, t.v);
});
world.onVoxelSeparate(({entity}) => {
//当有玩家停止接触方块(即有可能掉出虚空)
if(entity && entity.isPlayer){
if(entity.position['y'] < 9){
//如果y坐标小于9(玩家是10)
restart(entity);
//GO DEATH!
}
}
});
// 新玩家加入,会被随机分配到一个队伍(未魔改)
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;
//返回一布尔值,是否游戏中
}
// 当游戏结束时(未魔改)
while (gameInProgress()) {
// 等待 1 Tick
await world.nextTick();
}
// 计算各队伍的分数(其实就是把一队的玩家的Len加起来)
for (let i = 0; i < TEAMS.length; ++i) {
//遍历各队伍计算分数
for (let j = 0; j < players.length; j++){
//遍历各玩家,找出这一个队伍的
if (players[j].player.v == TEAMS[i].v){
//如果该玩家颜色与该队伍颜色一样
TEAMS[i].score += players[j].player.Len;
//将队伍分数加上该玩家分数
}
}
}
// 清除事件处理器(未魔改,似乎还要把putb.cancel()加上的,但咱也不敢动(小心翼翼))
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);
for(let i=0;i<TEAMS.length;i++ ){
TEAMS[i].score = 0
}
}
// 无限循环游戏(未魔改)
async function gameLoop () {
while (true) {
await startGame();
}
}
async function putb(){
//刷黑方块
voxels.setVoxelId((Math.random() * GRID_X), 8, (Math.random() * GRID_Z), voxels.id('black'));
await sleep(BLACK_TIME)
putb()
//无限套娃喵好!(滑稽)
};
async function restart(f_player){
//用于处理当玩家喵后的还原(包括掉下虚空与被别人触碰尾迹,似乎可以用forEach重构的)
console.log(f_player.player.name + "喵了")
f_player.player.Len = 1;
f_player.player.All = []
for(let ij=0; ij<GRID_X; ij++){
//通过遍历还原所有方块
for(let ji=0; ji<GRID_Z; ji++){
//c我电脑要炸了,*龙门粗口*,谁想试试就把注释符号删去:console.log(ij+ "," +ji)
if(voxels.getVoxel(ij,8,ji) == f_player.player.v){
//如果该方块为该玩家的颜色
//s**t:console.log("开始还原颜色")
voxels.setVoxelId(ij, 8, ji, voxels.id('black'));
};
};
};
}
gameLoop();点赞2
评论
墨染云天imgsrc="https://static.codemao.cn/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E7%82%B9%E8%B5%9E.gif"alt="emotion_编程猫_点赞"
点赞0
评论