//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//--------------------------------Alien City Destroyer---------------------------------------------
//---------------------------------By Abdullah Allami----------------------------------------------
//------------------------------------Assignment 2-------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
// You take the role of an allien ship that destroys randomly generated buildings
// Random buildings have a random number of floors, width, and color
// Use WASD to move your spaceship within the constrained area
// Use mouse click to fire oblitration laser beam
//================================================Global Variables==============================================
// Spacship Location variable
int spaceshipLocationX = 200;
int spaceshipLocationY = 100;
// We will store the values of the randomly generated building using the following variables
//Building 1 values======================
// Store building location
int buildingOneLocation = 100;
// Store Number of floors
int buildingOneFloors = 0;
// Store building width
int buildingOneWidth = 0;
// Store Building colors (RGB)
int buildingOneRed = 0;
int buildingOneGreen = 0;
int buildingOneBlue = 0;
// Store if the state of the building, if it was destroyed by laser or not
boolean buildingOneDestroyed = false;
// Store the explosion stage as it grows if the building is struck
int explosionGrowthOne = 0;
//Building 2 values===================================================================
// Store building location
int buildingTwoLocation = 200;
// Store Number of floors
int buildingTwoFloors = 0;
// Store building width
int buildingTwoWidth = 0;
// Store Building colors (RGB)
int buildingTwoRed = 0;
int buildingTwoGreen = 0;
int buildingTwoBlue = 0;
// Store if the state of the building, if it was destroyed by laser or not
boolean buildingTwoDestroyed = false;
// Store the explosion stage as it grows if the building is struck
int explosionGrowthTwo = 0;
//Building 3 values======================================================================
// Store building location
int buildingThreeLocation = 300;
// Store Number of floors
int buildingThreeFloors = 0;
// Store building width
int buildingThreeWidth = 0;
// Store Building colors (RGB)
int buildingThreeRed = 0;
int buildingThreeGreen = 0;
int buildingThreeBlue = 0;
// Store if the state of the building, if it was destroyed by laser or not
boolean buildingThreeDestroyed = false;
// Store the explosion stage as it grows if the building is struck
int explosionGrowthThree = 0;
//Building 4 values======================================================================
// Store building location
int buildingFourLocation = 400;
// Store Number of floors
int buildingFourFloors = 0;
// Store building width
int buildingFourWidth = 0;
// Store Building colors (RGB)
int buildingFourRed = 0;
int buildingFourGreen = 0;
int buildingFourBlue = 0;
// Store if the state of the building, if it was destroyed by laser or not
boolean buildingFourDestroyed = false;
// Store the explosion stage as it grows if the building is struck
int explosionGrowthFour = 0;
//Building 5 values======================================================================
// Store building location
int buildingFiveLocation = 500;
// Store Number of floors
int buildingFiveFloors = 0;
// Store building width
int buildingFiveWidth = 0;
// Store Building colors (RGB)
int buildingFiveRed = 0;
int buildingFiveGreen = 0;
int buildingFiveBlue = 0;
// Store if the state of the building, if it was destroyed by laser or not
boolean buildingFiveDestroyed = false;
// Store the explosion stage as it grows if the building is struck
int explosionGrowthFive = 0;
//==============================================Setup and draw=================================================
void setup() {
// Set Canvas Size
size(400, 400);
// Call a function to randomly generate the building that will be there when we first run the program
generateBuildingOne() ;
generateBuildingTwo() ;
generateBuildingThree() ;
generateBuildingFour() ;
generateBuildingFive() ;
}
void draw() {
// Draw Background Color
background(40, 40, 90);
// Call a Function to Draw Buildings
drawBuilding(buildingOneLocation, buildingOneFloors, buildingOneWidth, buildingOneRed, buildingOneGreen, buildingOneBlue, buildingOneDestroyed);
drawBuilding(buildingTwoLocation, buildingTwoFloors, buildingTwoWidth, buildingTwoRed, buildingTwoGreen, buildingTwoBlue, buildingTwoDestroyed);
drawBuilding(buildingThreeLocation, buildingThreeFloors, buildingThreeWidth, buildingThreeRed, buildingThreeGreen, buildingThreeBlue, buildingThreeDestroyed);
drawBuilding(buildingFourLocation, buildingFourFloors, buildingFourWidth, buildingFourRed, buildingFourGreen, buildingFourBlue, buildingFourDestroyed);
drawBuilding(buildingFiveLocation, buildingFiveFloors, buildingFiveWidth, buildingFiveRed, buildingFiveGreen, buildingFiveBlue, buildingFiveDestroyed);
// Draw Explosions if Building One is Struck
if (buildingOneDestroyed == true) {
drawExplosions(buildingOneLocation, buildingOneFloors, explosionGrowthOne);
explosionGrowthOne = explosionGrowthOne + 1;
}
// Draw Explosions if Building Two is Struck
if (buildingTwoDestroyed == true) {
drawExplosions(buildingTwoLocation, buildingTwoFloors, explosionGrowthTwo);
explosionGrowthTwo = explosionGrowthTwo + 1;
}
// Draw Explosions if Building Three is Struck
if (buildingThreeDestroyed == true) {
drawExplosions(buildingThreeLocation, buildingThreeFloors, explosionGrowthThree);
explosionGrowthThree = explosionGrowthThree + 1;
}
// Draw Explosions if Building Fouris Struck
if (buildingFourDestroyed == true) {
drawExplosions(buildingFourLocation, buildingFourFloors, explosionGrowthFour);
explosionGrowthFour = explosionGrowthFour + 1;
}
// Draw Explosions if Building Five is Struck
if (buildingFiveDestroyed == true) {
drawExplosions(buildingFiveLocation, buildingFiveFloors, explosionGrowthFive);
explosionGrowthFive = explosionGrowthFive + 1;
}
// Move all five building to the left, creating an illusion of motion
buildingOneLocation = buildingOneLocation - 1;
buildingTwoLocation = buildingTwoLocation - 1;
buildingThreeLocation = buildingThreeLocation - 1;
buildingFourLocation = buildingFourLocation - 1;
buildingFiveLocation = buildingFiveLocation - 1;
// If buildings one is out of sight, then we replace it with new buildings
if (buildingOneLocation == -50) {
// Place the building to the right of the scree, where the ship is heading
buildingOneLocation = 450;
// Ensure that the building is not destroyed when it spawns
buildingOneDestroyed = false;
// Call the random building generator to creat new parameters for the building
generateBuildingOne();
// Reset building explosion data
explosionGrowthOne = 0;
}
// If buildings two is out of sight, then we replace it with new buildings
if (buildingTwoLocation == -50) {
// Place the building to the right of the scree, where the ship is heading
buildingTwoLocation = 450;
// Ensure that the building is not destroyed when it spawns
buildingTwoDestroyed = false;
// Call the random building generator to creat new parameters for the building
generateBuildingTwo();
// Reset building explosion data
explosionGrowthTwo = 0;
}
// If buildings three is out of sight, then we replace it with new buildings
if (buildingThreeLocation == -50) {
buildingThreeLocation = 450;
// Ensure that the building is not destroyed when it spawns
buildingThreeDestroyed = false;
// Call the random building generator to creat new parameters for the building
generateBuildingThree();
// Reset building explosion data
explosionGrowthThree = 0;
}
// If buildings four is out of sight, then we replace it with new buildings
if (buildingFourLocation == -50) {
// Place the building to the right of the scree, where the ship is heading
buildingFourLocation = 450;
// Ensure that the building is not destroyed when it spawns
buildingFourDestroyed = false;
// Call the random building generator to creat new parameters for the building
generateBuildingFour();
// Reset building explosion data
explosionGrowthFour = 0;
}
// If buildings five is out of sight, then we replace it with new buildings
if (buildingFiveLocation == -50) {
// Place the building to the right of the scree, where the ship is heading
buildingFiveLocation = 450;
// Ensure that the building is not destroyed when it spawns
buildingFiveDestroyed = false;
// Call the random building generator to creat new parameters for the building
generateBuildingFive();
// Reset building explosion data
explosionGrowthFive = 0;
}
// Call a function to draw spaceship
drawSpaceship();
}
//====================================Controls=============================================================
void mousePressed() {
// Shoot laser with mouse pressed
// Call a function to draw laser
drawLaser();
// Collision test between the laser and building one
if (((spaceshipLocationX - 10) < (buildingOneLocation + (buildingOneWidth/2)))&&((spaceshipLocationX +10) > (buildingOneLocation - (buildingOneWidth/2)))) {
// if laser collided with this building, then destroy it
buildingOneDestroyed = true;
}
// Collision test between the laser and building two
if (((spaceshipLocationX - 10) < (buildingTwoLocation + (buildingTwoWidth/2)))&&((spaceshipLocationX +10) > (buildingTwoLocation - (buildingTwoWidth/2)))) {
// if laser collided with this building, then destroy it
buildingTwoDestroyed = true;
}
// Collision test between the laser and building three
if (((spaceshipLocationX - 10) < (buildingThreeLocation + (buildingThreeWidth/2)))&&((spaceshipLocationX +10) > (buildingThreeLocation - (buildingThreeWidth/2)))) {
// if laser collided with this building, then destroy it
buildingThreeDestroyed = true;
}
// Collision test between the laser and building four
if (((spaceshipLocationX - 10) < (buildingFourLocation + (buildingFourWidth/2)))&&((spaceshipLocationX +10) > (buildingFourLocation - (buildingFourWidth/2)))) {
// if laser collided with this building, then destroy it
buildingFourDestroyed = true;
}
// Collision test between the laser and building five
if (((spaceshipLocationX - 10) < (buildingFiveLocation + (buildingFiveWidth/2)))&&((spaceshipLocationX +10) > (buildingFiveLocation - (buildingFiveWidth/2)))) {
// if laser collided with this building, then destroy it
buildingFiveDestroyed = true;
}
}
void keyPressed() {
// Move left with "A" Key. unless you are too far to the left
if (key == 'a' && spaceshipLocationX > 3) {
spaceshipLocationX = spaceshipLocationX - 3;
}
// Move right with "D" Key. unless you are too far to the right
if (key == 'd'&& spaceshipLocationX < 395) {
spaceshipLocationX = spaceshipLocationX + 3;
}
// Move up with "W" Key. unless you are too high
if (key == 'w'&& spaceshipLocationY > 3) {
spaceshipLocationY = spaceshipLocationY - 3;
}
// Move down with "S" Key. unless you are too low
if (key == 's'&& spaceshipLocationY < 150) {
spaceshipLocationY = spaceshipLocationY + 3;
}
}
//======================================= User defined Functions=============================================
// Those functions randomly generate buildings.
// This function is used to randomly generate parameters for building one
void generateBuildingOne() {
// Generate a random number of floors
buildingOneFloors = int(random(1, 8));
// Generate random building width
buildingOneWidth = int(random(40, 90));
// Generate random building colors (RGB)
buildingOneRed = int(random(40, 120));
buildingOneGreen = int(random(40, 120));
buildingOneBlue = int(random(40, 120));
}
// This function is used to randomly generate parameters for building two
void generateBuildingTwo() {
// Generate a random number of floors
buildingTwoFloors = int(random(1, 8));
// Generate random building width
buildingTwoWidth = int(random(60, 90));
// Generate random building colors (RGB)
buildingTwoRed = int(random(40, 120));
buildingTwoGreen = int(random(40, 120));
buildingTwoBlue = int(random(40, 120));
}
// This function is used to randomly generate parameters for building three
void generateBuildingThree() {
// Generate a random number of floors
buildingThreeFloors = int(random(1, 8));
// Generate random building width
buildingThreeWidth = int(random(60, 90));
// Generate random building colors (RGB)
buildingThreeRed = int(random(40, 120));
buildingThreeGreen = int(random(40, 120));
buildingThreeBlue = int(random(40, 120));
}
// This function is used to randomly generate parameters for building four
void generateBuildingFour() {
// Generate a random number of floors
buildingFourFloors = int(random(1, 8));
// Generate random building width
buildingFourWidth = int(random(60, 90));
// Generate random building colors (RGB)
buildingFourRed = int(random(40, 120));
buildingFourGreen = int(random(40, 120));
buildingFourBlue = int(random(40, 120));
}
// This function is used to randomly generate parameters for building five
void generateBuildingFive() {
// Generate a random number of floors
buildingFiveFloors = int(random(1, 8));
// Generate random building width
buildingFiveWidth = int(random(60, 90));
// Generate random building colors (RGB)
buildingFiveRed = int(random(40, 120));
buildingFiveGreen = int(random(40, 120));
buildingFiveBlue = int(random(40, 120));
}
// Draw a building floor by floor using a loop that calls a floor drawing function
void drawBuilding(int buildingLocation, int buldingFloors, int buildingWidth, int buildingRed, int buildingGreen, int buildingBlue, boolean isDestroyed) {
for (int currentFloor = 0; currentFloor < buldingFloors; currentFloor = currentFloor +1) {
drawFloor(buildingLocation, currentFloor, buildingWidth, buildingRed, buildingGreen, buildingBlue, isDestroyed);
}
}
// Draw Floors
void drawFloor(int floorLocation, int floorNumber, int floorWidth, int floorRed, int floorGreen, int floorBlue, boolean floorBurned) {
rectMode(CENTER);
strokeWeight(2);
stroke(50);
// if building is destroyed the it is burned black
if (floorBurned == true) {
// set color to black
fill(20);
} else {
// if building is not destroyed then draw it with the colors that were randomly geneerated for this building
fill(floorRed, floorGreen, floorBlue);
}
rect(floorLocation, 390 - (floorNumber*30), floorWidth, 30);
}
//This function is used to draw the spaceship
void drawSpaceship() {
ellipseMode(CENTER);
strokeWeight(5);
// Spceship Colors
stroke(30, 200, 70);
fill(30, 150, 60);
// Spaceship is made of ellipses
ellipse(spaceshipLocationX, spaceshipLocationY, 60, 30);
ellipse(spaceshipLocationX, spaceshipLocationY, 30, 15);
fill(0, 20, 0);
ellipse(spaceshipLocationX, spaceshipLocationY, 20, 20);
}
// Draw laser
void drawLaser() {
// Prepare drawing modes for laser
rectMode(CORNER);
strokeWeight(10);
// Laser colors
stroke(200, 200, 250);
fill(180, 180, 220);
// The shape of the laser beam
rect(spaceshipLocationX-10, spaceshipLocationY, 20, 400);
}
//This function will cause each floor in the building to explode
void drawExplosions(int explosionLocation, int lastFloorExploding, int explosionWidth) {
// Loop explosion for each floor
for (int currentFloorExploding = 0; currentFloorExploding < lastFloorExploding; currentFloorExploding = currentFloorExploding +1) {
// Set colors and shape of explosion
ellipseMode(CENTER);
strokeWeight(8);
stroke(200, 200, 70);
fill(200, 20, 10);
// Draw explosions, they grow larger with time after the building is destroyed
ellipse(explosionLocation, 390 - (currentFloorExploding*30), 5+ (explosionWidth/3), 30);
}
}