fork download
  1. const canvas = document.getElementById("gameCanvas");
  2. const ctx = canvas.getContext("2d");
  3. canvas.width = 800;
  4. canvas.height = 600;
  5.  
  6. const player = {
  7. x: canvas.width / 2,
  8. y: canvas.height - 50,
  9. width: 50,
  10. height: 50,
  11. color: "blue",
  12. speed: 5,
  13. bullets: []
  14. };
  15.  
  16. const enemies = [];
  17. let score = 0;
  18.  
  19. function drawPlayer() {
  20. ctx.fillStyle = player.color;
  21. ctx.fillRect(player.x, player.y, player.width, player.height);
  22. }
  23.  
  24. function drawBullets() {
  25. ctx.fillStyle = "red";
  26. player.bullets.forEach((bullet, index) => {
  27. ctx.fillRect(bullet.x, bullet.y, 5, 10);
  28. bullet.y -= 5;
  29. if (bullet.y < 0) player.bullets.splice(index, 1);
  30. });
  31. }
  32.  
  33. function drawEnemies() {
  34. ctx.fillStyle = "green";
  35. enemies.forEach((enemy, index) => {
  36. ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
  37. enemy.y += 2;
  38. if (enemy.y > canvas.height) {
  39. enemies.splice(index, 1);
  40. }
  41. });
  42. }
  43.  
  44. function spawnEnemy() {
  45. enemies.push({
  46. x: Math.random() * (canvas.width - 50),
  47. y: 0,
  48. width: 50,
  49. height: 50
  50. });
  51. }
  52. setInterval(spawnEnemy, 1000);
  53.  
  54. function checkCollision() {
  55. player.bullets.forEach((bullet, bIndex) => {
  56. enemies.forEach((enemy, eIndex) => {
  57. if (
  58. bullet.x < enemy.x + enemy.width &&
  59. bullet.x + 5 > enemy.x &&
  60. bullet.y < enemy.y + enemy.height &&
  61. bullet.y + 10 > enemy.y
  62. ) {
  63. player.bullets.splice(bIndex, 1);
  64. enemies.splice(eIndex, 1);
  65. score += 10;
  66. }
  67. });
  68. });
  69. }
  70.  
  71. function drawScore() {
  72. ctx.fillStyle = "black";
  73. ctx.font = "20px Arial";
  74. ctx.fillText("Score: " + score, 10, 30);
  75. }
  76.  
  77. document.addEventListener("keydown", (event) => {
  78. if (event.key === "ArrowLeft" && player.x > 0) player.x -= player.speed;
  79. if (event.key === "ArrowRight" && player.x < canvas.width - player.width) player.x += player.speed;
  80. if (event.key === " ") player.bullets.push({ x: player.x + 22, y: player.y });
  81. });
  82.  
  83. function update() {
  84. ctx.clearRect(0, 0, canvas.width, canvas.height);
  85. drawPlayer();
  86. drawBullets();
  87. drawEnemies();
  88. checkCollision();
  89. drawScore();
  90. requestAnimationFrame(update);
  91. }
  92. update();
Success #stdin #stdout 0.02s 26104KB
stdin
Standard input is empty
stdout
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
canvas.width = 800;
canvas.height = 600;

const player = {
    x: canvas.width / 2,
    y: canvas.height - 50,
    width: 50,
    height: 50,
    color: "blue",
    speed: 5,
    bullets: []
};

const enemies = [];
let score = 0;

function drawPlayer() {
    ctx.fillStyle = player.color;
    ctx.fillRect(player.x, player.y, player.width, player.height);
}

function drawBullets() {
    ctx.fillStyle = "red";
    player.bullets.forEach((bullet, index) => {
        ctx.fillRect(bullet.x, bullet.y, 5, 10);
        bullet.y -= 5;
        if (bullet.y < 0) player.bullets.splice(index, 1);
    });
}

function drawEnemies() {
    ctx.fillStyle = "green";
    enemies.forEach((enemy, index) => {
        ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
        enemy.y += 2;
        if (enemy.y > canvas.height) {
            enemies.splice(index, 1);
        }
    });
}

function spawnEnemy() {
    enemies.push({
        x: Math.random() * (canvas.width - 50),
        y: 0,
        width: 50,
        height: 50
    });
}
setInterval(spawnEnemy, 1000);

function checkCollision() {
    player.bullets.forEach((bullet, bIndex) => {
        enemies.forEach((enemy, eIndex) => {
            if (
                bullet.x < enemy.x + enemy.width &&
                bullet.x + 5 > enemy.x &&
                bullet.y < enemy.y + enemy.height &&
                bullet.y + 10 > enemy.y
            ) {
                player.bullets.splice(bIndex, 1);
                enemies.splice(eIndex, 1);
                score += 10;
            }
        });
    });
}

function drawScore() {
    ctx.fillStyle = "black";
    ctx.font = "20px Arial";
    ctx.fillText("Score: " + score, 10, 30);
}

document.addEventListener("keydown", (event) => {
    if (event.key === "ArrowLeft" && player.x > 0) player.x -= player.speed;
    if (event.key === "ArrowRight" && player.x < canvas.width - player.width) player.x += player.speed;
    if (event.key === " ") player.bullets.push({ x: player.x + 22, y: player.y });
});

function update() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    drawPlayer();
    drawBullets();
    drawEnemies();
    checkCollision();
    drawScore();
    requestAnimationFrame(update);
}
update();