【讀書筆記】Html5遊戲開發

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   os   io   for   

      一直對HMTL5做遊戲饒有興趣,而這本書剛好就是HTML5 2遊戲初級入門的書。Demo簡單注釋詳細,可以拿來練練手,一個星期左右就可以讀完。若要追求酷炫高大上效果,這本書恐怕要讓你失望了。但作為上手書還是不錯的。

 

     http://pan.baidu.com/s/1dD29Nhf 

    一共十章,都是類似於下面的小遊戲,從淺到深。  Demo下載

  

    圖形和圖片的繪製都很簡單,關鍵的地方還是用數組和定時器去實現遊戲的商務邏輯和效果。簡單的本機存放區、聲音視頻播放。但含金量太少了,不能滿足學遊戲的胃口。噹噹上面評價卻不錯。 書的出發點也是做基本的入門。The Essential Guide to Html5

   1.基本圖形:

//ball 球function Ball(sx, sy, rad, stylestring) {    this.sx = sx;    this.sy = sy;    this.rad = rad;    this.draw = drawball;    this.moveit = moveball;    this.fillstyle = stylestring;}function drawball() {    ctx.fillStyle = this.fillstyle;    ctx.beginPath();    //ctx.fillStyle= rgb(0,0,0);    ctx.arc(this.sx, this.sy, this.rad, 0, Math.PI * 2, true);    ctx.fill();}function moveball(dx, dy) {    this.sx += dx;    this.sy += dy;}//Rect 方形function Myrectangle(sx, sy, swidth, sheight, stylestring) {    this.sx = sx;    this.sy = sy;    this.swidth = swidth;    this.sheight = sheight;    this.fillstyle = stylestring;    this.draw = drawrects;    this.moveit = moveball;//move方法是一樣的}function drawrects() {    ctx.fillStyle = this.fillstyle;    ctx.fillRect(this.sx, this.sy, this.swidth, this.sheight);}//多邊形function Polycard(sx, sy, rad, n, frontbgcolor, backcolor, polycolor) {    this.sx = sx;    this.sy = sy;    this.rad = rad;    this.draw = drawpoly;    this.frontbgcolor = frontbgcolor;    this.backcolor = backcolor;    this.polycolor = polycolor;    this.n = n;    this.angle = (2 * Math.PI) / n;  //parens may not be needed.    this.moveit = generalmove;}//畫多邊形function drawpoly() {    ctx.fillStyle = this.frontbgcolor;    ctx.strokeStyle = this.backcolor;    ctx.fillRect(this.sx - 2 * this.rad, this.sy - 2 * this.rad, 4 * this.rad, 4 * this.rad);    ctx.beginPath();    ctx.fillStyle = this.polycolor;    var i;    var rad = this.rad;    ctx.beginPath();    ctx.moveTo(this.sx + rad * Math.cos(-.5 * this.angle), this.sy + rad * Math.sin(-.5 * this.angle));    for (i = 1; i < this.n; i++) {        ctx.lineTo(this.sx + rad * Math.cos((i - .5) * this.angle), this.sy + rad * Math.sin((i - .5) * this.angle));    }    ctx.fill();}function generalmove(dx, dy) {    this.sx += dx;    this.sy += dy;}//映像function Picture(sx, sy, swidth, sheight, imga) {    this.sx = sx;    this.sy = sy;    this.img = imga;    this.swidth = swidth;    this.sheight = sheight;    this.draw = drawAnImage;}function drawAnImage() {    ctx.drawImage(this.img, this.sx, this.sy, this.swidth, this.sheight);}
View Code

  2.擷取滑鼠位置:

function check(ev) {    var mx;    var my;    if (ev.layerX || ev.layerX == 0) { // Firefox        mx = ev.layerX;        my = ev.layerY;    } else if (ev.offsetX || ev.offsetX == 0) { // Opera        mx = ev.offsetX;        my = ev.offsetY;    }
// do something}

 3. 擷取按鍵輸入:

function getkey(event) {  var keyCode;   if(event == null)  {    keyCode = window.event.keyCode;     window.event.preventDefault();  }  else   {    keyCode = event.keyCode;     event.preventDefault();  }  switch(keyCode)  {      case 68:  //按下D       deal();      break;      case 72:   //按下H     playerdone();      break;      case 78: //按下N     newgame();       break;     default:    alert("Press d, h, or n.");      }   }

4. 添加事件監聽:

      var canvas1 = document.getElementById(‘canvas‘);        canvas1.addEventListener(‘mousedown‘, startwall, false);//false表示事件冒泡的順序。        canvas1.addEventListener(‘mousemove‘, stretchwall, false);        canvas1.addEventListener(‘mouseup‘, finish, false);

5.運動的圖形一般都是統一載入在一個數組中,定時器每觸發一次就重繪一次。每一個對象都有draw方法。

    var mypent = new Token(100, 100, 20, "rgb(0,0,250)", 5);    everything.push(mypent);    function drawall() {        ctx.clearRect(0, 0, cwidth, cheight);        var i;        for (i = 0; i < everything.length; i++) {            everything[i].draw();        }    }

6.javascript物件導向的能力沒有那些進階語言強,很多功能的實現都是巧妙的運用了數組。比如洗牌的動作。

      //洗牌就是更換了牌的位置  function shuffle() {  var i = deck.length - 1;//deck代表一副牌  var s;  while (i>0) {//這裡迴圈一次 每張牌平均更換了兩次位置      s = Math.floor(Math.random()*(i+1));//隨機範圍是0-i (包括i)      swapindeck(s,i);//交換位置      i--;  }  }  function swapindeck(j,k) {    var hold = new MCard(deck[j].num,deck[j].suit,deck[j].picture.src); //MCard 是一張牌的對象。    deck[j] = deck[k];    deck[k] = hold; }

7.很多地方要用到數學知識:比如小球碰撞,就需要改變x和y的運動方向即可。判斷是否在擊中目標。就是判斷xy是否在一定的區間。但判斷一個移動的物體能不能經過前面的路,且不能能穿越牆。就有點複雜了。像迷宮那個遊戲。本質是要判斷線段到球心的距離不小於球的半徑。

//迷宮遊戲
//移動目標
function
movetoken(dx, dy) { this.sx += dx;//正常情況下 this.sy += dy; var i; var wall; for (i = 0; i < walls.length; i++) { wall = walls[i];
//如果發生相交 就要退回去 效果就是沒有移動
if (intersect(wall.sx, wall.sy, wall.fx, wall.fy, this.sx, this.sy, this.rad)) { this.sx -= dx; this.sy -= dy; break; } } }function intersect(sx, sy, fx, fy, cx, cy, rad) { var dx; var dy; var t; var rt; dx = fx - sx; dy = fy - sy; t = 0.0 - ((sx - cx) * dx + (sy - cy) * dy) / ((dx * dx) + (dy * dy)); if (t < 0.0) { t = 0.0; } else if (t > 1.0) { t = 1.0; } dx = (sx+t*(fx-sx))-cx; dy = (sy +t*(fy-sy))-cy; rt = (dx*dx) +(dy*dy); if (rt<(rad*rad)) { return true; } else { return false;} }

詳細的解釋在書的166頁。 

希望對你有協助~ 

正在找下一本Html5的書,路過大神求推薦。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.