詳細介紹html5小遊戲製作思路的程式碼實例

黄舟
發布: 2017-03-20 15:29:43
原創
3368 人瀏覽過

html5小遊戲製作想法詳解

簡介

建立畫布

遊戲循環

Hello world

創建player

鍵盤控制

a:使用jQuery Hotkeys

b:移動player

添加更多遊戲元素

砲彈

敵人

使用圖片

撞擊偵測

聲音

簡介

你想使用HTML5的Canvas製作一款遊戲嗎?跟著這個教程,你將立刻上道兒。

閱讀該教學需要至少熟悉javascript相關知識。

你可以先玩這款遊戲或直接閱讀文章並且下載遊戲原始碼。

建立畫布

在畫任何東西之前,我們必須先建立一個畫布。因為這是完全指南,並且我們將使用jQuery.

var CANVAS_WIDTH = 480; var CANVAS_HEIGHT = 320; var canvasElement = $(""); var canvas = canvasElement.get(0).getContext("2d"); canvasElement.appendTo('body');
登入後複製

遊戲循環

為了呈現給玩家連貫流暢的遊戲動畫,我們要頻繁地渲染畫布來欺騙玩家的眼睛。

var FPS = 30; setInterval(function() { update(); draw(); }, 1000/FPS);
登入後複製

現在我們先不管update和draw裡面的實現,重要的是我們要知道setInterval()會週期性的執行update和draw

Hello world
登入後複製

現在我們已經搭建好了一個循環的架子,我們去修改update和draw方法來寫一些文字到螢幕。

function draw() { canvas.fillStyle = "#000"; // Set color to black canvas.fillText("Sup Bro!", 50, 50); }
登入後複製

專家提醒: 當你稍微更改了一些程式碼的時候就執行一下程序,這樣可以更快的找到程序出錯地方。

靜止文字正常的顯示出來了。因為我們已經有了循環,所以我們可以很容易地讓文字動起來~~~

var textX = 50; var textY = 50; function update() { textX += 1; textY += 1; } function draw() { canvas.fillStyle = "#000"; canvas.fillText("Sup Bro!", textX, textY); }
登入後複製

執行程式。如果你一步一步照著上面做下來,可以看到文字移動。但是上一次的文字卻還留在螢幕上,因為我們沒有擦除畫布。現在我們在draw方法中加入擦除方法。

function draw() { canvas.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); canvas.fillStyle = "#000"; canvas.fillText("Sup Bro!", textX, textY); }
登入後複製

現在你可以看到文字在螢幕上移動了,它已經算是一個真正意義上的遊戲,只不過是個半成品。

建立player

建立一個包含player所有資訊的物件,並且要有draw方法。這裡創建了一個簡單的物件包含了所有的player資訊。

var player = { color: "#00A", x: 220, y: 270, width: 32, height: 32, draw: function() { canvas.fillStyle = this.color; canvas.fillRect(this.x, this.y, this.width, this.height); } };
登入後複製

我們現在用一個純色的矩形來代表player.當我們把它加入遊戲當中的時候,我們需要清除畫布並且畫上player.

function draw() { canvas.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); player.draw(); }
登入後複製

鍵盤控制

使用jQuery Hotkeys

jQuery Hotkeys plugin在處理鍵盤行為的時候,可以更容易的相容不同的瀏覽器。讓開發者不用因為不同瀏覽器之間的keyCode andcharCode不同而苦惱,我們這樣綁定事件

$(document).bind("keydown", "left", function() { ... }); 移动player function update() { if (keydown.left) { player.x -= 2; } if (keydown.right) { player.x += 2; } }
登入後複製

是不是感覺移動不夠快?那我們來提高它的移動速度。

function update() { if (keydown.left) { player.x -= 5; } if (keydown.right) { player.x += 5; } player.x = player.x.clamp(0, CANVAS_WIDTH - player.width); }
登入後複製

我們可以很容易的加入其他元素,像是砲彈:

