单个水管 Pipe
实现思路:
水管的实现也很简单,先是继承 Rectangle
但是需要注意的是,他也有一些属性是
水管的位置是随机生产的,不可能每一对水管的高度都是一样的,那游戏就失去了趣味性,所以
height
需要外部传入水管高度影响了位置,所以
top
需要外部传入水管速度需要和大地速度保持一致,所以
xSpeed
需要在外部传入水管是计时器里面不断生成的,所以
水管dom
需要外部传入
实现代码
点击 展开/收起 jsx 代码
/**
* @description 单个水管类
*/
import Rectangle from './Rectangle';
class Pipe extends Rectangle {
constructor(height, top, speed, pipeDom) {
// 游戏面板的宽度
const gameWidth = document.querySelector('.game').clientWidth;
super(50, height, gameWidth, top, speed, 0, pipeDom);
}
onMove() {
// 水管跑到最左边然后不见了之后,要移除dom
if (this.left < -this.width) {
this.dom.remove();
}
}
}
export default Pipe;
← 小鸟 Bird 成对水管 PipePare →