最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js實現(xiàn)貪吃蛇游戲(簡易版)

 更新時間:2020年09月29日 08:03:53   作者:穩(wěn)不住了,他們能贏  
這篇文章主要為大家詳細(xì)介紹了js實現(xiàn)貪吃蛇游戲簡易版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

直接開始

效果圖:

項目結(jié)構(gòu):圖片自己找的

1.html

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8" />
 <title></title>

 <style type="text/css">
  * {
  padding: 0;
  margin: 0;
  }
 </style>
 </head>
 <body>
 <canvas id='view' width="400" height="400" style="border:1px solid red;"></canvas>
 <button id='start'>開始</button>
 <button id='parse'>暫停</button>
 <button id='restart'>重新開始</button>
 <h4>最高分: <span id='scoreMax' style='color:red;'>0</span></h4>
 <h4>分?jǐn)?shù): <span id='score' style='color:skyblue;'>0</span></h4>
 <script src="js/config.js" type="text/javascript" charset="utf-8"></script>
 <script src="js/score.js" type="text/javascript" charset="utf-8"></script>
 <script src="js/area.js" type="text/javascript" charset="utf-8"></script>
 <script src="js/food.js" type="text/javascript" charset="utf-8"></script>
 <script src="js/snake.js" type="text/javascript" charset="utf-8"></script>
 <script src="js/move.js" type="text/javascript" charset="utf-8"></script>
 <script src="js/init.js" type="text/javascript" charset="utf-8"></script>
 <script type="text/javascript">
  var start = document.getElementById('start');
  var parse = document.getElementById('parse');
  var restart = document.getElementById('restart');
  start.onclick = function() {
  parse = false;
  }
  parse.onclick = function() {
  parse = true;
  }
  restart.onclick = function() {
  snake && snake.restart();
  parse = false;
  }
 </script>
 </body>
</html>

2.area.js

function render(x, y, color, img) {
 ctx.beginPath();
 ctx.fillStyle = color || 'red';
 if (img) {
 ctx.drawImage(img, x * w, y * h, img.width, img.height);
 } else {
 ctx.fillRect(x * w, y * h, w, h);
 }
 ctx.closePath();
 ctx.fill();
}

function clear(x, y) {
 ctx.clearRect(x * w, y * h, w, h);
}

3.config.js

var exit = [];

var parse = true;

var view = document.getElementById('view');
var ctx = view.getContext('2d');
var width = 400,
 height = 400;
var w = 20,
 h = 20;
var maxX = (width / w) - 1,
 maxY = (height / h) - 1;
var speed = 500;
var scoreMax = 0;

var imgsrcs = [
 './img/snakeheadup.png', //圖片自己找
 './img/snakeheaddown.png',
 './img/snakeheadleft.png',
 './img/snakeheadright.png'
];
var imgs = [];
var length = 4;
for (var i = 0, len = imgsrcs.length; i < len; i++) {
 var img = new Image(20,20);
 img.src = imgsrcs[i];
 imgs[i] = img;
}

var food, snake, move, score;

4.food.js

function Food() {
 this.init();
}

Food.prototype = {
 init: function() {
 this.update();
 },
 update: function() {
 var food = this.makeCoordinate();
 this.food = food;
 var foodX = food[0];
 var foodY = food[1];
 this.render(foodX, foodY);
 },
 render: function(x, y) {
 render(x, y, 'blue');
 },
 makeCoordinate: function() {
 var x = this.random(0, maxX);
 var y = this.random(0, maxY);
 for (var i = 0; i < exit.length; i++) {
  if (exit[i].toString() == [x, y].toString()) {
  return this.makeCoordinate();
  }
 }
 return [x, y];
 },
 random: function(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
 }
}

5.init.js

function init() {
 food = new Food();
 snake = new Snake();
 move = new Move();
 score = new Score();
 var timer = setInterval(function() {
 if (!parse) {
  snake.move();
 }
 }, speed);
}

for (var j = 0; j < imgs.length; j++) {
 imgs[j].onload = function() {
 length--;
 if (length == 0) {
  init();
 }
 }
}

6.move.js

function Move() {

}

Move.prototype = {
 up: function(s) {
 snake.move('up');
 },
 down: function(s) {
 snake.move('down');
 },
 left: function(s) {
 snake.move('left');
 },
 right: function(s) {
 snake.move('right');
 }
}


document.onkeydown = function(e) {
 var key = e.keyCode;
 if(!parse){
 switch (key) {
  case 37:
  case 65:
  move.left(snake);
  break;
  case 38:
  case 87:
  move.up(snake);
  break;
  case 39:
  case 68:
  move.right(snake);
  break;
  case 40:
  case 83:
  move.down(snake);
  break;
  default:
  break;
 }
 }
};

