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

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

    博客分类:
  • Web
阅读更多

 

HTML5 Game Development - Lesson 2

开发第一个Html5游戏(二)

今天我们继续学习Html5系列游戏开发,我们会在第一章的基础上做一些扩展,加入颜色渐变、

绘制文本、自定义文本样式、以及动画,我们还将学习UI中一个非常重要的元素,按钮。

我们的上一篇文章: (译)开发第一个Html5游戏(一) .

 

 

 

 

JS代码:(完整代码请看文章结尾链接)

var canvas, ctx;
var circles = [];
var selectedCircle;
var hoveredCircle;
var button;
var moving = false;
var speed = 2.0;

// -------------------------------------------------------------

// objects :

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;
}
// -------------------------------------------------------------

// draw functions :

function clear() { // clear canvas function
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}

function drawCircle(ctx, x, y, radius) { // draw circle function
    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(); // draw border
}

function drawScene() { // main drawScene function
    clear(); // clear canvas

    // 绘制文本
    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(); // custom shape begin
    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(); // custom shape end
    ctx.fill(); // fill custom shape

    ctx.lineWidth = 2;
    ctx.strokeStyle = 'rgba(0, 0, 255, 0.5)';
    ctx.stroke(); // draw border

    // reverting direction
    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);

    // draw button
    ctx.drawImage(button.image, 0, button.imageShift, button.w, button.h, button.x, button.y, button.w, button.h);

    // draw text
    ctx.font = '30px DS-Digital';
    ctx.fillStyle = '#ffffff';
    ctx.fillText('Play/Pause', 135, 480);
    ctx.fillText(button.state, 135, 515);
}

// -------------------------------------------------------------

// initialization

$(function(){
    canvas = document.getElementById('scene');
    ctx = canvas.getContext('2d');

    var circleRadius = 15;
    var width = canvas.width;
    var height = canvas.height;

    // lets add 4 circles manually
    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);

    // binding mousedown event (for dragging)
    $('#scene').mousedown(function(e) {

        var mouseX = e.layerX || 0;
        var mouseY = e.layerY || 0;
        for (var i=0; i<circles.length; i++) { // checking through all circles - are mouse down inside circle or not
            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;
            }
        }

        // 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 for dragging selected circle
        var mouseX = e.layerX || 0;
        var mouseY = e.layerY || 0;
        if (selectedCircle != undefined) {
            // var canvasPosition = $(this).offset();

            var radius = circles[selectedCircle].radius;
            circles[selectedCircle] = new Circle(mouseX, mouseY,radius); // changing position of selected circle
        }

        hoveredCircle = undefined;
        for (var i=0; i<circles.length; i++) { // checking through all circles - are mouse down inside circle or not
            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;
            }
        }

        // 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) { // on mouseup - cleaning selectedCircle
        selectedCircle = undefined;

        // button behavior
        if (button.state == 'pressed') {
            moving = !moving;
        }
        button.state = 'normal';
        button.imageShift = 0;
    });

    setInterval(drawScene, 30); // loop drawScene
});
 

原文地址 http://www.script-tutorials.com/html5-game-development-lesson-2/

Demo地址 http://www.script-tutorials.com/demos/157/index.html

源码下载  http://www.script-tutorials.com/demos/157/source.zip

0
0
分享到:
评论

相关推荐

    HTML5 Canvas游戏开发实战.pdf

    《HTML5 Canvas游戏开发实战》在介绍每个游戏开发的过程时,都会包括游戏分析、开发过程、代码解析和小结等相关内容,以帮助读者了解每种类型游戏开发的详细步骤,让读者彻底掌握各种类型游戏的开发思想。...

    新手入门级html5游戏开发源码(蘑菇熊)

    本资源“新手入门级html5游戏开发源码(蘑菇熊)”是一个很好的起点,它提供了一系列逐步教程,帮助初学者理解游戏开发的核心概念。 首先,我们来看标题中的"蘑菇熊",这很可能是游戏的主题或主角,展示了如何设计...

    HTML5游戏开发素材

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

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

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

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

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

    80个HTML5小游戏源码

    HTML5小游戏源码集合是一个非常宝贵的资源,尤其对于想要学习和提升HTML5游戏开发技能的开发者来说。这个压缩包包含了80个不同的小游戏,每个都是按照微信小游戏的标准和风格设计的,提供了丰富的实践素材和灵感来源...

    HTML5 Canvas核心技术—图形、动画与游戏开发一书源代码

    HTML5 Canvas是Web开发中的一个关键技术,它提供了一个在网页上绘制图形的API,使得开发者可以直接在浏览器中创建丰富的交互式2D图形、动画以及游戏。"HTML5 Canvas核心技术—图形、动画与游戏开发"这本书深入浅出地...

    jungle man-html5游戏开发

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

    Unity3D网络游戏实战游戏开发与设计技术丛书

    第一部分“单机游戏”:第1章至第5章,主要在于开发一款功能完整坦克单机游戏。除了让坦克行走、开炮,还将介绍基于代码和资源分离的界面系统、敌人AI。了解开发单机游戏的知识,也是为接下来的网络开发学习奠定基础...

    html5小游戏

    Canvas是HTML5中的一个核心元素,它是一个二维绘图上下文,允许开发者在网页上进行动态图形绘制。通过JavaScript,我们可以操纵Canvas的每一个像素,实现各种复杂的动画效果和游戏逻辑。Canvas的API提供了画线、填充...

    70多款html5小游戏代码案例

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

    95个HTML5微信小游戏源码

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

    五子棋inhtml5是一款使用Javascript开发的开源小游戏

    总结来说,五子棋inhtml5是利用HTML5和JavaScript技术开发的一款开源小游戏,它不仅提供了一种娱乐方式,更是一个学习和实践Web游戏开发的优秀资源。通过研究这个项目,开发者能够深入理解JavaScript如何与HTML5结合...

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

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

    (Unity源码) 第一人称射击游戏开发模板FPS.rar

    2-117 第一人称射击游戏开发模板FPS2-117 第一人称射击游戏开发模板FPS2-117 第一人称射击游戏开发模板FPS2-117 第一人称射击游戏开发模板FPS2-117 第一人称射击游戏开发模板FPS2-117 第一人称射击游戏开发模板FPS2-...

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

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

    html5实现拼图小游戏

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

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

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

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

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

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

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

Global site tag (gtag.js) - Google Analytics