资讯专栏INFORMATION COLUMN

Chrome 小恐龙游戏源码探究四 -- 随机绘制云朵

svtter / 792人阅读

摘要:文章首发于我的博客前言上一篇文章小恐龙游戏源码探究三进入街机模式实现了开场动画和街机模式。

</>复制代码

  1. 文章首发于我的 GitHub 博客
前言

上一篇文章:《Chrome 小恐龙游戏源码探究三 -- 进入街机模式》 实现了开场动画和街机模式。这一篇文章中,将实现云朵的随机绘制。

云朵类 Cloud

定义云朵类 Cloud

</>复制代码

  1. /**
  2. * 云朵类
  3. * @param {HTMLCanvasElement} canvas 画布
  4. * @param {Object} spritePos 图片在雪碧图中的位置信息
  5. * @param {Number} containerWidth 容器的宽度
  6. */
  7. function Cloud(canvas, spritePos, containerWidth) {
  8. this.canvas = canvas;
  9. this.ctx = canvas.getContext("2d");
  10. this.spritePos = spritePos;
  11. this.containerWidth = containerWidth;
  12. // 坐标
  13. this.xPos = containerWidth;
  14. this.yPos = 0;
  15. // 该云朵是否需要删除
  16. this.remove = false;
  17. // 随机云朵之间的间隙
  18. this.cloudGap = getRandomNum(Cloud.config.MIN_CLOUD_GAP,
  19. Cloud.config.MAX_CLOUD_GAP);
  20. this.init();
  21. }

相关的配置参数:

</>复制代码

  1. Cloud.config = {
  2. WIDTH: 46,
  3. HEIGHT: 14,
  4. MIN_CLOUD_GAP: 100, // 云之间的最小间隙
  5. MAX_CLOUD_GAP: 400, // 云之间的最大间隙
  6. MIN_SKY_LEVEL: 71, // 云的最小高度
  7. MAX_SKY_LEVEL: 30, // 云的最大高度
  8. BG_CLOUD_SPEED: 0.2, // 云的速度
  9. CLOUD_FREQUENCY: 0.5, // 云的频率
  10. MAX_CLOUDS: 6 // 云的最大数量
  11. };

补充本篇文章中会用到的数据:

</>复制代码

  1. Runner.spriteDefinition = {
  2. LDPI: {
  3. // ...
  4. + CLOUD: {x: 86, y: 2},
  5. },
  6. };

绘制和更新云朵的方法定义如下:

</>复制代码

  1. Cloud.prototype = {
  2. // 初始化云朵
  3. init: function () {
  4. this.yPos = getRandomNum(Cloud.config.MAX_SKY_LEVEL,
  5. Cloud.config.MIN_SKY_LEVEL);
  6. this.draw();
  7. },
  8. // 绘制云朵
  9. draw: function () {
  10. this.ctx.save();
  11. var sourceWidth = Cloud.config.WIDTH;
  12. var sourceHeight = Cloud.config.HEIGHT;
  13. var outputWidth = sourceWidth;
  14. var outputHeight = sourceHeight;
  15. this.ctx.drawImage(
  16. Runner.imageSprite,
  17. this.spritePos.x, this.spritePos.y,
  18. sourceWidth, sourceHeight,
  19. this.xPos, this.yPos,
  20. outputWidth, outputHeight
  21. );
  22. this.ctx.restore();
  23. },
  24. // 更新云朵
  25. update: function (speed) {
  26. if (!this.remove) {
  27. this.xPos -= speed;
  28. this.draw();
  29. // 云朵移出 canvas,将其删除
  30. if (!this.isVisible()) {
  31. this.remove = true;
  32. }
  33. }
  34. },
  35. // 云朵是否移出 canvas
  36. isVisible: function () {
  37. return this.xPos + Cloud.config.WIDTH > 0;
  38. },
  39. };
  40. /**
  41. * 获取 [min, max] 之间的随机数
  42. * @param {Number} min 最小值
  43. * @param {Number} max 最大值
  44. * @return {Number}
  45. */
  46. function getRandomNum(min, max) {
  47. return Math.floor(Math.random() * (max - min + 1)) + min;
  48. }

然后需要通过 Horizon 类调用 Cloud 类。首先在 Horizon 类中添加与云朵相关的属性:

