电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

原生js編寫貪吃蛇小游戲

瀏覽:117日期:2024-03-26 14:02:57

本文實(shí)例為大家分享了js編寫貪吃蛇小游戲的具體代碼,供大家參考,具體內(nèi)容如下

剛學(xué)完js模仿著教程,把自己寫的js原生小程序。

HTML部分

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta http-equiv='X-UA-Compatible' content='IE=edge'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Document</title> <link rel='stylesheet' href='http://www.hdgsjgj.cn/bcjs/css/index.css' ></head><body> <div class='content'> <!-- 游戲開啟按鈕 --><div class='btn startBtn'><button></button></div><!-- 蛇身 --><div id='snakeWrap'></div> </div> <!-- 引入外部js文件 --> <script src='http://www.hdgsjgj.cn/bcjs/js/index.js'></script></body></html>

css部分

/* 整體樣式 */.content{ width: 640px; height: 640px; margin: 100px auto; position: relative;} .btn{ width: 100%; height: 100%; position: absolute; left: 0; top: 0; background-color: rgba(0, 0, 0, 0.3); z-index: 2;}.btn button{ background: none; border: none; background-size: 100% 100%; cursor: pointer; outline: none; position: absolute; left: 50%; top: 50%;}.startBtn button{ width: 200px; height: 80px; background: url(../images/Snipaste_2021-05-08_08-52-45.png) no-repeat; background-size: contain; margin-left: -100px; margin-top: 222px;}#snakeWrap{ width: 600px; height: 600px; background: #73aad4; border: 20px solid #13649c; position: relative;}.snakeHead{ background-color: yellowgreen; border-radius: 50%;}.snakeBody{ background-color: black; border-radius: 50%;}.food{ background-color: red; border-radius: 50%;}

js部分

