xxxxxxxxxx
141
let myCars = [];
let myCar;
var img0;
var img1;
var img2;
var img3;
var img4;
var mode = 0;
let cnt = 0;
let ignitionSound;
let drivingSound;
let crashSound;
let taximusic;
function preload () {
img0 = loadImage("images/start1.png");
img1 = loadImage("images/instructions1.png");
img2 = loadImage("images/desert.png");
img3 = loadImage("images/gameover.png");
img4 = loadImage("images/pedestrian.png");
ignitionSound = loadSound("sounds/ignition1.mp3")
drivingSound= loadSound ("sounds/driving.wav")
crashSound= loadSound("sounds/car crash.wav")
taxiMusic= loadSound("sounds/taximusic.mp3")
// ignitionSound.setVolume(0.1);
mode = 0;
}
function setup() {
createCanvas(800, 960);
taxiMusic.play();
rectMode(CENTER);
for( let i=0; i<4; i++){
let x = width * 0.3125;
if (i % 2 == 0) {
x = width * 0.4375;
}
if (i % 3 == 0) {
x = width * 0.5625;
}
if (i % 4 == 0) {
x = width * 0.6875;
}
randomC = color(random(0,255), random(0,255), random(0,255));
myCars.push(new Car(x, 1000 , randomC));
}
myCar= new Car(250, 1000, color(255,244,79) );
// keyPressed();
}
function draw() {
if (mode== 0) {
image(img0, 0, 0, width, height);
}
if (mode== 1){
image(img1, 0, 0, width, height);
}
if (mode== 2){
image(img2, 0, 0, width, height);
//road
fill(0)
rect(400,0, 400, 2000)
//lanes
strokeWeight (3)
stroke(255);
line( 200, 0, 200, 1000)
line( 300, 0, 300, 1000)
line( 400, 0, 400, 1000)
line( 500, 0, 500, 1000)
line( 600, 0, 600, 1000)
noStroke();
fill(126,200,80);
rect(100, 0, 200,2000)
rect(700, 0, 200,2000)
noStroke();
fill(200);
rect(0, 0, 200,2000)
rect(800, 0, 200,2000)
push();
image(img4, 100, 450, 100, 200);
pop();
push();
image(img4, 600, 450, 100, 200);
pop();
myCar.running();
for( let i = 0 ; i < myCars.length; i++){
myCars[i].run();
textSize(30);
text("Total: " + cnt, 10,30);
}
if (mode== 4){ //game over
image(img3, 0, 0, width, height);
stroke(0);
textSize(100);
text("GAME OVER!",80, 430 );
textSize(40);
text("Click Enter to Keep Playing!",50, 540 );
taxiMusic.stop();
}
}
}
function keyPressed (){
if (key == "Enter" && mode == 0) {
mode = 1;
} else {
if(mode == 1){
mode = 2;
}
if (key == "Enter" && mode == 4) {
mode = 0;
taxiMusic.play();
}
}
}