DIV CSS 佈局教程網

純 JavaScript 打造小遊戲《Flappy Pig》實例
編輯:關於JavaScript     

本文分享一個用原生 JavaScript 寫的網頁小遊戲《Flappy Pig》——可以把它理解為「像素豬版」的 Flappy Bird。作者用了一個周末把它寫了出來,並把代碼拆分為多個模塊,結構清晰,很適合學習原生 JS 做遊戲的思路。

整個項目按職責拆分為若干文件:option.js 存放全局配置(重力、跳躍初速度、柱子速度、關卡數、精靈尺寸等);util.js 提供工具方法(阻止默認事件、$、獲取子元素);pig.js 負責小豬的物理運動;pillar.js 生成並移動柱子;position.js 做碰撞檢測;controller.js 與 game.js 則是控制與主流程。下面挑選幾個關鍵模塊展示。

option.js(配置):

 var flappy = (function (self) { 'use strict'; self.option = { g: 400, //重力加速度 v0: 400, //跳躍初速度 vp: 2.5, //柱子移動速度 frequency: 20, //動畫頻率(ms) levels: 100, //關卡數 safeLift: 500, floorHeight: 64, pigWidth: 33, pigHeight: 30, pigY: 300, pigLeft: 80, pillarHtml: '<div class="top"></div><div class="bottom"></div>', pillarWidth: 45, pillarGapY: 108, pillarGapX: 250, pillarTop: -550, pillarBottom: -500 }; return self; })(flappy || {}) 

pig.js(小豬物理):

 var flappy = (function (self) { 'use strict'; var option = self.option, $ = self.util.$; self.pig = { Y: 0, init: function (overCallback, controller) { var t = this; t.s = 0; t.time = 0; t.$pig = $('pig'); t.$pig.style.left = option.pigLeft + 'px'; t._controller = controller; t._addListener(overCallback); }, _addListener: function (overCallback) { this._overCallback = overCallback; }, start: function () { var t = this, interval = option.frequency / 1000; t.s = option.v0 * t.time - t.time * t.time * option.g * 2; t.Y = option.pigY + t.s; if (t.Y >= option.floorHeight) { t.$pig.style.bottom = t.Y + 'px'; } else { t._dead(); } t.time += interval; }, jump: function () { var t = this; option.pigY = parseInt(t.$pig.style.bottom); t.s = 0; t.time = 0; }, _dead: function () { this._overCallback.call(this._controller); } }; return self; })(flappy || {}) 

game.js(主流程):

 var flappy = (function (self) { 'use strict'; var pig = self.pig, pillar = self.pillar, pos = self.position, util = self.util, $ = util.$; self.game = { init: function () { var t = this; t._isStart = false; t._isEnd = false; t._timer = null; pig.init(t.fall, t); pillar.init(); pos.init(t.hit, t); t.addKeyListener(); }, addKeyListener: function () { var t = this; document.onkeydown = function (e) { var e = e || event; var currKey = e.keyCode || e.which || e.charCode; if (currKey == 32) { if (!t._isEnd) { t.jump(); } else { window.location.reload(); } util.preventDefaultEvent(e); } } }, jump: function () { var t = this; if (!t._isStart) { $('start').style.display = 'none'; t._createTimer(function () { pig.start(); pillar.move(); pos.judge(); $('score').innerHTML = pillar.currentId + 1; }); t._isStart = true; } else { pig.jump(); } }, hit: function () { var t = this; t.over(); pig.hit(); }, over: function () { var t = this; clearInterval(t._timer); t._isEnd = true; $('end').style.display = 'block'; }, _createTimer: function (fn) { var t = this; t._timer = setInterval(fn, option.frequency); } }; flappy.init = function () { self.game.init(); } return self; })(flappy || {}) 

小豬的運動採用豎直上拋運動公式 s = v0·t − ½·g·t²,配合鼠標或空格鍵( keyCode 32)控制跳躍;柱子由 pillar.js 隨機生成高度並持續左移;position.js 通過比較小豬與當前柱子的上下左右邊界判斷碰撞。整個遊戲用 setInterval 以固定頻率驅動,模塊之間通過全局對象 flappy 進行解耦,是學習原生 JS 遊戲開發的不錯範例。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved