`
天梯梦
  • 浏览: 13763212 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

html5游戏制作入门系列教程(二)

 
阅读更多

今天,我们继续html5游戏制作入门系列的系列文章。今天,我们将继续基础知识(也许甚至是高级技巧的基础)。我要告诉你如何具有渐变颜色填充对象,绘制文本,使用自定义的字体绘制文本,基本的动画,以及最重要的UI元素 – 按钮。

 

我们以前的文章中,你可以在这里阅读:html5游戏制作入门系列教程(一)。我们会用到以前的脚本,并将其功能加强。我要绘制文本,使用自定义字体,动画对象(方形)与渐变色填充,将利用“播放/暂停”按钮暂停动画。

 

这里有我们的演示和下载包:

在线演示 源码下载

 

好吧,下载所需文件,让我们开始编码!

 

步骤1: HTML

这里是我演示的HTML

 

index.html

<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>html5game-html5游戏制作入门系列教程(二)</title>

<link href="main.css" rel="stylesheet" type="text/css" />

<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div class="container">
<canvas id="scene" width="800" height="600"></canvas>
</div>

<footer>
<h2>html5游戏制作入门系列教程(二)</h2>
<a class="stuts" href="http://html5gamedev.org/?p=304">返回原文 <span>html5游戏制作入门系列教程(二)</span></a>
</footer>
</body>
</html>

 

步骤2:CSS

下面是CSS样式。

/* general styles */
*{
    margin:0;
    padding:0;
}
@font-face {
    font-family: "DS-Digital";
    src: url("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(01.jpg);
    position:relative;
}

 

注意@ font-face的这种使用方式,自定义字体文件(TTF)连接到我们的教程中(在画布上绘制)。

 

步骤3: 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

    // draw the title text
    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;
    }

    // central object behavior
    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));

    // load the guide sprite image
    buttonImage = new Image();
    buttonImage.src = '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
});

 

下面是关于一些新功能代码的解释:
1)我们可以绘制文本(自定义字体)使用下面的代码,如下:

ctx.font = '42px DS-Digital';
ctx.textAlign = 'center';
ctx.fillStyle = '#ffffff';
ctx.fillText('Welcome to lesson #2', ctx.canvas.width/2, 50);

 

2)运用渐变色:

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.fillStyle = bg_gradient;

 

3)按钮 – 我用一个精灵图像,绘制按钮的三个状态,并添加悬停/事件的事件处理程序。下面是加载和绘制图像画布的方式:

buttonImage = new Image();
buttonImage.src = 'button.png';
.......
ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, width, height);

 

结论

超级酷,不是吗?我会很高兴看到您的评论和意见。祝你好运!

在线演示 源码下载

 

20130812124645

 

转载请注明:HTML5游戏开发者社区 » html5游戏制作入门系列教程(二)

分享到:
评论

相关推荐

    html5游戏制作入门系列教程(三)

    在本篇“html5游戏制作入门系列教程(三)”中,我们将深入探讨HTML5游戏开发的基本要素,以及如何利用这些元素创建一个简单的互动游戏。HTML5作为一种强大的Web开发语言,为游戏制作提供了新的可能性,尤其在移动...

    html5游戏制作入门系列教程(一)

    HTML5游戏制作入门系列教程(一) HTML5作为现代网页技术的代表,为开发者提供了创建交互式、富媒体内容的强大工具。在这个入门系列教程中,我们将聚焦于如何使用HTML5来制作游戏,让读者逐步掌握游戏开发的基础...

    html5游戏制作入门系列教程(七)

    《HTML5游戏制作入门系列教程(七)》 在这一篇教程中,我们将深入探讨HTML5游戏开发的基础知识,特别是如何利用HTML5的特性来创建交互式游戏。HTML5是现代网页开发的标准,其强大的Canvas API和WebGL使得在浏览器...

    html5游戏制作入门系列教程(八)

    【HTML5游戏制作入门系列教程(八)】是面向初学者的一份教程,旨在帮助学习者掌握使用HTML5技术制作游戏的基本技能。本教程可能是通过一个具体的实例或项目来逐步讲解,让学习者能实际动手操作,理解HTML5游戏开发...

    html5游戏制作入门系列教程(四)

    HTML5游戏制作入门系列教程第四部分主要探讨了如何利用HTML5技术进行游戏开发,结合了源码和工具的使用,帮助初学者逐步掌握这一领域的基础知识。在这个教程中,我们可能会涉及以下几个关键知识点: 1. HTML5 ...

    html5游戏制作入门系列教程(五)

    HTML5游戏制作入门系列教程第五部分主要关注如何利用HTML5技术来创建互动游戏。这个教程可能涵盖HTML5的Canvas元素,JavaScript编程基础,以及相关的Web技术来构建游戏环境。在这个部分,我们可能会深入到以下几个...

    html5游戏制作入门系列教程(六)

    HTML5游戏制作入门系列教程(六)主要涵盖了利用HTML5技术进行游戏开发的基础知识和实践技巧。本教程可能包括以下几个核心知识点: 1. HTML5基础知识:HTML5是超文本标记语言的最新版本,它增强了对多媒体的支持,...

    HTML语言入门教程

    学习HTML语言.exe可能是这个教程的主程序或交互式学习平台,而其他文本文件(特别服务.txt、本站书籍制作章程.txt、书籍介绍.txt)可能包含了额外的信息,如教程的服务条款、制作过程的说明,以及书籍的详细介绍。...

    易语言编程入门教程、易语言教程约150套

    缘正则表达式系列教程+例程源码【全套打包下载解压后3.7G】.rar 易语言辅助教程(爱易编程论坛讲师 24课+讲师:远航 9课+爱易编程论坛讲师:爱易、小Call 8课).rar 时光论坛易语言全套教程【易语言零基础+易语言抓...

    SilverLight入门系列教程-1

    **SilverLight入门系列教程-1** SilverLight是微软推出的一款基于.NET Framework的浏览器插件,它主要用于构建丰富的、交互式的Web应用程序,特别是在多媒体、动画和图形处理方面具有强大的能力。本教程作为...

    HTML 5从入门到精通

    本教程旨在带你从零开始,逐步掌握HTML5的各项功能和应用,从而实现从入门到精通的过渡。 一、HTML5基础 HTML5的基础包括一系列新的标签、元素和API,它们旨在使网页结构更加清晰,内容更加语义化。例如,`&lt;header&gt;...

    HTML入门基础教程

    这个“HTML入门基础教程”旨在帮助初学者快速理解并掌握HTML的基本概念和语法,从而能够制作出自己的CHM电子书或者其他网页内容。 HTML由一系列元素组成,这些元素通过标签来定义。每个HTML元素都由开始标签和结束...

    网页制作单片机\HTML入门教程.doc

    在网页制作中,HTML入门并不需要复杂的工具。实际上,一个最基本的文本编辑器如Windows的记事本或Linux的Pico,甚至是Mac的Simple Text,就足以开始你的HTML学习之旅。这些文本编辑器不会干扰你输入的内容,允许你...

    HTML笔记,html零基础入门视频课程(最适合初学者的教程)

    本教程“HTML笔记,html零基础入门视频课程”是为初学者设计的一套系统学习资源,旨在帮助新手快速掌握HTML的基本概念和实际应用。 一、HTML基本结构 HTML文档由一系列的元素组成,这些元素通过标签来定义。每个HTML...

    Html+Css+Javascript从入门到精通.pdf

    《Html+Css+Javascript从入门到精通》是一本全面介绍了Web前端开发技术的教程。本书旨在帮助读者掌握Web开发的基础知识,并通过实践逐步深入理解HTML、CSS以及JavaScript这三种核心技术。以下是根据该书内容整理出的...

    Web开发入门系列教程

    在“Web开发入门系列教程”中,我们主要探讨的是如何踏入Web开发的大门,这是一个针对初学者精心设计的教程集合。教程可能涵盖了从基础到进阶的各个环节,旨在帮助新手快速掌握网页制作的核心技能。虽然描述中没有...

    初学者从入门到精通网页制作实例教程

    总的来说,"初学者从入门到精通网页制作实例教程"是一套全面的学习资源,涵盖了从互联网基础知识到HTML编程的各个方面。通过系统的理论学习与实践操作,你不仅可以掌握网页制作的基本技能,还能为将来深入学习...

    HTML教程软件(入门级)

    这个"HTML教程软件(入门级)"显然旨在帮助初学者掌握这一基础的Web开发技术。下面将详细介绍HTML的一些核心概念、语法以及它在网页制作中的作用。 1. **HTML结构**:HTML文档由一系列元素组成,每个元素都有其特定...

Global site tag (gtag.js) - Google Analytics