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

added Buttons #50

Closed
wants to merge 6 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 black;
border-radius: 20px;
background-color: #222831;
margin-top: -30%;
margin-left: 5%;
height: 10vh;
width: 25vw;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-around;
gap: 5px;
}
.controls button{
color: white;
background-color: rgb(37, 71, 246);
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</button></div>
<div class="button"><button id="mute">Mute</button></div>
<div class="button"><select>
<option>windowed</option>
<option>fullScreen-mode</option>
</select></div>
</div>
</div>

</div>

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

Expand Down
88 changes: 86 additions & 2 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 pause=document.querySelector("#pause");
var mute=document.querySelector("#mute");
var screen=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
pause.addEventListener("click",()=>{

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



})
//full screen and windowed screen feature
screen.addEventListener("change",function(){

if(screen.value==="fullScreen-mode")
body.requestFullscreen();
else{
document.exitFullscreen();
}


})
//mute function
var k=0;

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

k=1;

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

}

})


// Game Functions
function main(ctime) {
Expand All @@ -34,9 +96,10 @@ 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)) {
Expand All @@ -45,6 +108,10 @@ function gameEngine() {
inputDir = { x: 0, y: 0 };
alert("Game over. Press any key to play again");
snakeArr = [{ x: 13, y: 15 }];




// musicSound.play();
}

Expand All @@ -58,7 +125,21 @@ function gameEngine() {
let a = 2;
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("-----")
Expand All @@ -73,6 +154,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,7 +208,8 @@ function gameEngine() {
board.appendChild(snakeElement)
}
})



//part2: display the snake

foodElement = document.createElement('div');
Expand Down