Skip to content

Commit

Permalink
Trying to fix score and timer reset issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanamisra committed Aug 27, 2024
1 parent 3c28030 commit 42e007d
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/pages/Game.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ let Collision = Matter.Collision;

let ground, ball, world, engine, mCon, leftWall, rightWall, scoreBoard, audioButton;
let boxes = [];
let score;
let timer;
// let score;
// let timer;
let bgImage, ballImage, scoreboardImage, audioOnImage, audioOffImage;
let monsterImages = [];
let imgIndex = 0;
Expand All @@ -67,8 +67,8 @@ function Game() {
// navigate() and location() help in, well, navigation, specifically to the game over screen.
const navigate = useNavigate();
const location = useLocation();
score = 0;
timer = 42;
// score = 0;
// timer = 42;

// p5 and audio playing code.
const p5Container = useRef();
Expand All @@ -78,6 +78,8 @@ function Game() {
// more code.
hitAudioRef.current.volume = 0.2;
const [playing, setPlaying] = useState(false);
const [score, setScore] = useState(0);
const [timer, setTimer] = useState(42);

// This is a p5 function. It wraps up all of the p5 "stuff".
function sketch(p) {
Expand Down Expand Up @@ -218,7 +220,8 @@ function Game() {
ground.show(p);
ball.show(p);
if (p.frameCount % 60 === 0 && timer > 0) {
timer--;
let newTimer = timer - 1;
setTimer(newTimer);
if (timer === 0) {
gameOver = true;
navigate("/leaderboard", { state: { score: score } });
Expand All @@ -244,7 +247,8 @@ function Game() {
World.remove(engine.world, boxes[i].body);
boxes.splice(i, 1);
ball.reset(p);
score += 1;
let newScore = score + 1;
setScore(newScore);
hitAudioRef.current.play();
}
}
Expand Down

0 comments on commit 42e007d

Please sign in to comment.