Create a fun platformer game in HTML/CSS/Javascript that I can control with the arrow…
- User
Create a fun platformer game in HTML/CSS/Javascript that I can control with the arrow keys, make it fun and creative with lots of fun obstacles along the way
- Assistant
Certainly! Below is a simple yet fun platformer game created using HTML, CSS, and JavaScript. You can control the character with the arrow keys. The game features various obstacles and collectibles to make it more engaging.
Index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Fun Platformer Game</title> <link rel="stylesheet" href="styles.css"> </head> <body> <canvas id="gameCanvas" width="800" height="600"></canvas> <script src="game.js" defer></script> </body> </html>styles.css
body { margin: 0; overflow: hidden; background-color: #87CEEB; display: flex; justify-content: center; align-items: center; height: 100vh; } canvas { border: 2px solid #000; }game.js
const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); // Game variables const gravity = 0.9; const friction = 0.85; let gameFrame = 0; let score = 0; // Player const player = { x: 50, y: canvas.height - 150, width: 50, height: 50, color: 'red', speed: 5, velY: 0, jumping: false, grounded: false }; // Keys const keys = { ArrowUp: { pressed: false }, ArrowDown: { pressed: false }, ArrowLeft: { pressed: false }, ArrowRight: { pressed: false } }; // Platforms const platforms = [ {x: 0, y: canvas.height - 50, width: canvas.width, height: 50, color: 'green'}, {x: 200, y: canvas.height - 200, width: 200, height: 50, color: 'brown'}, {x: 500, y: canvas.height - 350, width: 200, height: 50, color: 'purple'}, {x: 100, y: canvas.height - 500, width: 200, height: 50, color: 'orange'} ]; // Obstacles const obstacles = [ {x: 400, y: canvas.height - 250, width: 50, height: 50, color: 'black'}, {x: 600, y: canvas.height - 400, width: 50, height: 50, color: 'black'}, ]; // Collectibles const collectibles = [ {x: 300, y: canvas.height - 450, width: 20, height: 20, color: 'yellow'}, {x: 700, y: canvas.height - 550, width: 20, height: 20, color: 'yellow'}, ]; function drawRectangle(x, y, width, height, color) { ctx.fillStyle = color; ctx.fillRect(x, y, width, height); } function drawPlayer() { drawRectangle(player.x, player.y, player.width, player.height, player.color); } function drawPlatforms() { platforms.forEach(platform => { drawRectangle(platform.x, platform.y, platform.width, platform.height, platform.color); }); } function drawObstacles() { obstacles.forEach(obstacle => { drawRectangle(obstacle.x, obstacle.y, obstacle.width, obstacle.height, obstacle.color); }); } function drawCollectibles() { collectibles.forEach(collectible => { drawRectangle(collectible.x, collectible.y, collectible.width, collectible.height, collectible.color); }); } function checkCollision(rect1, rect2) { return rect1.x < rect2.x + rect2.width && rect1.x + rect1.width > rect2.x && rect1.y < rect2.y + rect2.height && rect1.y + rect1.height > rect2.y; } function updatePlayer() { // Apply gravity if (!player.grounded) { player.velY += gravity; } else { player.velY = 0; } // Move player if (keys.ArrowLeft.pressed && player.x - player.speed > 0) { player.x -= player.speed; } if (keys.ArrowRight.pressed && player.x + player.speed + player.width < canvas.width) { player.x += player.speed; } if (keys.ArrowUp.pressed && player.grounded) { player.velY = -15; player.jumping = true; player.grounded = false; } player.y += player.velY; // Check for platform collision player.grounded = false; platforms.forEach(platform => { if (checkCollision(player, platform)) { player.grounded = true; player.y = platform.y - player.height; } }); // Check for obstacle collision obstacles.forEach(obstacle => { if (checkCollision(player, obstacle)) { alert("Game Over! Your score: " + score); location.reload(); } }); // Check for collectible collision for (let i = 0; i < collectibles.length; i++) { if (checkCollision(player, collectibles[i])) { score += 10; collectibles.splice(i, 1); break; } } } function animate() { ctx.clearRect(0, 0, canvas.width, canvas.height); drawPlatforms(); drawObstacles(); drawCollectibles(); drawPlayer(); updatePlayer(); // Display score ctx.fillStyle = 'black'; ctx.font = '20px Arial'; ctx.fillText(`Score: ${score}`, 10, 30); gameFrame++; requestAnimationFrame(animate); } // Key event listeners document.addEventListener('keydown', ({key}) => { switch (key) { case 'ArrowUp': keys.ArrowUp.pressed = true; break; case 'ArrowDown': keys.ArrowDown.pressed = true; break; case 'ArrowLeft': keys.ArrowRight.pressed = false; keys.ArrowLeft.pressed = true; break; case 'ArrowRight': keys.ArrowLeft.pressed = false; keys.ArrowRight.pressed = true; break; } }); document.addEventListener('keyup', ({key}) => { switch (key) { case 'ArrowUp': keys.ArrowUp.pressed = false; break; case 'ArrowDown': keys.ArrowDown.pressed = false; break; case 'ArrowLeft': keys.ArrowLeft.pressed = false; break; case 'ArrowRight': keys.ArrowRight.pressed = false; break; } }); animate();Explanation:
-
HTML (index.html):
- A simple HTML structure with a
<canvas>element where the game will be rendered. - Links to the
styles.cssfor styling andgame.jsfor the game logic.
- A simple HTML structure with a
-
CSS (styles.css):
- Basic styling to centre the canvas and remove scrollbars.
- A border is added to the canvas for better visibility.
-
JavaScript (game.js):
- Game Variables:
gravity,friction,gameFrame, andscoreto manage game physics and state.
- Player Object:
- Defines the player's properties like position, size, colour, speed, and velocity.
- Keys Object:
- Tracks the state (pressed or not) of the arrow keys.
- Platforms, Obstacles, and Collectibles Arrays:
- Define various platforms, obstacles, and collectibles with their positions, sizes, and colours.
- Drawing Functions:
drawRectangle: Draws a rectangle on the canvas.drawPlayer,drawPlatforms,drawObstacles, anddrawCollectibles: UtilisedrawRectangleto draw their respective objects.
- checkCollision Function:
- Checks if two rectangles collide.
- updatePlayer Function:
- Updates the player's position based on key presses and applies gravity.
- Checks for collisions with platforms, obstacles, and collectibles.
- animate Function:
- The main game loop that clears the canvas, draws all game objects, updates the player, and displays the score.
- Uses
requestAnimationFrameto create a smooth animation loop.
- Key Event Listeners:
- Listen for keydown and keyup events to update the
keysobject, controlling the player's movement.
- Listen for keydown and keyup events to update the
- Game Variables:
To play the game, open the
index.htmlfile in a web browser. Use the arrow keys to move the player around, avoid obstacles, and collect yellow squares to increase your score. If the player touches a black obstacle the game will end and reload. -
- User
Put it all in one HTML file
- Assistant
Certainly! Below is the complete game code consolidated into a single HTML file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Fun Platformer Game</title> <style> body { margin: 0; overflow: hidden; background-color: #87CEEB; display: flex; justify-content: center; align-items: center; height: 100vh; } canvas { border: 2px solid #000; } </style> </head> <body> <canvas id="gameCanvas" width="800" height="600"></canvas> <script> const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); // Game variables const gravity = 0.9; const friction = 0.85; let gameFrame = 0; let score = 0; // Player const player = { x: 50, y: canvas.height - 150, width: 50, height: 50, color: 'red', speed: 5, velY: 0, jumping: false, grounded: false }; // Keys const keys = { ArrowUp: { pressed: false }, ArrowDown: { pressed: false }, ArrowLeft: { pressed: false }, ArrowRight: { pressed: false } }; // Platforms const platforms = [ {x: 0, y: canvas.height - 50, width: canvas.width, height: 50, color: 'green'}, {x: 200, y: canvas.height - 200, width: 200, height: 50, color: 'brown'}, {x: 500, y: canvas.height - 350, width: 200, height: 50, color: 'purple'}, {x: 100, y: canvas.height - 500, width: 200, height: 50, color: 'orange'} ]; // Obstacles const obstacles = [ {x: 400, y: canvas.height - 250, width: 50, height: 50, color: 'black'}, {x: 600, y: canvas.height - 400, width: 50, height: 50, color: 'black'}, ]; // Collectibles const collectibles = [ {x: 300, y: canvas.height - 450, width: 20, height: 20, color: 'yellow'}, {x: 700, y: canvas.height - 550, width: 20, height: 20, color: 'yellow'}, ]; function drawRectangle(x, y, width, height, color) { ctx.fillStyle = color; ctx.fillRect(x, y, width, height); } function drawPlayer() { drawRectangle(player.x, player.y, player.width, player.height, player.color); } function drawPlatforms() { platforms.forEach(platform => { drawRectangle(platform.x, platform.y, platform.width, platform.height, platform.color); }); } function drawObstacles() { obstacles.forEach(obstacle => { drawRectangle(obstacle.x, obstacle.y, obstacle.width, obstacle.height, obstacle.color); }); } function drawCollectibles() { collectibles.forEach(collectible => { drawRectangle(collectible.x, collectible.y, collectible.width, collectible.height, collectible.color); }); } function checkCollision(rect1, rect2) { return rect1.x < rect2.x + rect2.width && rect1.x + rect1.width > rect2.x && rect1.y < rect2.y + rect2.height && rect1.y + rect1.height > rect2.y; } function updatePlayer() { // Apply gravity if (!player.grounded) { player.velY += gravity; } else { player.velY = 0; } // Move player if (keys.ArrowLeft.pressed && player.x - player.speed > 0) { player.x -= player.speed; } if (keys.ArrowRight.pressed && player.x + player.speed + player.width < canvas.width) { player.x += player.speed; } if (keys.ArrowUp.pressed && player.grounded) { player.velY = -15; player.jumping = true; player.grounded = false; } player.y += player.velY; // Check for platform collision player.grounded = false; platforms.forEach(platform => { if (checkCollision(player, platform)) { player.grounded = true; player.y = platform.y - player.height; } }); // Check for obstacle collision obstacles.forEach(obstacle => { if (checkCollision(player, obstacle)) { alert("Game Over! Your score: " + score); location.reload(); } }); // Check for collectible collision for (let i = 0; i < collectibles.length; i++) { if (checkCollision(player, collectibles[i])) { score += 10; collectibles.splice(i, 1); break; } } } function animate() { ctx.clearRect(0, 0, canvas.width, canvas.height); drawPlatforms(); drawObstacles(); drawCollectibles(); drawPlayer(); updatePlayer(); // Display score ctx.fillStyle = 'black'; ctx.font = '20px Arial'; ctx.fillText(`Score: ${score}`, 10, 30); gameFrame++; requestAnimationFrame(animate); } // Key event listeners document.addEventListener('keydown', ({key}) => { switch (key) { case 'ArrowUp': keys.ArrowUp.pressed = true; break; case 'ArrowDown': keys.ArrowDown.pressed = true; break; case 'ArrowLeft': keys.ArrowRight.pressed = false; keys.ArrowLeft.pressed = true; break; case 'ArrowRight': keys.ArrowLeft.pressed = false; keys.ArrowRight.pressed = true; break; } }); document.addEventListener('keyup', ({key}) => { switch (key) { case 'ArrowUp': keys.ArrowUp.pressed = false; break; case 'ArrowDown': keys.ArrowDown.pressed = false; break; case 'ArrowLeft': keys.ArrowLeft.pressed = false; break; case 'ArrowRight': keys.ArrowRight.pressed = false; break; } }); animate(); </script> </body> </html>How to Play
- Open the HTML file in a web browser.
- Use the arrow keys to move the player:
- Up Arrow to jump.
- Left Arrow to move left.
- Right Arrow to move right.
- Avoid the black obstacles.
- Collect the yellow squares to increase your score.
- If you touch an obstacle the game will end, and the page will reload.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.