xxxxxxxxxx
193
{
var player;
var enemy = []; //group of enemy
var score = 0;
var start = false;
var lose = false;
var control = [87, 68, 83, 65]; // wasd
var playerspeed = 5; //convient to modify
var enemyspeed = 3;
var backgroundcol;
var gameover, crash, pass, explosion, playercar, enemycar1, enemycar2, enemycar3, enemycar4, textborder;
var tile = []; //decoration
} //var stuffs
function preload() {
gameover = loadImage('assets/gameover.png');
explosion = loadImage('assets/explosion.png');
playercar = loadImage('assets/car.png');
enemycar1 = loadImage('assets/enemy1.png');
enemycar2 = loadImage('assets/enemy2.png');
enemycar3 = loadImage('assets/enemy3.png');
enemycar4 = loadImage('assets/enemy4.png');
textborder = loadImage('assets/textborder.png');
crash = loadSound('assets/crash.mp3');
pass = loadSound('assets/pass.wav');
}
function setup() {
backgroundcol = color(54, 89, 133);
var cnv = createCanvas(400, windowHeight);
var x = (windowWidth - width) / 2;
var y = (windowHeight - height) / 2;
cnv.position(x, y);
let playercarsz = [30, 55];
let playerxy = [(width / 2) - (playercarsz[0] / 2), height - playercarsz[1]];
let enemystart = -50;
player = new Car(playerxy[0], playerxy[1], playercarsz[0], playercarsz[1], playerspeed, playercar, control); // player car
enemy[0] = new Car(random(width), enemystart, 30, 55, enemyspeed, enemycar1); //basic car
enemy[1] = new Car(random(width), enemystart - 100, 50, 100, 0, enemycar2); //truck
enemy[2] = new Car(random(width), enemystart - 25, 25, 25, enemyspeed + 2, enemycar3); //cone
enemy[3] = new Car(random(width), enemystart - 200, 50, 200, -1, enemycar4); //trailer
}
function draw() {
background(backgroundcol);
noStroke();
fill(169, 192, 208);
rect(0, 0, 25, height);
rect(width - 25, 0, 25, height);
rect(width / 2 - 15, 0, 15, height);
rect(width / 2 + 15, 0, 15, height);
fill(0);
textAlign(CENTER);
textSize(32);
text(score, 30, 30);
player.movement(); //draw player
for (x = 0; x < enemy.length; x++) {
enemy[x].bot();
enemy[x].collision(player);
} //draw enemy
if (lose == true) noLoop();
if (start == false) {
noLoop();
image(textborder, 15, 10, 375, 150);
fill(0);
textSize(28);
textStyle(BOLD);
textAlign(CENTER);
text('CLICK FOR FULLSCREEN', width / 2, 50);
text('CONTROL WASD', width / 2, 100);
} //run before starting
}
function Car(carX, carY, carW, carH, speed, img, controls) {
this.carW = carW;
this.carH = carH;
this.x = carX;
this.y = carY;
this.speed = speed;
this.image = img; //overlay image
this.hit = false;
this.movement = function() {
if (keyIsDown(controls[0])) {
//up
this.y -= this.speed
}
if (keyIsDown(controls[1])) {
// right
this.x += this.speed
}
if (keyIsDown(controls[2])) {
//down
this.y += this.speed
}
if (keyIsDown(controls[3])) {
//left
this.x -= this.speed
}
if (this.x < 0) this.x = 0;
if (this.x > width - this.carW) this.x = width - this.carW;
if (this.y > carY) this.y = carY;
if (this.y < 0) this.y = 0;
noFill();
//rect(this.x, this.y, this.carW, this.carH);
image(this.image, this.x, this.y, this.carW, this.carH);
} //player car
this.bot = function() {
this.speed += 0.005; //speedup
this.y += this.speed;
image(this.image, this.x, this.y, this.carW, this.carH);
//rect(this.x, this.y - this.speed, this.carW, this.carH);
if (this.y > height + 50) { //car passed thru
this.y = carY - (this.speed);
this.x = random(width); //loop
score++;
pass.play();
}
} //enemy
this.collision = function(obj) {
this.hit = collideRectRect(this.x, this.y, this.carW, this.carH, obj.x, obj.y, obj.carW, obj.carH);
if (this.hit) {
GAMEOVER();
}
}
}
function keyPressed() { //game only start when player is ready
if (lose == false) {
start = true;
loop();
}
if (key == 'r' || key == 'R') { // restart function
lose = false;
score = 0;
let playercarsz = [30, 55];
let playerxy = [(width / 2) - (playercarsz[0] / 2), height - playercarsz[1]];
let enemystart = -50;
player = new Car(playerxy[0], playerxy[1], playercarsz[0], playercarsz[1], playerspeed, playercar, control); // player car
enemy[0] = new Car(random(width), enemystart, 30, 55, enemyspeed, enemycar1); //basic car
enemy[1] = new Car(random(width), enemystart - 100, 50, 100, 0, enemycar2); //truck
enemy[2] = new Car(random(width), enemystart - 25, 25, 25, enemyspeed + 2, enemycar3); //cone
enemy[3] = new Car(random(width), enemystart - 200, 50, 200, -1, enemycar4); //trailer
draw();
}
} //start
function GAMEOVER() {
crash.play();
image(explosion, player.x, player.y, 75, 75);
image(gameover, 0, 0, 400, 600);
textAlign(CENTER);
fill(0);
text('HIGH SCORE:' + score, width / 2, 450);
text('PRESS R TO RESTART', width / 2, 500);
lose = true;
noLoop();
}
function mousePressed() {
let fs = fullscreen();
fullscreen(!fs);
}