xxxxxxxxxx
137
//Variables
var s;
var img = [];
var song = [];
var vel = 4;
var score = 0;
var stop = true;
var screen = 0;
var mouse = false;
//Load images
function preload() {
img[0] = loadImage('Images/Black.png');
img[1] = loadImage('Images/Blue.png');
img[2] = loadImage('Images/Green.png');
img[3] = loadImage('Images/Red.png');
img[4] = loadImage('Images/White.png');
img[5] = loadImage('Images/Yellow.png');
img[6] = loadImage('Images/Street.png');
img[7] = loadImage('Images/Start.png');
img[8] = loadImage('Images/Menu.png');
img['7b'] = loadImage('Images/Start2.png');
img['8b'] = loadImage('Images/Menu2.png');
song[0] = loadSound('Images/Coin.wav');
song[1] = loadSound('Images/Crunch.wav');
backgroundMusic = loadSound("Images/background.mp3");
}
//Setup
function setup() {
createCanvas(windowWidth, windowHeight);
p = new player();
s = new street();
b = new button();
// Play background music when the game starts
backgroundMusic.play();
}
function draw() {
background(220);
//If start went press
if (stop == false) {
//Street
s.render();
s.update();
//Render player
p.render();
p.update();
//Render cars
for (var i = 0; i < c.length; i++) {
c[i].render();
c[i].update();
//If car touch edge
if (c[i].pos.y > width + c[i].r.y) {
c.splice(i, 1);
c.push(new cars());
score += 100;
vel +=0.1;
}
//If car touch player
if (//X
c[i].pos.x + c[i].r.x / 2 > p.pos.x - p.r.x /2 &&
c[i].pos.x - c[i].r.x / 2 < p.pos.x + p.r.x / 2&&
//Y
c[i].pos.y + c[i].r.y / 2 > p.pos.y - p.r.y / 2 &&
c[i].pos.y- c[i].r.y / 2 < p.pos.y + p.r.y / 2) {
stop = true;
screen = 1;
song[1].play();
c.splice(i, 3);
}
}
//Score and vel
strokeWeight(3);
stroke(0);
fill(100);
rect(0, 0, width / 3, width / 8);
strokeWeight(1);
fill(255);
textSize(width / 20);
text('score: ' + score, width / 40, width / 20);
text('KM/H: ' + floor(vel) * 10, width / 40, width / 10);
//Button to restart
b.render(8, width / 2, height / 10);
} else {
//Restart
//If game aren't start
if (screen == 0) {
fill(125);
strokeWeight(2);
stroke(0);
rect(0, 0, width, height);
textSize(width / 20);
//Text
fill(75);
text('-Avoid others cars', width / 6.85, height / 2 - width / 7);
text('-Move with "ARROW KEYS"', width / 6.85, height / 2 - width / 20);
b.render(7, width / 2, height / 1.5)
} else {
//If game over
fill(125);
strokeWeight(2);
stroke(0);
rect(0, 0, width, height);
textSize(width / 13.333);
//Text
fill(75);
text('YOUR SCORE:', width / 6.85, height / 2 - height / 5.33);
text(score, width / 2, height / 2 - height / 13.33)
textSize(width / 20);
b.render(7, width / 2, height / 1.5);
}
}
//Keys
keys();
}
//Keys
function keys() {
//Turn left
if (keyIsDown(LEFT_ARROW)) {
p.pos.x -= 5
}
//urn right
if (keyIsDown(RIGHT_ARROW)) {
p.pos.x += 5
}
}
//Mouse
function mousePressed() {
mouse = true;
}
function mouseReleased() {
mouse = false;
}