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

Impede spawn seguido no mesmo lugar e função reset #2

Open
wants to merge 4 commits into
base: main
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
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Awesome JSGame Detona Ralph

<p align="center">
<img src="https://github.com/digitalinnovationone/jsgame-detona-ralph/raw/main/assets/images/logo.png" alt="JSGame Detona Ralph Logo">
<img src="./src/images/ralph.png" alt="JSGame Detona Ralph Logo">
</p>

Bem-vindo ao **JSGame Detona Ralph**! Neste repositório, você encontrará um jogo divertido baseado no famoso filme "Detona Ralph". Este projeto não apenas oferece entretenimento, mas também demonstra várias técnicas avançadas de desenvolvimento de jogos em JavaScript.
Expand Down Expand Up @@ -29,7 +29,7 @@ Contribuições são bem-vindas! Se você deseja melhorar este jogo, adicionar n

### Créditos

Este jogo foi desenvolvido como parte de um projeto educacional da Digital Innovation One.
Este jogo foi desenvolvido como parte de um projeto educacional da DIO.

---

Expand Down
33 changes: 29 additions & 4 deletions src/scripts/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function countDown() {
clearInterval(state.actions.countDownTimerId);
clearInterval(state.actions.timerId);
alert("Game Over! O seu resultado foi: " + state.values.result);
restartGame();
}
}

Expand All @@ -34,15 +35,25 @@ function playSound(audioName) {
audio.play();
}

let shuffledSquares = [];

function shuffleSquares() {
shuffledSquares = [...state.view.squares].sort(() => Math.random() - 0.5);
}

function randomSquare() {
state.view.squares.forEach((square) => {
square.classList.remove("enemy");
});

let randomNumber = Math.floor(Math.random() * 9);
let randomSquare = state.view.squares[randomNumber];
randomSquare.classList.add("enemy");
state.values.hitPosition = randomSquare.id;
if (shuffledSquares.length === 0) {

shuffleSquares();
}

const nextSquare = shuffledSquares.pop();
nextSquare.classList.add("enemy");
state.values.hitPosition = nextSquare.id;
}

function addListenerHitBox() {
Expand All @@ -57,6 +68,20 @@ function addListenerHitBox() {
});
});
}
function restartGame() {
clearInterval(state.actions.countDownTimerId);
clearInterval(state.actions.timerId);

state.values.hitPosition = 0;
state.values.result = 0;
state.values.curretTime = 60;

state.view.score.textContent = state.values.result;
state.view.timeLeft.textContent = state.values.curretTime;

state.actions.timerId = setInterval(randomSquare, state.values.gameVelocity);
state.actions.countDownTimerId = setInterval(countDown, 1000);
}

function initialize() {
addListenerHitBox();
Expand Down