</>复制代码

  1. - function Horizon(canvas, spritePos) {
  2. + function Horizon(canvas, spritePos, dimensions) {
  3. this.canvas = canvas;
  4. this.ctx = this.canvas.getContext("2d");
  5. this.spritePos = spritePos;
  6. + this.dimensions = dimensions;
  7. + // 云的频率
  8. + this.cloudFrequency = Cloud.config.CLOUD_FREQUENCY;
  9. + // 云
  10. + this.clouds = [];
  11. + this.cloudSpeed = Cloud.config.BG_CLOUD_SPEED;
  12. // 地面
  13. this.horizonLine = null;
  14. this.init();
  15. }

修改在 Runner 中调用 Horizon 类时传入的参数:

</>复制代码

  1. Runner.prototype = {
  2. init: function () {
  3. // ...
  4. + // 加载背景类 Horizon
  5. - this.horizon = new Horizon(this.canvas, this.spriteDef);
  6. + this.horizon = new Horizon(this.canvas, this.spriteDef,
  7. + this.dimensions);
  8. // ...
  9. },
  10. };

添加生成云朵的方法,将生成的云朵存入数组 clouds

</>复制代码

  1. Horizon.prototype = {
  2. // 添加新的云朵
  3. addCloud: function () {
  4. this.clouds.push(new Cloud(this.canvas, this.spritePos.CLOUD,
  5. this.dimensions.WIDTH));
  6. },
  7. };

调用 addCloud 方法来初始化云朵:

</>复制代码

  1. Horizon.prototype = {
  2. init: function () {
  3. + this.addCloud();
  4. this.horizonLine = new HorizonLine(this.canvas, this.spritePos.HORIZON);
  5. },
  6. };

添加更新云朵的方法:

</>复制代码

  1. Horizon.prototype = {
  2. // 更新 canvas 上的云朵
  3. updateCloud: function (deltaTime, speed) {
  4. var cloudSpeed = Math.ceil(deltaTime * this.cloudSpeed * speed / 1000);
  5. var numClouds = this.clouds.length;
  6. if (numClouds) {
  7. for (var i = numClouds - 1; i >= 0; i--) {
  8. this.clouds[i].update(cloudSpeed);
  9. }
  10. var lastCloud = this.clouds[numClouds - 1];
  11. // 检查是否需要添加新的云朵
  12. // 添加云朵的条件:云朵数量少于最大数量、
  13. // 最后一个云朵后面的空间大于它的间隙、
  14. // 云朵出现频率符合要求
  15. if (numClouds < Cloud.config.MAX_CLOUDS &&
  16. (this.dimensions.WIDTH - lastCloud.xPos) > lastCloud.cloudGap &&
  17. this.cloudFrequency > Math.random()) {
  18. this.addCloud();
  19. }
  20. // 删除 remove 属性为 true 的云朵
  21. this.clouds = this.clouds.filter(function (item) {
  22. return !item.remove;
  23. });
  24. } else {
  25. this.addCloud();
  26. }
  27. },
  28. };

调用 updateCloud 方法,来实现对云朵的更新:

</>复制代码

  1. Horizon.prototype = {
  2. update: function (deltaTime, currentSpeed) {
  3. this.horizonLine.update(deltaTime, currentSpeed);
  4. + this.updateCloud(deltaTime, currentSpeed);
  5. },
  6. };

运行效果如下:

</>复制代码

  1. 查看添加或修改的代码:戳这里

Demo 体验地址:https://liuyib.github.io/blog/demo/game/google-dino/add-cloud/

上一篇 下一篇
Chrome 小恐龙游戏源码探究三 -- 进入街机模式 Chrome 小恐龙游戏源码探究五 -- 随机绘制障碍

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/103882.html

相关文章

  • Chrome 恐龙游戏源码探究一 -- 绘制静态地面

    摘要:首先是绘制静态的地面。上一篇下一篇无小恐龙游戏源码探究二让地面动起来 文章首发于我的 GitHub 博客 目录 Chrome 小恐龙游戏源码探究一 -- 绘制静态地面 Chrome 小恐龙游戏源码探究二 -- 让地面动起来 Chrome 小恐龙游戏源码探究三 -- 进入街机模式 Chrome 小恐龙游戏源码探究四 -- 随机绘制云朵 Chrome 小恐龙游戏源码探究五 -- 随机绘...

    lixiang 评论0 收藏0
  • Chrome 恐龙游戏源码探究五 -- 随机绘制障碍

    摘要:文章首发于我的博客前言上一篇文章小恐龙游戏源码探究四随机绘制云朵实现了云朵的随机绘制,这一篇文章中将实现仙人掌翼龙障碍物的绘制游戏速度的改变障碍物的类型有两种仙人掌和翼龙。 文章首发于我的 GitHub 博客 前言 上一篇文章:《Chrome 小恐龙游戏源码探究四 -- 随机绘制云朵》 实现了云朵的随机绘制,这一篇文章中将实现:1、仙人掌、翼龙障碍物的绘制 2、游戏速度的改变 障碍物...

    tomorrowwu 评论0 收藏0
  • Chrome 恐龙游戏源码探究三 -- 进入街机模式

    摘要:文章首发于我的博客前言上一篇文章小恐龙游戏源码探究二让地面动起来实现了地面的移动。街机模式的效果就是游戏开始后,进入全屏模式。例如可以看到,进入街机模式之前,有一段开场动画。 文章首发于我的 GitHub 博客 前言 上一篇文章:《Chrome 小恐龙游戏源码探究二 -- 让地面动起来》 实现了地面的移动。这一篇文章中,将实现效果:1、浏览器失焦时游戏暂停,聚焦游戏继续。 2、开场动...

    yeooo 评论0 收藏0
  • Chrome 恐龙游戏源码探究六 -- 记录游戏分数

    摘要:文章首发于我的博客前言上一篇文章小恐龙游戏源码探究五随机绘制障碍实现了障碍物仙人掌和翼龙的绘制。在游戏中,小恐龙移动的距离就是游戏的分数。 文章首发于我的 GitHub 博客 前言 上一篇文章:《Chrome 小恐龙游戏源码探究五 -- 随机绘制障碍》 实现了障碍物仙人掌和翼龙的绘制。这一篇将实现当前分数、最高分数的记录和绘制。 在游戏中,小恐龙移动的距离就是游戏的分数。分数每达 1...

    Jingbin_ 评论0 收藏0
  • Chrome 恐龙游戏源码探究八 -- 奔跑的恐龙

    摘要:例如,将函数修改为小恐龙眨眼这样小恐龙会不停的眨眼睛。小恐龙的开场动画下面来实现小恐龙对键盘按键的响应。接下来还需要更新动画帧才能实现小恐龙的奔跑动画。 文章首发于我的 GitHub 博客 前言 上一篇文章:《Chrome 小恐龙游戏源码探究七 -- 昼夜模式交替》实现了游戏昼夜模式的交替,这一篇文章中,将实现:1、小恐龙的绘制 2、键盘对小恐龙的控制 3、页面失焦后,重新聚焦会重置...

    paulquei 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<