Oi, Lucas
Fiz uns pequenos ajustes 🤓
function setup() {
createCanvas(500, 400);
for(let i = 0; i < carImage.length; i++){
carType.push(carImage[i]);
}
noLoop();
}
function draw() {
background(roadImage);
showActor()
moveActor()
showCar()
moveCar()
loopCars()
verifyCollision()
scorePoints()
showPoints()
limits()
showRecord()
iniciaJogo()
}
function limits(){
if (xActor < 0){
xActor = 0
}
if (xActor > 470){
xActor = 470
}
}
// Pausa e Inicia o Jogo
let jogoParado = true;
let textoJogo = "Click para Iniciar";
function mouseReleased()
{
if (jogoParado) {
jogoParado = false;
loop();
theme.play();
} else {
textoJogo = "Click para continuar";
jogoParado = true;
}
}
function iniciaJogo()
{
if (jogoParado) {
textAlign(CENTER);
textSize(50);
fill(color(0, 0, 0));
text(textoJogo, width /2, height/2);
theme.stop();
noLoop();
}
}
//Actor variables
let xActor = 100;
let yActor = 366;
let collision = false
let points = 0;
let record = 0;
let atorImovel = false;
function showActor(){
image(actor ,xActor , yActor, 40, 40);
yActor = constrain (yActor, -100, 366)
}
function moveActor(){
if (keyIsDown(UP_ARROW) && !atorImovel) yActor -= 4;
if (keyIsDown(DOWN_ARROW) && !atorImovel) yActor += 4;
if (keyIsDown(LEFT_ARROW) && !atorImovel) xActor -= 3.7;
if (keyIsDown(RIGHT_ARROW) && !atorImovel) xActor += 3.7;
}
function verifyCollision(){
for (let = i = 0; i < carImage.length; i = i + 1){
collision = collideRectCircle(xCar[i], yCar[i], carWidth, carHeight, xActor + 20, yActor + 20, 15);
if(collision){
collided();
collisionSound.play();
}
}
}
function collided(){
yActor = 366
points = 0
textoJogo = "Click para novo";
jogoParado = true;
}
function scorePoints(){
if (yActor < 2) {
points++;
scoreSound.play();
yActor = 366;
atorImovel = true;
setTimeout(function() {
atorImovel = false;
}, 2000);
}
}
function showPoints(){
textAlign(CENTER);
textSize(25);
fill(color(255, 240, 60));
text(points, width / 5, 27);
}
function showRecord(){
textAlign(CENTER);
textSize(25);
fill(color(0, 0, 255));
record = (points > record)? points: record;
text(record, 400, 27);
}