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.

game.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const $ = (s) => document.querySelector(s);
  2. const $$ = (s) => Array.from(document.querySelectorAll(s));
  3. const ROOT = $('#root');
  4. const X_BOUND = 0.05;
  5. const clouds = new Set();
  6. const bullets = new Set();
  7. let shootingPad = new ShootingPad();
  8. let running = true;
  9. let direction = 1;
  10. const initCloudField = () => {
  11. for (let y = 0; y < 6; y++) {
  12. for (let x = 0; x < 11; x++) {
  13. const cloud = new Cloud(X_BOUND + 0.06 * x, 0.04 + 0.04 * y, 0.03, 0.02);
  14. clouds.add(cloud);
  15. }
  16. }
  17. }
  18. const start = () => {
  19. $('.result')?.remove();
  20. shootingPad?.destroy();
  21. shootingPad = new ShootingPad();
  22. bullets.forEach(c => c.destroy());
  23. bullets.clear();
  24. clouds.forEach(c => c.destroy());
  25. clouds.clear();
  26. Cloud.reset();
  27. initCloudField();
  28. running = true;
  29. loop();
  30. }
  31. const gameOver = (result) => {
  32. const resultLabel = document.createElement('div');
  33. resultLabel.className = 'result';
  34. resultLabel.textContent = result;
  35. ROOT.appendChild(resultLabel);
  36. running = false;
  37. }
  38. const loop = () => {
  39. if (!running) return;
  40. let directionUpdated = false;
  41. Array.from(clouds).forEach(c => {
  42. c.update(direction);
  43. c.draw();
  44. if (c.x < X_BOUND - 0.01 || c.x > 1.01 - X_BOUND) {
  45. directionUpdated = true;
  46. }
  47. if (!c.alive) {
  48. clouds.delete(c);
  49. }
  50. });
  51. if (directionUpdated) {
  52. direction = -direction;
  53. }
  54. Array.from(bullets).forEach(b => {
  55. b.update();
  56. b.draw();
  57. if (!b.alive) {
  58. bullets.delete(b);
  59. }
  60. });
  61. shootingPad.update();
  62. shootingPad.draw();
  63. if (clouds.size === 0) {
  64. gameOver('WIN');
  65. }
  66. requestAnimationFrame(loop);
  67. };
  68. document.addEventListener('keypress', () => {
  69. if (!running) {
  70. start();
  71. }
  72. });
  73. start();