`
128kj
  • 浏览: 595159 次
  • 来自: ...
社区版块
存档分类
最新评论

学习:HTML5 游戏《银河系的掠夺》图片加载进度条

阅读更多
游戏来源网址:http://dougx.net/plunder/plunder.html


据网站介绍,这个游戏是用HTML5 Canvas 和Audio对象开发,游戏的第一级(第一关)已完全完成,第二级(第二关)正在开发之中。。。

游戏试玩与源码下载:http://www.108js.com/article/article11/b0020.html

试玩第一关后,感觉非常象流行的PC游戏“雷电”。今天先将它的图片加载方法写点笔记。
效果如图:


这个游戏的图片加载非常简单有特色,并配有进度条。它的加载方法是将所有图片文件用img标签写在html页面内,并绑定onload事件,如下所示:

<img id="splash_screen" src="splash_screen.jpg" onload="itemLoaded(this);">

在itemLoaded(item)函数中主要做四件事:
1、如果没有初始化canvas,则初始化画布canvas
2、绘制初始界面的2张图片(背景图和标题图片,这个游戏目前有46张图片)
3、g_itemsLoaded++,这是图片加载计数,到目前为止加载了多少张
4、绘制进度条,每加载一张图片,绘一次进度条。

代码:
function itemLoaded(item){
   if (g_context == undefined)
   {
      if ( !initCanvas() )//加载完第一张图片后初始化canvas
      {
         dbg("Critical error initializing canvas.", false);
         return;
      }
   }
   if (item.id == "splash_screen")//画背景
   {
      g_context.drawImage(item,0,0);
      g_context.strokeRect(35,220, 500,40);
   }
   else if (item.id == "loading")//画loading..图片
   {
      g_context.drawImage(item,0,0);
      g_context.fillStyle = "black";
      g_context.fillRect(400,300, 300,30);
      g_context.fillStyle = "green";
      g_context.fillText("Loading game sounds", 400,320);
      g_soundsLoaded= false;
     // loadGameSounds();
   }
   g_itemsLoaded++;/当前加载完的图片数
   alert(g_itemsLoaded);
   g_context.fillStyle = "black";
   g_context.fillRect(400,300, 300,30);
   g_context.fillStyle = "green";
   g_context.fillText(item.id, 400,320);
   var percent = g_itemsLoaded / g_totalItems;
   var width = Math.floor(percent * 500);
   g_context.fillRect(35,220, width,40);//画进度条
}


仔细看一下下面这个HTML文件,就明白了。
<!DOCTYPE HTML>
<title>Canvas 游戏 </title>
<script type="text/javascript">

  var g_canvas;
  var g_context;
  //var g_soundsLoaded;
    
 // var g_onscreenControls;
  var g_totalItems;
  var g_itemsLoaded;
  
 //这个函数在Loader.js中,这里把它提出来了。
  function itemLoaded(item){
   if (g_context == undefined)
   {
      if ( !initCanvas() )
      {
         dbg("Critical error initializing canvas.", false);
         return;
      }
   }
   if (item.id == "splash_screen")
   {
      g_context.drawImage(item,0,0);
      g_context.strokeRect(35,220, 500,40);
   }
   else if (item.id == "loading")
   {
      g_context.drawImage(item,0,0);
      g_context.fillStyle = "black";
      g_context.fillRect(400,300, 300,30);
      g_context.fillStyle = "green";
      g_context.fillText("Loading game sounds", 400,320);
      //g_soundsLoaded= false;
     // loadGameSounds();
   }
   g_itemsLoaded++;
   alert(g_itemsLoaded);
   g_context.fillStyle = "black";
   g_context.fillRect(400,300, 300,30);
   g_context.fillStyle = "green";
   g_context.fillText(item.id, 400,320);
   var percent = g_itemsLoaded / g_totalItems;
   var width = Math.floor(percent * 500);
   g_context.fillRect(35,220, width,40);
}
  //这个函数在Loader.js中,这里把它提出来了。
function initCanvas()
{
  g_canvas = document.getElementById('theCanvas');
  if (!g_canvas.getContext)
  {
    return false;
  }
  g_context = g_canvas.getContext('2d');
  g_context.font = "14pt Helvetica";
  g_context.lineWidth = 2;
  g_context.strokeStyle = "green";
  g_totalItems  = 46;
  g_itemsLoaded = 0;
 // g_onscreenControls = false;
  return true;
}

</script>
</head>
<body>
 <table cellpadding="0" border="0" cellspacing="0" frame="void">
 <tr>
  <td valign="top">
    <canvas align="left" id="theCanvas" width="600" height="400"></canvas>
  </td>
 <tr>
 <table>

<div id="dbg"></div>
<div id="hidden" style="visibility:hidden; width:1px; height:1px; overflow:hidden">
  <img id="splash_screen" src="splash_screen.jpg" onload="itemLoaded(this);">
  <img id="loading" src="loading.png" onload="itemLoaded(this);">
  <img id="title" src="title.png" onload="itemLoaded(this);">
  <img id="starfield" src="starfield.jpg" onload="itemLoaded(this);">
  <img id="foreground" src="foreground.png" onload="itemLoaded(this);">
  <img id="ship_center" src="ship_center.png" onload="itemLoaded(this);">
  <img id="ship_up_1" src="ship_up_1.png" onload="itemLoaded(this);">
  <img id="ship_up_2" src="ship_up_2.png" onload="itemLoaded(this);">
  <img id="ship_up_3" src="ship_up_3.png" onload="itemLoaded(this);">
  <img id="ship_down_1" src="ship_down_1.png" onload="itemLoaded(this);">
  <img id="ship_down_2" src="ship_down_2.png" onload="itemLoaded(this);">
  <img id="ship_down_3" src="ship_down_3.png" onload="itemLoaded(this);">
  <img id="ship_icon" src="ship_icon.png" onload="itemLoaded(this);">
  <img id="foreground_light" src="foreground_light.png" onload="itemLoaded(this);">
  <img id="sky" src="sky.jpg" onload="itemLoaded(this);">
  <img id="speed_image" src="speed_image.png" onload="itemLoaded(this);">
  <img id="gun_image" src="gun_image.png" onload="itemLoaded(this);">
  <img id="shot_image" src="shot_image.png" onload="itemLoaded(this);">
  <img id="double_image" src="double_image.png" onload="itemLoaded(this);">
  <img id="gem_image" src="gem_image.png" onload="itemLoaded(this);">
  <img id="enemy_small" src="enemy_small.png" onload="itemLoaded(this);">
  <img id="enemy_small_special" src="enemy_small_special.png" onload="itemLoaded(this);">
  <img id="enemy_small_2" src="enemy_small_2.png" onload="itemLoaded(this);">
  <img id="enemy_small_2_special" src="enemy_small_2_special.png" onload="itemLoaded(this);">
  <img id="enemy_small_3" src="enemy_small_3.png" onload="itemLoaded(this);">
  <img id="enemy_small_4" src="enemy_small_4.png" onload="itemLoaded(this);">
  <img id="enemy_small_4_special" src="enemy_small_4_special.png" onload="itemLoaded(this);">
  <img id="enemy_artifact" src="enemy_artifact.png" onload="itemLoaded(this);">
  <img id="enemy_artifact_2" src="enemy_artifact_2.png" onload="itemLoaded(this);">
  <img id="ship_death_1" src="ship_death_1.png" onload="itemLoaded(this);">
  <img id="ship_death_2" src="ship_death_2.png" onload="itemLoaded(this);">
  <img id="ship_death_3" src="ship_death_3.png" onload="itemLoaded(this);">
  <img id="ship_death_4" src="ship_death_4.png" onload="itemLoaded(this);">
  <img id="ship_death_5" src="ship_death_5.png" onload="itemLoaded(this);">
  <img id="ship_death_6" src="ship_death_6.png" onload="itemLoaded(this);">
  <img id="ship_death_7" src="ship_death_7.png" onload="itemLoaded(this);">
  <img id="splode_1" src="splode_1.png" onload="itemLoaded(this);">
  <img id="splode_2" src="splode_2.png" onload="itemLoaded(this);">
  <img id="splode_3" src="splode_3.png" onload="itemLoaded(this);">
  <img id="splode_4" src="splode_4.png" onload="itemLoaded(this);">
  <img id="splode_5" src="splode_5.png" onload="itemLoaded(this);">
  <img id="splode_6" src="splode_6.png" onload="itemLoaded(this);">
  <img id="splode_7" src="splode_7.png" onload="itemLoaded(this);">
  <img id="artifact_chard_image" src="artifact_chard.png" onload="itemLoaded(this);">
  <img id="artifact_skull_image" src="artifact_skull.png" onload="itemLoaded(this);">
  <img id="command_ship" src="command_ship.png" onload="itemLoaded(this);">
</div>
</body>
</html>


本学习笔记演示与源码下载:
http://www.108js.com/article/article3/30094.html
  • 大小: 59.8 KB
  • 大小: 64.2 KB
0
0
分享到:
评论

相关推荐

    《银河系的掠夺》HTML5 网页游戏源码地址

    试玩第一关后,感觉非常象流行的PC游戏“雷电”。学习HTML5游戏开发必备。

    html5 canvas酷炫的银河系动画特效

    比如,可以使用`drawImage`方法加载一张银河的图片,并通过`drawImage`的变形参数来扭曲图像,模拟银河的旋涡结构。还可以通过`createLinearGradient`或`createRadialGradient`创建渐变,以表现银河的明亮和暗淡区域...

    HTML5 Canvas绘制银河系特效.zip

    这个"HTML5 Canvas绘制银河系特效.zip"文件包含了一个利用Canvas API创建动态银河系场景的实例。下面将详细介绍如何使用HTML5 Canvas来实现这样的特效以及相关的编程知识。 首先,要创建一个银河系特效,我们需要在...

    破碎银河系M服服务端

    5. **游戏架构**:《破碎银河系》的服务端可能采用分布式或集中式架构。分布式架构将游戏逻辑分散到多个服务器上,提高系统的可扩展性和稳定性;而集中式则所有处理都在一个中心服务器进行,简化了管理和部署。具体...

    html5 canvas炫酷旋转银河系星空背景特效

    这个特效是利用Canvas API实现的“html5 canvas炫酷旋转银河系星空背景特效”,它为网站添加了一个动态且引人入胜的视觉元素。 首先,让我们深入了解一下HTML5 Canvas的基本概念。Canvas是一个基于矢量图形的画布,...

    仿破碎银河系单机版

    【标题】"仿破碎银河系单机版"是一款基于易语言开发的游戏源码,它旨在模仿知名的"破碎银河系"游戏,提供给游戏制作者学习和参考。通过研究这款源码,开发者可以深入了解游戏设计的基本原理,以及易语言在游戏编程中...

    html5跟随鼠标移动银河星系背景动画特效

    在这个“html5跟随鼠标移动银河星系背景动画特效”中,我们可以看到HTML5的强大潜力被充分利用,创造出一种引人入胜的视觉体验。这个特效将用户交互与动态图形相结合,使得背景中的银河星系随着鼠标的移动而变化,...

    HTML5 Canvas鼠标绘制银河系特效.zip

    "HTML5 Canvas鼠标绘制银河系特效"是一个利用Canvas API实现的交互式视觉效果,用户可以通过鼠标操作来展现一个模拟的银河系动态画面。这个特效通常用于网站背景或者游戏设计,可以提升用户体验,增加视觉吸引力。 ...

    HTML5 Canvas鼠标绘制银河系特效

    这个“HTML5 Canvas鼠标绘制银河系特效”项目利用了Canvas API的功能,让用户可以通过鼠标交互来创造富有动态感的银河系效果。下面我们将深入探讨相关知识点。 1. HTML5 Canvas基本结构: 在HTML中,Canvas元素...

    HTML5+Canvas实现的手绘银河系特效源码.zip

    在"HTML5+Canvas实现的手绘银河系特效源码.zip"这个压缩包中,我们可以推测包含的是一个使用HTML5和Canvas API开发的交互式银河系特效项目。这个特效可能包括模拟星系旋转、星星闪烁、星云流动等宇宙景象,为用户...

    唯美蓝色银河系PPT背景图片.zip

    这是一张唯美蓝色银河系PPT背景图片,第一PPT模板网提供唯美幻灯片背景图片免费下载; 关键词:蓝色星空PPT背景图片,宇宙星系幻灯片背景图片,唯美PowerPoint图片免费下载,.PPTX格式;

    jogo-reset-intergalactico:游戏“重置银河系”-网站开发Resilia教育课程

    "重置银河系"是一款基于Web开发的游戏,旨在通过寓教于乐的方式,教授用户关于网站开发的知识。这款教育游戏由Resilia Education提供,旨在帮助学习者深入理解JavaScript编程语言,这是现代网页开发中的核心部分。 ...

    HTML5小游戏 h5银河舰队雷霆战机源码.rar

    "HTML5小游戏 h5银河舰队雷霆战机源码"就是一个典型的例子,它允许开发者深入研究游戏的内部工作原理,学习如何构建类似的项目。 HTML5作为下一代超文本标记语言,引入了许多新特性,比如离线存储、 canvas绘图、...

    HTML5 Canvas绘制银河系特效特效代码

    7. **纹理和图像**:除了纯色和渐变,开发者可能还使用了预加载的纹理或图片来增强银河系的细节,例如星云、行星等。 8. **数学原理**:在创建宇宙特效时,开发者需要运用一些基础的数学知识,如欧几里得几何、向量...

    2016秋七年级科学上册8.4银河系和河外星系教案新版华东师大版.doc

    3. **太阳系在银河系中的位置**:太阳系位于银河系的盘面内,处于一个叫做猎户臂的旋臂上,距离银河系中心约2.5万光年。太阳并非位于银河系中心,而是绕着银河系的公共质心旋转。 4. **光年与天文单位**:光年是一...

    七年级科学星空巡礼银河系PPT教案.pptx

    【银河系概述】 银河系是我们所在的星系,它是一个庞大的天体...5. 银河系在宇宙中的位置,以及宇宙的广阔性,包括河外星系的存在。 以上知识为我们打开了探索宇宙的一扇窗,让我们对自身所处的星系有了更深的理解。

    唯美的蓝色星海旋转银河系星空动画效果源码下载

    该资源是一个关于创建“唯美的蓝色星海旋转银河系星空动画效果”的源代码包,适合对计算机图形学、游戏开发或者视觉艺术感兴趣的开发者和设计师。这个源码可能使用了编程语言如JavaScript、C++或者Python,配合图形...

    HTML5+JS实现的全屏星空特效源码

    HTML5和JavaScript是现代网页开发中的核心技术,它们的结合使得创建动态、交互性强的网页效果成为可能。在“HTML5+JS实现的全屏星空特效源码”项目中,我们可以看到这两个技术如何协同工作,为用户营造出一个...

    计算机行业周报:中标软件和银河麒麟正式合并,打造国产操作系统新旗舰.pdf

    计算机行业周报:中标软件和银河麒麟正式合并,打造国产操作系统新旗舰.pdf

Global site tag (gtag.js) - Google Analytics