Game for OLC Code Jam 2022
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

shooting_pad.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const keys = {};
  2. document.addEventListener('keydown', (e) => {
  3. keys[e.key] = true;
  4. });
  5. document.addEventListener('keyup', (e) => {
  6. keys[e.key] = false;
  7. });
  8. let blink = false;
  9. setInterval(() => blink = !blink, 200);
  10. const MAX_LIVES = 3;
  11. const INVULNERABILITY_INTERVAL = 2000;
  12. class ShootingPad extends Entity {
  13. constructor() {
  14. super(0.5, 0.95, 0.04, 0.03);
  15. const image = new Image();
  16. image.src = 'img/shooting-pad.svg';
  17. image.onload = () => this.image = image;
  18. this.shootSound = new Audio();
  19. this.shootSound.src = 'snd/shoot.ogg';
  20. this.hitSound = new Audio();
  21. this.hitSound.src = 'snd/hit.ogg';
  22. this.lastShootTime = Date.now();
  23. this.invulnerable = false;
  24. this.lives = MAX_LIVES;
  25. this.initLives();
  26. }
  27. update() {
  28. if (keys['ArrowLeft'] || keys['a']) {
  29. this.x -= VELOCITY[0] * 2;
  30. }
  31. if (keys['ArrowRight'] || keys['d']) {
  32. this.x += VELOCITY[0] * 2;
  33. }
  34. if (this.x < X_BOUND) {
  35. this.x = X_BOUND;
  36. }
  37. if (this.x > 1 - X_BOUND) {
  38. this.x = 1 - X_BOUND;
  39. }
  40. if (keys[' '] && Date.now() - this.lastShootTime >= 300) {
  41. this.lastShootTime = Date.now();
  42. const bullet = new Bullet(this.x, this.y - 0.009);
  43. bullets.add(bullet);
  44. if (soundEnabled) {
  45. this.shootSound.currentTime = 0;
  46. this.shootSound.play().catch(() => { });
  47. }
  48. }
  49. }
  50. draw() {
  51. if (!this.invulnerable || blink) {
  52. super.draw();
  53. }
  54. }
  55. initLives() {
  56. LIVES_LABEL.textContent = `× ${this.lives}`;
  57. }
  58. lifeOver() {
  59. if (this.invulnerable) return;
  60. this.lives--;
  61. this.initLives();
  62. if (soundEnabled) {
  63. this.hitSound.currentTime = 0;
  64. this.hitSound.play().catch(() => { });
  65. }
  66. if (this.lives === 0) {
  67. return gameOver('Game over');
  68. } else {
  69. this.invulnerable = true;
  70. setTimeout(() => {
  71. this.invulnerable = false;
  72. }, INVULNERABILITY_INTERVAL);
  73. }
  74. }
  75. }