123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- const keys = {};
-
- document.addEventListener('keydown', (e) => {
- keys[e.key] = true;
- });
- document.addEventListener('keyup', (e) => {
- keys[e.key] = false;
- });
-
- let blink = false;
-
- setInterval(() => blink = !blink, 200);
-
- const MAX_LIVES = 3;
- const INVULNERABILITY_INTERVAL = 2000;
-
- class ShootingPad extends Entity {
- constructor() {
- super(0.5, 0.95, 0.04, 0.03);
- const image = new Image();
- image.src = 'img/shooting-pad.svg';
- image.onload = () => this.image = image;
-
- this.lastShootTime = Date.now();
- this.invulnerable = false;
- this.lives = MAX_LIVES;
- this.initLives();
- }
-
- update() {
- if (keys['ArrowLeft'] || keys['a']) {
- this.x -= VELOCITY[0] * 2;
- }
- if (keys['ArrowRight'] || keys['d']) {
- this.x += VELOCITY[0] * 2;
- }
- if (this.x < X_BOUND) {
- this.x = X_BOUND;
- }
- if (this.x > 1 - X_BOUND) {
- this.x = 1 - X_BOUND;
- }
- if (keys[' '] && Date.now() - this.lastShootTime >= 300) {
- this.lastShootTime = Date.now();
- const bullet = new Bullet(this.x, this.y - 0.009);
- bullets.add(bullet);
- }
- }
-
- draw() {
- if (!this.invulnerable || blink) {
- super.draw();
- }
- }
-
- initLives() {
- $('#lives').textContent = `× ${this.lives}`;
- }
-
- lifeOver() {
- if (this.invulnerable) return;
-
- this.lives--;
- this.initLives();
-
- if (this.lives === 0) {
- return gameOver('Game over');
- } else {
- this.invulnerable = true;
- setTimeout(() => {
- this.invulnerable = false;
- }, INVULNERABILITY_INTERVAL);
- }
- }
- }
|