网站首页
关于深山
客户案例
业务范围
联系深山
网络投票系统
企业网站建设
旅行社网站建设
小程序
留言板
技术文章
许愿墙(qq爱墙)
技术首页
百度小程序开发
微信小程序开发
微信公众号开发
uni-app
asp函数库
ASP
asp.net
DIV+CSS
HTML
SEO搜索引擎忧化
下载类信息
个人空间
代码生成
电商
python
页面特效
表格特效
导航菜单
图形特效
表单特效
时间日期
色彩类别
链接特效
网页特效
系统硬件
网站公告
网页学习
技术类文章
网站类信息
订阅本栏目 RSS
您所在的位置:
深山工作室
>
页面特效
> 正文
一个简单的用java写的非常不错的贪食蛇游戏
网络 2009/4/7 20:15:13 深山行者 字体:
大
中
小
浏览 9019
以下为详细代码
<html>一个简单的用java写的非常不错的贪食蛇游戏 <head> <script type="text/javascript"> function cbsnake(){ //Pixels to move at once this.jump = 8; //Size of snake. Make this one less than jump. Doesn't have to be,but adds good effect this.sos = 7; //Size of board //DANGER!!! this.sofb must be EVENLY dividable by this.jump DANGER!!!! this.sofb = 400; //Set things up this.daway = this.sofb - this.jump; this.correct = new Array(); this.correct[0] = 0; while(this.correct[this.correct.length -1] != this.daway){ this.correct[this.correct.length] = this.correct[this.correct.length -1]+this.jump } this.zero = 0; var gameboard = ' <div class="board" id="board"> <div id="i2">这是一个简单的贪食蛇游戏。选择一个速度,然后用你的箭头键来尝试吃苹果(红色块),但要注意别碰到墙壁和自己。你可以在任何时候按下空格键以暂停游戏。</div> </div><div class="board" id="score"> <span id="cscore">0</span> <span id="buttons"> <button type="button" id="slow" onClick="snake.slow()">慢</button> <button type="button" id="medium" onClick="snake.medium()">中</button> <button type="button" id="fast" onClick="snake.fast()">快</button> </span></div>'; document.write(gameboard); } cbsnake.prototype.setup = function(setspeed){ var thisObj = this; //Score... this.score = 0; //Snake Direction this.sdir = 'none'; this.sdirb = 'none'; this.sdirp = 'none'; //Snake arrays this.ctop = new Array(); this.cleft = new Array(); //Top of snake class this.ctop[0] = 200; this.ctop[1] = -8; //Left of Snake class this.cleft[0] = 200; this.cleft[1] = -8; //current top of apple this.atop = 0; //current left of apple this.aleft = 0; //Milliseconds between move this.speed = setspeed; document.getElementById('board').innerHTML = '<div id="apple"></div><div id="snake0" class="snake"></div><div id="snake1" class="snake"></div>'; this.moveapple(); this.stopgame = false; setTimeout(function(){ thisObj.msnake() },this.speed); document.onkeydown = function(e){ return thisObj.snakedir(e); }; } cbsnake.prototype.slow = function(){ this.setup(100); this.buttons('true'); document.getElementById('slow').blur(); } cbsnake.prototype.medium = function(){ this.setup(70); this.buttons('true'); document.getElementById('medium').blur(); } cbsnake.prototype.fast = function(){ this.setup(30); this.buttons('true'); document.getElementById('fast').blur(); } cbsnake.prototype.rannum = function(num1,num2){ num1 = parseInt(num1); num2 = parseInt(num2); var generator = Math.random()*(Math.abs(num2-num1)); generator = Math.round(num1+generator); return generator; } cbsnake.prototype.moveapple = function(){ var usethis = false; while(!usethis){ this.atop = this.correct[this.rannum(0,this.correct.length-1)]; this.aleft = this.correct[this.rannum(0,this.correct.length-1)]; if(this.numInArray(this.ctop,this.cleft,this.atop,this.aleft) == 0){ usethis = true; } } document.getElementById('apple').style.top = this.atop+"px"; document.getElementById('apple').style.left = this.aleft+"px"; } cbsnake.prototype.snakedir = function(e){ if(!e){ //IE... e = window.event; } switch(e.keyCode){ case 38: if(this.sdir != 'down' && this.sdirp != 'down'){ this.sdirb = 'up'; this.sdirp = 'up'; } break; case 40: if(this.sdir != 'up' && this.sdirp != 'up'){ this.sdirb = 'down'; this.sdirp = 'down'; } break; case 37: if(this.sdir != 'right' && this.sdirp != 'right'){ this.sdirb = 'left'; this.sdirp = 'left'; } break; case 39: if(this.sdir != 'left' && this.sdirp != 'left'){ this.sdirb = 'right'; this.sdirp = 'right'; } break; case 32: if(this.sdir == 'none' && this.sdirp != 'none'){ this.sdirb = this.sdirp; this.sdirp = 'none'; } else{this.sdirp = this.sdir; this.sdirb = 'none'; } break; } return this.stopgame; } cbsnake.prototype.msnake = function(){ if(this.stopgame === false){ if(this.sdir != 'none'){ this.moveall(); } var thisObj = this; switch(this.sdir){ case 'up': this.ctop[0] = this.ctop[0] - this.jump; document.getElementById('snake0').style.top = this.ctop[0]+"px"; if((this.ctop[0] == this.zero && this.sdirb == 'up') || this.ctop[0] < this.zero){ this.gover(); } break; case 'down': this.ctop[0] = this.ctop[0] + this.jump; document.getElementById('snake0').style.top = this.ctop[0]+"px"; if((this.ctop[0] == this.daway && this.sdirb == 'down') || this.ctop[0] > this.daway){ this.gover(); } break; case 'left': this.cleft[0] = this.cleft[0] - this.jump; document.getElementById('snake0').style.left = this.cleft[0]+"px"; if((this.cleft[0] == this.zero && this.sdirb == 'left') || this.cleft[0] < this.zero){ this.gover(); } break; case 'right': this.cleft[0] = this.cleft[0] + this.jump; document.getElementById('snake0').style.left = this.cleft[0]+"px"; if((this.cleft[0] == this.daway && this.sdirb == 'right') || this.cleft[0] > this.daway){ this.gover(); } break; } if(this.sdir != 'none'){ this.hitself(); this.happle(); } this.sdir = this.sdirb setTimeout(function(){ thisObj.msnake() },this.speed); } } cbsnake.prototype.gover = function(){ if(!this.stopgame){ this.stopgame = true; var inner = document.getElementById('board').innerHTML; document.getElementById('board').innerHTML = inner+'<div id="notice">比赛结束了!你的得分为 '+this.score+'</div><div id="i2">这是一个简单的贪食蛇游戏。选择一个速度,然后用你的箭头键来尝试吃苹果(红色块),但要注意别碰到墙壁和自己。你可以在任何时候按下空格键以暂停游戏。</div>'; document.getElementById('apple').style.backgroundColor = '#D7BEBE'; for(i=0;i<this.cleft.length;i++){ document.getElementById('snake'+i).style.backgroundColor = '#BEBEBE'; } this.buttons(''); } } cbsnake.prototype.happle = function(){ if(this.atop == this.ctop[0] && this.aleft == this.cleft[0]){ //HIT!!! this.score++; document.getElementById('cscore').innerHTML = this.score; this.moveapple(); this.addsnake(); } } cbsnake.prototype.addsnake = function(){ var newsnake = document.createElement('div'); var newid = 'snake'+this.cleft.length; newsnake.setAttribute('id',newid); //this crap is for IE. I would rather add the class name. newsnake.style.position = 'absolute'; newsnake.style.top = '-10px'; newsnake.style.left = '-10px'; newsnake.style.display = 'none'; newsnake.style.backgroundColor = 'black'; newsnake.style.height = '7px'; newsnake.style.width = '7px'; newsnake.style.overflow = 'hidden'; document.getElementById('board').appendChild(newsnake); this.cleft[this.cleft.length] = -10; this.ctop[this.ctop.length] = -10; } cbsnake.prototype.moveall = function(){ var i = this.ctop.length - 1; while(i != 0){ document.getElementById('snake'+i).style.top = document.getElementById('snake'+(i-1)).style.top; document.getElementById('snake'+i).style.left = document.getElementById('snake'+(i-1)).style.left; document.getElementById('snake'+i).style.display = 'block'; this.ctop[i] = this.ctop[i-1]; this.cleft[i] = this.cleft[i-1]; i = i - 1; } } cbsnake.prototype.numInArray = function(array,array2,value,value2){ var n = 0; for (var i=0; i < array.length; i++) { if (array[i] === value && array2[i] === value2) { n++; } } return n; } cbsnake.prototype.hitself = function(){ if(this.numInArray(this.ctop,this.cleft,this.ctop[0],this.cleft[0]) > 1){ this.gover(); } } cbsnake.prototype.buttons = function(setto){ document.getElementById('slow').disabled = setto; document.getElementById('medium').disabled = setto; document.getElementById('fast').disabled = setto; } </script> <style type="text/css"> .board{ width: 399px; background-color: lightgrey; border: 1px solid gray; position: relative; margin-left: 0; margin-top: 0; } #board{ height: 399px; border-bottom: 0px; } #apple{ position: absolute; background-color: red; height: 7px; width: 7px; overflow: hidden; } .snake{ position: absolute; top: 200px; left: 200px; background-color: black; height: 7px; width: 7px; overflow: hidden; } .snake2{ position: absolute; top: -10px; left: -10px; background-color: black; height: 7px; width: 7px; overflow: hidden; } #score{ height: 50px; margin-top: 0px; } #cscore{ color: black; padding-left: 10px; float: left; width: 25%; font-size: xx-large; } #buttons{ float: right; width: 50%; text-align: right; padding-top: 10px; } #notice{ position: absolute; top: 1em; left: 1em; right: 1em; text-align: center; font-size: 150%; } #i2{ position: absolute; bottom: 1em; left: 1em; right: 1em; text-align: center; font-size: 95%; } </style> </head> <body> <script type="text/javascript"> var snake = new cbsnake(); </script> </body> </html>
前一页:
移动层,并且保存各个层的位置,可以方便的制作成为一个动态的页面
后一页:
可移动的弹出层
相关阅读
一个CSS特效生成器下载
利用CSS里面的A:hover鼠标滑过缩略图时放大图片
在网页标题栏上或收藏夹前面显示网站ico小图标的方法
深山留言板系统V3.6(游戏之穿越火线幽灵归来)
canvas绘制的文字如何换行
佳达国际货运代理有限公司
asp计算器
asp防止多个后台用户同时登陆
更多信息>>
栏目类别选择
百度小程序开发
微信小程序开发
微信公众号开发
uni-app
asp函数库
ASP
DIV+CSS
HTML
python
更多>>
同类信息
jquery下拉到某个固定位置然后某些元素发生改变样式状态
canvas绘制的文字如何换行
兼容pc、移动端用js实现复制内容到剪切板(支持苹果safari浏览器)
js兼容多个浏览器右下角漂浮广告
兼容ie6+和火狐的禁止右键
做在线客服时,聊天窗口的div滚动条始终在底部
更多>>
最新添加文章
dw里面查找替换使用正则删除sqlserver里面的CONSTRAINT
Android移动端自动化测试:使用UIAutomatorViewer与Selenium定位元素
抖音直播音挂载小雪花 懂车帝小程序
javascript获取浏览器指纹可以用来做投票
火狐Mozilla Firefox出现:无法载入您的Firefox配置文件 它可能已经丢失 或是无法访问 问题解决集合处理办法
在Android、iOS、Windows、MacOS中微信小程序的文件存放路径
python通过代码修改pip下载源让下载库飞起
python里面requests.post返回的res.text还有其它的吗
更多>>
随机抽取信息
脚本控制三行三列自适应高度DIV布局
css分页放大效果
ASP读取ACCSS数据代码
深山旅行社网站管理系统 v1.7
一个非常好的photoshop cs 教程下载地址
深山旅游网站管理系统V1.0
更多标签
热门标签
blog
电影
strong
居中
简易
资料
样式
遍历
ajax
适应
断线
站长
网站地图
焦点
绝对定位
素材
lightbox
排序
265
ewebeditor
ResponseXML
项目
电脑
rss
文章
exe
过滤
奋斗
方法
xml
天气预报
img
absolute
文本
在线
保护眼睛的电脑色
忧化
文件
翻页
iis
不对齐
radio
链接
获取
墙
验证码
Application
县
自动隐藏
验证
免责声明:本站所有资料信息,有部分为本人原创,部分为从网络收集而来,仅供网友查看阅读所用,所有信息版权归信息所有人或所有公司所有
如果信息内容侵犯到您的版权或权益请与我们联系,经确认后我们会立即移除相关内容或链接
Copyright © 2007-2026
深山工作室
All Rights Reserved
服务QQ:
565449214
手机:
13961347334
ICP备案:
苏ICP备15019627号
苏公网安备 32070502010230号