lover-fish
  • Introduction
  • 搭建环境
  • 绘制背景
  • 绘制海葵
  • 绘制果实
  • 果实动画
  • 控制果实的数量
  • 增加果实的类型
  • 绘制鱼
  • 让鱼动起来
  • 让鱼跟着鼠标动起来
  • 吃果实之碰撞检测
  • 创建鱼宝宝
  • 给鱼添加动画
  • 加入计分模块
  • 大鱼身体特效
  • 游戏结束逻辑
  • 增加吃果实特效
  • 海葵动画
  • 漂浮物
Powered by GitBook
On this page

漂浮物

原理就是通过 Math.Sin 跟随海葵的频率摆动起来,跟海葵摆动一样的。

import { cvs_height, cvs_width, ctx_one } from "./init";
import { deltaTime } from "./game-loop";


class Dust {
  dustPic: Array<HTMLImageElement> = []
  x: number[] = [];
  y: number[] = [];
  amp: number[] = [];
  No: number[] = []; // 渲染哪一张图片
  alpha = 0.6; // 角度
  num = 30 // 数量
  constructor() {
    for (var i = 0; i < 7; ++i) {
      this.dustPic[i] = new Image()
      this.dustPic[i].src = `assets/img/dust${i}.png`;
    }

    for (let i = 0; i < this.num; ++i) {
      this.x[i] = Math.random() * cvs_width;
      this.y[i] = Math.random() * cvs_height;
      this.amp[i] = 20 + Math.random() * 15;

      this.No[i] = Math.floor(Math.random() * 7)
    }
    this.alpha = 0
  }

  draw(){
    this.alpha += deltaTime * 0.001
    const l = Math.sin(this.alpha)
    for (let i = 0; i < this.num; ++i) {
      let no = this.No[i];
      ctx_one.drawImage(this.dustPic[this.No[i]], this.x[i] + l * this.amp[i] ,this.y[i])
    }
  }
}

export default Dust;

好了,现在已经完成了。存在一些 bug,这里就不修复了。

总算是体验了一把做游戏的感觉,感觉再也没下次了。

Previous海葵动画

Last updated 7 years ago