(译)开发第一个Html5游戏(三)
这一章加入了图片滚动功能,已经实现了一个游戏对话框。
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
});
原文地址 http://www.script-tutorials.com/html5-game-development-navigating-your-spaceship-lesson-3/
Demo地址 http://www.script-tutorials.com/demos/166/index.html
源码下载 http://www.script-tutorials.com/demos/166/source.zip
分享到:
相关推荐
《HTML5 Canvas游戏开发实战》在介绍每个游戏开发的过程时,都会包括游戏分析、开发过程、代码解析和小结等相关内容,以帮助读者了解每种类型游戏开发的详细步骤,让读者彻底掌握各种类型游戏的开发思想。...
首先,我们来看标题中的"蘑菇熊",这很可能是游戏的主题或主角,展示了如何设计和创建一个游戏的角色和场景。在HTML5游戏中,角色和场景通常通过CSS3来定义样式,用JavaScript来实现动态行为。源码中的每个"index...
HTML5游戏开发素材 HTML5游戏开发素材 HTML5游戏开发素材 HTML5游戏开发素材
《HTML5 Canvas游戏开发实战》.(张路斌).pdf电子高清扫描版
3. **HTML5**:作为项目的开发基础,HTML5的跨平台性和现代浏览器的广泛支持使得这个游戏可以在多种设备上运行,包括桌面电脑、智能手机和平板电脑。 【压缩包子文件的文件名称列表】"piano-play-master"可能包含...
Phaser是一个流行的游戏开发框架,它基于HTML5 Canvas构建,提供了丰富的游戏开发功能,包括精灵、动画、物理引擎、碰撞检测等。开发者可以利用Phaser快速搭建游戏结构,减少底层实现的复杂性,专注于游戏逻辑和用户...
HTML5 移动游戏开发高级编程
HTML5小游戏源码集合是一个非常宝贵的资源,尤其对于想要学习...通过深入研究每一个游戏的源码,开发者可以了解到如何利用HTML5和JavaScript创造出吸引人的互动体验,同时也能对游戏开发的整个过程有一个更全面的理解。
HTML5游戏开发是近年来随着移动互联网发展而兴起的一个热门领域,尤其在《Jungle Man》这样的游戏中得到了广泛应用。本文将深入探讨HTML5游戏开发的相关知识点,包括技术基础、开发工具、游戏引擎以及实际案例分析。...
财神HTML5游戏源码是一种基于HTML5技术开发的在线小游戏资源,它的核心在于使用了HTML5这一现代网页标准来构建游戏的用户界面、逻辑控制和互动功能。HTML5是超文本标记语言(HTML)的第五次重大修订,带来了许多新...
html5游戏开发
总结来说,这个"html5实现拼图小游戏"是一个很好的学习案例,它展示了HTML5、JavaScript以及可能的第三方库(如LufyLegend)如何协同工作来创建一个互动娱乐项目。通过分析和理解这个项目的源码,我们可以深入了解...
HTML5游戏开发是近年来互联网技术发展的一个热点,它利用HTML5、CSS3和JavaScript等现代Web技术,为用户带来了无需安装、跨平台的游戏体验。在这个“H5游戏开发完整版pdf(附源码)”压缩包中,我们有望深入学习到...
在这个"我和小狗的一天"的HTML5养成游戏中,我们可以看到HTML5技术在游戏开发中的实际应用,为用户提供了一种全新的娱乐体验。 首先,HTML5的Canvas元素是游戏的核心部分,它是用于在网页上进行动态图形绘制的画布...
总结来说,五子棋inhtml5是利用HTML5和JavaScript技术开发的一款开源小游戏,它不仅提供了一种娱乐方式,更是一个学习和实践Web游戏开发的优秀资源。通过研究这个项目,开发者能够深入理解JavaScript如何与HTML5结合...
总之,"html5手机端2048微信游戏源码下载"不仅是一个游戏,更是学习HTML5游戏开发、提升编程技能的实践案例。通过对源码的解析和学习,开发者可以深化对HTML5技术的理解,同时也能提升在游戏设计和用户体验方面的...
HTML5 Canvas核心技术 图形 动画与游戏开发
HTML5 Canvas核心技术-图形、动画与游戏开发,在网页游戏中的开发技术,适合初学者入门
总的来说,这个《欢乐斗地主》HTML5游戏源码是一个很好的学习资源,它涵盖了HTML5游戏开发的各个方面,包括UI设计、交互逻辑、AI算法以及音频处理。无论是初学者还是经验丰富的开发者,都能从中获益,加深对HTML5...
第1篇 益智游戏 第1章 连连看游戏 第2章 黑白棋游戏 第3章 汉诺塔游戏 第4章 推箱子游戏 第5章 扫雷游戏 第6章 七巧板游戏 第7章 21点扑克牌游戏 第8章 人物拼图游戏(一) 第9章 人物拼图游戏(二) ...