HTML5游戏开发系列教程2(译)
原文地址:http://www.script-tutorials.com/html5-game-development-lesson-2/
今天我们将继续HTML5游戏开发系列。这篇文章继续学习基础知识(可能包含一些高级知识)。将会给你们介绍怎么用渐变色来填充图形,使用特定的字体来绘制文本,还有基本的动画,最重要的是UI元素Button的使用。
之前的文章你可以通过访问http://www.cnblogs.com/pigzhu/archive/2013/05/26/3100018.html来读。我将继续利用前一篇的代码(增强它的功能)。我将利用特定的字体来绘制文本,用渐变色来填充变化大小的正方形,并且会绘制一个按钮,这个按钮用来控制正方形的动画。
第一步:HTML
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>HTML5 Game Development - Lesson 2 | 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-2.0.0.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 2</h2> <a href="http://www.script-tutorials.com/html5-game-development-lesson-2" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a> </footer> </body> </html>
第二步: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 { background-image:url(../images/01.jpg); position:relative; }
第三步:JS
js/jquery-2.0.0.min.js(原文中使用的是1.5.2)
在这个示例中我们使用jQuery,jQuery可以很方便的绑定不同的事件(比如鼠标事件).script.js是最重要的文件,因为它处理了所有的逻辑:
var canvas, ctx;
var circles = [];
var selectedCircle;
var hoveredCircle;
var button;
var moving = false; //标识是否移动圆圈
var speed = 2.0; //圆圈移动速度
function Circle(x, y, radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
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 clear() {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}
function drawCircle(ctx, x, y, radius) {
ctx.fillStyle = 'rgba(255, 35, 55, 1.0)';
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fill();
ctx.lineWidth = 1;
ctx.strokeStyle = 'rgba(0, 0, 0, 1.0)';
ctx.stroke(); //绘制图形的边框
}
function drawScene() {
clear();
//画布上绘制文本
ctx.font = '42px DS-Digital';
ctx.textAlign = 'center';
ctx.fillStyle = '#ffffff';
ctx.fillText('Welcome to lesson #2', ctx.canvas.width / 2, 50);
//渐变色
var bg_gradient = ctx.createLinearGradient(0, 200, 0, 400);
bg_gradient.addColorStop(0.0, 'rgba(255, 0, 0, 0.8)');
bg_gradient.addColorStop(0.5, 'rgba(0, 255, 0, 0.8)');
bg_gradient.addColorStop(1.0, 'rgba(0, 0, 255, 0.8)');
ctx.beginPath();
ctx.fillStyle = bg_gradient;
ctx.moveTo(circles[0].x, circles[0].y);
for (var i = 0; i < circles.length; i++) {
ctx.lineTo(circles[i].x, circles[i].y);
}
ctx.closePath();
ctx.fill();
ctx.lineWidth = 2;
ctx.strokeStyle = 'rgba(0, 0, 255, 0.5)';
ctx.stroke();
//改变移动方向
if (circles[0].x <= 300 || circles[0].x >= 385) {
speed = -speed;
}
if (moving) {
circles[0].x -= speed;
circles[0].y -= speed;
circles[1].x += speed;
circles[1].y -= speed;
circles[2].x += speed;
circles[2].y += speed;
circles[3].x -= speed;
circles[3].y += speed;
}
drawCircle(ctx, circles[0].x, circles[0].y, (hoveredCircle == 0) ? 25 : 15);
drawCircle(ctx, circles[1].x, circles[1].y, (hoveredCircle == 1) ? 25 : 15);
drawCircle(ctx, circles[2].x, circles[2].y, (hoveredCircle == 2) ? 25 : 15);
drawCircle(ctx, circles[3].x, circles[3].y, (hoveredCircle == 3) ? 25 : 15);
//绘制图片
ctx.drawImage(button.image, 0, button.imageShift, button.w, button.h, button.x, button.y, button.w, button.h);
//ctx.drawImage(button.image, 0, 0, button.w, button.h, button.x, button.y, button.w, button.h);
ctx.font = '30px DS-Digital';
ctx.fillStyle = '#ffffff';
ctx.fillText('Play/Pause', 135, 480);
ctx.fillText(button.state, 135, 515);
}
$(function() {
canvas = document.getElementById('scene');
ctx = canvas.getContext('2d'); //获得画图操作上下文
var circleRadius = 15;
var width = canvas.width;
var height = canvas.height;
circles.push(new Circle(width / 2 - 20, height / 2 - 20, circleRadius)); //左上角的圆圈
circles.push(new Circle(width / 2 + 20, height / 2 - 20, circleRadius));
circles.push(new Circle(width / 2 + 20, height / 2 + 20, circleRadius));
circles.push(new Circle(width / 2 - 20, height / 2 + 20, circleRadius));
buttonImage = new Image();
buttonImage.src = 'images/button.png';
buttonImage.onload = function() {
};
button = new Button(50, 450, 180, 120, 'normal', buttonImage);
$('#scene').mousedown(function(e) {
var mouseX = e.originalEvent.layerX || 0;
var mouseY = e.originalEvent.layerY || 0;
for (var i = 0; i < circles.length; i++) {
var circleX = circles[i].x;
var circleY = circles[i].y;
var radius = circles[i].radius;
if (Math.pow(mouseX - circleX, 2) + Math.pow(mouseY - circleY, 2) < Math.pow(radius, 2)) {
selectedCircle = i;
break;
}
}
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) {
var mouseX = e.originalEvent.layerX || 0;
var mouseY = e.originalEvent.layerY || 0;
if (selectedCircle != undefined) {
var radius = circles[selectedCircle].radius;
circles[selectedCircle] = new Circle(mouseX, mouseY, radius);
}
hoveredCircle = undefined;
for (var i = 0; i < circles.length; i++) {
var circleX = circles[i].x;
var circleY = circles[i].y;
var radius = circles[i].radius;
if (Math.pow(mouseX - circleX, 2) + Math.pow(mouseY - circleY, 2) < Math.pow(radius, 2)) {
hoveredCircle = i;
circles[hoveredCircle] = new Circle(circleX, circleY, 25);
break;
}
}
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) {
selectedCircle = undefined;
if (button.state == 'pressed') {
moving = !moving;
}
button.state = 'normal';
button.imageShift = 0;
});
setInterval(drawScene, 30);
});
上面代码中没有理解Button中imageShift的作用,哪位知道告诉我下?