Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Carros na pista oposta, não ficou compacto, mas está funcional!

let xCarros = [600, 600, 600];
let xCarros2 = [0, 0, 0];
let yCarros = [40, 95, 150];
let yCarros2 = [210, 265, 318];
let velocidadeCarros = [4, 5.5, 6.2];
let velocidadeCarros2 = [5.1, 5.9, 4.3];
let comprimentoCarros = 50;
let alturaCarros = 40;

function mostraCarro(){
  for(let i = 0; i < imagemCarros.length; i++){
    image(imagemCarros[i], xCarros[i] , yCarros[i], comprimentoCarros, alturaCarros);
    image(imagemCarros2[i], xCarros2[i], yCarros2[i], comprimentoCarros, alturaCarros);
  }
}

function movimentaCarro(){
  for(let i = 0; i < velocidadeCarros.length; i++){
  xCarros[i] -= velocidadeCarros[i];
  }
}

function movimentaCarro2(){
  for(let i = 0; i < velocidadeCarros2.length; i++){
  xCarros2[i] += velocidadeCarros2[i];
  }
}

function posicaoInicialCarro(){
  for(let i = 0; i < xCarros.length; i++){
    if(passouTodaTela(xCarros[i])){
      xCarros[i] = 650;
      velocidadeCarros [i] += 0.15;
      if(velocidadeCarros [i] > 8){
        velocidadeCarros [i] = 8;
      }
    }
  }
}

function posicaoInicialCarro2(){
  for(let i = 0; i < xCarros2.length; i++){
    if(passouTodaTela2(xCarros2[i])){
      xCarros2[i] = -50;
      velocidadeCarros2 [i] += 0.15;
      if(velocidadeCarros2 [i] > 8){
        velocidadeCarros2 [i] = 8;
      }
    }
  }
}

function passouTodaTela(xCarros){
  return xCarros < -50;
}

function passouTodaTela2(xCarros2){
  return xCarros2 > 650;
}
1 resposta
solução!

Bom dia, beleza?

Fiz o meu bem parecido, mas dei uma "enxugada", mas acredito que dá pra refaturar ainda mais.

//cars
let xCars = [600, 600, 600, -73, -73, -73];
let yCars = [40, 96, 150, 210, 264, 320];
let speedCars = [2, 3.5, 5, -5, -3.5, -2];
let widthCar = 73;
let heightCar = 40;

function showCar() {
  for(let i = 0; i < carsAssets.length; i++) {
    image(carsAssets[i], xCars[i], yCars[i], widthCar, heightCar);
  }
}

function moveCar() {
  for(let i = 0; i < xCars.length; i++) {
    xCars[i] -= speedCars[i];
  }
}

function loopCar() {
  for(let i = 0; i < xCars.length; i++) {
    if(carOffScreenLeft(xCars[i])) {
      xCars[i] = 600;
    } 
    if(carOffScreenRight(xCars[i])) {
      xCars[i] = -73;
    }
  }
}

function carOffScreenLeft(xCars) {
  return xCars < -93;
}

function carOffScreenRight(xCars) {
  return xCars >620;
}

Abraços.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software