DDR爱好者之家 Design By 杰米
原生JavaScript实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下
代码:
<script>
var timer = null;
var oMain = document.getElementById("main");
function Map(atom,xnum,ynum){//地图,设置单位大小,及根据单位大小创建地图
this.atom = atom;
this.xnum = xnum;
this.ynum = ynum;
this.create = function(){
this.canvas = document.createElement("div");
this.canvas.style.cssText = "position: relative;top: 40px;border: 1px solid red;background: #F1F1F1;"
this.canvas.style.width = this.atom * this.xnum + "px";//画布宽
this.canvas.style.height = this.atom * this.ynum + "px";//画布高
main.appendChild(this.canvas);
}
}
function Food(map){//食物
this.width = map.atom;
this.height = map.atom;
//实现随机背景色
this.bgColor = "rgb("+Math.floor(Math.random()*200)+","+Math.floor(Math.random()*200)+","+Math.floor(Math.random()*200)+")";
this.x = Math.floor(Math.random()*map.xnum);
this.y = Math.floor(Math.random()*map.ynum);
this.flag = document.createElement('div');
this.flag.style.width = this.width + 'px';
this.flag.style.height = this.height + 'px';
this.flag.style.backgroundColor = this.bgColor;
this.flag.style.borderRadius = '50%';
this.flag.style.position = "absolute";
this.flag.style.left = this.x * this.width + 'px';
this.flag.style.top = this.y * this.height + 'px';
map.canvas.appendChild(this.flag);
}
//重新开始
function restart(map,snake){
for(var i=0; i<snake.body.length; i++){
map.canvas.removeChild(snake.body[i].flag);
}
snake.body = [
{x : 2,y : 0},//蛇头
{x : 1,y : 0},//蛇身
{x : 0,y : 0}//蛇尾
]
snake.direction = "right";
snake.display();
map.canvas.removeChild(food.flag);
food = new Food(map);
}
function Snake(map){
this.width = map.atom;
this.height = map.atom;
this.direction = "right";
this.body = [
{x : 2,y : 0},
{x : 1,y : 0},
{x : 0,y : 0}
]
this.display = function(){
for(var i=0; i<this.body.length; i++){
if(this.body[i].x != null){
var s = document.createElement('div');
//将节点保存
this.body[i].flag = s;
//设置样式
s.style.width = this.width + 'px';
s.style.height = this.height + 'px';
s.style.backgroundColor = "rgb("+Math.floor(Math.random()*200)+","+Math.floor(Math.random()*200)+","+Math.floor(Math.random()*200)+")";
s.style.borderRadius = '50%';
//设置位置
s.style.position = "absolute";
s.style.left = this.body[i].x * this.width + 'px';
s.style.top = this.body[i].y * this.height + 'px';
//添加到地图
map.canvas.appendChild(s);
}
}
}
this.run = function(){
for(var i=this.body.length-1; i>0; i--){
this.body[i].x = this.body[i-1].x;
this.body[i].y = this.body[i-1].y
}
switch(this.direction){
case "left":this.body[0].x -= 1;break;
case "right":this.body[0].x += 1;break;
case "up":this.body[0].y -= 1;break;
case "down":this.body[0].y += 1;break;
}
//吃食物
if(this.body[0].x == food.x && this.body[0].y == food.y){
this.body.push({x : null,y : null,flag : null});
map.canvas.removeChild(food.flag);
food = new Food(map);
}
//判断游戏结束
if(this.body[0].x<0 || this.body[0].x>map.xnum-1 || this.body[0].y<0 || this.body[0].y>map.ynum-1){
clearInterval(timer);
alert("GAME OVER!");
restart(map,this);
return false;
}
for(var i=4; i<this.body.length; i++){
if(this.body[0].x == this.body[i].x && this.body[0].y == this.body[i].y){
clearInterval(timer);
alert("GAME OVER!");
restart(map,this);
return false;
}
}
//删除原来
for(var i=0; i<this.body.length; i++){
if(this.body[i].flag != null){
map.canvas.removeChild(this.body[i].flag);
}
}
this.display();
}
}
//创建地图对象
var map = new Map(20,40,20);
map.create();
//创建食物对象
var food = new Food(map);
//创建蛇对象
var snake = new Snake(map);
snake.display();
//加上键盘事件(改变蛇头方向)
window.onkeydown = function(e){
event = e || window.event;
switch(event.keyCode){
case 38:
if(snake.direction != "down")//禁止掉头
snake.direction = "up";
break;
case 40:
if(snake.direction != "up")
snake.direction = "down";
break;
case 37:
if(snake.direction != "right")
snake.direction = "left";
break;
case 39:
if(snake.direction != "left")
snake.direction = "right";
break;
}
}
var speed = 100;
function start(){
clearInterval(timer);
timer = setInterval(function(){
snake.run();
document.getElementById("score").innerHTML = snake.body.length-3;
start();
},speed)
}
//难度
document.getElementById("1").onclick = function(){
speed = 100;
}
document.getElementById("2").onclick = function(){
speed = 50;
}
document.getElementById("3").onclick = function(){
speed = 20;
}
document.getElementById("begin").onclick = function(){
start();
}
document.getElementById("pause").onclick = function(){
clearInterval(timer);
}
</script>
代码仅有js部分,完整代码可以上我的github免费下载
更多有趣的经典小游戏实现专题,分享给大家:
C++经典小游戏汇总
python经典小游戏汇总
python俄罗斯方块游戏集合
JavaScript经典游戏 玩不停
javascript经典小游戏汇总
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2025年11月06日
2025年11月06日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