var sw = 20,//一個方塊的寬 sh = 20,//一個方塊的寬 tr = 30,//行數(shù) td = 30;//列數(shù)var snake = null, //生成蛇的實(shí)例 food = null; //生成食物的實(shí)例 game = null; //創(chuàng)建游戲?qū)嵗? //把整體看成是一個一個小方塊 移動的的時候創(chuàng)建和刪除方塊(后續(xù)所有方塊的生成都會調(diào)用)// 方塊構(gòu)造函數(shù)function Square(x,y,classname){ //對應(yīng)css中三種蛇的樣式(蛇頭 蛇身 蛇尾) this.x = x * sw; this.y = y * sh; this.class = classname; this.viewContent = document.createElement(’div’); this.viewContent.className = this.class; //將創(chuàng)建出來的div添加對應(yīng)css樣式 this.parent = document.getElementById(’snakeWrap’); }//在方塊構(gòu)造函數(shù)的 原型鏈 上創(chuàng)建create方法 確定新div的具體信息//this指向SquareSquare.prototype.create = function(){ this.viewContent.style.position = ’absolute’; this.viewContent.style.width = sw + ’px’; this.viewContent.style.height = sh + ’px’; this.viewContent.style.left = this.x + ’px’; this.viewContent.style.top = this.y + ’px’; this.parent.appendChild(this.viewContent); //把新創(chuàng)建的div添加到頁面}//在方塊構(gòu)造函數(shù)的 原型鏈 上創(chuàng)建remove方法 用于移動時刪除方塊Square.prototype.remove = function(){ this.parent.removeChild(this.viewContent);}// 蛇function Snake(){ this.head = null; //存儲蛇頭信息 this.tail = null; //存儲蛇尾信息 this.pos = []; //存儲蛇身上的每一個方塊的位置 this.directionNum = { //存儲蛇走的方向left : { x : -1, y : 0},right : { x : 1, y : 0},up : { x : 0, y : -1},down : { x : 0, y : 1} }}//this 指向 SnakeSnake.prototype.init = function(){ //初始化 // 創(chuàng)建蛇頭 var snakeHead = new Square(2,0,’snakeHead’); snakeHead.create(); this.head = snakeHead; this.pos.push([2,0]); //儲存蛇頭信息 // 創(chuàng)建蛇身1 var snakeBody1 = new Square(1,0,’snakeBody’); snakeBody1.create(); this.pos.push([1,0]); //儲存蛇身信息 // 創(chuàng)建蛇尾 var snakeBody2 = new Square(0,0,’snakeBody’); snakeBody2.create(); this.tail = snakeBody2; this.pos.push([0,0]); /儲存蛇尾信心 //形成鏈表關(guān)系 //蛇頭 蛇身 蛇尾的前后關(guān)系 snakeHead.last = null; snakeHead.next = snakeBody1; snakeBody1.last = snakeHead; snakeBody1.next = snakeBody2; snakeBody2.last = snakeBody1; snakeBody2.next = null; //給蛇 添加一個默認(rèn)方向 向右 this.direction = this.directionNum.right;}// 獲取蛇頭的下一個位置對應(yīng)的元素(this指向Snake)// 獲取下一個點(diǎn)的坐標(biāo)并儲存到nextPos數(shù)組Snake.prototype.getNextPos = function(){ var nextPos = [this.head.x/sw + this.direction.x, //this.direction.x、y 下面會將方向與鍵盤事件綁定 來確定下一個點(diǎn)生成的位置this.head.y/sh + this.direction.y ]// 下個點(diǎn)是自己,撞到了自己 游戲結(jié)束 var selfCollied = false; this.pos.forEach(function(value){ //forEach遍歷數(shù)組 兩數(shù)組比較看是否有重復(fù)坐標(biāo)if (value[0] == nextPos[0] && value[1] == nextPos[1]){ selfCollied = true;} }) //撞到了自己 游戲結(jié)束 if(selfCollied){this.、.die.call(this);return; } // 下個點(diǎn)是圍墻 游戲結(jié)束if(nextPos[0] > 29 || nextPos[0] < 0 || nextPos[1] > 29 || nextPos[1] < 0){this.strategies.die.call(this);return; } // 下個點(diǎn)是食物 吃 if(food && food.pos[0] == nextPos[0] && food.pos[1] == nextPos[1]){this.strategies.eat.call(this);return; } // 下個點(diǎn)什么都不是 走 this.strategies.move.call(this);}// 碰撞后要做的事Snake.prototype.strategies = { move : function(format){ //參數(shù)用于判斷是否刪除蛇尾// 創(chuàng)建一個newbody,刪掉蛇頭var newBody = new Square(this.head.x/sw,this.head.y/sh,’snakeBody’)newBody.next = this.head.next;newBody.next.last = newBody;newBody.last = null;this.head.remove();newBody.create();// 創(chuàng)建一個新蛇頭var newx = this.head.x/sw + this.direction.x;var newy = this.head.y/sh + this.direction.y;var newHead = new Square(newx,newy,’snakeHead’)newHead.next = newBody;newBody.last = newHead;newHead.last = null;newHead.create();// 更新蛇身的坐標(biāo)this.pos.splice(0,0,[newx,newy]);this.head = newHead;//如果為false 則吃if(!format){ this.tail.remove(); this.tail = this.tail.last; this.pos.pop();} }, eat : function(){this.strategies.move.call(this,true);game.score ++;createFood(); }, die : function(){game.over(); }}snake = new Snake();// 創(chuàng)建食物function createFood(){ // 食物小方塊坐標(biāo) var x = null; var y = null; var include = true; while(include){x = Math.round(Math.random()*(td - 1));y = Math.round(Math.random()*(tr - 1));snake.pos.forEach(function(value){ if(x != value[0] && y != value[1]){include = false; }}); } // 生成食物 food = new Square(x,y,’food’); food.pos = [x,y]; var foodDom = document.querySelector(’.food’); if(foodDom){foodDom.style.left = x * sw + ’px’;foodDom.style.top = y * sh + ’px’; }else{food.create(); }}// 創(chuàng)建游戲邏輯function Game(){ this.timer = null; this.score = 0;}Game.prototype.init = function(){ snake.init(); createFood(); //這里曾經(jīng)的e.keycode e.which 都已禁用 使用e.key window.addEventListener(’keydown’,function(e){if(e.key == ’ArrowLeft’ && snake.direction != snake.directionNum.right){ snake.direction = snake.directionNum.left;}else if(e.key == ’ArrowUp’ && snake.direction != snake.directionNum.down){ snake.direction = snake.directionNum.up;}else if(e.key == ’ArrowRight’ && snake.direction != snake.directionNum.left){ snake.direction = snake.directionNum.right;}else if(e.key == ’ArrowDown’ && snake.direction != snake.directionNum.up){ snake.direction = snake.directionNum.down;} }); this.start();}Game.prototype.start = function(){ this.timer = setInterval(function(){snake.getNextPos(); },0.0000000000000001)}Game.prototype.over = function(){ clearInterval(this.timer); alert(’你的得分為’ + this.score); // 游戲回到最初始狀態(tài) var snakeWrap = document.getElementById(’snakeWrap’); snakeWrap.innerHTML = ’’; snake = new Snake(); game = new Game(); var startBtnWrap = document.querySelector(’.startBtn’); startBtnWrap.style.display = ’block’;}// 開啟游戲game = new Game();var startBtn = document.querySelector(’.startBtn button’);startBtn.onclick = function(){ startBtn.parentNode.style.display = ’none’; game.init();}

