近期学习js,假期无事,参照网上的代码写了个双人玩的贪吃蛇。
对网上的代码作了些改造,具体如下:
1,把m*n的二维数组变成m*n一维数组,映射关系为:
A[r]------>a[i][j];
i = r%m;
j = (r-r%m)/m;
j<n;
2,对随机数的获取作了些改造,
3,蛇尾总是第一个加入的元素,
代码编写仓促,也没写注释,望各位见谅
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>snake</title>
<script type='text/javascript' >
(function(){
if (!window.Snake)
window.Snake = {};
Snake = function(container, name){
this.container = container;
this.name = '_' + name + '_';
this.dir = 0;
this.testDir = 0;
this.head = -1;
this.run = 0;
this.speed=300;
this.count={};
if (typeof Snake.prototype._init == 'undefined') {
Snake.prototype.control = function(n){
if (Math.abs(this.testDir) == Math.abs(n))
return;
this.dir = n;
};
Snake.prototype.move = function(){
if (this.container.win) {
clearInterval(this.run);
return;
}
var _left = this.head % this.container.width;
var _top = (this.head - this.head % this.container.width) / this.container.width;
this.dir % this.container.width == 0 ? _top += this.dir / this.container.width : _left += this.dir;
if (_left < 0 || _left >= this.container.width || _top < 0 || _top >= this.container.height || (this.container.all[this.head + this.dir] && this.container.all[this.head + this.dir].indexOf(this.name) != -1)) {
this.container.win = true;
alert(this.name+' lose ~_~');
return;
}
if (!this.container.all[this.head + this.dir]) {
this.container.all[this.head + this.dir] = this.name;
var _div = this.getDivByClass(this.name)[0];
this.container.all[parseInt(_div.style.left) / 20 + parseInt(_div.style.top) / 20 * this.container.width] = this.container.all[parseInt(_div.style.left) / 20 + parseInt(_div.style.top) / 20 * this.container.width].replace(this.name, '');
_div.style.left = _left * 20 + 'px';
_div.style.top = _top * 20 + 'px';
this.container.div.appendChild(_div);
}
else
if (this.head + this.dir == this.container.food) {
var _div = this.getDivByClass(this.container.foodClass)[0];
_div.className = this.name;
this.container.div.appendChild(_div);
this.container.createFood();
this.container.all[this.head + this.dir] = this.name;
}
else {
var _str = this.container.all[this.head + this.dir];
var _divs = this.getDivByClass(_str);
var _div = _divs[0];
if (_divs.length > 1 && parseInt(_div.style.left) / 20 + parseInt(_div.style.top) / 20 * this.container.width == this.head + this.dir) {
this.container.all[this.head + this.dir] = this.name;
_div.className = this.name;
this.container.div.appendChild(_div);
}
else {
this.container.all[this.head + this.dir] += this.name;
var _div = this.getDivByClass(this.name)[0];
this.container.all[parseInt(_div.style.left) / 20 + parseInt(_div.style.top) / 20 * this.container.width] = this.container.all[parseInt(_div.style.left) / 20 + parseInt(_div.style.top) / 20 * this.container.width].replace(this.name, '');
_div.style.left = _left * 20 + 'px';
_div.style.top = _top * 20 + 'px';
this.container.div.appendChild(_div);
}
}
var lth=this.getDivByClass(this.name).length
if(lth>=this.container.bet){
this.container.win=true;
alert(this.name+' win ^-^');
}
this.count.innerHTML=this.name+': '+lth.toString();
this.head += this.dir;
this.testDir = this.dir;
};
Snake.prototype.getDivByClass = function(name){
var _divs = this.container.div.getElementsByTagName('DIV');
var _result = [];
for (var i = 0; i < _divs.length; i++) {
if (_divs[i].className == name)
_result.push(_divs[i]);
}
return _result;
};
Snake.prototype.acc=function(n){
return this.speed+n>50? this.speed+n:this.speed;
}
Snake.prototype.init = function(){
this.head = this.container.getRandom();
this.container.all[this.head] = this.name;
var _left = this.head % this.container.width * 20 + 'px';
var _top = (this.head - this.head % this.container.width) / this.container.width * 20 + 'px';
var _div = document.createElement('DIV');
_div.className = this.name;
_div.style.left = _left;
_div.style.top = _top;
this.count=document.createElement('P');
this.count.innerHTML=this.name+': 1';
this.container.counts.push(this.count);
this.container.div.appendChild(_div);
}
}
Snake.prototype._init = true;
}
if (!window.Container)
window.Container = {};
Container = function(width, height, foodClass, bet){
this.width = width;
this.height = height;
this.food = -1;
this.foodClass = '_' + foodClass + '_';
this.bet = bet;
this.win = false;
this.all = [];
this.counts=[];
var _div = document.createElement('DIV');
_div.className = 'container';
//_div.style.position = 'absolute';
_div.style.width = this.width * 20 + 'px';
_div.style.height = this.height * 20 + 'px';
this.div = _div;
this.getRandom = function(){
var _remain = [];
for (var i = 0; i < width * height; i++) {
if (this.all[i])
continue;
_remain.push(i);
}
var _index = Math.floor(Math.random() * _remain.length);
return _remain[_index];
}
this.createFood = function(){
var _food = this.getRandom();
this.food = _food;
this.all[_food] = this.foodClass;
var _fDiv = document.createElement('DIV');
_fDiv.className = this.foodClass;
var _left = _food % this.width * 20 + 'px';
var _top = (_food - _food % this.width) / this.width * 20 + 'px';
_fDiv.style.left = _left;
_fDiv.style.top = _top;
this.div.appendChild(_fDiv);
}
}
})();
var container = new Container(30, 30, 'food', 20);
var snakeA = new Snake(container, 'snakeA');
var snakeB = new Snake(container, 'snakeB');
function game(){
container.createFood();
snakeA.init();
snakeB.init();
function allcontrol(e){
e = e || window.event;
var n = e.keyCode;
switch (n) {
case 37:
snakeA.control(-1);
break;
case 38:
snakeA.control(-container.width);
break;
case 39:
snakeA.control(1);
break;
case 40:
snakeA.control(container.width);
break;
case 65:
snakeB.control(-1);
break;
case 87:
snakeB.control(-container.width);
break;
case 68:
snakeB.control(1);
break;
case 83:
snakeB.control(container.width);
break;
case 96:
if(snakeA.run){
clearInterval(snakeA.run);
snakeA.speed=snakeA.acc(-50);
snakeA.run=setInterval('snakeA.move()',snakeA.acc(-50));
}
break;
case 110:
if(snakeA.run){
clearInterval(snakeA.run);
snakeA.speed=snakeA.acc(20);
snakeA.run=setInterval('snakeA.move()',snakeA.acc(50));
}
break;
case 71:
if(snakeB.run){
clearInterval(snakeB.run);
snakeB.speed=snakeB.acc(-20);
snakeB.run=setInterval('snakeB.move()',snakeB.acc(-50));
}
break;
case 72:
if(snakeB.run){
clearInterval(snakeB.run);
snakeB.speed=snakeB.acc(20);
snakeB.run=setInterval('snakeB.move()',snakeB.acc(50));
}
break;
}
if (!snakeA.run && snakeA.dir)
snakeA.run = setInterval('snakeA.move()', 300);
if (!snakeB.run && snakeB.dir)
snakeB.run = setInterval('snakeB.move()', 300);
}
var _wrap=document.createElement('DIV');
_wrap.appendChild(container.counts[1]);
_wrap.appendChild(container.div);
_wrap.appendChild(container.counts[0]);
document.body.appendChild(_wrap);
var des=document.createElement('DIV');
des.style.clear='both';
des.style.position='relative';
des.style.padding='20px';
des.innerHTML='snakeA:键盘上↑↓←→代表方向控制,小键盘上0加速,.减速<br/>snakeB:键盘上A W S D代表方向控制,键盘上G加速,H减速<br/>两条蛇可以相互吃掉对方尾部,先吃到20个食物为赢';
document.body.appendChild(des);
if (document.addEventListener) {
document.addEventListener('keydown', allcontrol, false);
}
else
if (document.attachEvent) {
document.attachEvent('onkeydown', allcontrol);
}
}
window.onload = game;
</script>
<style type='text/css'>
p{
float:left;
margin:10px;
padding:20px;
font-weight:bolder;
border:1px solid #000;
text-align:center;
vertical-align:text-bottom;;
}
.container{
position:relative;
float:left;
margin:10px;
padding:0;
border:1px solid #000;
}
._snakeA_{
position:absolute;
margin:0;
padding:0;
width:20px;
height:20px;
border:1px solid #ccc;
background-color:red;
}
._food_{
position:absolute;
margin:0;
padding:0;
width:20px;
height:20px;
border:1px solid #ccc;
background-color:green;
}
._snakeB_{
position:absolute;
margin:0;
padding:0;
width:20px;
height:20px;
border:1px solid #ccc;
background-color:#2E2E2E;
}
</style>
<script type='text/javascript'>
</script>
</head>
<body >
</body>
</html>
分享到:
相关推荐
纯c语言写的双人贪吃蛇小游戏,没有用图形库
双人贪吃蛇 源代码中的点阵的头文件可以删除
双人版对战贪吃蛇,可输入获胜所需积分
在家无聊,花了两三天用TC写了个双人贪吃蛇游戏.UI很粗糙,但是游戏核心是完整的.玩法很简单:wasd和方向键分别控制两条蛇,吃食物长个子,撞到墙壁或者任何蛇身都会使长度变短.长度变为0的就输. 目前仅在XP下测试通过....
Pygame游戏源代码:网络版双人对战贪吃蛇 包含代码、图片、声音等所有资源,可直接运行 游戏时先运行SnakeServer,启动服务器。在分别运行SnakeP1和SnakeP2启动两个客户端。服务器和两个客户端的IP可在源代码中直接...
用纯javascript和jquery编写的双人对战版贪吃蛇, 这个版本内包含所有注释, 是我在初学前端的时候课后感兴趣自己编写的
该源代码为C语言双人/单人贪吃蛇 编译软件WIN-TC 操作系统WINDOWS XP SP3 有单人双人模式选择 开场动画 分数统计 随机障碍 速度变换 但是由于我的操作系统中delay()的控制单位不是MS 所以在源代码中进行了一定的...
该双人版贪吃蛇是用C++语言所写,利用头文件绘制图形,代码量很小,言简意赅,基本实现了所有的贪吃蛇双人版功能。适合学习
贪吃蛇的一个小游戏,还可以两个人玩,进行双人对站
双人贪吃蛇,本人的即兴之作,希望能够带给大家帮助。(由于是在tc3.0上写的,所以win7上无法运行)
双人贪吃蛇 ,两只贪吃蛇中吃的多并且不触碰到自己的身体和对方的身体以及障碍物的为赢家
贪吃蛇 。。。。。。。。。。。。。。。。。。。。。。。。
代码有详细的注释,只有游戏部分,没有积分的部分,中间的障碍是Love,双方分别从上下两头开始,在里面进行PK,写着练习的,仅供参考,嘿嘿。操作方法在文件里的 txt文档里
使用贪吃蛇,VS 必须安装图形库才能运行程序 。 贪吃蛇比较简单,大多数人都会,留给初学者参考使用
c语言贪吃蛇游戏的双人对战版c语言贪吃蛇游戏的双人对战版c语言贪吃蛇游戏的双人对战版c语言贪吃蛇游戏的双人对战版c语言贪吃蛇游戏的双人对战版c语言贪吃蛇游戏的双人对战版c语言贪吃蛇游戏的双人对战版c语言贪吃蛇...
基于C语言的双人贪吃蛇游戏程序设计(含链表、文件操作、背景音乐)。适合C语言初学者,以及可以作为C语言大作业的参考
网页玩贪吃蛇网页玩贪吃蛇
用C语言写的一个游戏 贪吃蛇双人对战版(计分 吃炸弹) 代码用codeblocks编译通过,其他编译器自行测试。