使用JavaScript和Canvas开发游戏(四)

来源:互联网 发布:有淘宝优惠券的网站 编辑:程序博客网 时间:2024/05/17 02:24

4、写一个游戏框架(二)
http://www.brighthub.com/internet/web-development/articles/40512.aspx
在这篇文章里,我们继续介绍构成这个基本JavaScript游戏框架的其他必要的类。
前一篇文章介绍了GameObjectManager类,该类负责画布的渲染并允许GameObject类更新和删除它们自己。下面就来看一看GameObject类。
GameObject.js

  1. /**
  2. 游戏中出现的所有元素的基类
  3. @class
  4. */
  5. function GameObject(){
  6. /** 显示的深度次序。较小的zOrder值表示先渲染,因而会在背景中。
  7. @type Number
  8. */
  9. this.zOrder = 0;
  10. /**
  11. x轴的坐标
  12. @type Number
  13. */
  14. this.x = 0;
  15. /**
  16. y轴的坐标
  17. @type Number
  18. */
  19. this.y = 0;
  20. /**
  21. 初始化游戏对象,并将其添加到GameObjectManager维护的对象列表中
  22. @param x        x轴的坐标
  23. @param y        y轴的坐标
  24. @param z        元素的z次序(背景元素的z值较小)
  25. */
  26. this.startupGameObject = function(/**Number*/ x, /**Number*/ y, /**Number*/ z){
  27. this.zOrder = z;
  28. this.x = x;
  29. this.y = y;
  30. g_GameObjectManager.addGameObject(this);
  31. return this;
  32. }
  33. /**
  34. 清理当前对象,将其从GameObjectManager维护的对象列表中删除
  35. */
  36. this.shutdownGameObject = function(){
  37. g_GameObjectManager.removeGameObject(this);
  38. }
  39. }

 

这个GameObject类(是一个引擎类)的目的,是为游戏中将会出现的所有对象定义一些共有的属性,包括它们的位置(x和y)和深度(z)。需要注意的是,我们不会直接创建GameObject类的实例,而是会再创建一个类来扩展它。
这个类的x和y坐标值没有什么好说的——就是相应对象左上角位置在画布上的坐标。关键是GameObject中的z值,这个值定义的是对象的深度。理解这 个值很重要,这个值较小的GameObject会先绘制到画布上。换句话说,z值较大的GameObject将被绘制到z值较小的GameObject上 面。
上一篇文章里介绍过,所有类都是通过一个类似startupClassName的函数完成自身初始化的。因此,GameObject类就有一个名为 startupGameObject的函数。在这个函数里,除了初始化所有变量外,还会通过addGameObject函数把当前的GameObject 添加到由GameObjectManager维护的GameObject列表中。

  1. /**
  2. 初始化游戏对象,并将其添加到GameObjectManager维护的对象列表中
  3. @param x        x轴的坐标
  4. @param y        y轴的坐标
  5. @param z        元素的z次序(背景元素的z值较小)
  6. */
  7. this.startupGameObject = function(/**Number*/ x, /**Number*/ y, /**Number*/ z){
  8. this.zOrder = z;
  9. this.x = x;
  10. this.y = y;
  11. g_GameObjectManager.addGameObject(this);
  12. return this;
  13. }


函数shutdownGameObject用于清除GameObject。这里所谓的清除,是指GameObject通过removeGameObject函数将自身从GameObjectManager中删除。

  1. /**
  2. 清理当前对象,将其从GameObjectManager维护的对象列表中删除
  3. */
  4. this.shutdownGameObject = function(){
  5. g_GameObjectManager.removeGameObject(this);
  6. }


VisualGameObject.js

  1. /**
  2. 出现在游戏中的所有元素的基类
  3. @class
  4. */
  5. function VisualGameObject(){
  6. /**
  7. 由当前对象显示的图像
  8. @type Image
  9. */
  10. this.image = null;
  11. /**
  12. 将当前元素绘制到后台缓冲
  13. @param dt 自上一帧绘制起经过的秒数
  14. */
  15. this.draw = function(/**Number*/ dt, /**CanvasRenderingContext2D*/ context, /**Number*/ xScroll, /**Number*/ yScroll){
  16. context.drawImage(this.image, this.x – xScroll, this.y – yScroll);
  17. }
  18. /**
  19. 初始化当前对象
  20. @param image 要显示的图像
  21. */
  22. this.startupVisualGameObject = function(/**Image*/ image, /**Number*/ x, /**Number*/ y, /**Number*/ z){
  23. this.startupGameObject(x, y, z);
  24. this.image = image;
  25. return this;
  26. }
  27. /**
  28. 清理当前对象
  29. */
  30. this.shutdownVisualGameObject = function(){
  31. this.shutdownGameObject();
  32. }
  33. }
  34. VisualGameObject.prototype = new GameObject;


VisualGameObject也是一个引擎类,它扩展了GameObject类,为将在屏幕上绘制的对象定义了更具体的属性和函数。顾名思义,可见 对象显然是需要绘制的对象,因此VisualGameObject定义了一个image属性,当把当前对象绘制到后台缓冲时,将以这个属性作为图形的来 源。

  1. /**
  2. 由当前对象显示的图像
  3. @type Image
  4. */
  5. this.image = null;