簡單的一個小游戲,如有問題請大佬指正。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 手术室净化厂家-成都做医院净化工程的公司-四川华锐-15年特殊科室建设经验 | 回转窑-水泥|石灰|冶金-巩义市瑞光金属制品有限责任公司 | 贴片电容-贴片电阻-二三极管-国巨|三星|风华贴片电容代理商-深圳伟哲电子 | 顺景erp系统_erp软件_erp软件系统_企业erp管理系统-广东顺景软件科技有限公司 | 无压烧结银_有压烧结银_导电银胶_导电油墨_导电胶-善仁(浙江)新材料 | 微型气泵-真空-蠕动-水泵-厂家-深圳市品亚科技有限公司 | 英超直播_英超免费在线高清直播_英超视频在线观看无插件-24直播网 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 山东艾德实业有限公司| 粤丰硕水性环氧地坪漆-防静电自流平厂家-环保地坪涂料代理 | 搪玻璃冷凝器_厂家-越宏化工设备| 拉力测试机|材料拉伸试验机|电子拉力机价格|万能试验机厂家|苏州皖仪实验仪器有限公司 | 蜘蛛车-高空作业平台-升降机-高空作业车租赁-臂式伸缩臂叉装车-登高车出租厂家 - 普雷斯特机械设备(北京)有限公司 | 耐高温硅酸铝板-硅酸铝棉保温施工|亿欧建设工程 | 不锈钢监控杆_监控立杆厂家-廊坊耀星光电科技有限公司 | 鲁尔圆锥接头多功能测试仪-留置针测试仪-上海威夏环保科技有限公司 | 神超官网_焊接圆锯片_高速钢锯片_硬质合金锯片_浙江神超锯业制造有限公司 | 聚合氯化铝_喷雾聚氯化铝_聚合氯化铝铁厂家_郑州亿升化工有限公司 | 原子吸收设备-国产分光光度计-光谱分光光度计-上海光谱仪器有限公司 | 数控车床-立式加工中心-多功能机床-小型车床-山东临沂金星机床有限公司 | 色油机-色母机-失重|称重式混料机-称重机-米重机-拌料机-[东莞同锐机械]精密计量科技制造商 | 菲希尔FISCHER测厚仪-铁素体检测仪-上海吉馨实业发展有限公司 | 冷镦机-多工位冷镦机-高速冷镦机厂家-温州金诺机械设备制造有限公司 | 层流手术室净化装修-检验科ICU改造施工-华锐净化工程-特殊科室建设厂家 | 壹作文_中小学生优秀满分作文大全| 政府回应:200块在义乌小巷能买到爱情吗?——揭秘打工族省钱约会的生存智慧 | 欧版反击式破碎机-欧版反击破-矿山石料破碎生产线-青州奥凯诺机械 | 上海租车公司_上海包车_奔驰租赁_上海商务租车_上海谐焕租车 | 刹车盘机床-刹车盘生产线-龙口亨嘉智能装备 | 济南铝方通-济南铝方通价格-济南方通厂家-山东鲁方通建材有限公司 | 一体化污水处理设备_生活污水处理设备_全自动加药装置厂家-明基环保 | led太阳能路灯厂家价格_风光互补庭院灯_农村市政工程路灯-中山华可路灯品牌 | 起好名字_取个好名字_好名网免费取好名在线打分 | 技德应用| 温州中研白癜风专科_温州治疗白癜风_温州治疗白癜风医院哪家好_温州哪里治疗白癜风 | 酒万铺-酒水招商-酒水代理 | 小型气象站_车载气象站_便携气象站-山东风途物联网 | 【连江县榕彩涂料有限公司】官方网站| 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 杜康白酒加盟_杜康酒代理_杜康酒招商加盟官网_杜康酒厂加盟总代理—杜康酒神全国运营中心 | 首页_欧瑞传动官方网站--主营变频器、伺服系统、新能源、软起动器、PLC、HMI |