Skip to content

Commit

Permalink
adjusted 'game over' sign and button positions
Browse files Browse the repository at this point in the history
  • Loading branch information
FairlyTales committed Sep 2, 2021
1 parent 19cd172 commit c3ac992
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
10 changes: 8 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
<div class="wrapper">
<div class="fields"></div>
<div class="score-container">Score: <span id="score">0</span></div>
<div class="game-over-container hidden">Game Over</div>
<div class="btn-container hidden"><button id="restart">Restart</button></div>

<div class="game-over-container hidden">
<span>Game Over</span>
<div class="btn-container">
<button id="restart">Restart</button>
</div>
</div>

</div>
</body>
</html>
29 changes: 14 additions & 15 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import Fruit from './fruit.js';
import Matrix from './matrix.js';
import Snake from './snake.js';
import Utility from './random.js'
import Utility from './random.js';

window.onload = function (e) {
const field = document.querySelector('.fields');
const scoreField = document.getElementById('score');
const restartBtn = document.getElementById('restart');
const gameOver = document.querySelector('.game-over-container');
const btnContainer = document.querySelector('.btn-container');



// set board dimensions and cell size
const boardDimensions = {
x: 17,
y: 17,
cellSize: 40
}
cellSize: 40,
};

// snake starting position
const snakeStartPosition = [[8, 9], [7, 9]];
const snakeStartPosition = [
[8, 9],
[7, 9],
];

// create gameboard(matrix) and snake objects
const matrix = new Matrix(field, boardDimensions);
Expand All @@ -32,31 +32,30 @@ window.onload = function (e) {
snake.initInputListener();

// create first fruit
(new Fruit(matrix, Utility.generateFruit(matrix, snake))).show();
new Fruit(matrix, Utility.generateFruit(matrix, snake)).show();

// game score
let score = 0;

// game loop
const gameLoop = setInterval(() => {
if(!snake.snekDead) {
if(snake.ateFruit) {
if (!snake.snekDead) {
if (snake.ateFruit) {
score++;
snake.ateFruit = false;

(new Fruit(matrix, Utility.generateFruit(matrix, snake))).show();
new Fruit(matrix, Utility.generateFruit(matrix, snake)).show();
}

scoreField.textContent = score;
snake.move();
} else {
clearInterval(gameLoop);
gameOver.classList.remove('hidden');
btnContainer.classList.remove('hidden');
}
}, 500);

restartBtn.addEventListener('click', (e) => {
restartBtn.addEventListener('click', e => {
location.reload();
})
}
});
};
8 changes: 6 additions & 2 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ body {
}

.wrapper {
position: relative;
height: 95vh;

display: flex;
Expand Down Expand Up @@ -41,11 +42,14 @@ body {
font-size: 40px;
}

.game-over {
margin-top: 10px;
.game-over-container {
position: absolute;
top: 1110px;
}

.btn-container {
display: flex;
justify-content: center;
margin-top: 10px;
}

Expand Down

0 comments on commit c3ac992

Please sign in to comment.