honkrun.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. class Vector2D {
  2. constructor(x, y) {
  3. this.x = x;
  4. this.y = y;
  5. }
  6. }
  7. class Entity {
  8. constructor(pos, vel, size, color) {
  9. this.pos = pos;
  10. this.vel = vel;
  11. this.size = size;
  12. this.color = color;
  13. }
  14. collides(e) {
  15. return !(this.pos.x > e.pos.x + e.size.x || this.pos.x + this.size.x < e.pos.x)
  16. && !(this.pos.y > e.pos.y + e.size.y || this.pos.y + this.size.y < e.pos.y);
  17. }
  18. }
  19. var player = new Entity(
  20. new Vector2D(50, 0),
  21. new Vector2D(0, 0),
  22. new Vector2D(100, 100),
  23. "#FF0000"
  24. );
  25. var enemies = [];
  26. var spawnCooldown = 0;
  27. var c = document.getElementById("myCanvas");
  28. var ctx = c.getContext("2d");
  29. // resize the canvas to fill browser window dynamically
  30. window.addEventListener('resize', resizeCanvas, false);
  31. document.onkeypress = function(evt) {
  32. evt = evt || window.event;
  33. var charCode = evt.keyCode || evt.which;
  34. var charStr = String.fromCharCode(charCode);
  35. evt.preventDefault();
  36. onKeyPress(charStr);
  37. };
  38. function resizeCanvas() {
  39. c.width = window.innerWidth;
  40. c.height = window.innerHeight;
  41. draw();
  42. }
  43. resizeCanvas();
  44. function randInt(min, max) {
  45. return min + Math.random() * (max - min);
  46. }
  47. function drawEntity(e) {
  48. ctx.fillStyle = e.color;
  49. ctx.fillRect(e.pos.x, e.pos.y, e.size.x, e.size.y);
  50. }
  51. function draw() {
  52. ctx.clearRect(0, 0, c.width, c.height);
  53. drawEntity(player);
  54. enemies.forEach(drawEntity);
  55. }
  56. function update() {
  57. // Spawn enemies
  58. if (enemies.length < 5 && spawnCooldown <= 0) {
  59. enemies.push(new Entity(new Vector2D(c.width + randInt(50, 100), 0), new Vector2D(randInt(-15, -5), 0), new Vector2D(randInt(50, 200), randInt(50, 200)), "#00FF00"));
  60. spawnCooldown = randInt(1 * 60, 3 * 60); // 1-3 seconds
  61. }
  62. spawnCooldown--;
  63. // Apply physics to all entities
  64. (enemies.concat([player])).forEach(function (e) {
  65. e.pos.x += e.vel.x;
  66. e.pos.y += e.vel.y;
  67. e.vel.y += 1;
  68. if (e.pos.y + e.size.y > c.height) {
  69. e.vel.y = 0;
  70. e.pos.y = c.height - e.size.y;
  71. }
  72. });
  73. // Recolor enemies
  74. enemies.forEach(function (e) {
  75. if (e.pos.x < player.pos.x) {
  76. e.color = "#0000FF";
  77. }
  78. });
  79. // Kill enemies
  80. enemies = enemies.filter(function (e) {
  81. return e.pos.x > -e.size.x;
  82. });
  83. // Kill player
  84. enemies.forEach(function (e) {
  85. if (e.collides(player)) {
  86. console.log("Game over!");
  87. clearInterval(interval);
  88. }
  89. });
  90. }
  91. function onKeyPress(key) {
  92. switch (key) {
  93. case "w":
  94. if (player.pos.y + player.size.y == c.height) {
  95. player.vel.y = -40;
  96. }
  97. break;
  98. }
  99. }
  100. function mainloop() {
  101. update();
  102. draw();
  103. }
  104. var interval = setInterval(mainloop, 1000 / 60); // 60 fps