-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Gravity Switch Game using HTML5
- Loading branch information
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Gravity Switcher</title> | ||
<link rel="stylesheet" href="styles.css"> | ||
</head> | ||
<body> | ||
<canvas id="gameCanvas"></canvas> | ||
<div class="controls"> | ||
<button id="startButton">Start Game</button> | ||
<button id="gravityButton" disabled>Switch Gravity</button> | ||
<div id="gravityMeter">Gravity: Down</div> | ||
<div id="score">Score: 0</div> | ||
</div> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
const canvas = document.getElementById('gameCanvas'); | ||
const ctx = canvas.getContext('2d'); | ||
canvas.width = 400; | ||
canvas.height = 600; | ||
|
||
let player = { | ||
x: canvas.width / 2 - 15, | ||
y: canvas.height / 2 - 15, | ||
width: 30, | ||
height: 30, | ||
speed: 3, | ||
gravity: 0.5, | ||
velocityY: 0, | ||
isJumping: false, | ||
}; | ||
|
||
let items = []; | ||
let score = 0; | ||
let isGravityDown = true; | ||
let gameStarted = false; | ||
|
||
function createItems() { | ||
for (let i = 0; i < 5; i++) { | ||
items.push({ | ||
x: Math.random() * (canvas.width - 20), | ||
y: Math.random() * (canvas.height - 20), | ||
width: 20, | ||
height: 20, | ||
collected: false | ||
}); | ||
} | ||
} | ||
|
||
function update() { | ||
ctx.clearRect(0, 0, canvas.width, canvas.height); | ||
|
||
// Apply gravity | ||
if (isGravityDown) { | ||
player.velocityY += player.gravity; | ||
player.y += player.velocityY; | ||
} else { | ||
player.velocityY -= player.gravity; | ||
player.y -= player.velocityY; | ||
} | ||
|
||
// Check boundaries | ||
if (player.y + player.height > canvas.height) { | ||
player.y = canvas.height - player.height; | ||
player.velocityY = 0; | ||
player.isJumping = false; | ||
} else if (player.y < 0) { | ||
player.y = 0; | ||
player.velocityY = 0; | ||
} | ||
|
||
// Draw player | ||
ctx.fillStyle = '#61dafb'; | ||
ctx.fillRect(player.x, player.y, player.width, player.height); | ||
|
||
// Draw items | ||
items.forEach(item => { | ||
if (!item.collected) { | ||
ctx.fillStyle = 'gold'; | ||
ctx.fillRect(item.x, item.y, item.width, item.height); | ||
checkCollision(item); | ||
} | ||
}); | ||
|
||
requestAnimationFrame(update); | ||
} | ||
|
||
function checkCollision(item) { | ||
if (player.x < item.x + item.width && | ||
player.x + player.width > item.x && | ||
player.y < item.y + item.height && | ||
player.y + player.height > item.y) { | ||
item.collected = true; | ||
score += 10; | ||
document.getElementById('score').innerText = `Score: ${score}`; | ||
} | ||
} | ||
|
||
document.getElementById('gravityButton').addEventListener('click', () => { | ||
isGravityDown = !isGravityDown; | ||
document.getElementById('gravityMeter').innerText = `Gravity: ${isGravityDown ? 'Down' : 'Up'}`; | ||
}); | ||
|
||
document.getElementById('startButton').addEventListener('click', () => { | ||
if (!gameStarted) { | ||
gameStarted = true; | ||
document.getElementById('gravityButton').disabled = false; | ||
createItems(); | ||
update(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
body { | ||
margin: 0; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
height: 100vh; | ||
background-color: #282c34; | ||
color: white; | ||
font-family: 'Arial', sans-serif; | ||
} | ||
|
||
canvas { | ||
border: 1px solid white; | ||
background-color: #333; | ||
} | ||
|
||
.controls { | ||
margin-top: 10px; | ||
text-align: center; | ||
} | ||
|
||
button { | ||
padding: 10px 20px; | ||
font-size: 16px; | ||
cursor: pointer; | ||
background-color: #61dafb; | ||
border: none; | ||
border-radius: 5px; | ||
transition: background-color 0.3s; | ||
margin: 5px; | ||
} | ||
|
||
button:hover { | ||
background-color: #21a1f1; | ||
} | ||
|
||
#gravityMeter, #score { | ||
margin-top: 10px; | ||
font-size: 18px; | ||
} |