make a jump over obbstacole game using html, css and script js
- User
make a jump over obbstacole game using html, css and script js
- Assistant
make a jump over obbstacole game using html, css and script js
Creating a simple "Jump Over Obstacles" game using HTML, CSS, and JavaScript is a fun project! Below, I'll guide you through building a basic version of this game. The game will feature a character that can jump over incoming obstacles. When the character successfully avoids an obstacle, the player scores a point.
For simplicity, we'll place all the HTML, CSS, and JavaScript in a single HTML file. You can copy the entire code below into a file named index.html and open it in your web browser to play the game.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Jump Over Obstacles Game</title>
<style>
/* Basic reset and styling */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: #f0f0f0;
overflow: hidden;
font-family: Arial, sans-serif;
}
/* Game container */
.game-container {
position: relative;
width: 100%;
height: 100vh;
background-color: #87CEEB; /* Sky blue background */
overflow: hidden;
}
/* Ground */
.ground {
position: absolute;
bottom: 0;
width: 100%;
height: 100px;
background-color: #654321; /* Brown ground */
}
/* Player */
.player {
position: absolute;
bottom: 100px; /* Same as ground height */
left: 50px;
width: 50px;
height: 50px;
background-color: #FF5733; /* Reddish color */
border-radius: 10px;
transition: bottom 0.3s;
}
/* Obstacle */
.obstacle {
position: absolute;
bottom: 100px; /* Same as ground height */
right: -50px;
width: 50px;
height: 50px;
background-color: #333;
border-radius: 10px;
animation: moveObstacle 2s linear infinite;
}
/* Obstacle Movement */
@keyframes moveObstacle {
from { right: -50px; }
to { right: 100%; }
}
/* Jump Animation */
.jump {
animation: jump 0.5s ease;
}
@keyframes jump {
0% { bottom: 100px; }
50% { bottom: 200px; }
100% { bottom: 100px; }
}
/* Score Display */
.score {
position: absolute;
top: 20px;
left: 20px;
font-size: 24px;
color: #333;
}
/* Game Over Overlay */
.game-over {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
color: #fff;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 36px;
display: none;
z-index: 2;
}
.game-over button {
margin-top: 20px;
padding: 10px 20px;
font-size: 24px;
cursor: pointer;
border: none;
border-radius: 5px;
background-color: #FF5733;
color: #fff;
}
</style>
</head>
<body>
<div class="game-container">
<div class="ground"></div>
<div class="player" id="player"></div>
<div class="score" id="score">Score: 0</div>
<div class="game-over" id="gameOver">
<div>Game Over</div>
<button onclick="restartGame()">Restart</button>
</div>
</div>
<script>
const player = document.getElementById('player');
const gameContainer = document.querySelector('.game-container');
const scoreDisplay = document.getElementById('score');
const gameOverDisplay = document.getElementById('gameOver');
let isJumping = false;
let score = 0;
let obstacleInterval;
let collisionCheckInterval;
let isGameOver = false;
// Handle Jump
function jump() {
if (isJumping) return;
isJumping = true;
player.classList.add('jump');
setTimeout(() => {
player.classList.remove('jump');
isJumping = false;
}, 500); // Duration of jump animation
}
// Handle Key Press
document.addEventListener('keydown', (e) => {
if (e.code === 'Space') {
jump();
}
});
// Create Obstacles
function createObstacle() {
const obstacle = document.createElement('div');
obstacle.classList.add('obstacle');
gameContainer.appendChild(obstacle);
// Remove obstacle after it crosses the screen
obstacle.addEventListener('animationiteration', () => {
obstacle.remove();
score += 1;
scoreDisplay.textContent = 'Score: ' + score;
});
}
// Start Obstacles at Intervals
function startObstacles() {
obstacleInterval = setInterval(createObstacle, 2000); // Every 2 seconds
}
// Collision Detection
function checkCollision() {
const obstacles = document.querySelectorAll('.obstacle');
obstacles.forEach(obstacle => {
const obstacleRect = obstacle.getBoundingClientRect();
const playerRect = player.getBoundingClientRect();
// Simple collision detection
if (
playerRect.right > obstacleRect.left &&
playerRect.left < obstacleRect.right &&
playerRect.bottom > obstacleRect.top &&
playerRect.top < obstacleRect.bottom
) {
endGame();
}
});
}
// End Game
function endGame() {
if (isGameOver) return;
isGameOver = true;
clearInterval(obstacleInterval);
clearInterval(collisionCheckInterval);
gameOverDisplay.style.display = 'flex';
}
// Restart Game
function restartGame() {
// Reset variables
isGameOver = false;
score = 0;
scoreDisplay.textContent = 'Score: ' + score;
gameOverDisplay.style.display = 'none';
// Remove existing obstacles
const obstacles = document.querySelectorAll('.obstacle');
obstacles.forEach(obstacle => obstacle.remove());
// Restart obstacle creation and collision detection
startObstacles();
collisionCheckInterval = setInterval(checkCollision, 100);
}
// Initialize Game
function init() {
startObstacles();
collisionCheckInterval = setInterval(checkCollision, 100);
}
init();
</script>
</body>
</html>
HTML Structure:
.game-container: The main container that holds all game elements..ground: Represents the ground at the bottom of the game area..player: The character controlled by the player..score: Displays the current score..game-over: An overlay that appears when the game ends, showing a "Game Over" message and a restart button.CSS Styling:
@keyframes moveObstacle: Moves the obstacle from right to left across the screen..jump: Adds a jump animation to the player, moving it upwards and then back down.JavaScript Logic:
score, isJumping, isGameOver), and interval IDs for obstacles and collision checking.jump class to the player to trigger the CSS jump animation. It prevents multiple jumps while already jumping.createObstacle: Dynamically creates obstacle elements that move across the screen using CSS animations.checkCollision: Iterates through all obstacles and checks if any collide with the player using their bounding rectangles.Run the Game:
index.html.Controls:
Objective:
Game Over:
Feel free to expand upon this basic game to make it more engaging:
This simple game demonstrates the fundamentals of game development using HTML, CSS, and JavaScript. By understanding the core concepts presented here, you can build more complex and feature-rich games. Happy coding!
Not yet included in a published release.
endGame: Stops obstacle creation and collision detection, and displays the "Game Over" overlay.restartGame: Resets the game state, clears existing obstacles, and restarts obstacle creation and collision detection.init: Starts the game by initiating obstacle creation and collision detection intervals.