7.score.js

var scoreDom = document.getElementById('score');
var scoreMaxDom = document.getElementById('scoreMax');

function Score() {
 this.score = 0;
}

Score.prototype = {
 scoreAdd: function() {
 this.score += 1;
 scoreDom.innerHTML = this.score;
 },
 scoreClear: function() {
 this.score = 0;
 scoreDom.innerHTML = 0;
 },
 highScore: function() {
 scoreMax = scoreMax > this.score ? scoreMax : this.score;
 scoreMaxDom.innerHTML = scoreMax;
 }
}

8.snake.js

function Snake() {
 this.head = [0, 5, imgs[1]]; // x,y坐標(biāo),頭
 this.body = []; // 二維數(shù)組,身體
 this.snake = []; //完整的蛇
 this.init(6);
 this.direction = 'down'; // 默認(rèn)向下運動
}

Snake.prototype = {
 init: function(len) {
 len = len ? len > 10 ? 10 : len : 5;
 var head = this.head = [0, len, imgs[1]];
 var body = this.body;
 for (var i = 0; i < len; i++) {
  body.push([0, i]);
 }
 this.getSnake();
 },
 restart: function() {
 ctx.clearRect(0, 0, width, height);
 this.head = [0, 5, imgs[1]]; // x,y坐標(biāo),頭
 this.body = []; // 二維數(shù)組,身體
 this.snake = []; //完整的蛇
 exit = [];
 this.direction = 'down'; // 默認(rèn)向下運動
 this.init(6);
 food.update();
 score.scoreClear();
 },
 eat: function() {
 var head = [this.head[0], this.head[1]];
 var body = this.body;
 if (food.food.toString() == head.toString()) {
  food.update();
  score.scoreAdd();
  return true;
 }
 return false;
 },
 update: function(direction) {
 var oldDirection = this.direction;
 this.direction = direction || this.direction;
 if ((this.direction == 'down' && oldDirection == 'up') ||
  (this.direction == 'up' && oldDirection == 'down') ||
  (this.direction == 'left' && oldDirection == 'right') ||
  (this.direction == 'right' && oldDirection == 'left')) {
  this.direction = oldDirection;
 }
 if (this.direction == 'left' && oldDirection != 'right') {
  this.head[0] -= 1;
  this.head[2] = imgs[2];
 } else if (this.direction == 'right' && oldDirection != 'left') {
  this.head[0] += 1;
  this.head[2] = imgs[3];
 } else if (this.direction == 'up' && oldDirection != 'down') {
  this.head[1] -= 1;
  this.head[2] = imgs[0];
 } else if (this.direction == 'down' && oldDirection != 'up') {
  this.head[1] += 1;
  this.head[2] = imgs[1];
 }
 this.die();
 },
 gameOver: function() {
 alert('game over!')
 console.log('game over!');
 parse = true;
 score.highScore();
 },
 die: function() {
 var head = this.head;
 var x = head[0],
  y = head[1];
 if (x < 0 || x > maxX || y < 0 || y > maxY) {
  this.gameOver();
 }
 var body = this.body;
 for (var i = 0; i < body.length; i++) {
  if (body[i].toString() == [x, y].toString()) {
  this.gameOver();
  }
 }
 },
 render: function() {
 var snake = this.snake;
 for (var i = 0, len = snake.length; i < len; i++) {
  var item = snake[i];
  render(item[0], item[1], 'green', item[2]);
 }
 },
 move: function(direction) {
 var head = this.head;
 var headCopy = head.slice(); //之前的頭
 var body = this.body;
 body.push([headCopy[0], headCopy[1]]);
 this.update(direction);
 var eat = this.eat();
 if (!eat) {
  var del = body.shift();
  clear(del[0], del[1]);
 }
 this.getSnake();
 },
 getSnake: function() {
 var head = this.head;
 var body = this.body;
 var snake = this.snake = [];
 for (var i = 0, l = body.length; i < l; i++) {
  snake.push(body[i]);
 }
 snake.push(head);
 exit = [];
 for (var i = 0; i < snake.length; i++) {
  exit.push(snake[i]);
 }
 this.render();
 }
}

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

