Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur-kalita authored Feb 7, 2024
1 parent 0a86ea9 commit a92046c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
16 changes: 16 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Jump Game</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="game">
<div id="character"></div>
<div id="block"></div>
</div>
<div id="score">Score: 0</div>
<script src="script.js"></script>
</body>
</html>
32 changes: 32 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var character = document.getElementById("character");
document.addEventListener("click", jump);
var score = 0;
var scoreDiv = document.getElementById("score");

function jump(){
if(character.classList == "animate"){return;}
character.classList.add("animate");
setTimeout(removeJump,300);
};
function removeJump(){
character.classList.remove("animate");
}

var block = document.getElementById("block");

function updateScore(){
score++;
scoreDiv.innerHTML = "Score: " + score;
}

function checkDead(){
let characterTop = parseInt(window.getComputedStyle(character).getPropertyValue("top"));
let blockLeft = parseInt(window.getComputedStyle(block).getPropertyValue("left"));
if(blockLeft<20 && blockLeft>-20 && characterTop>=130){
alert("Game over");
}else if(blockLeft<20 && blockLeft>-20){
updateScore();
}
}

setInterval(checkDead, 10);
47 changes: 47 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#game{
width: 500px;
height: 200px;
border: 1px solid black;
margin: auto;
}

#character{
width: 20px;
height: 50px;
background-color: red;
position: relative;
top: 150px;
}

@keyframes jump{
0%{top: 150px;}
30%{top: 100px;}
70%{top: 100px;}
100%{top: 150px;}
}

.animate{
animation: jump 300ms linear;
}

#block{
width: 20px;
height: 20px;
background-color: blue;
position: relative;
top: 130px;
left: 480px;
animation: block 1s infinite linear;
}

@keyframes block{
0%{left: 500px}
100%{left: -20px}
}

#score{
position: absolute;
top: 10px;
left: 10px;
font-size: 20px;
}

0 comments on commit a92046c

Please sign in to comment.