此外,还需要写几行代码,以便把这个对象实际地绘制到后台缓冲——这就是draw函数了,它接受图像并基于GameObject类中定义的x和y值将其复制到后台缓冲。

  1. /**
  2. 将当前元素绘制到后台缓冲
  3. @param dt 自上一帧绘制起经过的秒数
  4. */
  5. this.draw = function(/**Number*/ dt, /**CanvasRenderingContext2D*/ context, /**Number*/ xScroll, /**Number*/ yScroll){
  6. context.drawImage(this.image, this.x – xScroll, this.y – yScroll);
  7. }


ApplicationManager.js

  1. /**
  2. ApplicationManager用于管理应用
  3. @class
  4. */
  5. function ApplicationManager(){
  6. /**
  7. 初始化对象
  8. @return 对初始化对象的引用
  9. */
  10. this.startupApplicationManager = function(){
  11. this.bounce = new Bounce().startupBounce(g_image);
  12. return this;
  13. }
  14. }


ApplicationManager是第一个应用类,之所以将其归为应用类,是因为它用来定义应用的运行方式,而不是定义与浏览器的底层交互。这个类 非常简单,只用来创建并初始化Bounce类的一个新实例。表面上看,创建一个类仅仅是为了创建一个对象有点多此一举。但在更复杂的应用中,把创建和管理 游戏对象的逻辑放到一起是很有必要的。
Bounce.js

  1. /**
  2. 测试类,用于演示VisualGameObject类的用法
  3. @class
  4. */
  5. function Bounce(){
  6. /** x轴的运动方向
  7. @type Number
  8. */
  9. this.xDirection = 1;
  10. /** y轴的运动方向
  11. @type Number
  12. */
  13. this.yDirection = 1;
  14. /** 运动速度
  15. @type Number
  16. */
  17. this.speed = 10;
  18. /**
  19. 初始化对象
  20. @return 对初始化对象的引用
  21. */
  22. this.startupBounce = function(image){
  23. this.startupVisualGameObject(image, 0, 0, 0);
  24. return this;
  25. }
  26. /**
  27. 更新对象
  28. @param dt 自上一帧绘制起经过的秒数
  29. @param context 绘制上下文
  30. @param xScroll x轴的全局滚动值
  31. @param yScroll y轴的全局滚动值
  32. */
  33. this.update = function (/**Number*/ dt, /**CanvasRenderingContext2D*/context, /**Number*/ xScroll, /**Number*/ yScroll){
  34. this.x += dt * this.speed * this.xDirection;
  35. this.y += dt * this.speed * this.yDirection;
  36. if (this.x >= 450){
  37. this.x = 450;
  38. this.xDirection = -1;
  39. }
  40. else if (this.x <= 0){
  41. this.x = 0;
  42. this.xDirection = 1;
  43. }
  44. if (this.y >= 250){
  45. this.y = 250;
  46. this.yDirection = -1;
  47. }
  48. else if (this.y <= 0){
  49. this.y = 0;
  50. this.yDirection = 1;
  51. }
  52. }
  53. }
  54. Bounce.prototype = new VisualGameObject;


Bounce是第二个应用类,它扩展了VisualGameObject类,并将把自己绘制到屏幕上。Bounce类会显示一幅在屏幕上反弹的图像,效果非常类似第一篇文章中举的例子。这个类是在前面所有类的基础上实现最终动画的关键。
startupBounce函数接受一幅图像,通过调用startupVisualGameObject来初始化这个基本的类。

  1. /**
  2. 初始化对象
  3. @return 对初始化对象的引用
  4. */
  5. this.startupBounce = function(image){
  6. this.startupVisualGameObject(image, 0, 0, 0);
  7. return this;
  8. }


而update函数(将被GameObjectManager在渲染期间调用)会更新图像的位置,在图像到达画布边缘时反转方向。

  1. /**
  2. 更新对象
  3. @param dt 自上一帧绘制起经过的秒数
  4. @param context 绘制上下文
  5. @param xScroll x轴的全局滚动值
  6. @param yScroll y轴的全局滚动值
  7. */
  8. this.update = function (/**Number*/ dt, /**CanvasRenderingContext2D*/context, /**Number*/ xScroll, /**Number*/ yScroll){
  9. this.x += dt * this.speed * this.xDirection;
  10. this.y += dt * this.speed * this.yDirection;
  11. if (this.x >= 450){
  12. this.x = 450;
  13. this.xDirection = -1;
  14. }
  15. else if (this.x <= 0){
  16. this.x = 0;
  17. this.xDirection = 1;
  18. }
  19. if (this.y >= 250){
  20. this.y = 250;
  21. this.yDirection = -1;
  22. }
  23. else if (this.y <= 0){
  24. this.y = 0;
  25. this.yDirection = 1;
  26. }
  27. }
  28. }


就这些了。你可能会想,怎么没有与绘制这个对象有关的代码呢?相应的代码都在VisualGameObject类的draw函数中了。而且,由于 VisualGameObject类扩展了GameObject类,所以我们知道每渲染一帧都会调用一次update和draw函数。Bounce类中的 所有代码只跟让图像反弹有关,也就是修改变量x和y。
好啦,我们已经创建了一批类,基于这些类也实现了与第一个示例相同的效果。而有了这个框架,再创建游戏就不必因为绘制画布等底层逻辑以及管理游戏对象等问题而重复编码了。
看看示例Demo吧。http://webdemos.sourceforge.net/jsplatformer3/jsplatformer3.html


原创粉丝点击