xxxxxxxxxx
162
//Variables
var s; // For the street object
var img = []; // Array to store the images
var vel = 4; // Velocity
var score = 0; // For the players score
var stop = true; // To control game state (start/stop)
var screen = 0; // To manage different game screens
// Load images
function preload() {
// Load images into the 'img' array
img[0] = loadImage('Black.png');
img[1] = loadImage('Blue.png');
img[2] = loadImage('Green.png');
img[3] = loadImage('Red.png');
img[4] = loadImage('White.png');
img[5] = loadImage('Yellow.png');
img[6] = loadImage('Street.png');
}
// Setup function
function setup() {
createCanvas(windowWidth, windowHeight);
p = new player(); // Create a player object
s = new street(); // Create a street object
}
// Draw function (main game loop)
function draw() {
background(220);
// If the game is running
if (stop == false) {
// Render and update the street
s.render();
s.update();
// Render and update the player
p.render();
p.update();
// Render and update cars
for (var i = 0; i < c.length; i++) {
c[i].render();
c[i].update();
// Check if a car has gone off the screen
if (c[i].pos.y > width + c[i].r.y) {
c.splice(i, 1);
c.push(new cars());
score += 100;
vel += 0.1; // Increase velocity as the game progresses
}
// Check if a car has collided with the player
if (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 &&
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; // Stop the game
screen = 1; // Switch to the game over screen
c.splice(i, 3); // Remove collided cars
}
}
// Render the car
this.render = function() {
push();
translate(this.pos.x, this.pos.y);
imageMode(CENTER); // Set the image mode to center
// Increase the size of the image
let scaleFactor = 2; // Change this value to adjust the size
image(img[this.type], 0, 0, this.r.x * scaleFactor, this.r.y * scaleFactor);
pop();
}
// Display the player's score and velocity
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);
} else { // If the game is stopped (game over or not started)
// Restart the game if the space bar is pressed
if (keyIsDown(32)) {
score = 0;
vel = 2;
screen = 1;
stop = false;
for (var j = 0; j < 3; j++) {
c.push(new cars());
}
}
// If the game hasn't started yet
if (screen == 0) {
fill(0);
strokeWeight(2);
stroke(255);
rect(0, 0, width, height);
textSize(width / 20);
// Display game instructions
text('- Avoid other cars', width / 2 - width / 2.85, height / 2);
text('- Move with "ARROW KEYS"', width / 2 - width / 2.85, height / 2 + width / 20);
text('- Press "SPACE BAR" to start', width / 2 - width / 2.85, height / 2 + width / 10);
} else { // If the game is over
fill(0);
strokeWeight(2);
stroke(255);
rect(0, 0, width, height);
textSize(width / 13.333);
// Display the player's score
text('YOUR SCORE:', width / 2 - 120, height / 2 - height / 5.33);
text(score, width / 2, height / 2 - height / 13.33)
textSize(width / 20);
text('- Press "SPACE BAR" to restart', width / 2 - width / 2.66, height / 2);
}
}
// Check for key inputs (left and right arrow keys)
keys();
}
// Street object constructor
function street() {
// Set the initial position of the street
this.pos = createVector(width / 2, height / 8);
// Set the size of the street
this.r = createVector(width * 1.75, height * 1.75);
// Render the street
this.render = function() {
push();
translate(this.pos.x, this.pos.y);
image(img[6], -this.r.x / 2, -this.r.y / 2, this.r.x, this.r.y);
pop();
}
// Update the street's position
this.update = function() {
this.pos.y += vel;
if (this.pos.y > height / 1.92) {
this.pos.y = height / 8;
}
}
}
// Function to handle arrow key inputs for player movement
function keys() {
// Turn the player left
if (keyIsDown(LEFT_ARROW)) {
p.pos.x -= 5;
}
// Turn the player right
if (keyIsDown(RIGHT_ARROW)) {
p.pos.x += 5;
}
}