相關(guān)文章

  • javascript sort()對數(shù)組中的元素進(jìn)行排序詳解

    javascript sort()對數(shù)組中的元素進(jìn)行排序詳解

    在本篇文章里小編給大家整理是一篇關(guān)于javascript sort()對數(shù)組中的元素進(jìn)行排序的相關(guān)知識點內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-10-10
  • JavaScript 對象深入學(xué)習(xí)總結(jié)(經(jīng)典)

    JavaScript 對象深入學(xué)習(xí)總結(jié)(經(jīng)典)

    JavaScript中,除了五種原始類型(即數(shù)字,字符串,布爾值,null,undefined)之外的都是對象了,所以,不把對象學(xué)明白怎么繼續(xù)往下學(xué)習(xí)呢?本篇文章給大家分享javascript對象深入學(xué)習(xí)總結(jié),小伙伴們跟著小編一起深入學(xué)習(xí)吧
    2015-09-09
  • JS如何循環(huán)遍歷JSON數(shù)據(jù)

    JS如何循環(huán)遍歷JSON數(shù)據(jù)

    這篇文章主要介紹了JS如何循環(huán)遍歷JSON數(shù)據(jù)的方法,本文提供了 JS 循環(huán) JSON 數(shù)據(jù)列,以及 JS 循環(huán)遍歷 JSON 數(shù)據(jù)的例子,需要的朋友可以參考下
    2024-01-01
  • javascript?中動畫制作方法?animate()屬性

    javascript?中動畫制作方法?animate()屬性

    這篇文章主要介紹了javascript?中動畫制作方法?animate()屬性,animate是所有dom元素都有的方法,可以用來最做過度動畫,關(guān)鍵幀動畫,下面文章的相關(guān)介紹需要的小伙伴可以參考一下
    2022-04-04
  • 原生js編寫設(shè)為首頁兼容ie、火狐和谷歌

    原生js編寫設(shè)為首頁兼容ie、火狐和谷歌

    這篇文章主要介紹了如何通過原生js實現(xiàn)設(shè)為首頁并兼容ie、火狐和谷歌,需要的朋友可以參考下
    2014-06-06
  • 移動端網(wǎng)頁開發(fā)調(diào)試神器Eruda的介紹與使用技巧

    移動端網(wǎng)頁開發(fā)調(diào)試神器Eruda的介紹與使用技巧

    在日常的移動端開發(fā)時,一般都是試用chrome瀏覽器的移動端模式進(jìn)行開發(fā)和調(diào)試,只有在chrome調(diào)試完成,而最近發(fā)現(xiàn)了一個新的調(diào)試方法,所以這篇文章主要給大家介紹了關(guān)于移動端網(wǎng)頁開發(fā)調(diào)試神器Eruda的基本資料,以及其使用的一些技巧,需要的朋友可以參考下。
    2017-10-10
  • 用原生JS獲取CLASS對象(很簡單實用)

    用原生JS獲取CLASS對象(很簡單實用)

    這篇文章主要介紹了如何用原生JS獲取CLASS對象,看過dom編程藝術(shù)的朋友或許會知道的
    2014-10-10
  • 如何利用JavaScript?實現(xiàn)繼承

    如何利用JavaScript?實現(xiàn)繼承

    這篇文章主要介紹了如何利用JavaScript?實現(xiàn)繼承,JavaScript?在編程語言界是個特殊種類,它和其他編程語言很不一樣,JavaScript可以在運行的時候動態(tài)地改變某個變量的類型,下面小編將繼續(xù)介紹JavaScript如何實現(xiàn)繼承,需要的朋友可以參考下
    2022-02-02
  • IE JS無提示關(guān)閉窗口不提示的方法

    IE JS無提示關(guān)閉窗口不提示的方法

    好多情況下,用戶想關(guān)閉頁面不需要提示,提高用戶體驗,下面的代碼經(jīng)腳本之家測試通過。
    2010-04-04
  • 一文詳解JS?類型轉(zhuǎn)換方法以及如何避免隱式轉(zhuǎn)換

    一文詳解JS?類型轉(zhuǎn)換方法以及如何避免隱式轉(zhuǎn)換

    這篇文章主要為大家介紹了JS?類型轉(zhuǎn)換方法以及如何避免隱式轉(zhuǎn)換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04

最新評論

江华| 鄂托克前旗| 炎陵县| 海林市| 凤庆县| 马尔康县| 曲松县| 南充市| 韶山市| 修水县| 龙山县| 慈溪市| 塔城市| 西林县| 建宁县| 沙湾县| 分宜县| 高陵县| 芜湖县| 红河县| 牙克石市| 甘孜| 宝清县| 蓝田县| 石柱| 封丘县| 鸡泽县| 义乌市| 保靖县| 同德县| 山西省| 昭觉县| 吴旗县| 东乡| 兰考县| 保山市| 河北省| 喀喇沁旗| 海阳市| 颍上县| 奉贤区|