unity3d小游戏代码
2015年01月18日 09:42
0 点赞
0 评论
更新于 2025-11-21 14:38
最近我在学习Unity小游戏开发,整理了部分植物大战僵尸的小游戏代码,下面为大家详细展示。
僵尸移动函数
// 僵尸移动函数
function MoveZombies() {
// 若游戏失败,直接返回
if (Win === -1) {
return false;
}
// 获取所有僵尸元素
var aZom = getElementsByName('img', 'Zombies');
var ob, C, OZombie;
// 遍历所有僵尸
for (var n = 0; n < aZom.length; n++) {
ob = aZom[n];
// 获取僵尸对象
eval('OZombie=' + ob.EName);
// 判断是否取消减速
if (ob.FreeSlowTime!== '') {
if (new Date() > ob.FreeSlowTime && ob.HP > OZombie.BreakPoint) {
ob.FreeSlowTime = '';
ob.style.filter = '';
ob.Speed = OZombie.Speed;
}
}
// 若僵尸未攻击且生命值大于断点值,触发攻击
if (ob.isAttacking === 0 && ob.HP > OZombie.BreakPoint) {
ZombieAttack(ob.id);
}
// 获取僵尸的左、右受攻击点的X坐标
var LX = ob.AttackedLX;
var RX = ob.AttackedRX;
if (ob.isAttacking === 0) {
if (ob.WalkDirection === 0) {
// 向左走
ob.X -= ob.Speed;
ob.style.pixelLeft = Math.floor(ob.X);
LX -= ob.Speed;
RX -= ob.Speed;
C = GetC(LX);
switch (true) {
case C === 0:
// 走到剪草机
var jcj = document.getElementById('LawnMower' + ob.R);
if (jcj!== null) {
// 剪草机还在,触发剪草机杀敌
LawnMowerKill(ob.R);
}
break;
case C < 0:
// 进入房间,游戏失败
Win = -1;
GameOver();
return false;
break;
}
} else {
// 向右走
ob.X += ob.Speed;
ob.style.pixelLeft = Math.ceil(ob.X);
LX += ob.Speed;
RX += ob.Speed;
if (ob.style.pixelLeft >= oGP.MaxWidth) {
// 向右走出了屏幕,僵尸死亡
OZombie.Die(ob);
continue;
}
C = GetC(RX);
}
// 更新僵尸的左、右受攻击点的X坐标
ob.AttackedLX = LX;
ob.AttackedRX = RX;
if (C!== ob.C && C > 0) {
// 更新僵尸所在列的信息
oGP.aZombie[ob.C][ob.R] = oGP.aZombie[ob.C][ob.R].replace(ob.id + ',', '');
oGP.aZombie[C][ob.R] += ob.id + ',';
ob.C = C;
}
}
}
// 定时调用移动函数
setTimeout('MoveZombies()', 100);
}
普通僵尸类
/*-------------------普通僵尸---------------------------*/
function CZombie() {
this.EName = 'oZombie';
this.CName = '普通僵尸';
this.width = 87;
this.height = 144;
this.HP = 270;
this.BreakPoint = 90;
this.Ornaments = 0; // 饰品种类,123三种
this.OrnHP = 0; // 饰品HP
this.Speed = 1.8;
this.WalkDirection = 0;
this.Attack = 100;
this.canAppear = '[草地][屋顶]';
this.Location = '地上';
this.againstFreeze = 0; // 免疫冰冻
this.againstSlow = 0; // 免疫减速
this.againstSputtering = 0; // 免疫溅射
this.againstRaven = 0; // 是否免疫食人花的吞噬
this.againstSetbody = 0; // 是否免疫定身
this.beAttackedPointL = 10;
this.beAttackedPointR = 77;
// 加载普通动画
var tmpImg1 = new Image();
tmpImg1.src = 'images/Zombies/Zombie/Zombie.gif';
dicZombiesGif.add('oZombie', tmpImg1);
// 加载攻击动画
var tmpImg2 = new Image();
tmpImg2.src = 'images/Zombies/Zombie/ZombieAttack.gif';
dicZombiesGif.add('oZombieAttack', tmpImg2);
this.NormalGif = dicZombiesGif('oZombie').src;
this.AttackGif = dicZombiesGif('oZombieAttack').src;
}
// 僵尸出生方法
CZombie.prototype.Birth = function (ob) {
oGP.aZombie[ob.C][ob.R] += (ob.id) + ',';
};
// 僵尸死亡方法
CZombie.prototype.Die = function (ob) {
NumZombies -= 1;
oGP.aR[ob.R] -= 1;
oGP.aZombie[ob.C][ob.R] = oGP.aZombie[ob.C][ob.R].replace(ob.id + ',', '');
document.getElementById('dAll').removeChild(ob);
};
// 僵尸濒死方法
CZombie.prototype.GoingDie = function (ob) {
ob.beAttacked = 0;
ob.HP = ob.HP - 60;
ob.style.filter = 'gray';
if (ob.HP <= 0) {
this.Die(ob);
} else {
setTimeout('oZombie.GoingDie(' + ob.id + ')', 1000);
}
};
旗帜僵尸类
/*-------------------旗帜僵尸---------------------------*/
function CFlagZombie() {
this.EName = 'oFlagZombie';
this.CName = '旗帜僵尸';
this.width = 111;
this.height = 143;
this.HP = 270;
this.BreakPoint = 90;
this.Ornaments = 0; // 饰品种类,123三种
this.OrnHP = 0; // 饰品HP
this.Speed = 2.2;
this.WalkDirection = 0;
this.Attack = 100;
this.canAppear = '[草地][屋顶]';
this.Location = '地上';
this.againstFreeze = 0; // 免疫冰冻
this.againstSlow = 0; // 免疫减速
this.againstSputtering = 0; // 免疫溅射
this.againstRaven = 0; // 是否免疫食人花的吞噬
this.beAttackedPointL = 10;
this.beAttackedPointR = 101;
// 加载普通动画
var tmpImg1 = new Image();
tmpImg1.src = 'images/Zombies/FlagZombie/FlagZombie.gif';
dicZombiesGif.add('oFlagZombie', tmpImg1);
// 加载攻击动画
var tmpImg2 = new Image();
tmpImg2.src = 'images/Zombies/FlagZombie/FlagZombieAttack.gif';
dicZombiesGif.add('oFlagZombieAttack', tmpImg2);
this.NormalGif = dicZombiesGif('oFlagZombie').src;
this.AttackGif = dicZombiesGif('oFlagZombieAttack').src;
}
// 僵尸出生方法
CFlagZombie.prototype.Birth = function (ob) {
oGP.aZombie[ob.C][ob.R] += (ob.id) + ',';
};
// 僵尸死亡方法
CFlagZombie.prototype.Die = function (ob) {
NumZombies -= 1;
oGP.aR[ob.R] -= 1;
oGP.aZombie[ob.C][ob.R] = oGP.aZombie[ob.C][ob.R].replace(ob.id + ',', '');
document.getElementById('dAll').removeChild(ob);
};
// 僵尸濒死方法
CFlagZombie.prototype.GoingDie = function (ob) {
ob.beAttacked = 0;
ob.HP = ob.HP - 60;
ob.style.filter = 'gray';
if (ob.HP <= 0) {
this.Die(ob);
} else {
setTimeout('oFlagZombie.GoingDie(' + ob.id + ')', 1000);
}
};
路障僵尸类
/*-------------------路障僵尸---------------------------*/
function CConeheadZombie() {
this.EName = 'oConeheadZombie';
this.CName = '路障僵尸';
this.width = 87;
this.height = 144;
this.HP = 270;
this.BreakPoint = 90;
this.Ornaments = 1; // 饰品种类,123三种
this.OrnHP = 370; // 饰品HP
this.Speed = 1.6;
this.WalkDirection = 0;
this.Attack = 100;
this.canAppear = '[草地][屋顶]';
this.Location = '地上';
this.againstFreeze = 0; // 免疫冰冻
this.againstSlow = 0; // 免疫减速
this.againstSputtering = 0; // 免疫溅射
this.againstRaven = 0; // 是否免疫食人花的吞噬
this.beAttackedPointL = 10;
this.beAttackedPointR = 77;
// 加载普通动画
var tmpImg1 = new Image();
tmpImg1.src = 'images/Zombies/ConeheadZombie/ConeheadZombie.gif';
dicZombiesGif.add('oConeheadZombie', tmpImg1);
// 加载攻击动画
var tmpImg2 = new Image();
tmpImg2.src = 'images/Zombies/ConeheadZombie/ConeheadZombieAttack.gif';
dicZombiesGif.add('oConeheadZombieAttack', tmpImg2);
// 使用普通僵尸的行动和攻击动画作为饰品消失后的动画
if (!dicZombiesGif.Exists('oZombie')) {
var tmpImg3 = new Image();
tmpImg3.src = 'images/Zombies/Zombie/Zombie.gif';
dicZombiesGif.add('oZombie', tmpImg3);
var tmpImg4 = new Image();
tmpImg4.src = 'images/Zombies/Zombie/ZombieAttack.gif';
dicZombiesGif.add('oZombieAttack', tmpImg4);
}
this.NormalGif = dicZombiesGif('oConeheadZombie').src;
this.AttackGif = dicZombiesGif('oConeheadZombieAttack').src;
this.OrnLostNormalGif = dicZombiesGif('oZombie').src;
this.OrnLostAttackGif = dicZombiesGif('oZombieAttack').src;
}
// 僵尸出生方法
CConeheadZombie.prototype.Birth = function (ob) {
oGP.aZombie[ob.C][ob.R] += (ob.id) + ',';
};
// 僵尸死亡方法
CConeheadZombie.prototype.Die = function (ob) {
NumZombies -= 1;
oGP.aR[ob.R] -= 1;
oGP.aZombie[ob.C][ob.R] = oGP.aZombie[ob.C][ob.R].replace(ob.id + ',', '');
document.getElementById('dAll').removeChild(ob);
};
// 僵尸濒死方法
CConeheadZombie.prototype.GoingDie = function (ob) {
ob.beAttacked = 0;
ob.HP = ob.HP - 60;
ob.style.filter = 'gray';
if (ob.HP <= 0) {
this.Die(ob);
} else {
setTimeout('oConeheadZombie.GoingDie(' + ob.id + ')', 1000);
}
};
铁桶僵尸类
/*-------------------铁桶僵尸---------------------------*/
function CBucketheadZombie() {
this.EName = 'oBucketheadZombie';
this.CName = '铁桶僵尸';
this.width = 97;
this.height = 142;
this.HP = 270;
this.BreakPoint = 90;
this.Ornaments = 1; // 饰品种类,123三种
this.OrnHP = 1100; // 饰品HP
this.Speed = 1.6;
this.WalkDirection = 0;
this.Attack = 100;
this.canAppear = '[草地][屋顶]';
this.Location = '地上';
this.againstFreeze = 0; // 免疫冰冻
this.againstSlow = 0; // 免疫减速
this.againstSputtering = 0; // 免疫溅射
this.againstRaven = 0; // 是否免疫食人花的吞噬
this.beAttackedPointL = 10;
this.beAttackedPointR = 87;
// 加载普通动画
var tmpImg1 = new Image();
tmpImg1.src = 'images/Zombies/BucketheadZombie/BucketheadZombie.gif';
dicZombiesGif.add('oBucketheadZombie', tmpImg1);
// 加载攻击动画
var tmpImg2 = new Image();
tmpImg2.src = 'images/Zombies/BucketheadZombie/BucketheadZombieAttack.gif';
dicZombiesGif.add('oBucketheadZombieAttack', tmpImg2);
// 使用普通僵尸的行动和攻击动画作为饰品消失后的动画
if (!dicZombiesGif.Exists('oZombie')) {
var tmpImg3 = new Image();
tmpImg3.src = 'images/Zombies/Zombie/Zombie.gif';
dicZombiesGif.add('oZombie', tmpImg3);
var tmpImg4 = new Image();
tmpImg4.src = 'images/Zombies/Zombie/ZombieAttack.gif';
dicZombiesGif.add('oZombieAttack', tmpImg4);
}
this.NormalGif = dicZombiesGif('oBucketheadZombie').src;
this.AttackGif = dicZombiesGif('oBucketheadZombieAttack').src;
this.OrnLostNormalGif = dicZombiesGif('oZombie').src;
this.OrnLostAttackGif = dicZombiesGif('oZombieAttack').src;
}
// 僵尸出生方法
CBucketheadZombie.prototype.Birth = function (ob) {
oGP.aZombie[ob.C][ob.R] += (ob.id) + ',';
};
// 僵尸死亡方法
CBucketheadZombie.prototype.Die = function (ob) {
NumZombies -= 1;
oGP.aR[ob.R] -= 1;
oGP.aZombie[ob.C][ob.R] = oGP.aZombie[ob.C][ob.R].replace(ob.id + ',', '');
document.getElementById('dAll').removeChild(ob);
};
// 僵尸濒死方法
CBucketheadZombie.prototype.GoingDie = function (ob) {
ob.beAttacked = 0;
ob.HP = ob.HP - 60;
ob.style.filter = 'gray';
if (ob.HP <= 0) {
this.Die(ob);
} else {
setTimeout('oBucketheadZombie.GoingDie(' + ob.id + ')', 1000);
}
};
橄榄球僵尸类
/*-------------------橄榄球僵尸---------------------------*/
function CFootballZombie() {
this.EName = 'oFootballZombie';
this.CName = '橄榄球僵尸';
this.width = 154;
this.height = 160;
this.HP = 270;
this.BreakPoint = 90;
this.Ornaments = 1; // 饰品种类,123三种
this.OrnHP = 1400; // 饰品HP
this.Speed = 3.2;
this.WalkDirection = 0;
this.Attack = 100;
this.canAppear = '[草地][屋顶]';
this.Location = '地上';
this.againstFreeze = 0; // 免疫冰冻
this.againstSlow = 0; // 免疫减速
this.againstSputtering = 0; // 免疫溅射
this.againstRaven = 0; // 是否免疫食人花的吞噬
this.beAttackedPointL = 20;
this.beAttackedPointR = 134;
// 加载普通动画
var tmpImg1 = new Image();
tmpImg1.src = 'images/Zombies/FootballZombie/FootballZombie.gif';
dicZombiesGif.add('oFootballZombie', tmpImg1);
// 加载攻击动画
var tmpImg2 = new Image();
tmpImg2.src = 'images/Zombies/FootballZombie/FootballZombieAttack.gif';
dicZombiesGif.add('oFootballZombieAttack', tmpImg2);
// 加载饰品消失后的普通动画
var tmpImg3 = new Image();
tmpImg3.src = 'images/Zombies/FootballZombie/FootballZombieOrnLost.gif';
dicZombiesGif.add('oFootballZombieOrnLost', tmpImg3);
// 加载饰品消失后的攻击动画
var tmpImg4 = new Image();
tmpImg4.src = 'images/Zombies/FootballZombie/FootballZombieOrnLostAttack.gif';
dicZombiesGif.add('oFootballZombieOrnLostAttack', tmpImg4);
this.NormalGif = dicZombiesGif('oFootballZombie').src;
this.AttackGif = dicZombiesGif('oFootballZombieAttack').src;
this.OrnLostNormalGif = dicZombiesGif('oFootballZombieOrnLost').src;
this.OrnLostAttackGif = dicZombiesGif('oFootballZombieOrnLostAttack').src;
}
// 僵尸出生方法
CFootballZombie.prototype.Birth = function (ob) {
oGP.aZombie[ob.C][ob.R] += (ob.id) + ',';
};
// 僵尸死亡方法
CFootballZombie.prototype.Die = function (ob) {
NumZombies -= 1;
oGP.aR[ob.R] -= 1;
oGP.aZombie[ob.C][ob.R] = oGP.aZombie[ob.C][ob.R].replace(ob.id + ',', '');
document.getElementById('dAll').removeChild(ob);
};
// 僵尸濒死方法
CFootballZombie.prototype.GoingDie = function (ob) {
ob.beAttacked = 0;
ob.HP = ob.HP - 60;
ob.style.filter = 'gray';
if (ob.HP <= 0) {
this.Die(ob);
} else {
setTimeout('oFootballZombie.GoingDie(' + ob.id + ')', 1000);
}
};
僵尸攻击函数
function ZombieAttack(id) {
var ob = document.getElementById(id);
if (ob === null) {
return false;
}
var OZombie;
eval('OZombie=' + ob.EName);
if (ob.HP <= OZombie.BreakPoint) {
return false;
}
var n, obPlant, OPlant;
if (ob.WalkDirection === 0) {
// 向左走的僵尸
for (n = ob.C; n >= (ob.C - 1); n--) {
if (n > oGP.MaxC || n < 1) {
continue;
}
if (oGP.aPlant[n][ob.R][1]!== '') {
obPlant = document.getElementById(oGP.aPlant[n][ob.R][1]);
break;
} else if (oGP.aPlant[n][ob.R][0]!== '') {
obPlant = document.getElementById(oGP.aPlant[n][ob.R][0]);
break;
} else if (oGP.aGroundPlant[n][ob.R][0]!== '') {
obPlant = document.getElementById(oGP.aGroundPlant[n][ob.R][0]);
break;
}
}
if (obPlant === null) {
// 没有植物
if (ob.isAttacking === 1) {
ob.src = ob.NormalGif;
ob.isAttacking = 0;
}
return false;
}
eval('OPlant=' + obPlant.EName);
if (OPlant.canEat === 0) {
// 植物不能被吃,比如地刺
if (ob.isAttacking === 1) {
ob.src = ob.NormalGif;
ob.isAttacking = 0;
}
return false;
}
if ((ob.AttackedLX <= obPlant.AttackedRX) && (ob.AttackedRX >= obPlant.AttackedLX)) {
// 僵尸的左攻击点小于植物的右攻击点,并且僵尸右攻击点大于植物左攻击点
if (ob.isAttacking === 0) {
ob.src = ob.AttackGif;
}
if (obPlant.EName === 'oGralic') {
obPlant.HP -= 20;
if (obPlant.HP < OPlant.BreakPoint) {
OPlant.Die(obPlant);
ob.src = ob.NormalGif;
ob.isAttacking = 0;
return false;
} else {
PlantStatus(obPlant.id);
ob.isAttacking = 1;
setTimeout('ZombieEatGralic("' + ob.id + '")', 1000);
return false;
}
} else {
ob.isAttacking = 1;
setTimeout('ZombieEatPlant("' + ob.id + '","' + obPlant.id + '")', 1000);
}
} else {
if (ob.isAttacking === 1) {
ob.src = ob.NormalGif;
ob.isAttacking = 0;
}
return false;
}
}
}
僵尸吃植物函数
function ZombieEatPlant(zombieid, plantid) {
var ob = document.getElementById(zombieid);
var obPlant = document.getElementById(plantid);
if (ob === null || obPlant === null) {
if (ob!== null && ob.isAttacking === 1) {
ob.src = ob.NormalGif;
ob.isAttacking = 0;
}
return false;
}
var OZombie, OPlant;
eval('OZombie=' + ob.EName);
eval('OPlant=' + obPlant.EName);
if (obPlant.unrivaled === 0) {
if (ob.FreeSlowTime === '') {
obPlant.HP -= OZombie.Attack;
} else {
obPlant.HP -= OZombie.Attack * 0.25;
}
PlantStatus(obPlant.id);
}
if (obPlant.HP <= OPlant.BreakPoint) {
OPlant.Die(obPlant);
ob.src = ob.NormalGif;
ob.isAttacking = 0;
return false;
}
setTimeout('ZombieEatPlant("' + zombieid + '","' + plantid + '")', 1000);
}
僵尸吃大蒜函数
function ZombieEatGralic(id) {
var ob = document.getElementById(id);
if (ob === null) {
return false;
}
ob.isAttacking = 0;
ob.src = dicZombiesGif(ob.EName).src;
var tmpR, tmpC, tmpA = [];
if (ob.R > 1) {
tmpR = ob.R - 1;
if (oGP.aP[1][tmpR] === oGP.aP[1][ob.R]) {
tmpA.push(tmpR);
}
}
if (ob.R < oGP.MaxR) {
tmpR = ob.R + 1;
if (oGP.aP[1][tmpR] === oGP.aP[1][ob.R]) {
tmpA.push(tmpR);
}
}
if (tmpA.length > 0) {
ob.src = dicZombiesGif(ob.EName).src;
tmpR = tmpA[Math.floor(Math.random() * tmpA.length)];
ob.style.pixelTop = GetY(tmpR) - parseInt(ob.height);
ob.style.zIndex = 3 * tmpR + 1;
var LX = ob.AttackedLX;
var RX = ob.AttackedRX;
if (ob.WalkDirection === 0) {
// 向左走
ob.style.pixelLeft -= 10;
ob.X -= 10;
LX -= 10;
RX -= 10;
tmpC = GetC(LX);
} else {
// 向右走
ob.style.pixelLeft += 10;
ob.X += 10;
LX += 10;
RX += 10;
tmpC = GetC(RX);
}
ob.AttackedLX = LX;
ob.AttackedRX = RX;
oGP.aZombie[ob.C][ob.R] = oGP.aZombie[ob.C][ob.R].replace(ob.id + ',', '');
oGP.aZombie[tmpC][tmpR] += ob.id + ',';
oGP.aR[ob.R] -= 1;
ob.C = tmpC;
ob.R = tmpR;
oGP.aR[tmpR] += 1;
}
}
以上代码实现了植物大战僵尸中僵尸的移动、攻击、死亡等基本功能,通过这些代码可以对游戏中僵尸的行为逻辑有一个基本的了解。在实际开发中,还需要结合其他部分的代码来完成整个游戏的开发。