`
gutou9
  • 浏览: 146398 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

(译)开发第一个Html5游戏(三)

    博客分类:
  • Web
阅读更多

HTML5 Game Development - Lesson 3

(译)开发第一个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游戏开发源码(蘑菇熊)

    首先,我们来看标题中的"蘑菇熊",这很可能是游戏的主题或主角,展示了如何设计和创建一个游戏的角色和场景。在HTML5游戏中,角色和场景通常通过CSS3来定义样式,用JavaScript来实现动态行为。源码中的每个"index...

    HTML5游戏开发素材

    HTML5游戏开发素材 HTML5游戏开发素材 HTML5游戏开发素材 HTML5游戏开发素材

    《HTML5 Canvas游戏开发实战》.(张路斌).pdf

    《HTML5 Canvas游戏开发实战》.(张路斌).pdf电子高清扫描版

    80个HTML5小游戏源码

    HTML5小游戏源码集合是一个非常宝贵的资源,尤其对于想要学习...通过深入研究每一个游戏的源码,开发者可以了解到如何利用HTML5和JavaScript创造出吸引人的互动体验,同时也能对游戏开发的整个过程有一个更全面的理解。

    jungle man-html5游戏开发

    HTML5游戏开发是近年来随着移动互联网发展而兴起的一个热门领域,尤其在《Jungle Man》这样的游戏中得到了广泛应用。本文将深入探讨HTML5游戏开发的相关知识点,包括技术基础、开发工具、游戏引擎以及实际案例分析。...

    html 5移动开发

    HTML5,即第五代超文本标记语言,引入了一系列新的元素、API和功能,使得开发者能够创建功能丰富、交互性强且适应多平台的应用程序。 首先,HTML5的核心优势在于其对移动设备的原生支持。它提供了离线存储能力...

    Unity3D 游戏开发 PDF完整版

    这使得Unity特别适合于需要在多个平台发布游戏的开发者,也正因为这一特性,Unity在移动设备和网页游戏开发中变得越来越流行。 在当前的游戏行业发展中,各种移动平台的智能手机迅速崛起,为游戏开发提供了新的平台...

    91个HTML小游戏源码集合

    HTML小游戏是一种基于网页技术开发的轻量级游戏,它们通常使用HTML、CSS和JavaScript这三种核心技术构建。这个压缩包集合了91个HTML小游戏源码,对于初学者来说,这是一个非常宝贵的资源,可以帮助他们理解游戏开发...

    《HTML5 Canvas 开发详解》第二版pdf

    《HTML5 Canvas 开发详解》第二版pdf 版

    95个HTML5微信小游戏源码

    HTML5微信小游戏是一种基于HTML5技术开发的轻量级游戏,它们可以在微信内置浏览器中运行,无需用户下载安装,只需扫一扫或点击链接即可快速体验。这些小游戏通常利用了HTML5的Canvas、WebGL等图形渲染技术,以及Web ...

    html5实现拼图小游戏

    总结来说,这个"html5实现拼图小游戏"是一个很好的学习案例,它展示了HTML5、JavaScript以及可能的第三方库(如LufyLegend)如何协同工作来创建一个互动娱乐项目。通过分析和理解这个项目的源码,我们可以深入了解...

    70多款html5小游戏代码案例

    1. **HTML5 Canvas**: HTML5的Canvas元素是一个二维绘图表面,允许开发者通过JavaScript进行动态图形绘制。在这些小游戏案例中,大部分的游戏逻辑和动画效果都是通过Canvas API实现的,例如`drawImage()`用于绘制...

    html5小游戏

    HTML5作为现代网页开发的标准,引入了一系列增强网页体验的功能,对于初学者来说,它提供了一个友好的学习平台,尤其是对想要尝试游戏开发的人来说。 Canvas是HTML5中的一个核心元素,它是一个二维绘图上下文,允许...

    HTML5养成游戏《我和小狗的一天》源码

    在这个"我和小狗的一天"的HTML5养成游戏中,我们可以看到HTML5技术在游戏开发中的实际应用,为用户提供了一种全新的娱乐体验。 首先,HTML5的Canvas元素是游戏的核心部分,它是用于在网页上进行动态图形绘制的画布...

    html5 canvas核心技术图形、动画与游戏开发完整源码

    HTML5 Canvas是Web开发中的一个强大工具,它允许开发者在网页上进行动态图形绘制,创建丰富的交互式用户体验,包括动画和游戏。这个“HTML5 Canvas核心技术图形、动画与游戏开发完整源码”压缩包可能包含了多个示例...

    html5手机端2048微信游戏源码下载

    总之,"html5手机端2048微信游戏源码下载"不仅是一个游戏,更是学习HTML5游戏开发、提升编程技能的实践案例。通过对源码的解析和学习,开发者可以深化对HTML5技术的理解,同时也能提升在游戏设计和用户体验方面的...

    HTML5 Canvas核心技术 图形、动画与游戏开发

    HTML5 Canvas核心技术 图形、动画与游戏开发,HTML5 Canvas核心技术 图形、动画与游戏开发

    HTML5 Canvas核心技术 图形、动画与游戏开发.pdf

    HTML5 Canvas核心技术 图形 动画与游戏开发

    Qt5程序开发指南(三合一)

    首先,第一本《Qt5 C++ GUI Programming Cookbook》是针对Qt5图形用户界面(GUI)编程的实践指南。这本书涵盖了C++与Qt5结合的基础知识,如安装配置Qt开发环境,以及创建基本的窗口和控件。通过一系列实用的食谱,...

    斗地主html5源代码

    总的来说,这个《欢乐斗地主》HTML5游戏源码是一个很好的学习资源,它涵盖了HTML5游戏开发的各个方面,包括UI设计、交互逻辑、AI算法以及音频处理。无论是初学者还是经验丰富的开发者,都能从中获益,加深对HTML5...

Global site tag (gtag.js) - Google Analytics