o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Generate a HTML CSS JavaScript code for a five-level platformer game
- User
Generate a HTML CSS JavaScript code for a five-level platformer game
- Assistant
Certainly! Below is a simple platformer game built using HTML, CSS, and JavaScript. This game consists of five levels where the player must navigate through obstacles to reach the goal.
HTML (
index.html):<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Platformer Game</title> <link rel="stylesheet" href="style.css"> </head> <body> <canvas id="gameCanvas" width="800" height="600"></canvas> <script src="game.js"></script> </body> </html>
CSS (
style.css):body { margin: 0; padding: 0; overflow: hidden; background-color: #222; } canvas { display: block; margin: 0 auto; background-color: #333; border: 1px solid #fff; }
JavaScript (
game.js):const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const TILE_SIZE = 40; const GRAVITY = 1.5; const LEVELS = [ // Level 1 [ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' P ', ' XXX G ', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', ], // Level 2 [ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' XXX ', ' ', ' XXX ', ' ', ' XXX ', ' P ', 'XXXX G ', 'XXXXXXXXXXXXXXXXXX ', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', ], // Level 3 [ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' G ', ' XXX ', ' X ', ' X ', ' X ', ' X ', ' P X ', 'XXXXX XX ', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', ], // Level 4 [ ' ', ' ', ' ', ' ', ' ', ' ', 'P ', 'XXXXXXXXXXXXXXXXXXXXXXXX G', ' ', ' ', ' ', ' ', ' ', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', ' ', ' ', ' ', ' ', ' ', ' ', ], // Level 5 [ ' ', ' ', ' ', ' ', ' XXX XXX ', ' ', ' XXX XXX ', ' ', ' XXX XXX ', ' ', ' XXX XXX ', ' ', 'XXX XXX', 'P G', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', ], ]; let currentLevel = 0; let level = LEVELS[currentLevel]; class Player { constructor(x, y) { this.x = x; this.y = y; this.width = TILE_SIZE * 0.8; this.height = TILE_SIZE * 0.8; this.velocityX = 0; this.velocityY = 0; this.onGround = false; } draw() { ctx.fillStyle = 'yellow'; ctx.fillRect(this.x, this.y, this.width, this.height); } update() { this.velocityY += GRAVITY; this.x += this.velocityX; this.y += this.velocityY; // Simple boundary collision if (this.x < 0) this.x = 0; if (this.x + this.width > canvas.width) this.x = canvas.width - this.width; if (this.y + this.height > canvas.height) { this.y = canvas.height - this.height; this.velocityY = 0; this.onGround = true; } this.onGround = false; } } class Tile { constructor(x, y, type) { this.x = x * TILE_SIZE; this.y = y * TILE_SIZE; this.width = TILE_SIZE; this.height = TILE_SIZE; this.type = type; } draw() { if (this.type === 'X') { ctx.fillStyle = '#555'; ctx.fillRect(this.x, this.y, this.width, this.height); } else if (this.type === 'G') { ctx.fillStyle = 'green'; ctx.fillRect(this.x, this.y, this.width, this.height); } } } let player; let tiles = []; function loadLevel(level) { tiles = []; for (let y = 0; y < level.length; y++) { const row = level[y]; for (let x = 0; x < row.length; x++) { const tileType = row[x]; if (tileType === 'P') { player = new Player(x * TILE_SIZE, y * TILE_SIZE); } else if (tileType !== ' ') { tiles.push(new Tile(x, y, tileType)); } } } } let keys = {}; window.addEventListener('keydown', function(e) { keys[e.code] = true; }); window.addEventListener('keyup', function(e) { keys[e.code] = false; }); function update() { if (keys['ArrowLeft']) { player.velocityX = -5; } else if (keys['ArrowRight']) { player.velocityX = 5; } else { player.velocityX = 0; } if (keys['Space'] && player.onGround) { player.velocityY = -30; player.onGround = false; } player.update(); // Collision detection tiles.forEach(tile => { if (rectCollision(player, tile)) { if (tile.type === 'X') { resolveCollision(player, tile); } else if (tile.type === 'G') { currentLevel++; if (currentLevel >= LEVELS.length) { alert('You Win!'); currentLevel = 0; } level = LEVELS[currentLevel]; loadLevel(level); } } }); } function resolveCollision(player, tile) { // Calculate collision sides let dx = (player.x + player.width / 2) - (tile.x + tile.width / 2); let dy = (player.y + player.height / 2) - (tile.y + tile.height / 2); let width = (player.width + tile.width) / 2; let height = (player.height + tile.height) / 2; let crossWidth = width * dy; let crossHeight = height * dx; if (Math.abs(dx) <= width && Math.abs(dy) <= height) { if (crossWidth > crossHeight) { if (crossWidth > -crossHeight) { // Collision on the bottom side player.y = tile.y + tile.height; player.velocityY = 0; } else { // Collision on the left side player.x = tile.x - player.width; } } else { if (crossWidth > -crossHeight) { // Collision on the right side player.x = tile.x + tile.width; } else { // Collision on the top side player.y = tile.y - player.height; player.velocityY = 0; player.onGround = true; } } } } function rectCollision(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 draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); tiles.forEach(tile => tile.draw()); player.draw(); } function gameLoop() { update(); draw(); requestAnimationFrame(gameLoop); } loadLevel(level); gameLoop();
How to Run the Game:
-
Create the Files:
- index.html
- style.css
- game.js
-
Copy the Code:
- Paste the HTML, CSS, and JavaScript code into their respective files.
-
Open the Game:
- Open index.html in your web browser.
-
Controls:
- Left Arrow (〈—): Move Left
- Right Arrow (—〉): Move Right
- Spacebar: Jump
Game Explanation:
-
Player (
PlayerClass):-
Properties:
x,y: Position on the canvas.width,height: Size of the player.velocityX,velocityY: Movement speed.onGround: Whether the player is on the ground.
-
Methods:
draw(): Renders the player on the canvas.update(): Updates the player's position and handles gravity.
-
-
Tiles (
TileClass):-
Type of Tiles:
'X': Solid platform.'G': Goal (level exit).'P': Player spawn point.
-
Methods:
draw(): Renders the tile based on its type.
-
-
Levels (
LEVELSArray):- Each level is an array of strings representing a grid.
- Each character in the string represents a tile.
-
Game Loop:
update(): Handles player input, movement, and collision detection.draw(): Clears the canvas and redraws all game objects.gameLoop(): Continuously updates and draws the game every frame.
-
Collision Detection:
rectCollision(): Checks for overlapping rectangles (player and tiles).resolveCollision(): Adjusts player's position when a collision is detected.
-
Level Progression:
- When the player reaches the goal tile
'G', the next level loads. - After completing the last level, the game restarts.
- When the player reaches the goal tile
Customizing the Game:
Feel free to enhance the game by adding:
- Enemies: Introduce moving obstacles.
- Collectibles: Items that the player can collect for points.
- Sound Effects and Music: Add audio for jumps, collisions, and level completion.
- Improved Graphics: Use images or sprites for the player and tiles.
- Additional Mechanics: Power-ups, health system, or special abilities.
Tips:
- Testing: Regularly test each level after making changes to ensure everything works as expected.
- Organization: Keep your code organized and comment sections to make future updates easier.
- Learning Resources: If you're new to game development, consider exploring tutorials on HTML5 canvas games.
Enjoy building and playing your platformer game!
-