新建 score.ts 文件

    1. import { cvs_one, ctx_one } from "./init";
    2. export default class Socre {
    3. fruitNum = 0; // 吃到果实的数量
    4. doubleMode = 1; // 是否开启双倍模式
    5. total = 0;
    6. constructor() {}
    7. // 进行一轮计分,回到初始状态
    8. reset(){
    9. this.total += this.fruitNum * 10 * this.doubleMode;
    10. this.fruitNum = 0;
    11. this.doubleMode = 1;
    12. }
    13. // 渲染分数到界面上面去
    14. draw(){
    15. const w = cvs_one.width;
    16. const h = cvs_one.height;
    17. ctx_one.save()
    18. ctx_one.fillStyle ='white' // 设置画笔颜色
    19. ctx_one.textAlign = 'center' // 设置对齐方式
    20. ctx_one.fillText(this.total.toString() , w * .5, h - 50)
    21. ctx_one.fillText(this.fruitNum.toString(), w * .5, h - 80)
    22. ctx_one.restore()
    23. }
    24. }

    并在 init.ts 中通过 new 初始化它,并导出。因为我们需要在 game-loop 里面使用它。具体代码我就不写了,因为太简单了,没必要又复制一个文件代码过来,实在不知道看完整源码吧。

    在 game-loop.ts 里面添加计分逻辑

    因为需要判断果实类型,所以需要从 fruits.ts 里面拿到枚举,当大鱼没有吃到果实的时候,与小鱼碰撞不会生效。

    1. import { bgPic, cvs_width , cvs_height, ctx_two, ctx_one, anemones, fruits, fish_mother, fish_baby, score } from "./init";
    2. import utils from "./utils";
    3. import { FruitType } from "./fruits";
    4. let lastTime: number = Date.now(), // 记录上一次绘制的时间
    5. deltaTime: number = 0; // requestAnimationFrame 执行完成所用的时间 = 当前时间 - 上一次绘制的世界
    6. /**
    7. * 鱼妈妈与果实的碰撞检测
    8. */
    9. function fishAndFruitsCollision() {
    10. for (let i = fruits.num; i >= 0; i--) {
    11. // 假如或者就计算鱼儿与果实的距离
    12. if(fruits.alive[i]) {
    13. // 得到距离的平方根
    14. const distance = utils.getDistance(
    15. {x: fruits.x[i], y: fruits.y[i]},
    16. {x: fish_mother.x, y: fish_mother.y}
    17. );
    18. // 假如距离小于 500 让果实死亡
    19. if(distance < 500) {
    20. fruits.dead(i)
    21. score.fruitNum++; // 计分系统的果实数加一
    22. if(fruits.fruitType[i] === FruitType.Blue) { // 假如吃到蓝色道具果实,开启双倍模式
    23. score.doubleMode = 2;
    24. }
    25. }
    26. }
    27. }
    28. }
    29. /**
    30. * 鱼妈妈与鱼宝宝的碰撞检测
    31. */
    32. function fishMotherAndBabyCollision() {
    33. // 得到距离的平方根
    34. const distance = utils.getDistance(
    35. {x: fish_baby.x, y: fish_baby.y},
    36. {x: fish_mother.x, y: fish_mother.y}
    37. );
    38. // 假如距离小于 900 就喂食给 baby,且必须要鱼妈妈吃到了果实才能喂给小鱼
    39. if (distance < 900 && score.fruitNum != 0) {
    40. fish_baby.recover();
    41. // 把能力给小鱼,果实数清零,计算分数一次
    42. score.reset()
    43. }
    44. }
    45. function gameLoop() {
    46. const now = Date.now()
    47. deltaTime = now - lastTime;
    48. lastTime = now;
    49. // 给 deltaTime 设置上线
    50. if(deltaTime > 40) deltaTime = 40;
    51. // console.log(deltaTime);
    52. drawBackbround() // 画背景图片
    53. anemones.draw() // 海葵绘制
    54. fruits.draw() // 果实绘制
    55. fruits.monitor() // 监视果实,让死去的果实得到新生
    56. ctx_one.clearRect(0, 0, cvs_width, cvs_width); // 清除掉所有,再进行绘制,要不然的话会多次绘制而进行重叠。
    57. fish_mother.draw() // 绘制鱼妈妈
    58. fish_baby.draw() // 绘制鱼宝宝
    59. fishAndFruitsCollision() // 每一帧都进行碰撞检测
    60. fishMotherAndBabyCollision()
    61. score.draw() // 绘制分数
    62. requestAnimationFrame(gameLoop); // 不断的循环 gameLoop,且流畅性提升
    63. }
    64. function drawBackbround() {
    65. ctx_two.drawImage(bgPic, 0, 0, cvs_width, cvs_height)
    66. }
    67. export { deltaTime }
    68. export default gameLoop;