đ CODE2GAME HTML RUNNER IS LIVE! đŽ
đģ Apna HTML/CSS/JS code paste karo
âļī¸ RUN CODE dabao
đšī¸ Aur apna game instantly test karo!
đĨ Try it now:
đ
[https://anadverma956989936-lab.github.io/Code2Game-Runner/]
CODE âĸ RUN âĸ PLAY âĸ CREATE âĄ
đģ Apna HTML/CSS/JS code paste karo
âļī¸ RUN CODE dabao
đšī¸ Aur apna game instantly test karo!
đĨ Try it now:
đ
[https://anadverma956989936-lab.github.io/Code2Game-Runner/]
CODE âĸ RUN âĸ PLAY âĸ CREATE âĄ
đĨ1
đǍđâī¸ DAY 2 â ROCK PAPER SCISSORS! đĨ
Ready for today's challenge? đŽ
đ¤ Computer ke against khelo!
đ Dekho kaun zyada rounds jeetta hai!
đģ HTML + CSS + JavaScript
đą Mobile Friendly
⥠Easy to Run
đ FULL SOURCE CODE NEXT POST MEIN
đĨ Try it âĸ Play it âĸ Share it
CodeForge Gaming Community đ
#Day2 #RockPaperScissors #HTML #JavaScript #GameCoding
Ready for today's challenge? đŽ
đ¤ Computer ke against khelo!
đ Dekho kaun zyada rounds jeetta hai!
đģ HTML + CSS + JavaScript
đą Mobile Friendly
⥠Easy to Run
đ FULL SOURCE CODE NEXT POST MEIN
đĨ Try it âĸ Play it âĸ Share it
CodeForge Gaming Community đ
#Day2 #RockPaperScissors #HTML #JavaScript #GameCoding
đ1đĨ1
<!DOCTYPE html>
<html>
<head>
<title>Rock Paper Scissors</title>
<style>
body{
margin:0;
height:100vh;
display:flex;
justify-content:center;
align-items:center;
background:#090909;
color:white;
font-family:Arial;
}
.game{
text-align:center;
padding:30px;
width:350px;
border-radius:25px;
background:#151515;
box-shadow:0 0 30px #ff007755;
}
h1{
color:#ff2d8d;
}
button{
font-size:35px;
margin:8px;
padding:15px;
border:0;
border-radius:15px;
cursor:pointer;
background:#222;
}
button:hover{
transform:scale(1.1);
}
#result{
margin-top:25px;
font-size:20px;
}
.score{
font-size:18px;
}
</style>
</head>
<body>
<div class="game">
<h1>đǍ Rock Paper Scissors</h1>
<p>Choose your move</p>
<button onclick="play('Rock')">đǍ</button>
<button onclick="play('Paper')">đ</button>
<button onclick="play('Scissors')">âī¸</button>
<div id="result"></div>
<p class="score">
You: <span id="you">0</span>
AI: <span id="ai">0</span>
</p>
</div>
<script>
let you=0;
let ai=0;
function play(player){
const choices=["Rock","Paper","Scissors"];
const computer=
choices[Math.floor(Math.random()*3)];
let result="";
if(player===computer){
result="đ¤ Draw!";
}else if(
(player==="Rock" && computer==="Scissors") ||
(player==="Paper" && computer==="Rock") ||
(player==="Scissors" && computer==="Paper")
){
you++;
result="đ You Win!";
}else{
ai++;
result="đ¤ AI Wins!";
}
document.getElementById("result").innerHTML=
result+"<br>You: "+player+
"<br>AI: "+computer;
document.getElementById("you").textContent=you;
document.getElementById("ai").textContent=ai;
}
</script>
</body>
</html>
<html>
<head>
<title>Rock Paper Scissors</title>
<style>
body{
margin:0;
height:100vh;
display:flex;
justify-content:center;
align-items:center;
background:#090909;
color:white;
font-family:Arial;
}
.game{
text-align:center;
padding:30px;
width:350px;
border-radius:25px;
background:#151515;
box-shadow:0 0 30px #ff007755;
}
h1{
color:#ff2d8d;
}
button{
font-size:35px;
margin:8px;
padding:15px;
border:0;
border-radius:15px;
cursor:pointer;
background:#222;
}
button:hover{
transform:scale(1.1);
}
#result{
margin-top:25px;
font-size:20px;
}
.score{
font-size:18px;
}
</style>
</head>
<body>
<div class="game">
<h1>đǍ Rock Paper Scissors</h1>
<p>Choose your move</p>
<button onclick="play('Rock')">đǍ</button>
<button onclick="play('Paper')">đ</button>
<button onclick="play('Scissors')">âī¸</button>
<div id="result"></div>
<p class="score">
You: <span id="you">0</span>
AI: <span id="ai">0</span>
</p>
</div>
<script>
let you=0;
let ai=0;
function play(player){
const choices=["Rock","Paper","Scissors"];
const computer=
choices[Math.floor(Math.random()*3)];
let result="";
if(player===computer){
result="đ¤ Draw!";
}else if(
(player==="Rock" && computer==="Scissors") ||
(player==="Paper" && computer==="Rock") ||
(player==="Scissors" && computer==="Paper")
){
you++;
result="đ You Win!";
}else{
ai++;
result="đ¤ AI Wins!";
}
document.getElementById("result").innerHTML=
result+"<br>You: "+player+
"<br>AI: "+computer;
document.getElementById("you").textContent=you;
document.getElementById("ai").textContent=ai;
}
</script>
</body>
</html>
đĨ1đĨ°1
đŽđĨ DAY 2 â ROCK PAPER SCISSORS đǍđâī¸
Code paste kar diya? Ab game ko LIVE PLAY karo! đ
đ¤ Computer ko challenge karo
đ Apna score dekho
đĨ Try to beat the computer!
đģ PLAY / RUN THE GAME:
https://anadverma956989936-lab.github.io/Code2Game-Runner/
đą How to run:
1ī¸âŖ HTML â HTML code
2ī¸âŖ CSS â CSS code
3ī¸âŖ JS â JavaScript code
4ī¸âŖ Preview mein game play karo đŽ
â¤ī¸ Like | đ Share | đĸ Forward
CodeForge Gaming Community âĄ
#Day2 #RockPaperScissors #HTML #CSS #JavaScript #GameCoding
Code paste kar diya? Ab game ko LIVE PLAY karo! đ
đ¤ Computer ko challenge karo
đ Apna score dekho
đĨ Try to beat the computer!
đģ PLAY / RUN THE GAME:
https://anadverma956989936-lab.github.io/Code2Game-Runner/
đą How to run:
1ī¸âŖ HTML â HTML code
2ī¸âŖ CSS â CSS code
3ī¸âŖ JS â JavaScript code
4ī¸âŖ Preview mein game play karo đŽ
â¤ī¸ Like | đ Share | đĸ Forward
CodeForge Gaming Community âĄ
#Day2 #RockPaperScissors #HTML #CSS #JavaScript #GameCoding
đĨ°1đ1đ¯1
âĄđĨ DAY 3 â SPEED CLICK CHALLENGE! đĨâĄ
You have only âąī¸ 10 SECONDS!
đąī¸ Click as fast as you can
đ Beat your HIGH SCORE
đą Mobile Friendly
đģ HTML + CSS + JavaScript
đŽ PLAY THE GAME:
đ
[https://anadverma956989936-lab.github.io/Code2Game-Runner/]
đ CHALLENGE:
Can you beat 50 clicks? đ
đŦ Apna score batao!
â¤ī¸ Like | đ Share | đĸ Forward
⥠CodeForge Gaming Community
#Day3 #SpeedClick #HTML #CSS #JavaScript #GameCoding
You have only âąī¸ 10 SECONDS!
đąī¸ Click as fast as you can
đ Beat your HIGH SCORE
đą Mobile Friendly
đģ HTML + CSS + JavaScript
đŽ PLAY THE GAME:
đ
[https://anadverma956989936-lab.github.io/Code2Game-Runner/]
đ CHALLENGE:
Can you beat 50 clicks? đ
đŦ Apna score batao!
â¤ī¸ Like | đ Share | đĸ Forward
⥠CodeForge Gaming Community
#Day3 #SpeedClick #HTML #CSS #JavaScript #GameCoding
â¤1đĨ1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Speed Click Challenge</title>
<style>
*{
box-sizing:border-box;
}
body{
margin:0;
min-height:100vh;
display:flex;
justify-content:center;
align-items:center;
font-family:Arial,sans-serif;
background:#070b18;
color:white;
}
.game{
width:92%;
max-width:450px;
padding:30px 20px;
text-align:center;
border-radius:25px;
background:#111a30;
box-shadow:0 0 35px #00eaff55;
}
h1{
color:#00eaff;
margin-bottom:5px;
}
.subtitle{
color:#aaa;
margin-bottom:25px;
}
.stats{
display:flex;
justify-content:space-around;
margin-bottom:25px;
}
.stat{
padding:12px 18px;
border-radius:12px;
background:#192541;
}
.stat span{
display:block;
margin-top:5px;
color:#00eaff;
font-size:24px;
font-weight:bold;
}
#clickButton{
width:220px;
height:220px;
border:none;
border-radius:50%;
background:#00eaff;
color:#061018;
font-size:24px;
font-weight:bold;
cursor:pointer;
box-shadow:0 0 40px #00eaff66;
transition:0.1s;
}
#clickButton:active{
transform:scale(0.92);
}
#clickButton:disabled{
opacity:0.5;
cursor:not-allowed;
}
#startButton{
margin-top:25px;
padding:13px 28px;
border:none;
border-radius:25px;
background:#ff2d75;
color:white;
font-size:16px;
font-weight:bold;
cursor:pointer;
}
.message{
margin-top:20px;
min-height:24px;
color:#ddd;
}
</style>
</head>
<body>
<div class="game">
<h1>⥠SPEED CLICK</h1>
<p class="subtitle">
Click as fast as you can in 10 seconds!
</p>
<div class="stats">
<div class="stat">
CLICKS
<span id="score">0</span>
</div>
<div class="stat">
TIME
<span id="time">10</span>
</div>
<div class="stat">
BEST
<span id="best">0</span>
</div>
</div>
<button id="clickButton" disabled>
START FIRST
</button>
<br>
<button id="startButton">
đ START GAME
</button>
<div id="message" class="message">
Ready for the challenge?
</div>
</div>
<script>
const clickButton =
document.getElementById("clickButton");
const startButton =
document.getElementById("startButton");
const scoreDisplay =
document.getElementById("score");
const timeDisplay =
document.getElementById("time");
const bestDisplay =
document.getElementById("best");
const message =
document.getElementById("message");
let score = 0;
let time = 10;
let playing = false;
let timer = null;
let best =
Number(localStorage.getItem("speedBest")) || 0;
bestDisplay.textContent = best;
startButton.addEventListener("click", startGame);
clickButton.addEventListener("click", () => {
if(!playing) return;
score++;
scoreDisplay.textContent = score;
});
function startGame(){
clearInterval(timer);
score = 0;
time = 10;
playing = true;
scoreDisplay.textContent = score;
timeDisplay.textContent = time;
clickButton.disabled = false;
clickButton.textContent = "CLICK!";
startButton.disabled = true;
message.textContent =
"đĨ GO! Click as fast as possible!";
timer = setInterval(() => {
time--;
timeDisplay.textContent = time;
if(time <= 0){
endGame();
}
},1000);
}
function endGame(){
clearInterval(timer);
playing = false;
clickButton.disabled = true;
clickButton.textContent = "TIME UP!";
startButton.disabled = false;
if(score > best){
best = score;
localStorage.setItem(
"speedBest",
best
);
bestDisplay.textContent = best;
message.textContent =
"đ NEW HIGH SCORE! " + score + " CLICKS!";
}
else{
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Speed Click Challenge</title>
<style>
*{
box-sizing:border-box;
}
body{
margin:0;
min-height:100vh;
display:flex;
justify-content:center;
align-items:center;
font-family:Arial,sans-serif;
background:#070b18;
color:white;
}
.game{
width:92%;
max-width:450px;
padding:30px 20px;
text-align:center;
border-radius:25px;
background:#111a30;
box-shadow:0 0 35px #00eaff55;
}
h1{
color:#00eaff;
margin-bottom:5px;
}
.subtitle{
color:#aaa;
margin-bottom:25px;
}
.stats{
display:flex;
justify-content:space-around;
margin-bottom:25px;
}
.stat{
padding:12px 18px;
border-radius:12px;
background:#192541;
}
.stat span{
display:block;
margin-top:5px;
color:#00eaff;
font-size:24px;
font-weight:bold;
}
#clickButton{
width:220px;
height:220px;
border:none;
border-radius:50%;
background:#00eaff;
color:#061018;
font-size:24px;
font-weight:bold;
cursor:pointer;
box-shadow:0 0 40px #00eaff66;
transition:0.1s;
}
#clickButton:active{
transform:scale(0.92);
}
#clickButton:disabled{
opacity:0.5;
cursor:not-allowed;
}
#startButton{
margin-top:25px;
padding:13px 28px;
border:none;
border-radius:25px;
background:#ff2d75;
color:white;
font-size:16px;
font-weight:bold;
cursor:pointer;
}
.message{
margin-top:20px;
min-height:24px;
color:#ddd;
}
</style>
</head>
<body>
<div class="game">
<h1>⥠SPEED CLICK</h1>
<p class="subtitle">
Click as fast as you can in 10 seconds!
</p>
<div class="stats">
<div class="stat">
CLICKS
<span id="score">0</span>
</div>
<div class="stat">
TIME
<span id="time">10</span>
</div>
<div class="stat">
BEST
<span id="best">0</span>
</div>
</div>
<button id="clickButton" disabled>
START FIRST
</button>
<br>
<button id="startButton">
đ START GAME
</button>
<div id="message" class="message">
Ready for the challenge?
</div>
</div>
<script>
const clickButton =
document.getElementById("clickButton");
const startButton =
document.getElementById("startButton");
const scoreDisplay =
document.getElementById("score");
const timeDisplay =
document.getElementById("time");
const bestDisplay =
document.getElementById("best");
const message =
document.getElementById("message");
let score = 0;
let time = 10;
let playing = false;
let timer = null;
let best =
Number(localStorage.getItem("speedBest")) || 0;
bestDisplay.textContent = best;
startButton.addEventListener("click", startGame);
clickButton.addEventListener("click", () => {
if(!playing) return;
score++;
scoreDisplay.textContent = score;
});
function startGame(){
clearInterval(timer);
score = 0;
time = 10;
playing = true;
scoreDisplay.textContent = score;
timeDisplay.textContent = time;
clickButton.disabled = false;
clickButton.textContent = "CLICK!";
startButton.disabled = true;
message.textContent =
"đĨ GO! Click as fast as possible!";
timer = setInterval(() => {
time--;
timeDisplay.textContent = time;
if(time <= 0){
endGame();
}
},1000);
}
function endGame(){
clearInterval(timer);
playing = false;
clickButton.disabled = true;
clickButton.textContent = "TIME UP!";
startButton.disabled = false;
if(score > best){
best = score;
localStorage.setItem(
"speedBest",
best
);
bestDisplay.textContent = best;
message.textContent =
"đ NEW HIGH SCORE! " + score + " CLICKS!";
}
else{
â¤1đĨ1
message.textContent =
"đŽ Game Over! Your score: " +
score +
" clicks.";
}
}
</script>
</body>
</html>
"đŽ Game Over! Your score: " +
score +
" clicks.";
}
}
</script>
</body>
</html>
đ1đĨ1
https://www.youtube.com/@pookiejanta_party-s4m
Follow my YouTube channel @pookiejanta_partyđâ¤ī¸
Likeđ, Share đ and Subscribe đ
Follow my YouTube channel @pookiejanta_partyđâ¤ī¸
Likeđ, Share đ and Subscribe đ
â¤1đĨ1
đ§ đĨ DAY 4 â MEMORY MATCH GAME! đŽ
Can you match all 8 pairs? đ
đ§ Test Your Memory
đ Complete it in minimum moves
đą Mobile Friendly
⥠Fast & Fun
đŽ PLAY THE GAME:
đ
[https://anadverma956989936-lab.github.io/Code2Game-Runner/]
đ CHALLENGE:
Can you complete it in under 20 moves? đ
đŦ ⤠ā¤Ēā¤¨ā¤ž score ā¤Ŧā¤¤ā¤žā¤!
â¤ī¸ Like | đ Share | đĸ Forward
⥠CodeForge Gaming Community
#Day4 #MemoryGame #HTML #CSS #JavaScript #GameCoding
Can you match all 8 pairs? đ
đ§ Test Your Memory
đ Complete it in minimum moves
đą Mobile Friendly
⥠Fast & Fun
đŽ PLAY THE GAME:
đ
[https://anadverma956989936-lab.github.io/Code2Game-Runner/]
đ CHALLENGE:
Can you complete it in under 20 moves? đ
đŦ ⤠ā¤Ēā¤¨ā¤ž score ā¤Ŧā¤¤ā¤žā¤!
â¤ī¸ Like | đ Share | đĸ Forward
⥠CodeForge Gaming Community
#Day4 #MemoryGame #HTML #CSS #JavaScript #GameCoding
đ1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Memory Match Game</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
background: #070b18;
color: white;
}
.game {
width: 94%;
max-width: 430px;
padding: 25px;
text-align: center;
border-radius: 25px;
background: #111a30;
box-shadow: 0 0 35px #00eaff55;
}
h1 {
color: #00eaff;
margin-bottom: 5px;
}
.subtitle {
color: #aaa;
}
.stats {
display: flex;
justify-content: space-around;
margin: 20px 0;
padding: 12px;
border-radius: 12px;
background: #192541;
}
.board {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
.card {
height: 75px;
border: none;
border-radius: 14px;
background: #263655;
color: transparent;
font-size: 30px;
cursor: pointer;
transition: 0.2s;
}
.card:hover {
transform: scale(1.05);
}
.card.flipped,
.card.matched {
background: #00a9c7;
color: white;
}
.card.matched {
background: #18b878;
}
.restart {
margin-top: 20px;
padding: 12px 25px;
border: none;
border-radius: 25px;
background: #00eaff;
color: #061018;
font-weight: bold;
font-size: 16px;
cursor: pointer;
}
@media (max-width: 360px) {
.card {
height: 65px;
}
}
</style>
</head>
<body>
<div class="game">
<h1>đ§ MEMORY MATCH</h1>
<p class="subtitle">
Match all 8 pairs!
</p>
<div class="stats">
<span>
Moves: <b id="moves">0</b>
</span>
<span>
Pairs: <b id="pairs">0</b>/8
</span>
</div>
<div id="board" class="board"></div>
<button class="restart" onclick="restartGame()">
đ Restart Game
</button>
</div>
<script>
const symbols = [
"đ","đ",
"đ","đ",
"đŽ","đŽ",
"âĄ","âĄ",
"đĨ","đĨ",
"đ","đ",
"đą","đą",
"đž","đž"
];
let firstCard = null;
let secondCard = null;
let lockBoard = false;
let moves = 0;
let pairs = 0;
function shuffle(array) {
return array.sort(() => Math.random() - 0.5);
}
function createBoard() {
const board = document.getElementById("board");
board.innerHTML = "";
shuffle([...symbols]).forEach(symbol => {
const card = document.createElement("button");
card.className = "card";
card.dataset.symbol = symbol;
card.textContent = "?";
card.onclick = () => flipCard(card);
board.appendChild(card);
});
}
function flipCard(card) {
if (
lockBoard ||
card === firstCard ||
card.classList.contains("matched")
) {
return;
}
card.classList.add("flipped");
card.textContent = card.dataset.symbol;
if (!firstCard) {
firstCard = card;
return;
}
secondCard = card;
moves++;
document.getElementById("moves").textContent = moves;
checkMatch();
}
function checkMatch() {
const match =
firstCard.dataset.symbol ===
secondCard.dataset.symbol;
if (match) {
firstCard.classList.add("matched");
secondCard.classList.add("matched");
pairs++;
document.getElementById("pairs").textContent = pairs;
resetTurn();
if (pairs === 8) {
setTimeout(() => {
alert(
"đ YOU WON!\n\n" +
"Total Moves: " + moves
);
}, 300);
}
} else {
lockBoard = true;
setTimeout(() => {
firstCard.classList.remove("flipped");
secondCard.classList.remove("flipped");
firstCard.textContent = "?";
secondCard.textContent = "?";
resetTurn();
}, 800);
}
}
function resetTurn() {
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Memory Match Game</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
background: #070b18;
color: white;
}
.game {
width: 94%;
max-width: 430px;
padding: 25px;
text-align: center;
border-radius: 25px;
background: #111a30;
box-shadow: 0 0 35px #00eaff55;
}
h1 {
color: #00eaff;
margin-bottom: 5px;
}
.subtitle {
color: #aaa;
}
.stats {
display: flex;
justify-content: space-around;
margin: 20px 0;
padding: 12px;
border-radius: 12px;
background: #192541;
}
.board {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
.card {
height: 75px;
border: none;
border-radius: 14px;
background: #263655;
color: transparent;
font-size: 30px;
cursor: pointer;
transition: 0.2s;
}
.card:hover {
transform: scale(1.05);
}
.card.flipped,
.card.matched {
background: #00a9c7;
color: white;
}
.card.matched {
background: #18b878;
}
.restart {
margin-top: 20px;
padding: 12px 25px;
border: none;
border-radius: 25px;
background: #00eaff;
color: #061018;
font-weight: bold;
font-size: 16px;
cursor: pointer;
}
@media (max-width: 360px) {
.card {
height: 65px;
}
}
</style>
</head>
<body>
<div class="game">
<h1>đ§ MEMORY MATCH</h1>
<p class="subtitle">
Match all 8 pairs!
</p>
<div class="stats">
<span>
Moves: <b id="moves">0</b>
</span>
<span>
Pairs: <b id="pairs">0</b>/8
</span>
</div>
<div id="board" class="board"></div>
<button class="restart" onclick="restartGame()">
đ Restart Game
</button>
</div>
<script>
const symbols = [
"đ","đ",
"đ","đ",
"đŽ","đŽ",
"âĄ","âĄ",
"đĨ","đĨ",
"đ","đ",
"đą","đą",
"đž","đž"
];
let firstCard = null;
let secondCard = null;
let lockBoard = false;
let moves = 0;
let pairs = 0;
function shuffle(array) {
return array.sort(() => Math.random() - 0.5);
}
function createBoard() {
const board = document.getElementById("board");
board.innerHTML = "";
shuffle([...symbols]).forEach(symbol => {
const card = document.createElement("button");
card.className = "card";
card.dataset.symbol = symbol;
card.textContent = "?";
card.onclick = () => flipCard(card);
board.appendChild(card);
});
}
function flipCard(card) {
if (
lockBoard ||
card === firstCard ||
card.classList.contains("matched")
) {
return;
}
card.classList.add("flipped");
card.textContent = card.dataset.symbol;
if (!firstCard) {
firstCard = card;
return;
}
secondCard = card;
moves++;
document.getElementById("moves").textContent = moves;
checkMatch();
}
function checkMatch() {
const match =
firstCard.dataset.symbol ===
secondCard.dataset.symbol;
if (match) {
firstCard.classList.add("matched");
secondCard.classList.add("matched");
pairs++;
document.getElementById("pairs").textContent = pairs;
resetTurn();
if (pairs === 8) {
setTimeout(() => {
alert(
"đ YOU WON!\n\n" +
"Total Moves: " + moves
);
}, 300);
}
} else {
lockBoard = true;
setTimeout(() => {
firstCard.classList.remove("flipped");
secondCard.classList.remove("flipped");
firstCard.textContent = "?";
secondCard.textContent = "?";
resetTurn();
}, 800);
}
}
function resetTurn() {
â¤1đ1
firstCard = null;
secondCard = null;
lockBoard = false;
}
function restartGame() {
moves = 0;
pairs = 0;
document.getElementById("moves").textContent = "0";
document.getElementById("pairs").textContent = "0";
resetTurn();
createBoard();
}
createBoard();
</script>
</body>
</html>
secondCard = null;
lockBoard = false;
}
function restartGame() {
moves = 0;
pairs = 0;
document.getElementById("moves").textContent = "0";
document.getElementById("pairs").textContent = "0";
resetTurn();
createBoard();
}
createBoard();
</script>
</body>
</html>
đđĨ DAY 5 â SNAKE GAME! đŽ
The classic game is back! đ
đ Eat the đ
đ Increase your score
đĨ Don't hit the wall or yourself!
đą Mobile Controls Available
â¨ī¸ Keyboard Controls Available
đģ HTML + CSS + JavaScript
đŽ PLAY THE GAME:
đ
[https://anadverma956989936-lab.github.io/Code2Game-Runner/]
đ CHALLENGE:
How high can you score? đ
đŦ Apna highest score batao!
â¤ī¸ Like | đ Share | đĸ Forward
⥠CodeForge Gaming Community
#Day5 #SnakeGame #HTML #CSS #JavaScript #GameCoding
The classic game is back! đ
đ Eat the đ
đ Increase your score
đĨ Don't hit the wall or yourself!
đą Mobile Controls Available
â¨ī¸ Keyboard Controls Available
đģ HTML + CSS + JavaScript
đŽ PLAY THE GAME:
đ
[https://anadverma956989936-lab.github.io/Code2Game-Runner/]
đ CHALLENGE:
How high can you score? đ
đŦ Apna highest score batao!
â¤ī¸ Like | đ Share | đĸ Forward
⥠CodeForge Gaming Community
#Day5 #SnakeGame #HTML #CSS #JavaScript #GameCoding
â¤1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake Game</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
background: #070b18;
color: white;
}
.game {
width: 94%;
max-width: 430px;
padding: 22px;
text-align: center;
border-radius: 25px;
background: #111a30;
box-shadow: 0 0 35px #00eaff55;
}
h1 {
color: #00eaff;
margin: 0 0 15px;
}
.stats {
display: flex;
justify-content: space-around;
margin-bottom: 15px;
padding: 12px;
border-radius: 12px;
background: #192541;
}
.stats b {
color: #00eaff;
}
canvas {
display: block;
width: 100%;
max-width: 360px;
height: auto;
margin: auto;
border-radius: 15px;
background: #050912;
border: 2px solid #00eaff;
touch-action: none;
}
.message {
min-height: 25px;
margin: 15px 0 8px;
color: #ddd;
}
button {
border: none;
border-radius: 20px;
padding: 11px 20px;
margin: 5px;
background: #00eaff;
color: #061018;
font-weight: bold;
cursor: pointer;
}
.controls {
display: grid;
grid-template-columns: repeat(3, 55px);
justify-content: center;
gap: 7px;
margin-top: 12px;
}
.controls button {
width: 55px;
height: 45px;
padding: 0;
font-size: 20px;
}
.empty {
visibility: hidden;
}
</style>
</head>
<body>
<div class="game">
<h1>đ SNAKE GAME</h1>
<div class="stats">
<span>Score: <b id="score">0</b></span>
<span>Best: <b id="best">0</b></span>
</div>
<canvas id="gameCanvas" width="360" height="360"></canvas>
<div id="message" class="message">
Press Start to play!
</div>
<button id="startBtn">đ Start Game</button>
<div class="controls">
<button class="empty">âĸ</button>
<button onclick="changeDirection('up')">âŦī¸</button>
<button class="empty">âĸ</button>
<button onclick="changeDirection('left')">âŦ ī¸</button>
<button onclick="changeDirection('down')">âŦī¸</button>
<button onclick="changeDirection('right')">âĄī¸</button>
</div>
</div>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const scoreDisplay = document.getElementById("score");
const bestDisplay = document.getElementById("best");
const message = document.getElementById("message");
const startBtn = document.getElementById("startBtn");
const grid = 18;
const cell = canvas.width / grid;
let snake;
let food;
let direction;
let nextDirection;
let score = 0;
let gameRunning = false;
let gameTimer;
let best = Number(localStorage.getItem("snakeBest")) || 0;
bestDisplay.textContent = best;
function startGame() {
clearInterval(gameTimer);
snake = [
{ x: 9, y: 9 },
{ x: 8, y: 9 },
{ x: 7, y: 9 }
];
direction = "right";
nextDirection = "right";
score = 0;
scoreDisplay.textContent = score;
food = createFood();
gameRunning = true;
message.textContent = "đĨ Eat the food and grow!";
startBtn.textContent = "đ Restart";
draw();
gameTimer = setInterval(update, 120);
}
function createFood() {
let newFood;
do {
newFood = {
x: Math.floor(Math.random() * grid),
y: Math.floor(Math.random() * grid)
};
} while (
snake &&
snake.some(part =>
part.x === newFood.x &&
part.y === newFood.y
)
);
return newFood;
}
function update() {
if (!gameRunning) return;
direction = nextDirection;
const head = {
x: snake[0].x,
y: snake[0].y
};
if (direction === "up") head.y--;
if (direction === "down") head.y++;
if (direction === "left") head.x--;
if (direction === "right") head.x++;
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake Game</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
background: #070b18;
color: white;
}
.game {
width: 94%;
max-width: 430px;
padding: 22px;
text-align: center;
border-radius: 25px;
background: #111a30;
box-shadow: 0 0 35px #00eaff55;
}
h1 {
color: #00eaff;
margin: 0 0 15px;
}
.stats {
display: flex;
justify-content: space-around;
margin-bottom: 15px;
padding: 12px;
border-radius: 12px;
background: #192541;
}
.stats b {
color: #00eaff;
}
canvas {
display: block;
width: 100%;
max-width: 360px;
height: auto;
margin: auto;
border-radius: 15px;
background: #050912;
border: 2px solid #00eaff;
touch-action: none;
}
.message {
min-height: 25px;
margin: 15px 0 8px;
color: #ddd;
}
button {
border: none;
border-radius: 20px;
padding: 11px 20px;
margin: 5px;
background: #00eaff;
color: #061018;
font-weight: bold;
cursor: pointer;
}
.controls {
display: grid;
grid-template-columns: repeat(3, 55px);
justify-content: center;
gap: 7px;
margin-top: 12px;
}
.controls button {
width: 55px;
height: 45px;
padding: 0;
font-size: 20px;
}
.empty {
visibility: hidden;
}
</style>
</head>
<body>
<div class="game">
<h1>đ SNAKE GAME</h1>
<div class="stats">
<span>Score: <b id="score">0</b></span>
<span>Best: <b id="best">0</b></span>
</div>
<canvas id="gameCanvas" width="360" height="360"></canvas>
<div id="message" class="message">
Press Start to play!
</div>
<button id="startBtn">đ Start Game</button>
<div class="controls">
<button class="empty">âĸ</button>
<button onclick="changeDirection('up')">âŦī¸</button>
<button class="empty">âĸ</button>
<button onclick="changeDirection('left')">âŦ ī¸</button>
<button onclick="changeDirection('down')">âŦī¸</button>
<button onclick="changeDirection('right')">âĄī¸</button>
</div>
</div>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const scoreDisplay = document.getElementById("score");
const bestDisplay = document.getElementById("best");
const message = document.getElementById("message");
const startBtn = document.getElementById("startBtn");
const grid = 18;
const cell = canvas.width / grid;
let snake;
let food;
let direction;
let nextDirection;
let score = 0;
let gameRunning = false;
let gameTimer;
let best = Number(localStorage.getItem("snakeBest")) || 0;
bestDisplay.textContent = best;
function startGame() {
clearInterval(gameTimer);
snake = [
{ x: 9, y: 9 },
{ x: 8, y: 9 },
{ x: 7, y: 9 }
];
direction = "right";
nextDirection = "right";
score = 0;
scoreDisplay.textContent = score;
food = createFood();
gameRunning = true;
message.textContent = "đĨ Eat the food and grow!";
startBtn.textContent = "đ Restart";
draw();
gameTimer = setInterval(update, 120);
}
function createFood() {
let newFood;
do {
newFood = {
x: Math.floor(Math.random() * grid),
y: Math.floor(Math.random() * grid)
};
} while (
snake &&
snake.some(part =>
part.x === newFood.x &&
part.y === newFood.y
)
);
return newFood;
}
function update() {
if (!gameRunning) return;
direction = nextDirection;
const head = {
x: snake[0].x,
y: snake[0].y
};
if (direction === "up") head.y--;
if (direction === "down") head.y++;
if (direction === "left") head.x--;
if (direction === "right") head.x++;
â¤1
// Wall collision
if (
head.x < 0 ||
head.x >= grid ||
head.y < 0 ||
head.y >= grid
) {
gameOver();
return;
}
// Self collision
if (
snake.some(part =>
part.x === head.x &&
part.y === head.y
)
) {
gameOver();
return;
}
snake.unshift(head);
// Food
if (
head.x === food.x &&
head.y === food.y
) {
score++;
scoreDisplay.textContent = score;
if (score > best) {
best = score;
bestDisplay.textContent = best;
localStorage.setItem("snakeBest", best);
}
food = createFood();
} else {
snake.pop();
}
draw();
}
function draw() {
ctx.fillStyle = "#050912";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Grid
ctx.strokeStyle = "#14213d";
ctx.lineWidth = 1;
for (let i = 0; i <= grid; i++) {
ctx.beginPath();
ctx.moveTo(i * cell, 0);
ctx.lineTo(i * cell, canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * cell);
ctx.lineTo(canvas.width, i * cell);
ctx.stroke();
}
// Food
ctx.font =
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(
"đ",
food.x * cell + cell / 2,
food.y * cell + cell / 2
);
// Snake
snake.forEach((part, index) => {
ctx.fillStyle =
index === 0 ? "#00eaff" : "#18b878";
ctx.beginPath();
ctx.roundRect(
part.x * cell + 2,
part.y * cell + 2,
cell - 4,
cell - 4,
5
);
ctx.fill();
});
}
function gameOver() {
clearInterval(gameTimer);
gameRunning = false;
message.textContent =
"đĨ Game Over! Score: " + score;
startBtn.textContent = "đ Play Again";
draw();
}
function changeDirection(newDirection) {
if (!gameRunning) return;
if (
newDirection === "up" &&
direction !== "down"
) {
nextDirection = "up";
}
if (
newDirection === "down" &&
direction !== "up"
) {
nextDirection = "down";
}
if (
newDirection === "left" &&
direction !== "right"
) {
nextDirection = "left";
}
if (
newDirection === "right" &&
direction !== "left"
) {
nextDirection = "right";
}
}
// Keyboard controls
document.addEventListener("keydown", event => {
if (event.key === "ArrowUp")
changeDirection("up");
if (event.key === "ArrowDown")
changeDirection("down");
if (event.key === "ArrowLeft")
changeDirection("left");
if (event.key === "ArrowRight")
changeDirection("right");
});
startBtn.addEventListener("click", startGame);
// Initial screen
snake = [
{ x: 9, y: 9 },
{ x: 8, y: 9 },
{ x: 7, y: 9 }
];
food = createFood();
draw();
</script>
</body>
</html>
if (
head.x < 0 ||
head.x >= grid ||
head.y < 0 ||
head.y >= grid
) {
gameOver();
return;
}
// Self collision
if (
snake.some(part =>
part.x === head.x &&
part.y === head.y
)
) {
gameOver();
return;
}
snake.unshift(head);
// Food
if (
head.x === food.x &&
head.y === food.y
) {
score++;
scoreDisplay.textContent = score;
if (score > best) {
best = score;
bestDisplay.textContent = best;
localStorage.setItem("snakeBest", best);
}
food = createFood();
} else {
snake.pop();
}
draw();
}
function draw() {
ctx.fillStyle = "#050912";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Grid
ctx.strokeStyle = "#14213d";
ctx.lineWidth = 1;
for (let i = 0; i <= grid; i++) {
ctx.beginPath();
ctx.moveTo(i * cell, 0);
ctx.lineTo(i * cell, canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * cell);
ctx.lineTo(canvas.width, i * cell);
ctx.stroke();
}
// Food
ctx.font =
${cell * 0.8}px Arial;ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(
"đ",
food.x * cell + cell / 2,
food.y * cell + cell / 2
);
// Snake
snake.forEach((part, index) => {
ctx.fillStyle =
index === 0 ? "#00eaff" : "#18b878";
ctx.beginPath();
ctx.roundRect(
part.x * cell + 2,
part.y * cell + 2,
cell - 4,
cell - 4,
5
);
ctx.fill();
});
}
function gameOver() {
clearInterval(gameTimer);
gameRunning = false;
message.textContent =
"đĨ Game Over! Score: " + score;
startBtn.textContent = "đ Play Again";
draw();
}
function changeDirection(newDirection) {
if (!gameRunning) return;
if (
newDirection === "up" &&
direction !== "down"
) {
nextDirection = "up";
}
if (
newDirection === "down" &&
direction !== "up"
) {
nextDirection = "down";
}
if (
newDirection === "left" &&
direction !== "right"
) {
nextDirection = "left";
}
if (
newDirection === "right" &&
direction !== "left"
) {
nextDirection = "right";
}
}
// Keyboard controls
document.addEventListener("keydown", event => {
if (event.key === "ArrowUp")
changeDirection("up");
if (event.key === "ArrowDown")
changeDirection("down");
if (event.key === "ArrowLeft")
changeDirection("left");
if (event.key === "ArrowRight")
changeDirection("right");
});
startBtn.addEventListener("click", startGame);
// Initial screen
snake = [
{ x: 9, y: 9 },
{ x: 8, y: 9 },
{ x: 7, y: 9 }
];
food = createFood();
draw();
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Space Shooter</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
background: #030712;
color: white;
}
.game {
width: 94%;
max-width: 430px;
padding: 20px;
text-align: center;
border-radius: 25px;
background: #0b1224;
box-shadow: 0 0 35px #00eaff55;
}
h1 {
margin: 0 0 12px;
color: #00eaff;
}
.stats {
display: flex;
justify-content: space-around;
margin-bottom: 12px;
padding: 10px;
border-radius: 12px;
background: #151f38;
}
.stats b {
color: #00eaff;
}
canvas {
width: 100%;
max-width: 360px;
height: auto;
display: block;
margin: auto;
background: #02050d;
border: 2px solid #00eaff;
border-radius: 15px;
touch-action: none;
}
.message {
min-height: 24px;
margin: 12px 0;
color: #ddd;
}
button {
border: 0;
border-radius: 20px;
padding: 11px 20px;
margin: 4px;
background: #00eaff;
color: #061018;
font-weight: bold;
cursor: pointer;
}
.controls {
display: flex;
justify-content: center;
gap: 12px;
margin-top: 8px;
}
.controls button {
width: 65px;
height: 45px;
padding: 0;
font-size: 20px;
}
.small {
font-size: 12px;
color: #888;
}
</style>
</head>
<body>
<div class="game">
<h1>đ SPACE SHOOTER</h1>
<div class="stats">
<span>Score: <b id="score">0</b></span>
<span>Lives: <b id="lives">3</b></span>
<span>Best: <b id="best">0</b></span>
</div>
<canvas id="gameCanvas" width="360" height="520"></canvas>
<div id="message" class="message">
Defeat the enemy ships!
</div>
<button id="startBtn">đ Start Game</button>
<div class="controls">
<button onclick="moveLeft()">âŦ ī¸</button>
<button onclick="shoot()">đĨ</button>
<button onclick="moveRight()">âĄī¸</button>
</div>
<p class="small">
Keyboard: â â to move âĸ SPACE to shoot
</p>
</div>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const scoreEl = document.getElementById("score");
const livesEl = document.getElementById("lives");
const bestEl = document.getElementById("best");
const messageEl = document.getElementById("message");
const startBtn = document.getElementById("startBtn");
let player;
let bullets = [];
let enemies = [];
let stars = [];
let score = 0;
let lives = 3;
let best = Number(localStorage.getItem("spaceBest")) || 0;
let running = false;
let animationId;
let enemyTimer = 0;
let shootCooldown = 0;
bestEl.textContent = best;
function createStars() {
stars = [];
for (let i = 0; i < 60; i++) {
stars.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
speed: 0.5 + Math.random() * 1.5,
size: 1 + Math.random() * 2
});
}
}
function startGame() {
cancelAnimationFrame(animationId);
score = 0;
lives = 3;
scoreEl.textContent = score;
livesEl.textContent = lives;
player = {
x: canvas.width / 2 - 18,
y: canvas.height - 60,
width: 36,
height: 28,
speed: 6
};
bullets = [];
enemies = [];
enemyTimer = 0;
shootCooldown = 0;
createStars();
running = true;
messageEl.textContent =
"đĨ Destroy the enemy ships!";
startBtn.textContent = "đ Restart";
gameLoop();
}
function moveLeft() {
if (!running) return;
player.x -= player.speed;
if (player.x < 0) {
player.x = 0;
}
}
function moveRight() {
if (!running) return;
player.x += player.speed;
if (player.x + player.width > canvas.width) {
player.x = canvas.width - player.width;
}
}
function shoot() {
if (!running || shootCooldown > 0) return;
bullets.push({
x: player.x + player.width / 2 - 2,
y: player.y,
width: 4,
height: 14,
speed: 8
});
shootCooldown = 10;
}
function createEnemy() {
const size = 30;
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Space Shooter</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
background: #030712;
color: white;
}
.game {
width: 94%;
max-width: 430px;
padding: 20px;
text-align: center;
border-radius: 25px;
background: #0b1224;
box-shadow: 0 0 35px #00eaff55;
}
h1 {
margin: 0 0 12px;
color: #00eaff;
}
.stats {
display: flex;
justify-content: space-around;
margin-bottom: 12px;
padding: 10px;
border-radius: 12px;
background: #151f38;
}
.stats b {
color: #00eaff;
}
canvas {
width: 100%;
max-width: 360px;
height: auto;
display: block;
margin: auto;
background: #02050d;
border: 2px solid #00eaff;
border-radius: 15px;
touch-action: none;
}
.message {
min-height: 24px;
margin: 12px 0;
color: #ddd;
}
button {
border: 0;
border-radius: 20px;
padding: 11px 20px;
margin: 4px;
background: #00eaff;
color: #061018;
font-weight: bold;
cursor: pointer;
}
.controls {
display: flex;
justify-content: center;
gap: 12px;
margin-top: 8px;
}
.controls button {
width: 65px;
height: 45px;
padding: 0;
font-size: 20px;
}
.small {
font-size: 12px;
color: #888;
}
</style>
</head>
<body>
<div class="game">
<h1>đ SPACE SHOOTER</h1>
<div class="stats">
<span>Score: <b id="score">0</b></span>
<span>Lives: <b id="lives">3</b></span>
<span>Best: <b id="best">0</b></span>
</div>
<canvas id="gameCanvas" width="360" height="520"></canvas>
<div id="message" class="message">
Defeat the enemy ships!
</div>
<button id="startBtn">đ Start Game</button>
<div class="controls">
<button onclick="moveLeft()">âŦ ī¸</button>
<button onclick="shoot()">đĨ</button>
<button onclick="moveRight()">âĄī¸</button>
</div>
<p class="small">
Keyboard: â â to move âĸ SPACE to shoot
</p>
</div>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const scoreEl = document.getElementById("score");
const livesEl = document.getElementById("lives");
const bestEl = document.getElementById("best");
const messageEl = document.getElementById("message");
const startBtn = document.getElementById("startBtn");
let player;
let bullets = [];
let enemies = [];
let stars = [];
let score = 0;
let lives = 3;
let best = Number(localStorage.getItem("spaceBest")) || 0;
let running = false;
let animationId;
let enemyTimer = 0;
let shootCooldown = 0;
bestEl.textContent = best;
function createStars() {
stars = [];
for (let i = 0; i < 60; i++) {
stars.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
speed: 0.5 + Math.random() * 1.5,
size: 1 + Math.random() * 2
});
}
}
function startGame() {
cancelAnimationFrame(animationId);
score = 0;
lives = 3;
scoreEl.textContent = score;
livesEl.textContent = lives;
player = {
x: canvas.width / 2 - 18,
y: canvas.height - 60,
width: 36,
height: 28,
speed: 6
};
bullets = [];
enemies = [];
enemyTimer = 0;
shootCooldown = 0;
createStars();
running = true;
messageEl.textContent =
"đĨ Destroy the enemy ships!";
startBtn.textContent = "đ Restart";
gameLoop();
}
function moveLeft() {
if (!running) return;
player.x -= player.speed;
if (player.x < 0) {
player.x = 0;
}
}
function moveRight() {
if (!running) return;
player.x += player.speed;
if (player.x + player.width > canvas.width) {
player.x = canvas.width - player.width;
}
}
function shoot() {
if (!running || shootCooldown > 0) return;
bullets.push({
x: player.x + player.width / 2 - 2,
y: player.y,
width: 4,
height: 14,
speed: 8
});
shootCooldown = 10;
}
function createEnemy() {
const size = 30;
enemies.push({
x: Math.random() * (canvas.width - size),
y: -size,
width: size,
height: size,
speed: 1.5 + Math.random() * 1.5
});
}
function collision(a, b) {
return (
a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y
);
}
function update() {
// Stars
stars.forEach(star => {
star.y += star.speed;
if (star.y > canvas.height) {
star.y = 0;
star.x = Math.random() * canvas.width;
}
});
// Cooldown
if (shootCooldown > 0) {
shootCooldown--;
}
// Bullets
bullets.forEach(bullet => {
bullet.y -= bullet.speed;
});
bullets = bullets.filter(
bullet => bullet.y + bullet.height > 0
);
// Enemies
enemyTimer++;
if (enemyTimer > 45) {
createEnemy();
enemyTimer = 0;
}
enemies.forEach(enemy => {
enemy.y += enemy.speed;
});
// Bullet vs enemy
for (let i = enemies.length - 1; i >= 0; i--) {
let destroyed = false;
for (let j = bullets.length - 1; j >= 0; j--) {
if (collision(enemies[i], bullets[j])) {
enemies.splice(i, 1);
bullets.splice(j, 1);
score += 10;
scoreEl.textContent = score;
if (score > best) {
best = score;
bestEl.textContent = best;
localStorage.setItem(
"spaceBest",
best
);
}
destroyed = true;
break;
}
}
if (destroyed) continue;
}
// Enemy reaches bottom
for (let i = enemies.length - 1; i >= 0; i--) {
if (enemies[i].y > canvas.height) {
enemies.splice(i, 1);
lives--;
livesEl.textContent = lives;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Enemy hits player
for (let i = enemies.length - 1; i >= 0; i--) {
if (collision(enemies[i], player)) {
enemies.splice(i, 1);
lives--;
livesEl.textContent = lives;
if (lives <= 0) {
gameOver();
return;
}
}
}
}
function draw() {
ctx.fillStyle = "#02050d";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Stars
stars.forEach(star => {
ctx.fillStyle = "white";
ctx.fillRect(
star.x,
star.y,
star.size,
star.size
);
});
// Player
ctx.fillStyle = "#00eaff";
ctx.beginPath();
ctx.moveTo(
player.x + player.width / 2,
player.y
);
ctx.lineTo(
player.x,
player.y + player.height
);
ctx.lineTo(
player.x + player.width,
player.y + player.height
);
ctx.closePath();
ctx.fill();
// Bullets
bullets.forEach(bullet => {
ctx.fillStyle = "#ffe600";
ctx.fillRect(
bullet.x,
bullet.y,
bullet.width,
bullet.height
);
});
// Enemies
enemies.forEach(enemy => {
ctx.fillStyle = "#ff315c";
ctx.beginPath();
ctx.moveTo(
enemy.x + enemy.width / 2,
enemy.y + enemy.height
);
ctx.lineTo(
enemy.x,
enemy.y
);
ctx.lineTo(
enemy.x + enemy.width,
enemy.y
);
ctx.closePath();
ctx.fill();
ctx.fillStyle = "#ffffff";
ctx.fillRect(
enemy.x + 8,
enemy.y + 9,
5,
5
);
ctx.fillRect(
enemy.x + 17,
enemy.y + 9,
5,
5
);
});
}
function gameLoop() {
if (!running) return;
update();
draw();
animationId =
requestAnimationFrame(gameLoop);
}
function gameOver() {
running = false;
cancelAnimationFrame(animationId);
messageEl.textContent =
"đĨ GAME OVER! Final Score: " + score;
startBtn.textContent = "đ Play Again";
draw();
}
startBtn.addEventListener(
"click",
startGame
);
// Keyboard controls
document.addEventListener(
"keydown",
event => {
if (event.key === "ArrowLeft") {
moveLeft();
}
if (event.key === "ArrowRight") {
moveRight();
}
if (event.code === "Space") {
event.preventDefault();
shoot();
}
}
);
x: Math.random() * (canvas.width - size),
y: -size,
width: size,
height: size,
speed: 1.5 + Math.random() * 1.5
});
}
function collision(a, b) {
return (
a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y
);
}
function update() {
// Stars
stars.forEach(star => {
star.y += star.speed;
if (star.y > canvas.height) {
star.y = 0;
star.x = Math.random() * canvas.width;
}
});
// Cooldown
if (shootCooldown > 0) {
shootCooldown--;
}
// Bullets
bullets.forEach(bullet => {
bullet.y -= bullet.speed;
});
bullets = bullets.filter(
bullet => bullet.y + bullet.height > 0
);
// Enemies
enemyTimer++;
if (enemyTimer > 45) {
createEnemy();
enemyTimer = 0;
}
enemies.forEach(enemy => {
enemy.y += enemy.speed;
});
// Bullet vs enemy
for (let i = enemies.length - 1; i >= 0; i--) {
let destroyed = false;
for (let j = bullets.length - 1; j >= 0; j--) {
if (collision(enemies[i], bullets[j])) {
enemies.splice(i, 1);
bullets.splice(j, 1);
score += 10;
scoreEl.textContent = score;
if (score > best) {
best = score;
bestEl.textContent = best;
localStorage.setItem(
"spaceBest",
best
);
}
destroyed = true;
break;
}
}
if (destroyed) continue;
}
// Enemy reaches bottom
for (let i = enemies.length - 1; i >= 0; i--) {
if (enemies[i].y > canvas.height) {
enemies.splice(i, 1);
lives--;
livesEl.textContent = lives;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Enemy hits player
for (let i = enemies.length - 1; i >= 0; i--) {
if (collision(enemies[i], player)) {
enemies.splice(i, 1);
lives--;
livesEl.textContent = lives;
if (lives <= 0) {
gameOver();
return;
}
}
}
}
function draw() {
ctx.fillStyle = "#02050d";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Stars
stars.forEach(star => {
ctx.fillStyle = "white";
ctx.fillRect(
star.x,
star.y,
star.size,
star.size
);
});
// Player
ctx.fillStyle = "#00eaff";
ctx.beginPath();
ctx.moveTo(
player.x + player.width / 2,
player.y
);
ctx.lineTo(
player.x,
player.y + player.height
);
ctx.lineTo(
player.x + player.width,
player.y + player.height
);
ctx.closePath();
ctx.fill();
// Bullets
bullets.forEach(bullet => {
ctx.fillStyle = "#ffe600";
ctx.fillRect(
bullet.x,
bullet.y,
bullet.width,
bullet.height
);
});
// Enemies
enemies.forEach(enemy => {
ctx.fillStyle = "#ff315c";
ctx.beginPath();
ctx.moveTo(
enemy.x + enemy.width / 2,
enemy.y + enemy.height
);
ctx.lineTo(
enemy.x,
enemy.y
);
ctx.lineTo(
enemy.x + enemy.width,
enemy.y
);
ctx.closePath();
ctx.fill();
ctx.fillStyle = "#ffffff";
ctx.fillRect(
enemy.x + 8,
enemy.y + 9,
5,
5
);
ctx.fillRect(
enemy.x + 17,
enemy.y + 9,
5,
5
);
});
}
function gameLoop() {
if (!running) return;
update();
draw();
animationId =
requestAnimationFrame(gameLoop);
}
function gameOver() {
running = false;
cancelAnimationFrame(animationId);
messageEl.textContent =
"đĨ GAME OVER! Final Score: " + score;
startBtn.textContent = "đ Play Again";
draw();
}
startBtn.addEventListener(
"click",
startGame
);
// Keyboard controls
document.addEventListener(
"keydown",
event => {
if (event.key === "ArrowLeft") {
moveLeft();
}
if (event.key === "ArrowRight") {
moveRight();
}
if (event.code === "Space") {
event.preventDefault();
shoot();
}
}
);