function update() { if (keydown.space) { player.shoot(); } if (keydown.left) { player.x -= 5; } if (keydown.right) { player.x += 5; } player.x = player.x.clamp(0, CANVAS_WIDTH - player.width); } player.shoot = function() { console.log("Pew pew"); // :) Well at least adding the key binding was easy... };
登入後複製

加入更多遊戲元素

砲彈

我們開始真正意義上的加上砲彈,首先,我們需要一個集合來儲存它:

var playerBullets = [];
登入後複製

然後,我們需要一個構造器來創建砲彈:

function Bullet(I) { I.active = true; I.xVelocity = 0; I.yVelocity = -I.speed; I.width = 3; I.height = 3; I.color = "#000"; I.inBounds = function() { return I.x >= 0 && I.x <= CANVAS_WIDTH && I.y >= 0 && I.y <= CANVAS_HEIGHT; }; I.draw = function() { canvas.fillStyle = this.color; canvas.fillRect(this.x, this.y, this.width, this.height); }; I.update = function() { I.x += I.xVelocity; I.y += I.yVelocity; I.active = I.active && I.inBounds(); }; return I; }
登入後複製

當玩家開火,我們需要向集合中新增砲彈:

player.shoot = function() { var bulletPosition = this.midpoint(); playerBullets.push(Bullet({ speed: 5, x: bulletPosition.x, y: bulletPosition.y })); }; player.midpoint = function() { return { x: this.x + this.width/2, y: this.y + this.height/2 }; };
登入後複製

修改update和draw方法,實作開火:

function update() { ... playerBullets.forEach(function(bullet) { bullet.update(); }); playerBullets = playerBullets.filter(function(bullet) { return bullet.active; }); } function draw() { ... playerBullets.forEach(function(bullet) { bullet.draw(); }); }
登入後複製
#敵人
enemies = []; function Enemy(I) { I = I || {}; I.active = true; I.age = Math.floor(Math.random() * 128); I.color = "#A2B"; I.x = CANVAS_WIDTH / 4 + Math.random() * CANVAS_WIDTH / 2; I.y = 0; I.xVelocity = 0 I.yVelocity = 2; I.width = 32; I.height = 32; I.inBounds = function() { return I.x >= 0 && I.x <= CANVAS_WIDTH && I.y >= 0 && I.y <= CANVAS_HEIGHT; }; I.draw = function() { canvas.fillStyle = this.color; canvas.fillRect(this.x, this.y, this.width, this.height); }; I.update = function() { I.x += I.xVelocity; I.y += I.yVelocity; I.xVelocity = 3 * Math.sin(I.age * Math.PI / 64); I.age++; I.active = I.active && I.inBounds(); }; return I; }; function update() { ... enemies.forEach(function(enemy) { enemy.update(); }); enemies = enemies.filter(function(enemy) { return enemy.active; }); if(Math.random() < 0.1) { enemies.push(Enemy()); } }; function draw() { ... enemies.forEach(function(enemy) { enemy.draw(); }); }
登入後複製

使用圖片

rrreee

碰撞偵測

player.sprite = Sprite("player"); player.draw = function() { this.sprite.draw(canvas, this.x, this.y); }; function Enemy(I) { ... I.sprite = Sprite("enemy"); I.draw = function() { this.sprite.draw(canvas, this.x, this.y); }; ... }
登入後複製

加入聲音

function collides(a, b) { return a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y; } function handleCollisions() { playerBullets.forEach(function(bullet) { enemies.forEach(function(enemy) { if (collides(bullet, enemy)) { enemy.explode(); bullet.active = false; } }); }); enemies.forEach(function(enemy) { if (collides(enemy, player)) { enemy.explode(); player.explode(); } }); } function update() { ... handleCollisions(); } function Enemy(I) { ... I.explode = function() { this.active = false; // Extra Credit: Add an explosion graphic }; return I; }; player.explode = function() { this.active = false; // Extra Credit: Add an explosion graphic and then end the game };
登入後複製

以上是詳細介紹html5小遊戲製作思路的程式碼實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!