亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

原生js編寫貪吃蛇小游戲

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

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

剛學完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.aoyou183.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.aoyou183.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,//行數 td = 30;//列數var snake = null, //生成蛇的實例 food = null; //生成食物的實例 game = null; //創建游戲實例 //把整體看成是一個一個小方塊 移動的的時候創建和刪除方塊(后續所有方塊的生成都會調用)// 方塊構造函數function Square(x,y,classname){ //對應css中三種蛇的樣式(蛇頭 蛇身 蛇尾) this.x = x * sw; this.y = y * sh; this.class = classname; this.viewContent = document.createElement(’div’); this.viewContent.className = this.class; //將創建出來的div添加對應css樣式 this.parent = document.getElementById(’snakeWrap’); }//在方塊構造函數的 原型鏈 上創建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); //把新創建的div添加到頁面}//在方塊構造函數的 原型鏈 上創建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(){ //初始化 // 創建蛇頭 var snakeHead = new Square(2,0,’snakeHead’); snakeHead.create(); this.head = snakeHead; this.pos.push([2,0]); //儲存蛇頭信息 // 創建蛇身1 var snakeBody1 = new Square(1,0,’snakeBody’); snakeBody1.create(); this.pos.push([1,0]); //儲存蛇身信息 // 創建蛇尾 var snakeBody2 = new Square(0,0,’snakeBody’); snakeBody2.create(); this.tail = snakeBody2; this.pos.push([0,0]); /儲存蛇尾信心 //形成鏈表關系 //蛇頭 蛇身 蛇尾的前后關系 snakeHead.last = null; snakeHead.next = snakeBody1; snakeBody1.last = snakeHead; snakeBody1.next = snakeBody2; snakeBody2.last = snakeBody1; snakeBody2.next = null; //給蛇 添加一個默認方向 向右 this.direction = this.directionNum.right;}// 獲取蛇頭的下一個位置對應的元素(this指向Snake)// 獲取下一個點的坐標并儲存到nextPos數組Snake.prototype.getNextPos = function(){ var nextPos = [this.head.x/sw + this.direction.x, //this.direction.x、y 下面會將方向與鍵盤事件綁定 來確定下一個點生成的位置this.head.y/sh + this.direction.y ]// 下個點是自己,撞到了自己 游戲結束 var selfCollied = false; this.pos.forEach(function(value){ //forEach遍歷數組 兩數組比較看是否有重復坐標if (value[0] == nextPos[0] && value[1] == nextPos[1]){ selfCollied = true;} }) //撞到了自己 游戲結束 if(selfCollied){this.、.die.call(this);return; } // 下個點是圍墻 游戲結束if(nextPos[0] > 29 || nextPos[0] < 0 || nextPos[1] > 29 || nextPos[1] < 0){this.strategies.die.call(this);return; } // 下個點是食物 吃 if(food && food.pos[0] == nextPos[0] && food.pos[1] == nextPos[1]){this.strategies.eat.call(this);return; } // 下個點什么都不是 走 this.strategies.move.call(this);}// 碰撞后要做的事Snake.prototype.strategies = { move : function(format){ //參數用于判斷是否刪除蛇尾// 創建一個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();// 創建一個新蛇頭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();// 更新蛇身的坐標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();// 創建食物function createFood(){ // 食物小方塊坐標 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(); }}// 創建游戲邏輯function Game(){ this.timer = null; this.score = 0;}Game.prototype.init = function(){ snake.init(); createFood(); //這里曾經的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); // 游戲回到最初始狀態 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();}

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 成人网在线| 亚洲国产成人久久综合野外 | 欧美 日韩 国产 成人 在线观看 | 美美女高清毛片视频黄的一免费 | 国产乱理伦片在线观看大陆 | 亚洲国产成人精彩精品 | 天堂mv亚洲mv在线播放9蜜 | 乱爱性全过程免费视频 | 欧美一区二区三区免费不卡 | 国产精品视频免费播放 | 欧美毛片aaa激情 | 97国产大学生情侣11在线视频 | 中国hd高清╳xxx | 可以在线观看的黄色网址 | 精品欧美日韩一区二区 | 免费观看欧美成人h | 国产综合久久一区二区三区 | 韩国黄色一级毛片 | 成年网址网站在线观看 | 中文字幕无线码一区二区三区 | aaa国产一级毛片 | 久久久全国免费视频 | 在线精品福利视频你懂的 | 老妇女五级毛片 | 国产一区视频在线播放 | 最新国产美女一区二区三区 | 免费在线你懂的 | 1024手机在线观看视频 | 久草热线视频 | 欧美日本韩国一区二区 | 国产精品第三页 | 狠狠色综合久久丁香婷婷 | 免费一级毛片一级毛片aa | 六月丁香激情综合成人 | 日本黄色小视频网站 | 免费精品国产日韩热久久 | 视色4se成人午夜精品 | 欧美精品中出 | 亚洲精品播放 | 1769国内精品视频在线观看 | 国产精品不卡高清在线观看 |