xxxxxxxxxx
311
/*
Course: Introduction to interactive media
Midterm Project
Section: Mang-F2024
Name: Bismark Buernortey Buer
Title: Superman Saves (Enhanced Game)
*/
// Variables for Superman's position and movement
let supermanX, supermanY;
// Variables for the person's position (person to be saved)
let personX, personY;
// Variables for lives, timer, and difficulty (levels)
let lives = 3;
let timer = 10; // Time for each rescue attempt
let level = 1; // Start at level 1
// Flag to track if Superman is saving the person
let saving = false;
// Score to keep track of how many people Superman has saved
let score = 0;
// Variables for cloud positions and speeds
let cloudX1, cloudY1, cloudSpeed1;
let cloudX2, cloudY2, cloudSpeed2;
let cloudX3, cloudY3, cloudSpeed3;
// Variables for additional obstacles (birds)
let birdX, birdY, birdSpeed;
// Arrays for the animated background stars
let stars = []; // Array to store star data
let numStars = 50; // Number of stars to be displayed
// Variables for the dynamic background gradient
let gradientColor1, gradientColor2;
// Setup function
function setup() {
createCanvas(400, 600); // Set canvas size
// Set Superman's initial position near the bottom of the screen
supermanX = width / 2;
supermanY = height - 100;
// Set random position for the person at the bottom of the screen
resetPerson();
// Initialize cloud positions and speeds
cloudX1 = random(width);
cloudY1 = random(height / 2);
cloudSpeed1 = random(1, 3);
cloudX2 = random(width);
cloudY2 = random(height / 2);
cloudSpeed2 = random(1, 3);
cloudX3 = random(width);
cloudY3 = random(height / 2);
cloudSpeed3 = random(1, 3);
// Initialize bird positions and speeds
birdX = random(width);
birdY = random(height / 2);
birdSpeed = random(2, 4);
// Initialize stars
for (let i = 0; i < numStars; i++) {
let star = {
x: random(width),
y: random(height),
size: random(2, 5),
speed: random(0.5, 2)
};
stars.push(star);
}
// Initialize random colors for the background gradient
gradientColor1 = color(random(255), random(255), random(255));
gradientColor2 = color(random(255), random(255), random(255));
// Start the timer
setInterval(countdown, 1000); // Reduce timer every second
}
// Function to create animation
function draw() {
// Draw the gradient background
drawGradient(gradientColor1, gradientColor2);
// Add stars to the background (animated starry sky)
drawStars();
// Draw and move clouds across the screen
drawClouds();
// Draw the bird obstacles
drawBirds();
// Draw the person at the bottom of the screen
drawPerson(personX, personY);
// Draw Superman in his current position
drawSuperman(supermanX, supermanY);
// Move Superman based on arrow key inputs
if (keyIsDown(LEFT_ARROW)) {
supermanX -= 5; // Move left
}
if (keyIsDown(RIGHT_ARROW)) {
supermanX += 5; // Move right
}
if (keyIsDown(UP_ARROW)) {
supermanY -= 5; // Move up
}
if (keyIsDown(DOWN_ARROW)) {
supermanY += 5; // Move down
}
// Constrain Superman's movement to stay within the canvas
supermanX = constrain(supermanX, 0, width);
supermanY = constrain(supermanY, 0, height);
// Check if Superman reaches the person to start saving
if (dist(supermanX, supermanY, personX, personY) < 50 && !saving) {
saving = true; // Start saving the person
}
// Move the person with Superman if saving
if (saving) {
personX = supermanX;
personY = supermanY + 80; // Person follows below Superman
}
// If Superman reaches the top with the person, reset person and update score
if (supermanY < 50 && saving) {
saving = false; // Stop saving the person
score += 1; // Increase the score by 1
resetPerson(); // Reset person to the bottom
level += 1; // Increase the level
timer = 10; // Reset the timer
increaseDifficulty(); // Make the game harder
}
// Display the score, timer, lives, and level
displayHUD();
// If timer runs out, lose a life
if (timer <= 0) {
lives -= 1;
resetPerson();
timer = 10; // Reset timer
}
// If lives reach zero, end the game
if (lives <= 0) {
noLoop(); // Stop the game loop
textSize(32);
fill(255, 0, 0);
text('Game Over!', width / 2 - 80, height / 2);
}
}
// Function to display heads-up display (HUD) for score, timer, lives, and level
function displayHUD() {
fill(0);
textSize(24);
text('Score: ' + score, 10, 30);
text('Lives: ' + lives, 10, 60);
text('Timer: ' + timer, 10, 90);
text('Level: ' + level, 10, 120);
}
// Function to increase the difficulty by speeding up clouds and birds
function increaseDifficulty() {
cloudSpeed1 += 0.5;
cloudSpeed2 += 0.5;
cloudSpeed3 += 0.5;
birdSpeed += 0.5;
}
// Function to count down the timer
function countdown() {
if (timer > 0) {
timer -= 1;
}
}
// Function to draw Superman with body, head, and cape
function drawSuperman(x, y) {
fill(0, 0, 255); // Blue suit
rect(x - 15, y, 30, 60); // Superman's body rectangle
fill(255, 224, 189); // Skin color for head
ellipse(x, y - 30, 30, 30); // Superman's head
fill(255, 0, 0); // Red cape
triangle(x - 15, y, x + 15, y, x, y + 50); // Cape
}
// Function to draw the person (to be saved)
function drawPerson(x, y) {
fill(255, 224, 189); // Skin color for head
ellipse(x, y - 20, 20, 20); // Person's head
fill(0, 255, 0); // Green clothes
rect(x - 10, y - 10, 20, 30); // Person's body
}
// Function to draw birds (obstacles)
function drawBirds() {
fill(255, 0, 0); // Bird color
ellipse(birdX, birdY, 40, 30); // Draw bird as an ellipse
// Move the bird
birdX -= birdSpeed;
// Reset bird position when it moves off-screen
if (birdX < -30) {
birdX = width + 30;
birdY = random(height / 2);
}
// Check if Superman collides with the bird
if (dist(supermanX, supermanY, birdX, birdY) < 50) {
supermanX = width / 2;
supermanY = height - 100; // Reset Superman's position
lives -= 1; // Lose a life
}
}
// Function to create a dynamic gradient background
function drawGradient(c1, c2) {
for (let y = 0; y < height; y++) {
let inter = map(y, 0, height, 0, 1); // Calculate interpolation factor
let c = lerpColor(c1, c2, inter); // Interpolate between colors
stroke(c);
line(0, y, width, y); // Draw each line with gradient color
}
}
// Function to draw stars and animate them
function drawStars() {
for (let i = 0; i < stars.length; i++) {
let star = stars[i];
fill(255); // Star color (white)
noStroke();
ellipse(star.x, star.y, star.size, star.size); // Draw star
star.y += star.speed; // Move star downward
// Reset star position when it goes off-screen
if (star.y > height) {
star.y = 0;
star.x = random(width); // New random x-position
}
}
}
// Function to draw clouds and animate them
function drawClouds() {
drawCloud(cloudX1, cloudY1, cloudSpeed1);
drawCloud(cloudX2, cloudY2, cloudSpeed2);
drawCloud(cloudX3, cloudY3, cloudSpeed3);
// Move clouds
cloudX1 -= cloudSpeed1;
cloudX2 -= cloudSpeed2;
cloudX3 -= cloudSpeed3;
// Reset cloud positions when they move off-screen
if (cloudX1 < -30) {
cloudX1 = width + 30;
cloudY1 = random(height / 2);
}
if (cloudX2 < -30) {
cloudX2 = width + 30;
cloudY2 = random(height / 2);
}
if (cloudX3 < -30) {
cloudX3 = width + 30;
cloudY3 = random(height / 2);
}
}
//function to draw cloud and check collision
function drawCloud(x, y) {
fill(255); // Cloud color (white)
ellipse(x, y, 60, 40); // Draw cloud as an ellipse
// Check if Superman collides with a cloud
if (dist(supermanX, supermanY, x, y) < 50) {
supermanX = width / 2;
supermanY = height - 100; // Reset Superman's position
lives -= 1; // Lose a life
}
}
// Function to reset the person's position to a random spot at the bottom of the screen
function resetPerson() {
personX = random(50, width - 50); // Random x-position
personY = height - 50; // Position near the bottom of the screen
}
// Function to change background gradient colors when the mouse is pressed
function mousePressed() {
gradientColor1 = color(random(255), random(255), random(255)); // New random color 1
gradientColor2 = color(random(255), random(255), random(255)); // New random color 2
}