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

W1 challenge #73

Open
wants to merge 2 commits into
base: w1-challenge
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions game.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
const SNAKE_SPEED = 5;

const gameBoard = document.getElementById('game-board');
const gameBoard = document.getElementById("game-board");
let isGameOver = false;

const main = () => {
update();
draw();
if (isGameOver) {
alert('Game Over');
alert("Game Over");
clearInterval(gameLoop);
}
};

let gameLoop = setInterval(main, 1000 / SNAKE_SPEED);

const update = () => {
console.log('Updating');
console.log("Updating");
updateSnake();
updateFood();
isGameOver = checkGameOver();
};

const draw = () => {
gameBoard.innerHTML = '';
gameBoard.innerHTML = "";
drawSnake(gameBoard);
drawFood(gameBoard);
};
Expand All @@ -31,6 +31,12 @@ const checkGameOver = () => {
return snakeOutOfBounds() || snakeIntersectSelf();
};

const restart = document
.getElementById("reset")
.addEventListener("click", () => {
resetGame();
});

const resetGame = () => {
// Make sure the game loop is not still running
clearInterval(gameLoop);
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
</head>
<body>
<div id="game-board"></div>
<button id="reset">restart</button>
</body>
</html>
7 changes: 7 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ body {
background-color: red;
border: 0.25vmin solid gray;
}
#reset {
padding: 20px;
border: 1px solid;
position: absolute;
top: 500px;
right: 100px;
}