xxxxxxxxxx
90
const s = ( sketch ) => {
var mySoundGameOver;
var mySoundStartGame;
var mySoundGameOver;
var mySoundMove;
var mySoundEat;
sketch.preload = function() {
mySoundGameOver = sketch.loadSound('assets/Death.mp3');
mySoundStartGame = sketch.loadSound('assets/Intro.mp3');
mySoundEat = sketch.loadSound('assets/Fruit.mp3');
mySoundMove = sketch.loadSound('assets/Chomp.mp3');
}
sketch.setup = function() {
sketch.createCanvas(myGame.columns * myGame.sizeImage, myGame.rows * myGame.sizeImage + HEIGHT_TEXT);
sketch.frameRate(fr); // Attempt to refresh at starting FPS
fillMap();
};
function fillMap(){
mySoundStartGame.play();
for (var i = 0; i < myGame.rows; i++) {
for (var j = 0; j < myGame.columns; j++) { //Pacdot
if (myGame.maze[i][j] === 0) {
arrayPacdotMaze.push(new Pacdot(j * myGame.sizeImage, i * myGame.sizeImage));
} else if (myGame.maze[i][j] === 1) { //Roca
arrayRockMaze.push(new Rock(j * myGame.sizeImage, i * myGame.sizeImage));
} else if (myGame.maze[i][j] === 2) { //Food
arrayFoodMaze.push(new Food(j * myGame.sizeImage, i * myGame.sizeImage));
}
}
}
}
sketch.draw = function() {
sketch.background(0);
testplayerLose();
testplayerWin();
}
function testplayerLose(){
if(timeGame == 0 || myPacman.lives == 0) {
mySoundGameOver.play();
let miss = "Game Over!\nVols tornar a jugar?";
var continuar = confirm(miss);
if(continuar == true){
sketch.noLoop();
restartGame();
}
}
}
function testplayerWin() {
if(timeGame > 0){
if(myPacman.lives > 0 && arrayFoodMaze == 0 && arrayPacdotMaze == 0){
mySoundStartGame.play();
let miss = "Enhorabona has guanyat!!\nTemps Restant = " + timeGame + "\nVides Restants = " + myPacman.lives + "\nPunts Totals = " + myPacman.score + "\nPrem Ok per tornar a Jugar o Cancel per Sortir.";
var continuar = confirm(miss);
if(continuar == true){
sketch.noLoop();
restartGame();
}
}
}
}
function restartGame(){
sketch.clear();
myPacman.coordX = 12 * myGame.sizeImage;
myPacman.coordY = 12 * myGame.sizeImage;
myPacman.lives = 5;
myPacman.score = 0;
timeGame = 180;
fillMap();
sketch.loop();
}
}
var myp5 = new p5(s, 'myContainer');