HTML5 Game Development – Lesson 3 Today we continue a series of articles on game development in HTML5 using canvas. Finally, today we will start adding animation, and few more interesting features. Our demonstration will include a spaceship flying through space, and a new element – the Dialogue. The dialogue will contain two pages, and our button will toggle the dialog pages + hide the dialog on the second click. Our previous article you can read here: Developing Your First HTML5 Game – Lesson 2. Our base script will taken from previous letter.
HTML5游戏开发–第3课今天,我们继续使用画布在HTML5中开发游戏的一系列文章。 最后,今天我们将开始添加动画以及更多有趣的功能。 我们的演示将包括在太空中飞行的宇宙飞船,以及一个新元素–对话。 对话框将包含两个页面,并且我们的按钮将切换对话框页面,并在第二次单击时隐藏对话框。 您可以在此处阅读上一篇文章: 开发您的第一个HTML5游戏–第2课 。 我们的基本脚本将取自上一封信。
Here are our demo and downloadable package:
这是我们的演示和可下载的软件包:
现场演示
[sociallocker]
[社交储物柜]
打包下载
[/sociallocker]
[/ sociallocker]
Ok, download the example files and lets start coding !
好的,下载示例文件并开始编码!
步骤1. HTML (Step 1. HTML)
Here are all html of my demo
这是我的演示的全部html
index.html (index.html)
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>HTML5 Game Development - Lesson 3 | Script Tutorials</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script type="text/javascript" src="js/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</head>
<body>
<div class="container">
<canvas id="scene" width="800" height="600"></canvas>
</div>
<footer>
<h2>HTML5 Game Development - Lesson 3</h2>
<a href="https://www.script-tutorials.com/html5-game-development-navigating-your-spaceship-lesson-3" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
</footer>
</body>
</html>
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>HTML5 Game Development - Lesson 3 | Script Tutorials</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script type="text/javascript" src="js/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</head>
<body>
<div class="container">
<canvas id="scene" width="800" height="600"></canvas>
</div>
<footer>
<h2>HTML5 Game Development - Lesson 3</h2>
<a href="https://www.script-tutorials.com/html5-game-development-navigating-your-spaceship-lesson-3" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
</footer>
</body>
</html>
步骤2. CSS (Step 2. CSS)
Here are used CSS styles.
这是使用CSS样式。
css / main.css (css/main.css)
/* general styles */
*{
margin:0;
padding:0;
}
@font-face {
font-family: "DS-Digital";
src: url("../fonts/Ds-digib.ttf");
}
body {
background-color:#bababa;
background-image: -webkit-radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
background-image: -moz-radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
background-image: -o-radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
background-image: radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
color:#fff;
font:14px/1.3 Arial,sans-serif;
min-height:1000px;
}
.container {
width:100%;
}
.container > * {
display:block;
margin:50px auto;
}
footer {
background-color:#212121;
bottom:0;
box-shadow: 0 -1px 2px #111111;
display:block;
height:70px;
left:0;
position:fixed;
width:100%;
z-index:100;
}
footer h2{
font-size:22px;
font-weight:normal;
left:50%;
margin-left:-400px;
padding:22px 0;
position:absolute;
width:540px;
}
footer a.stuts,a.stuts:visited{
border:none;
text-decoration:none;
color:#fcfcfc;
font-size:14px;
left:50%;
line-height:31px;
margin:23px 0 0 110px;
position:absolute;
top:0;
}
footer .stuts span {
font-size:22px;
font-weight:bold;
margin-left:5px;
}
h3 {
text-align:center;
}
#scene {
position:relative;
}
/* general styles */
*{
margin:0;
padding:0;
}
@font-face {
font-family: "DS-Digital";
src: url("../fonts/Ds-digib.ttf");
}
body {
background-color:#bababa;
background-image: -webkit-radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
background-image: -moz-radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
background-image: -o-radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
background-image: radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
color:#fff;
font:14px/1.3 Arial,sans-serif;
min-height:1000px;
}
.container {
width:100%;
}
.container > * {
display:block;
margin:50px auto;
}
footer {
background-color:#212121;
bottom:0;
box-shadow: 0 -1px 2px #111111;
display:block;
height:70px;
left:0;
position:fixed;
width:100%;
z-index:100;
}
footer h2{
font-size:22px;
font-weight:normal;
left:50%;
margin-left:-400px;
padding:22px 0;
position:absolute;
width:540px;
}
footer a.stuts,a.stuts:visited{
border:none;
text-decoration:none;
color:#fcfcfc;
font-size:14px;
left:50%;
line-height:31px;
margin:23px 0 0 110px;
position:absolute;
top:0;
}
footer .stuts span {
font-size:22px;
font-weight:bold;
margin-left:5px;
}
h3 {
text-align:center;
}
#scene {
position:relative;
}
Pay attention to ‘@font-face’. We will using this way to link custom font file (ttf) to our lesson (to draw at canvas).
注意“ @ font-face”。 我们将使用这种方式将自定义字体文件(ttf)链接到我们的课程(在画布上绘制)。
步骤3. JS (Step 3. JS)
js / jquery-1.5.2.min.js (js/jquery-1.5.2.min.js)
We will using jQuery for our demo. This allows easy bind different events (for mouse etc). Next file most important (here are all our html5 functional):
我们将使用jQuery进行演示。 这样可以轻松绑定不同事件(例如鼠标等)。 下一个最重要的文件(这是我们所有的html5功能):
js / script.js (js/script.js)
// inner variables
var canvas, ctx;
var button;
var backgroundImage;
var spaceShip;
var iBgShiftX = 1024;
var bDrawDialog = true;
var iDialogPage = 1;
// -------------------------------------------------------------
// objects :
function Button(x, y, w, h, state, image) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.state = state;
this.imageShift = 0;
this.image = image;
}
function SpaceShip(x, y, w, h, image) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.image = image;
this.bDrag = false;
}
// -------------------------------------------------------------
// draw functions :
function clear() { // clear canvas function
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}
function drawDialog() { // draw dialog function
if (bDrawDialog) {
var bg_gradient = ctx.createLinearGradient(0, 200, 0, 400);
bg_gradient.addColorStop(0.0, 'rgba(160, 160, 160, 0.8)');
bg_gradient.addColorStop(1.0, 'rgba(250, 250, 250, 0.8)');
ctx.beginPath(); // custom shape begin
ctx.fillStyle = bg_gradient;
ctx.moveTo(100, 100);
ctx.lineTo(700, 100);
ctx.lineTo(700, 500);
ctx.lineTo(100, 500);
ctx.lineTo(100, 100);
ctx.closePath(); // custom shape end
ctx.fill(); // fill custom shape
ctx.lineWidth = 2;
ctx.strokeStyle = 'rgba(128, 128, 128, 0.5)';
ctx.stroke(); // draw border
// draw the title text
ctx.font = '42px DS-Digital';
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
ctx.shadowColor = '#000';
ctx.shadowOffsetX = 2;
ctx.shadowOffsetY = 2;
ctx.shadowBlur = 2;
ctx.fillStyle = '#fff';
if (iDialogPage == 1) {
ctx.fillText('Welcome to lesson #3', ctx.canvas.width/2, 150);
ctx.font = '24px DS-Digital';
ctx.fillText('After closing dialog you will able', ctx.canvas.width/2, 250);
ctx.fillText('to handle with spaceship with your mouse', ctx.canvas.width/2, 280);
} else if (iDialogPage == 2) {
ctx.fillText('Second page of dialog', ctx.canvas.width/2, 150);
ctx.font = '24px DS-Digital';
ctx.fillText('Any another text', ctx.canvas.width/2, 250);
}
}
}
function drawScene() { // main drawScene function
clear(); // clear canvas
// draw background
iBgShiftX -= 10;
if (iBgShiftX <= 0) {
iBgShiftX = 1024;
}
ctx.drawImage(backgroundImage, 0 + iBgShiftX, 0, 1024, 768, 0, 0, 800, 600);
// draw space ship
ctx.drawImage(spaceShip.image, 0, 0, spaceShip.w, spaceShip.h, spaceShip.x-128, spaceShip.y-128, spaceShip.w, spaceShip.h);
// draw dialog
drawDialog();
// draw button
ctx.drawImage(button.image, 0, button.imageShift, button.w, button.h, button.x, button.y, button.w, button.h);
// draw button's text
ctx.font = '22px DS-Digital';
ctx.fillStyle = '#ffffff';
ctx.fillText('next/hide/show', 400, 465);
ctx.fillText('dialog', 400, 500);
}
// -------------------------------------------------------------
// initialization
$(function(){
canvas = document.getElementById('scene');
ctx = canvas.getContext('2d');
var width = canvas.width;
var height = canvas.height;
// load background image
backgroundImage = new Image();
backgroundImage.src = 'images/stars.jpg';
backgroundImage.onload = function() {
}
backgroundImage.onerror = function() {
console.log('Error loading the background image.');
}
// initialization of space ship
var oSpShipImage = new Image();
oSpShipImage.src = 'images/space_ship.png';
oSpShipImage.onload = function() {
}
spaceShip = new SpaceShip(400, 300, 256, 256, oSpShipImage);
// load the button sprite image
var buttonImage = new Image();
buttonImage.src = 'images/button.png';
buttonImage.onload = function() {
}
button = new Button(310, 450, 180, 120, 'normal', buttonImage);
$('#scene').mousedown(function(e) { // binding mousedown event (for dragging)
var mouseX = e.layerX || 0;
var mouseY = e.layerY || 0;
if (!bDrawDialog &&
mouseX > spaceShip.x-128 && mouseX < spaceShip.x-128+spaceShip.w &&
mouseY > spaceShip.y-128 && mouseY < spaceShip.y-128+spaceShip.h) {
spaceShip.bDrag = true;
spaceShip.x = mouseX;
spaceShip.y = mouseY;
}
// button behavior
if (mouseX > button.x && mouseX < button.x+button.w && mouseY > button.y && mouseY < button.y+button.h) {
button.state = 'pressed';
button.imageShift = 262;
}
});
$('#scene').mousemove(function(e) { // binding mousemove event
var mouseX = e.layerX || 0;
var mouseY = e.layerY || 0;
if (!bDrawDialog && spaceShip.bDrag) {
spaceShip.x = mouseX;
spaceShip.y = mouseY;
}
// button behavior
if (button.state != 'pressed') {
button.state = 'normal';
button.imageShift = 0;
if (mouseX > button.x && mouseX < button.x+button.w && mouseY > button.y && mouseY < button.y+button.h) {
button.state = 'hover';
button.imageShift = 131;
}
}
});
$('#scene').mouseup(function(e) { // binding mouseup event
spaceShip.bDrag = false;
// button behavior
if (button.state == 'pressed') {
if (iDialogPage == 0) {
iDialogPage++;
bDrawDialog = !bDrawDialog;
} else if (iDialogPage == 2) {
iDialogPage = 0;
bDrawDialog = !bDrawDialog;
} else {
iDialogPage++;
}
}
button.state = 'normal';
button.imageShift = 0;
});
setInterval(drawScene, 30); // loop drawScene
});
// inner variables
var canvas, ctx;
var button;
var backgroundImage;
var spaceShip;
var iBgShiftX = 1024;
var bDrawDialog = true;
var iDialogPage = 1;
// -------------------------------------------------------------
// objects :
function Button(x, y, w, h, state, image) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.state = state;
this.imageShift = 0;
this.image = image;
}
function SpaceShip(x, y, w, h, image) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.image = image;
this.bDrag = false;
}
// -------------------------------------------------------------
// draw functions :
function clear() { // clear canvas function
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}
function drawDialog() { // draw dialog function
if (bDrawDialog) {
var bg_gradient = ctx.createLinearGradient(0, 200, 0, 400);
bg_gradient.addColorStop(0.0, 'rgba(160, 160, 160, 0.8)');
bg_gradient.addColorStop(1.0, 'rgba(250, 250, 250, 0.8)');
ctx.beginPath(); // custom shape begin
ctx.fillStyle = bg_gradient;
ctx.moveTo(100, 100);
ctx.lineTo(700, 100);
ctx.lineTo(700, 500);
ctx.lineTo(100, 500);
ctx.lineTo(100, 100);
ctx.closePath(); // custom shape end
ctx.fill(); // fill custom shape
ctx.lineWidth = 2;
ctx.strokeStyle = 'rgba(128, 128, 128, 0.5)';
ctx.stroke(); // draw border
// draw the title text
ctx.font = '42px DS-Digital';
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
ctx.shadowColor = '#000';
ctx.shadowOffsetX = 2;
ctx.shadowOffsetY = 2;
ctx.shadowBlur = 2;
ctx.fillStyle = '#fff';
if (iDialogPage == 1) {
ctx.fillText('Welcome to lesson #3', ctx.canvas.width/2, 150);
ctx.font = '24px DS-Digital';
ctx.fillText('After closing dialog you will able', ctx.canvas.width/2, 250);
ctx.fillText('to handle with spaceship with your mouse', ctx.canvas.width/2, 280);
} else if (iDialogPage == 2) {
ctx.fillText('Second page of dialog', ctx.canvas.width/2, 150);
ctx.font = '24px DS-Digital';
ctx.fillText('Any another text', ctx.canvas.width/2, 250);
}
}
}
function drawScene() { // main drawScene function
clear(); // clear canvas
// draw background
iBgShiftX -= 10;
if (iBgShiftX <= 0) {
iBgShiftX = 1024;
}
ctx.drawImage(backgroundImage, 0 + iBgShiftX, 0, 1024, 768, 0, 0, 800, 600);
// draw space ship
ctx.drawImage(spaceShip.image, 0, 0, spaceShip.w, spaceShip.h, spaceShip.x-128, spaceShip.y-128, spaceShip.w, spaceShip.h);
// draw dialog
drawDialog();
// draw button
ctx.drawImage(button.image, 0, button.imageShift, button.w, button.h, button.x, button.y, button.w, button.h);
// draw button's text
ctx.font = '22px DS-Digital';
ctx.fillStyle = '#ffffff';
ctx.fillText('next/hide/show', 400, 465);
ctx.fillText('dialog', 400, 500);
}
// -------------------------------------------------------------
// initialization
$(function(){
canvas = document.getElementById('scene');
ctx = canvas.getContext('2d');
var width = canvas.width;
var height = canvas.height;
// load background image
backgroundImage = new Image();
backgroundImage.src = 'images/stars.jpg';
backgroundImage.onload = function() {
}
backgroundImage.onerror = function() {
console.log('Error loading the background image.');
}
// initialization of space ship
var oSpShipImage = new Image();
oSpShipImage.src = 'images/space_ship.png';
oSpShipImage.onload = function() {
}
spaceShip = new SpaceShip(400, 300, 256, 256, oSpShipImage);
// load the button sprite image
var buttonImage = new Image();
buttonImage.src = 'images/button.png';
buttonImage.onload = function() {
}
button = new Button(310, 450, 180, 120, 'normal', buttonImage);
$('#scene').mousedown(function(e) { // binding mousedown event (for dragging)
var mouseX = e.layerX || 0;
var mouseY = e.layerY || 0;
if (!bDrawDialog &&
mouseX > spaceShip.x-128 && mouseX < spaceShip.x-128+spaceShip.w &&
mouseY > spaceShip.y-128 && mouseY < spaceShip.y-128+spaceShip.h) {
spaceShip.bDrag = true;
spaceShip.x = mouseX;
spaceShip.y = mouseY;
}
// button behavior
if (mouseX > button.x && mouseX < button.x+button.w && mouseY > button.y && mouseY < button.y+button.h) {
button.state = 'pressed';
button.imageShift = 262;
}
});
$('#scene').mousemove(function(e) { // binding mousemove event
var mouseX = e.layerX || 0;
var mouseY = e.layerY || 0;
if (!bDrawDialog && spaceShip.bDrag) {
spaceShip.x = mouseX;
spaceShip.y = mouseY;
}
// button behavior
if (button.state != 'pressed') {
button.state = 'normal';
button.imageShift = 0;
if (mouseX > button.x && mouseX < button.x+button.w && mouseY > button.y && mouseY < button.y+button.h) {
button.state = 'hover';
button.imageShift = 131;
}
}
});
$('#scene').mouseup(function(e) { // binding mouseup event
spaceShip.bDrag = false;
// button behavior
if (button.state == 'pressed') {
if (iDialogPage == 0) {
iDialogPage++;
bDrawDialog = !bDrawDialog;
} else if (iDialogPage == 2) {
iDialogPage = 0;
bDrawDialog = !bDrawDialog;
} else {
iDialogPage++;
}
}
button.state = 'normal';
button.imageShift = 0;
});
setInterval(drawScene, 30); // loop drawScene
});
Here are several explanations about new features. 1. Drawing animated space with stars (pretty easy – we will shift image coordinates):
以下是有关新功能的几种说明。 1.用星星绘制动画空间(非常简单–我们将移动图像坐标):
iBgShiftX -= 10;
if (iBgShiftX <= 0) {
iBgShiftX = 1024;
}
ctx.drawImage(backgroundImage, 0 + iBgShiftX, 0, 1024, 768, 0, 0, 800, 600);
iBgShiftX -= 10;
if (iBgShiftX <= 0) {
iBgShiftX = 1024;
}
ctx.drawImage(backgroundImage, 0 + iBgShiftX, 0, 1024, 768, 0, 0, 800, 600);
Hope that all rest code pretty understandable. I added comments quite anywhere.
希望所有其余代码都可以理解。 我在任何地方都添加了评论。
步骤4.自定义文件 (Step 4. Custom files)
字体/Ds-digib.ttf、images/button.png、images/space_ship.png和images / stars.jpg (fonts/Ds-digib.ttf, images/button.png, images/space_ship.png and images/stars.jpg)
All these files will available in our package
所有这些文件将在我们的软件包中提供
现场演示
Separate big thanks to this book. This tell us about development of games in HTML5. Now it’s one of my favorite books :-)
非常感谢这本书 。 这告诉我们有关HTML5中游戏开发的信息。 现在,这是我最喜欢的书之一:-)
结论 (Conclusion)
Are you like our new spaceship? :-) I will be glad to see your thanks and comments. Good luck!
你喜欢我们的新飞船吗? :-)很高兴看到您的感谢和评论。 祝好运!
翻译自: https://www.script-tutorials.com/html5-game-development-navigating-your-spaceship-lesson-3/