diff options
-rw-r--r-- | js/hill/game.js | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/js/hill/game.js b/js/hill/game.js index 6413119..ef7cd65 100644 --- a/js/hill/game.js +++ b/js/hill/game.js @@ -9,13 +9,12 @@ const jumpForce = 14.75; const player = { x: 200, y: 0, - vx: 2, - vy: 2, + vx: 16, + vy: 16, jumpForce: gravity + jumpForce, onGround: false, width: 20, - height: 20, - health: 10 + height: 20 }; const terrain = { @@ -33,14 +32,12 @@ function initialize() { for (let i = 0; i < 50; i++) { const object = { x: i * 10 * canvas.width / 10, - width: 20, + width: 50, height: Math.random() * 22 }; - // Calculate the y-coordinate of the terrain line at the x-coordinate of the object let terrainY = ((terrain.end.y - terrain.start.y) / (terrain.end.x - terrain.start.x)) * (object.x - terrain.start.x) + terrain.start.y; - // Set the y-coordinate of the object to the y-coordinate of the terrain line minus the height of the object object.y = terrainY - object.height; objects.push(object); @@ -54,13 +51,13 @@ function draw() { ctx.translate(-player.x + canvas.width / 4, -player.y + canvas.height / 2); - ctx.fillStyle = 'blue'; + ctx.fillStyle = '#b6daf9'; ctx.fillRect(player.x, player.y, player.width, player.height); terrain.start.x = player.x - canvas.width; terrain.end.x = player.x + canvas.width; - ctx.fillStyle = 'brown'; + ctx.fillStyle = '#aaa'; objects.forEach(object => { ctx.beginPath(); ctx.arc(object.x, object.y, object.width / 2, 0, Math.PI * 2, true); @@ -70,13 +67,29 @@ function draw() { ctx.beginPath(); ctx.moveTo(terrain.start.x, terrain.start.y); ctx.lineTo(terrain.end.x, terrain.end.y); - ctx.strokeStyle = 'green'; + ctx.strokeStyle = '#ddd'; ctx.lineWidth = terrain.height; ctx.stroke(); - ctx.font = '20px Arial'; - ctx.fillStyle = 'black'; - ctx.fillText('Health: ' + player.health, player.x, player.y - 22); + // ctx.font = '20px Arial'; + // ctx.fillStyle = 'black'; + // ctx.fillText('Health: ' + player.health, player.x, player.y - 22); + + ctx.fillStyle = '#a2a2f2'; + for (let i = particles.length - 1; i >= 0; i--) { + let particle = particles[i]; + + ctx.fillRect(particle.x, particle.y, 5, 5); + + particle.x += particle.vx; + particle.y += particle.vy; + + particle.lifespan--; + + if (particle.lifespan === 0) { + particles.splice(i, 1); + } + } ctx.restore(); } @@ -97,6 +110,8 @@ const checkCollision = () => { }); }; +const particles = []; + const gameLoop = () => { player.vy += gravity; @@ -111,18 +126,23 @@ const gameLoop = () => { } for (let object of objects) { - // Update y-coordinate based on gravity object.y += gravity; - // Calculate the y-coordinate of the terrain line at the x-coordinate of the object let terrainY = ((terrain.end.y - terrain.start.y) / (terrain.end.x - terrain.start.x)) * (object.x - terrain.start.x) + terrain.start.y; - // If the new y-coordinate is below the terrain line, set it to the terrain line if (object.y + object.height > terrainY) { object.y = terrainY - object.height; } } + particles.push({ + x: player.x, + y: player.y, + vx: Math.random() * 2 - 1, + vy: Math.random() * 2 - 1, + lifespan: 17 + }); + checkCollision(); initialize(); draw(); |