xxxxxxxxxx
454
let scene = 0;
let myCar;
let rock;
let life = 5;
let house;
let home;
let rocks=[];
let myself;
let my;
let car;
let cars = [];
let carnum = 11;
let rockImg = [];
let rocknum =11;
let x1 = 250;
let y1 = 350;
let wo;
let font;
let timew = 300;
let timeh = 15;
let ae86;
let carImg = [];
// let cover;
let t = 0;
function preload(){
ae86 = loadImage('myCar.png');
// cover = loadImage('Cover.jpg');
my = loadImage('Me.png');
font = loadFont('Nice Dream.ttf');
home = loadImage('House.png');
// car = loadImage('myCar.png');
for(let j = 0; j < 11; j++){
rockImg[j] = loadImage('rock' + j + '.png');
}
for(let i = 0; i < 11; i++){
carImg[i] = loadImage('car' + i + '.png');
}
}
function setup() {
createCanvas(500,700);
textFont(font);
myCar = new MyCar(width/2, height, 45,80);
car = new MyCar(width/2, 200, 45,80);
myself = new Me(250,height-100, 50,50);
house = new House(width/2, 70,80,80);
wo = new Me(250, 100,100,100);
// rocks = new Rock(x1,y1,50,50);
for(let j = 0; j<rocknum; j++){
rocks[j] = new Rock(random(width), random(height-150));
}
for(let i = 0; i < carnum; i++){
cars[i] = new Car(random(width),random(height-200));
}
}
function draw() {
background(255);
switch(scene){
case 0:
for(let x = 0; x < width; x = x+random(0,300)){ //background moving
for(let y = 0; y<height; y = y+random(0,300)){
strokeWeight(2);
point(x,y);
}
}
push();
textSize(40);
fill(10);
text('Click To Start',120, 70);
image(ae86, width/2-50,height/2);
console.log('scene 0');
pop();
break;
case 1:
background(255);
push();
textSize(40);
fill(10);
text('Get into Your car',120, 70);
push();
fill("#fcefb5");
noStroke();
rect(0, 0, timew,timeh);
timew--;
if(timew == 0){
life--;
timew = 300;
if(life == 0){
scene = 4;
}
} pop();
for(let j = 0; j<rocknum; j++){
rocks[j].body(j);
rocks[j].checkCollision(j);
}
myself.body();
myself.move();
myself.checkCollision();
car.body();
if(life == 0){
scene = 4;
}
// car.checkCollision();
break;
case 2:
t++;
textSize(20);
fill(10);
text(t + 'second',400,70);
push();
fill("#fcefb5");
rect(0, 0, timew,timeh);
timew--;
if(timew == 0){
life--;
timew = 300;
if(life == 0){
scene = 4;
}
} pop();
for (let i = 0; i < carnum; i++){
cars[i].body(i);
cars[i].move();
cars[i].checkCollision(i);
}
myCar.body();
myCar.move();
console.log('scene 3');
if(myCar.y < 0){
scene = 3;
}
else if(life == 0){
scene = 4;
}
break;
case 3:
textSize(40);
fill(10);
text('Deliver food',120, 470);
// myself = new Me(250,height-100, 50,50);
push();
noStroke();
fill("#fcefb5");
rect(0, 0, timew,timeh);
timew--;
if(timew == 0){
life--;
timew = 300;
if(life == 0){
scene = 4;
}
} pop();
for(let j = 0; j < rocknum; j++){
rocks[j].body(j);
rocks[j].checkCollision(j);
}
myself.body();
myself.move();
// myself.checkCollision();
// myself.checkRock();
house.body();
house.checkCollision();
if(life == 0){
scene = 4;
}
break;
case 4:
textSize(20);
fill(255);
text(t + 'second',width/2,height/2);
background(10);
textSize(30);
fill(255);
text('Oops! Your gonna pay for your Car fixing fee, and come on man! you gonna do this! try it one more time! you will get more tips from the customers if you deliver fast!',30,250,400,600);
textSize(20);
fill(255);
text('Refresh to restart',30,height-70);
wo.body();
console.log('scene 2');
break;
case 5:
background(10);
textSize(30);
fill(255);
text('Ohhhhhhhhhhhh!You won! and you got $100 tips from the customers! you are rich now!',30,250,400,200);
textSize(20);
wo.body();
textSize(20);
fill(255);
text('Refresh to restart',30,height-70);
break;
}
lives();
// for(let i = 0; i < 4; i++){
// image(carImg[i], i*30, 30, carImg[i].width * 0.8, carImg[i].height* 0.8);
// }
}
// function timeCounter(){
// }
class Car{
constructor(x,y){
this.x = x;
this.y = y;
this.w = 45;
this.h = 70;
this.t = t;
}
body(index){
//rect(this.x, this.y, this.w, this.h);
imageMode(CORNER);
image(carImg[index], this.x, this.y, carImg[index].width * 0.8, carImg[index].height* 0.8);
}
move(){
this.y += 3;
if(this.y > height){
this.y = 0;
}
}
checkCollision(){
if(myCar.x + myCar.w/2 > this.x && myCar.x < this.x+this.w && myCar.y + myCar.h/2 > this.y && myCar.y < this.y + this.h){
myCar.y = height - 100;
life--;
}
// if(life == 0){
// scene = 4;
// }
}
time(){
t++;
textSize(20);
fill(10);
text(t + 'second',400,70);
}
}
class MyCar{
constructor(x,y,w,h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
body(){
//ellipse(this.x,this.y, this.w, this.h);
imageMode(CENTER);
image(ae86,this.x, this.y, this.w, this.h);
}
// findCar(){
// image(ae86, 250,200, this.w, this.h);
// if(myCar.x + myCar.w/2 > this.x && myCar.x < this.x+this.w && myCar.y + myCar.h/2 > this.y && myCar.y < this.y + this.h){
// scene = 2;
// }
// }
move(){
if(keyIsPressed){
if(keyCode === UP_ARROW){
if(this.y>=0){
this.y-=5
}
}
else if(keyCode === LEFT_ARROW){
this.x -= 5;
if(this.x<0){
this.x = width;
}
}
else if(keyCode === RIGHT_ARROW){
this.x += 5;
if(this.x>width){
this.x=0;
}
}
else if(keyCode === DOWN_ARROW){
if(this.y<=height){
this.y += 5;
}
}
}
}
}
class Me{
constructor(x,y,w,h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
body(){
imageMode(CENTER);
image(my, this.x, this.y,this.w, this.h);
}
move(){
if(keyIsPressed){
if(keyCode === UP_ARROW){
if(this.y>=0){
this.y-=5
}
}
else if(keyCode === LEFT_ARROW){
this.x -= 5;
if(this.x<0){
this.x = width;
}
}
else if(keyCode === RIGHT_ARROW){
this.x += 5;
if(this.x>width){
this.x=0;
}
}
else if(keyCode === DOWN_ARROW){
if(this.y<=height){
this.y += 5;
}
}
}
}
checkCollision(){
if(car.x + car.w/2 > this.x && car.x < this.x+this.w && car.y + car.h/2 > this.y && car.y < this.y + this.h){
scene = 2;
myself.y = height - 50;
}
}
}
class Rock{
constructor(x,y,w,h){
this.x = x;
this.y = y;
this.w = 50;
this.h = 50;
}
body(index){
imageMode(CORNER);
image(rockImg[index], this.x, this.y, this.w, this.h);
}
checkCollision(){
if(myself.x + myself.w/2 > this.x && myself.x < this.x+this.w && myself.y + myself.h/2 > this.y && myself.y < this.y + this.h){
myself.y = height - 50;
life--;
}
}
}
class House{
constructor(x,y,w,h){
this.x = x;
this.y = y;
this.w = 80;
this.h = 80;
}
body(){
imageMode(CENTER);
image(home,this.x,this.y,this.w,this.h);
}
checkCollision(){
if(myself.x + myself.w/2 > this.x && myself.x < this.x+this.w && myself.y + myself.h/2 > this.y && myself.y < this.y + this.h){
scene = 5;
}
}
}
function lives(){
for(let i = 0; i < life; i++){
fill(255);
// ellipse(i*20, height-10, 20);
imageMode(CENTER);
image(my,i*30, height-10,30,30);
}
}
function mousePressed(){
if(scene == 0){
scene = 1;
}
if(scene == 5 || scene == 4){
scene = 0;
}
}