溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在html5中使用canvas創(chuàng)建一個(gè)太空游戲

發(fā)布時(shí)間:2021-03-25 17:33:05 來(lái)源:億速云 閱讀:209 作者:Leah 欄目:web開(kāi)發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)怎么在html5中使用canvas創(chuàng)建一個(gè)太空游戲,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。


1.向網(wǎng)頁(yè)添加 Canvas 元素
2.創(chuàng)建黑色背景
3.在背景上繪制隨機(jī)星星
4.向背景添加宇宙飛船
代碼示例的末尾是討論材料,說(shuō)明有關(guān)這些任務(wù)的設(shè)計(jì)和結(jié)構(gòu)以及工作方式的詳細(xì)信息。
Canvas 代碼示例:

代碼如下:


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
// This function is called on page load.
function canvasSpaceGame() {
// Get the canvas element.
canvas = document.getElementById("myCanvas");
// Make sure you got it.
if (canvas.getContext)
// If you have it, create a canvas user inteface element.
{
// Specify 2d canvas type.
ctx = canvas.getContext("2d");</p> <p>// Paint it black.
ctx.fillStyle = "black";
ctx.rect(0, 0, 300, 300);
ctx.fill();</p> <p>// Paint the starfield.
stars();</p> <p>// Draw space ship.
makeShip();
}
}</p> <p>// Paint a random starfield.</p> <p>
function stars() {</p> <p>// Draw 50 stars.
for (i = 0; i <= 50; i++) {
// Get random positions for stars.
var x = Math.floor(Math.random() * 299)
var y = Math.floor(Math.random() * 299)</p> <p>// Make the stars white
ctx.fillStyle = "white";</p> <p>// Give the ship some room.
if (x < 30 || y < 30) ctx.fillStyle = "black";</p> <p>// Draw an individual star.
ctx.beginPath();
ctx.arc(x, y, 3, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fill();
}
}</p> <p>function makeShip() {</p> <p>// Draw saucer bottom.
ctx.beginPath();
ctx.moveTo(28.4, 16.9);
ctx.bezierCurveTo(28.4, 19.7, 22.9, 22.0, 16.0, 22.0);
ctx.bezierCurveTo(9.1, 22.0, 3.6, 19.7, 3.6, 16.9);
ctx.bezierCurveTo(3.6, 14.1, 9.1, 11.8, 16.0, 11.8);
ctx.bezierCurveTo(22.9, 11.8, 28.4, 14.1, 28.4, 16.9);
ctx.closePath();
ctx.fillStyle = "rgb(222, 103, 0)";
ctx.fill();</p> <p>// Draw saucer top.
ctx.beginPath();
ctx.moveTo(22.3, 12.0);
ctx.bezierCurveTo(22.3, 13.3, 19.4, 14.3, 15.9, 14.3);
ctx.bezierCurveTo(12.4, 14.3, 9.6, 13.3, 9.6, 12.0);
ctx.bezierCurveTo(9.6, 10.8, 12.4, 9.7, 15.9, 9.7);
ctx.bezierCurveTo(19.4, 9.7, 22.3, 10.8, 22.3, 12.0);
ctx.closePath();
ctx.fillStyle = "rgb(51, 190, 0)";
ctx.fill();
}
</script>
</head>
<body onload="canvasSpaceGame()">
<h2>
Canvas Space Game
</h2>
<canvas id="myCanvas" width="300" height="300">
</canvas>
</body>
</html>

Canvas 代碼示例討論

本節(jié)說(shuō)明本代碼示例的設(shè)計(jì)和結(jié)構(gòu)。 它為您講解代碼的不同部分,以及整合它們的方式。 Canvas 示例使用標(biāo)準(zhǔn) HTML5 標(biāo)頭 ,以便瀏覽器可以將它作為 HTML5 規(guī)格的一部分加以區(qū)別。

代碼分成兩個(gè)主要部分:
1.主體代碼
2.腳本代碼

主體代碼
在頁(yè)面加載時(shí),主體標(biāo)記使用 onload 函數(shù)調(diào)用 canvasSpaceGame 函數(shù)。 Canvas 標(biāo)記是主體的一部分。 指定了 Canvas 初始寬度和高度,還指定了 ID 屬性。 必須使用 ID,才能將 canvas 元素添加到頁(yè)面的對(duì)象模型中。

腳本代碼
腳本代碼包括三個(gè)函數(shù): canvasSpaceGame、stars 和 makeShip。 加載頁(yè)面時(shí)將調(diào)用 canvasSpaceGame 函數(shù)。 stars 和 makeShip 都是從 canvasSpaceGame 調(diào)用的。

canvasSpaceGame 函數(shù)
加載頁(yè)面時(shí)將調(diào)用此函數(shù)。 它通過(guò)在主體代碼中使用 Canvas 元素 ID 來(lái)獲取畫(huà)布, 然后獲取畫(huà)布的上下文,并準(zhǔn)備好接受繪圖。 將上下文初始化為 2D 畫(huà)布后,使用 fillStyle、rect 和 fill 方法將畫(huà)布繪制為黑色。

stars 函數(shù)
此函數(shù)是從 canvasSpaceGame 調(diào)用的。 它使用 for loop 在二維平面上生成 50 個(gè)潛在的星星位置,然后使用 fillStyle 創(chuàng)建白色。 隨后,會(huì)進(jìn)行一項(xiàng)檢查,確認(rèn) x,y 坐標(biāo)是否與左上角過(guò)于靠近。 如果星星繪制得與左上角過(guò)于靠近,則會(huì)將 fillStyle 更改為黑色,使其不會(huì)妨礙宇宙飛船。 隨后,使用 arc 方法繪制每個(gè)星星并使用相應(yīng)的填充顏色。
makeShip
此函數(shù)是從 canvasSpaceGame 調(diào)用的。 使用一系列的 beginPath、moveTo、bezierCurveTo、closePath、fillStyle 和 fill 方法,繪制一個(gè)簡(jiǎn)單的宇宙飛船。
飛船是通過(guò)繪制兩個(gè)橢圓來(lái)創(chuàng)建的,一個(gè)橢圓在另一個(gè)的上面。 它首先在 Adobe Illustrator CS5 中繪制,然后使用 的 Ai2Canvas 插件將圖像導(dǎo)出到 Canvas。 

關(guān)于怎么在html5中使用canvas創(chuàng)建一個(gè)太空游戲就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI