1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      如何使用HTML5實(shí)現(xiàn)貪吃蛇游戲

      這篇文章將為大家詳細(xì)講解有關(guān)如何使用HTML5實(shí)現(xiàn)貪吃蛇游戲,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

      北流網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,北流網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為北流成百上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個(gè)售后服務(wù)好的北流做網(wǎng)站的公司定做!

      游戲操作說明

      通過方向鍵控制貪吃蛇上下左右移動(dòng)。貪吃蛇吃到食物之后會(huì)變長一個(gè)長度。

      游戲具體實(shí)現(xiàn)

      游戲難點(diǎn)是怎么模擬貪吃蛇的移動(dòng)。如果只是一個(gè)方塊的話顯然很簡單。但是當(dāng)蛇的長度變長之后要怎么樣控制

      每個(gè)方塊的移動(dòng)呢?

      如果觀察蛇的移動(dòng),可以發(fā)現(xiàn),從蛇的頭部到尾部,每個(gè)方塊在下一時(shí)刻的位置就是它的前一個(gè)方塊在當(dāng)前時(shí)刻

      的位置。因此我們需要做的只是控制貪吃蛇的頭部的運(yùn)動(dòng)。其他部分的位置都可以依次類推。

      另外一個(gè)值得注意的問題是

      貪吃蛇吃下食物之后,新增加的方塊應(yīng)該放在哪個(gè)位置。

      答案就是在下一時(shí)刻,新增加的方塊應(yīng)該出現(xiàn)在當(dāng)前時(shí)刻的尾部位置。

      因此,在吃下食物之后應(yīng)該在更新蛇的每個(gè)位置之前,增加一個(gè)方塊,并且將其位置設(shè)定在當(dāng)前時(shí)刻的尾部位置。

      然后在當(dāng)前時(shí)刻更新出了新增方塊之外的所有方塊的位置

      index.html

      snake.js

      var canvas;

      var ctx;

      var timer;

      //measures

      var x_cnt = 15;

      var y_cnt = 15;

      var unit = 48;

      var box_x = 0;

      var box_y = 0;

      var box_width = 15 * unit;

      var box_height = 15 * unit;

      var bound_left = box_x;

      var bound_right = box_x + box_width;

      var bound_up = box_y;

      var bound_down = box_y + box_height;

      //images

      var image_sprite;

      //objects

      var snake;

      var food;

      var food_x;

      var food_y;

      //functions

      function Role(sx, sy, sw, sh, direction, status, speed, image, flag)

      {

      this.x = sx;

      this.y = sy;

      this.w = sw;

      this.h = sh;

      this.direction = direction;

      this.status = status;

      this.speed = speed;

      this.image = image;

      this.flag = flag;

      }

      function transfer(keyCode)

      {

      switch (keyCode)

      {

      case 37:

      return 1;

      case 38:

      return 3;

      case 39:

      return 2;

      case 40:

      return 0;

      }

      }

      function addFood()

      {

      //food_x=box_x+Math.floor(Math.random()*(box_width-unit));

      //food_y=box_y+Math.floor(Math.random()*(box_height-unit));

      food_x = unit * Math.floor(Math.random() * x_cnt);

      food_y = unit * Math.floor(Math.random() * y_cnt);

      food = new Role(food_x, food_y, unit, unit, 0, 0, 0, image_sprite, true);

      }

      function play(event)

      {

      var keyCode;

      if (event == null)

      {

      keyCode = window.event.keyCode;

      window.event.preventDefault();

      }

      else

      {

      keyCode = event.keyCode;

      event.preventDefault();

      }

      var cur_direction = transfer(keyCode);

      snake[0].direction = cur_direction;

      }

      function update()

      {

      //add a new part to the snake before move the snake

      if (snake[0].x == food.x && snake[0].y == food.y)

      {

      var length = snake.length;

      var tail_x = snake[length - 1].x;

      var tail_y = snake[length - 1].y;

      var tail = new Role(tail_x, tail_y, unit, unit, snake[length - 1].direction, 0, 0, image_sprite, true);

      snake.push(tail);

      addFood();

      }

      //modify attributes

      //move the head

      switch (snake[0].direction)

      {

      case 0: //down

      snake[0].y += unit;

      if (snake[0].y > bound_down - unit)

      snake[0].y = bound_down - unit;

      break;

      case 1: //left

      snake[0].x -= unit;

      if (snake[0].x < bound_left)

      snake[0].x = bound_left;

      break;

      case 2: //right

      snake[0].x += unit;

      if (snake[0].x > bound_right - unit)

      snake[0].x = bound_right - unit;

      break;

      case 3: //up

      snake[0].y -= unit;

      if (snake[0].y < bound_up)

      snake[0].y = bound_up;

      break;

      }

      //move other part of the snake

      for (var i = snake.length - 1; i >= 0; i--)

      {

      if (i > 0)

      //snake[i].direction=snake[i-1].direction;

      {

      snake[i].x = snake[i - 1].x;

      snake[i].y = snake[i - 1].y;

      }

      }

      }

      function drawScene()

      {

      ctx.clearRect(box_x, box_y, box_width, box_height);

      ctx.strokeStyle = "rgb(0,0,0";

      ctx.strokeRect(box_x, box_y, box_width, box_height);

      //detection collisions

      //draw images

      for (var i = 0; i < snake.length; i++)

      {

      ctx.drawImage(image_sprite, snake[i].x, snake[i].y);

      }

      ctx.drawImage(image_sprite, food.x, food.y);

      }

      function init()

      {

      canvas = document.getElementById("scene");

      ctx = canvas.getContext('2d');

      //images

      image_sprite = new Image();

      image_sprite.src = "images/sprite.png";

      image_sprite.onLoad = function ()

      {}

      //ojects

      snake = new Array();

      var head = new Role(0 * unit, 0 * unit, unit, unit, 5, 0, 1, image_sprite, true);

      snake.push(head);

      window.addEventListener('keydown', play, false);

      addFood();

      setInterval(update, 300); //note

      setInterval(drawScene, 30);

      }

      關(guān)于“如何使用HTML5實(shí)現(xiàn)貪吃蛇游戲”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。


      分享題目:如何使用HTML5實(shí)現(xiàn)貪吃蛇游戲
      本文地址:http://www.ef60e0e.cn/article/ijhesh.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        东海县| 洛南县| 商南县| 新宾| 平塘县| 郯城县| 黄陵县| 永安市| 贡嘎县| 肇州县| 棋牌| 元谋县| 阜城县| 集贤县| 高要市| 聂拉木县| 朝阳市| 福海县| 定日县| 保山市| 长海县| 桂东县| 台南市| 长兴县| 方城县| 军事| 枞阳县| 甘泉县| 汕头市| 平安县| 阿拉善左旗| 府谷县| 仁寿县| 华安县| 北川| 那曲县| 手游| 霞浦县| 呼伦贝尔市| 开化县| 莲花县|