Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moreoptions Added #56

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions index.css
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,34 @@
border-radius: 50%;
margin: auto;
/* border-radius: 50%; */
}
.controls{
border: 2px solid rgb(228, 217, 217);
border-radius: 20px;
background-color: #20973d;
margin-top: -35%;
margin-left: 5%;
height: 30vh;
width: 12vw;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
gap: 5px;
}
.controls button{
color: white;
background-color: rgb(234, 95, 129);
height: 5vh;
width: 6vw;
border: 3px solid white;
border-radius: 30px;
}
.controls .button :hover{
cursor: pointer;
}
.controls select{
height: 5vh;
border: 3px solid black;
border-radius: 20px;
}
17 changes: 15 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@

<body>
<div class="body">
<div id="score">Score: 0</div>
<div id="maxScoreCont">Max Score: 0</div>
<div id="score">Score:0</div>
<div id="maxScoreCont">Max Score:0</div>
<div id="board">

</div>

<div class="controls">
<div class="button"><button id="pause">Pause_me</button></div>
<div class="button"><button id="mute">Mute_karo</button></div>
<div class="button"><select>
<option>Windowed</option>
<option>FullScreen</option>
</select></div>
</div>
</div>

</div>

<script src="index.js"></script>
</body>

Expand Down
92 changes: 73 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
let board = document.getElementById('board')
let body=document.querySelector("body");
let scoreCont = document.getElementById('score')
let maxScoreCont = document.getElementById('maxScoreCont');
var pausing=document.querySelector("#pause");
var muting=document.querySelector("#mute");
var screenratio=document.querySelector('select')



let HeadEle;
// console.log(HeadEle);
let inputDir = { x: 0, y: 0 };
Expand All @@ -17,6 +24,61 @@ let snakeArr = [
let food = {
x: 6, y: 7
};
var i=0;
//pauses the game
pausing.addEventListener("click",()=>{

if(i===0){
speed=0;
pausing.innerHTML="Resume"
i=1;
}
else{
speed=5;
pausing.innerHTML="Pause";
i=0;
}



})

screenratio.addEventListener("change",function(){

if(screenratio.value==="FullScreen")
body.requestFullscreen();
else{
document.exitFullscreen();
}


})

var f=0;

muting.addEventListener("click",function(){
if(f==0){
moveSound.muted=true;
foodSound.muted=true;
musicSound.muted=true;
gameOverSound.muted=true;
mute.innerHTML="sound";

f=1;

}
else{
moveSound.muted=false;
foodSound.muted=false;
musicSound.muted=false;
gameOverSound.muted=false;
mute.innerHTML="mute";
f=0;

}

})


// Game Functions
function main(ctime) {
Expand All @@ -34,18 +96,19 @@ function isCollide(snake) {
//if you into yourself

if (snake[0].x > 18 || snake[0].x < 0 || snake[0].y > 18 || snake[0].y < 0) {
return true;
return true ;
}
}

function gameEngine() {
//part1: updating the snake array and food

if (isCollide(snakeArr)) {
gameOverSound.play();
musicSound.pause();
inputDir = { x: 0, y: 0 };
alert("Game over. Press any key to play again");
snakeArr = [{ x: 13, y: 15 }];
// musicSound.play();
// musicSound.play();
}

//IF you have eaten the food, increment the score and regenerate the food
Expand All @@ -59,7 +122,6 @@ function gameEngine() {
let b = 16;
food = { x: 2 + Math.round(a + (b - a) * Math.random()), y: Math.round(a + (b - a) * Math.random()) }
}

//Moving the snake
// console.log("-----")
// console.log(snakeArr.l)
Expand All @@ -73,6 +135,8 @@ function gameEngine() {
snakeArr[0].x += inputDir.x;
snakeArr[0].y += inputDir.y;



//part2: display the snake and food
//display the snake
board.innerHTML = "";
Expand Down Expand Up @@ -125,8 +189,9 @@ function gameEngine() {
board.appendChild(snakeElement)
}
})

//part2: display the snake




foodElement = document.createElement('div');
foodElement.style.gridRowStart = food.y;
Expand All @@ -136,19 +201,9 @@ function gameEngine() {

}










//Main logic starts here
window.requestAnimationFrame(main);
window.addEventListener('keydown', e => {
// inputDir = { x: 0, y: 1 } //start the game

moveSound.play();
switch (e.key) {
case "ArrowUp":
Expand All @@ -173,5 +228,4 @@ window.addEventListener('keydown', e => {
default:
break;
}
});

});