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

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

 
阅读更多

今天,我们继续一系列文章,使用HTML5的canvas组件进行游戏开发。接下来,我们将开始学习如何添加动画以及一些更有趣的功能。我 们的演示将包括一艘太空船飞越时空,并使用一个新的游戏元素 – 对话框。对话框将包含两页,我们的按钮用来切换对话框的页面,并隐藏对话框。

 

你可以点击这里阅读这一系列教程的前一篇文章:html5游戏制作入门系列教程(二)。我们的将基于之前的程序和代码进行开发。

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

在线演示 源码下载

 

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

 

步骤1: HTML
这里是我演示的HTML

<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>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=312">返回原文 <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 {
    position:relative;
}

 

步骤3: 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 = '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 = '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 = '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
});

 

下面是关于一些新功能代码的解释:
1)我们绘制星空背景的移动,使用下面的代码:

iBgShiftX -= 10;
if (iBgShiftX <= 0) {
    iBgShiftX = 1024;
}
ctx.drawImage(backgroundImage, 0 + iBgShiftX, 0, 1024, 768, 0, 0, 800, 600);

 

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

在线演示 源码下载

 

20130814085240

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

分享到:
评论

相关推荐

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

    NULL 博文链接:https://justcoding.iteye.com/blog/2166576

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

    NULL 博文链接:https://justcoding.iteye.com/blog/2166903

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

    NULL 博文链接:https://justcoding.iteye.com/blog/2166906

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

    NULL 博文链接:https://justcoding.iteye.com/blog/2166907

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

    NULL 博文链接:https://justcoding.iteye.com/blog/2166904

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

    NULL 博文链接:https://justcoding.iteye.com/blog/2166905

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

    NULL 博文链接:https://justcoding.iteye.com/blog/2166577

    Dreamweaver8基础入门教程HTML版.rar

    Dreamweaver8 基础入门教程,TeliuTe系列教程之Dreamweaver8基础入门,一步一步教你学会Dreamweaver网页设计。 使用说明:解压后,请使用IE打开index.html即可。 1. 学习平台为Dreamweaver 8.0 中文版; 2. 本软件...

    零基础学习HTML5系列视频课程

    本套 《HTML5 零基础入门课程》,是专门面向未来的 web 开发人员打造的入门课程,零基础即可学习,从最简单的标签,一一讲解,不再纠结老式的 HTML4、XHTML1.0 的规范,直接上手 HTML5。为以后学习更深入的 web 开发...

    Blazor入门100天配套源码

    系列文章 https://www.cnblogs.com/densen2014/p/16027851.html Blazor入门100天 演示地址:https://blazor.app1.es 1.使用JS隔离封装viewerjs库 2.使用JS隔离制作手写签名组件 3.使用JS隔离封装ZXing扫码 4.使用JS...

    网管教程 从入门到精通软件篇.txt

    网管教程 从入门到精通软件篇 ★一。★详细的xp修复控制台命令和用法!!! 放入xp(2000)的光盘,安装时候选R,修复! Windows XP(包括 Windows 2000)的控制台命令是在系统出现一些意外情况下的一种非常有效的...

    HTML + CSS零基础经典教程系列

    HTML 不是一种编程语言,而是一种标记语言 (markup language),是网页制作所必备的。 层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML或XML等文件样式的计算机语言。 CSS不仅可以静态地修饰网页,...

    Access2003中文版应用基础教程part1

    5-4-4 设定图表的数据系列格式 5-5 关于窗体与数据输入的二三事 自我突破练习 第6章 无人能敌的数据查询 6-1 数据记录的排序技巧 6-1-1 设置升序或降序排序 6-1-2 使用筛选窗口设置排序方式 6-1-3 取消和应用...

    Access2003中文版应用基础教程part2

    5-4-4 设定图表的数据系列格式 5-5 关于窗体与数据输入的二三事 自我突破练习 第6章 无人能敌的数据查询 6-1 数据记录的排序技巧 6-1-1 设置升序或降序排序 6-1-2 使用筛选窗口设置排序方式 6-1-3 取消和应用...

    PHP基础教程 是一个比较有价值的PHP新手教程!

    你只需要30分钟就可以将PHP的核心语言特点全部掌握,你可能已经非常了解HTML,甚至你已经知道怎样用编辑设计软件或者手工来制作好看的WEB站点。由于PHP代码能够无障碍的添加进你的站点,在你设计和维护站点的同时,...

    从零开始学习JQuery

    在写作的同时我参考了网上jQuery的系列教程文章, 在博客园和Google上并没有找到让我满意的系列教程. 我喜欢将知识系统的,深入浅出的讲解.不喜欢写那种"学习笔记"式的文章. 同时本系列将很快全部写完(有工作压力就是...

    asp.net知识库

    深入剖析ASP.NET组件设计]一书第三章关于ASP.NET运行原理讲述的补白 asp.net 运行机制初探(httpModule加载) 利用反射来查看对象中的私有变量 关于反射中创建类型实例的两种方法 ASP.Net应用程序的多进程模型 NET委托...

Global site tag (gtag.js) - Google Analytics