/*
Assignment #3 - OBJECT ORIENTED TOY
"SPACE BATTLE"
Created by Eric James
October 20, 2016
In this program you control a spaceship which can move in 8 directions using the WASD keys.
Three enemies will be moving around the screen which you can shoot.
You fire bullets from your spaceship by pressing the spacebar.
The goal of the game is to get as many points as possible.
Every time the player hits a square they will receive 100 points.
Try to go for a high score!
*/
//This declares all objects used in the program as global variables.
Ship spaceShip;
BadGuys enemies;
void setup() {
//initializes the objects used in the program.
spaceShip = new Ship(200, 300, 200, 300);
enemies = new BadGuys();
//Initial setup of the window size and framerate.
size(400, 400);
frameRate(60);
//Setup of some functions which are called from other classes.
spaceShip.setupShip();
}
void draw() {
//Draws a constantly refreshing backround.
background(0);
//Will not draw lines around objects
noStroke();
//Rectangles are drawn in corner mode.
rectMode(CORNERS);
//Calculates where the emeny will move.
enemies.moveEnemy();
//checks the boundaries of the enemies.
enemies.enemyBoundaries();
//checks the collision for if a bullet is hitting an enemy.
enemies.enemyCollision();
//Draws the enemies on the screen.
enemies.drawEnemy1();
//This function is used to draw the bar which shows the player how long it will be before they can fire their gun again.
spaceShip.bulletCooldownTimer();
//These functions are used to calculate where the ship will move and then draws the ship on the screen.
//'shipBoundaries' is used to make sure the ship does not go off screen.
spaceShip.moveShip();
spaceShip.shipBoundaries();
spaceShip.drawShip();
//This function is used to fire the gun on the ship.
spaceShip.fireShip();
}
void keyPressed() {
//Used to determine which keys are currently being pressed.
spaceShip.isShipMoving();
}
void keyReleased() {
//Used to determine which keys have now been released.
spaceShip.isShipStopping();
//This is used to keep track of the player's score.
scoreTracker();
} //This class is used to control the emimies you will face in the game.
//Three enimies will appear on the screen and move around randomly.
//If the player can shoot an enemy they will gain 100 points.
//There are three enemies: one starts on the left, the middle, and the right.
class BadGuys {
//Variables which will be used to calculate the position of enemies.
float enemyShipX;
float enemyShipY;
//These arrays are used to store location data for each of the three enemies.
float xCoordinate [] = new float[3];
float yCoordinate [] = new float[3];
//This PVector is used to calculate the position for each enemy.
PVector square = new PVector(100, 100);
BadGuys() {
//These 'if' statements are used to set the values of the coordinates for each enemy which are stored two seperate arrays.
//X Coordinates.
int i = 0;
while (i < 3) {
xCoordinate[i] = square.x * (i +1);
i = i + 1;
}
//Y Coordinates.
int a = 0;
while (a < 3) {
yCoordinate[a] = square.y;
a = a + 1;
}
}
//This Variable is used to draw each of the enemies on the screen.
void drawEnemy1() {
//Enemy on the left.
rect(xCoordinate[0] - 15, yCoordinate[0] - 15, xCoordinate[0] + 15, yCoordinate[0] + 15);
//enemy in the middle.
rect(xCoordinate[1] - 15, yCoordinate[1] - 15, xCoordinate[1] + 15, yCoordinate[1] + 15);
//enemy on the right.
rect(xCoordinate[2] - 15, yCoordinate[2] - 15, xCoordinate[2] + 15, yCoordinate[2] + 15);
}
//This function is used to move the enemies randomly around the screen.
//Each enemy will move at a different speed and is coloured randomly each cycle.
void moveEnemy() {
//Variables used for assigning random values.
int randomMovement1 = int(random(0, 5));
int randomMovement2 = int(random(0, 5));
int randomMovement3 = int(random(0, 5));
//Enemy on the left.
fill (randomMovement1 * 20, randomMovement2 * 20, randomMovement3 * 20);
if (randomMovement1 == 1) {
xCoordinate[0] = xCoordinate[0] + 10;
} else if (randomMovement1 == 2) {
xCoordinate[0] = xCoordinate[0] - 10;
} else if (randomMovement1 == 3) {
yCoordinate[0] = yCoordinate[0] + 10;
} else if (randomMovement1 == 4) {
yCoordinate[0] = yCoordinate[0] - 10;
}
//enemy in the middle.
fill (randomMovement3 * 20, randomMovement1 * 20, randomMovement2 * 20);
if (randomMovement2 == 1) {
xCoordinate[1] = xCoordinate[1] + 12;
} else if (randomMovement2 == 2) {
xCoordinate[1] = xCoordinate[1] - 12;
} else if (randomMovement2 == 3) {
yCoordinate[1] = yCoordinate[1] + 12;
} else if (randomMovement2 == 4) {
yCoordinate[1] = yCoordinate[1] - 12;
}
//enemy on the right.
fill (randomMovement2 * 20, randomMovement3 * 20, randomMovement1 * 20);
if (randomMovement3 == 1) {
xCoordinate[2] = xCoordinate[2] + 8;
} else if (randomMovement3 == 2) {
xCoordinate[2] = xCoordinate[2] - 8;
} else if (randomMovement3 == 3) {
yCoordinate[2] = yCoordinate[2] + 8;
} else if (randomMovement3 == 4) {
yCoordinate[2] = yCoordinate[2] - 8;
}
}
//This function is used to check the boundries for each enemy.
//This makes it so the enemies cannot go offscreen.
void enemyBoundaries() {
//Boundaries for enemy on the left.
if (xCoordinate[0] <= 0 + 15) {
xCoordinate[0] = 0 + 15;
}
if (xCoordinate[0] >= 400 - 15) {
xCoordinate[0] = 400 - 15;
}
if (yCoordinate[0] <= 0 + 15) {
yCoordinate[0] = 0 + 15;
}
if (yCoordinate[0] >= 400 - 15) {
yCoordinate[0] = 400 - 15;
}
//Boundaries for enemy in the middle.
if (xCoordinate[1] <= 0 + 15) {
xCoordinate[1] = 0 + 15;
}
if (xCoordinate[1] >= 400 - 15) {
xCoordinate[1] = 400 - 15;
}
if (yCoordinate[1] <= 0 + 15) {
yCoordinate[1] = 0 + 15;
}
if (yCoordinate[1] >= 400 - 15) {
yCoordinate[1] = 400 - 15;
}
//Boundaries for enemy on the left.
if (xCoordinate[2] <= 0 + 15) {
xCoordinate[2] = 0 + 15;
}
if (xCoordinate[2] >= 400 - 15) {
xCoordinate[2] = 400 - 15;
}
if (yCoordinate[2] <= 0 + 15) {
yCoordinate[2] = 0 + 15;
}
if (yCoordinate[2] >= 400 - 15) {
yCoordinate[2] = 400 - 15;
}
}
//This function is used to check if a bullet the player fired has hit an enemy.
//This is accomplished by checking if the billet is in the same space as an enemy.
//If the bullet has hit an enemy the screen will flash a specific colour for each enemy and the player gains 100 points.
//Enemy on the left.
void enemyCollision() {
if (bulletY - 20 <= yCoordinate[0] + 15 && bulletY - 20 >= yCoordinate[0] - 15 && (bulletX - 5 >= xCoordinate[0] - 15 || bulletX + 5 >= xCoordinate[0] - 15) && (bulletX - 5 <= xCoordinate[0] + 15 || bulletX + 5 <= xCoordinate[0] + 15)) {
background(50, 0, 0);
points = true;
}
//Enemy in the middle.
if (bulletY - 20 <= yCoordinate[1] + 15 && bulletY - 20 >= yCoordinate[1] - 15 && (bulletX - 5 >= xCoordinate[1] - 15 || bulletX + 5 >= xCoordinate[1] - 15) && (bulletX - 5 <= xCoordinate[1] + 15 || bulletX + 5 <= xCoordinate[1] + 15)) {
background(0, 50, 0);
points = true;
}
//Enemy on the right.
if (bulletY - 20 <= yCoordinate[2] + 15 && bulletY - 20 >= yCoordinate[2] - 15 && (bulletX - 5 >= xCoordinate[2] - 15 || bulletX + 5 >= xCoordinate[2] - 15) && (bulletX - 5 <= xCoordinate[2] + 15 || bulletX + 5 <= xCoordinate[2] + 15)) {
background(0, 0, 50);
points = true;
}
}
}//Used to track the score for the game.
//Every time the player shoots a square they gain 100 points.
//Variables which are used to track the score.
int score = 0;
boolean points = false;
//This function is used to add 100 points to the score of the player each time a bullet they shoot hits an enemy.
void scoreTracker() {
//If collision is true the score increases by 100 and the score is printed.
if (points == true) {
score = score + 100;
println("Score: " + score);
points = false;
}
}//This class is used to control the movement, firing and hit detection of the ship.
// The ship can move in 8 directions using WASD and fires a bullet when the player presses the spacebar.
//If the player hits an enemy with a bullet the scrren will flash and they get 100 points.
//The player cannot leave the boundaries of the screen.
//Varibles for the location of the bullet the player fires.
//I did not place these inside the class as I needed to use these variables in other parts of the program.
float bulletX;
float bulletY;
class Ship {
//Variables which will be used to calculate the position of the ship and the bullets the ship fires.
float shipX;
float shipY;
boolean bulletFired = false;
//These variables are the cooldown for firing your ship's gun.
int cooldown = 0;
boolean cooldownBar = false;
int cooldownBarLength = 0;
//This boolean array is used to determine what keys are currently being pressed.
//This array hold information for keys 'w', 's', 'a', 'd' and the spacebar.
//***I got the idea for using a boolean array to press multiple keys at the same time from user JohnG who posted this information on the processing.org forum.***
boolean[] actionKeys = new boolean[5];
//Constructor for the ship class.
Ship(float tempShipX, float tempShipY, float tempBulletX, float tempBulletY) {
shipX = tempShipX;
shipY = tempShipY;
bulletX = tempBulletX;
bulletY = tempBulletY;
}
//This sets all of the 'actionKeys' array variables to 'false' during the setup of the program.
void setupShip() {
//***I got the idea for using a while statement to complete comlete this task instead of indavidually listing each variable from Daniel Schiffman's book 'Learning Processing'.
int i = 0;
while (i < 5) {
actionKeys[i] = false;
i = i + 1;
}
}
//Checks to see if any of the keys which cause the ship to move are being pressed.
//If this is the case, the boolean valuse for that key will be set to true.
//***I got the idea for using a boolean array to press multiple keys at the same time from user JohnG who posted this information on the processing.org forum.***
void isShipMoving() {
//Checks if 'a' key is pressed.
if (key == 'a') {
actionKeys[0] = true;
}
//Checks if 'd' key is pressed.
if (key == 'd') {
actionKeys[1] = true;
}
//Checks if 'w' key is pressed.
if (key == 'w') {
actionKeys[2] = true;
}
//Checks if 's' key is pressed.
if (key == 's') {
actionKeys[3] = true;
}
//Checks if spacebar is pressed.
if (key == ' ') {
actionKeys[4] = true;
}
}
//Checks to see if any of the keys which cause the ship to move are have been released.
//If this is the case, the boolean valuse for that key will be set to false.
//***I got the idea for using a boolean array to press multiple keys at the same time from user JohnG who posted this information on the processing.org forum.***
void isShipStopping() {
//Checks if 'a' key is released.
if (key == 'a') {
actionKeys[0] = false;
}
//Checks if 'd' key is released.
if (key == 'd') {
actionKeys[1] = false;
}
//Checks if 'w' key is released.
if (key == 'w') {
actionKeys[2] = false;
}
//Checks if 's' key is released.
if (key == 's') {
actionKeys[3] = false;
}
//Checks if spacebar is released.
if (key == ' ') {
actionKeys[4] = false;
}
}
//If a key is being pressed to move the ship, this function will move the ship in that direction until the key is released.
void moveShip() {
//Moves ship left.
if (actionKeys[0] == true) {
shipX = shipX - 4;
}
//Moves ship right.
if (actionKeys[1] == true) {
shipX = shipX + 4;
}
//Moves ship up.
if (actionKeys[2] == true) {
shipY = shipY - 4;
}
//Moves ship down.
if (actionKeys[3] == true) {
shipY = shipY + 4;
}
}
//If the spacebar had been pressed, the ship will fire a bullet.
//If the spacebar is held down the ship will fire a bullet every time the 'bulletcooldown' variable reaches zero.
void fireShip() {
//if the spacebar is pressed this statement will start the process of firing a bullet.
if (actionKeys[4] == true && cooldown <= 0) {
bulletFired = true;
//This makes sure the bullet is shot from the location of the ship.
bulletX = shipX;
bulletY = shipY;
//This cooldown stops the player from firing another bullet.
cooldown = 140;
cooldownBar = true;
}
//This statement actually draws the bullet and moves it upward. It also decreases the 'bulletcooldown' variable so another bullet can be shot.
if (bulletFired == true) {
fill(232, 141, 12);
rect(bulletX - 5, bulletY - 20, bulletX + 5, bulletY - 30);
bulletY = bulletY - 6;
cooldown = cooldown - 2;
}
}
//This function is used to display a bar on the screen which shows the player how long they have to wait before they fire a bullet.
void bulletCooldownTimer() {
//When the player fires a bullet a bar appears on the bottom of the screen which will slowly shrink until there is no bar left.
//This lets the player know they can now fire another bullet.
if (cooldownBar == true) {
fill(200, 20, 20);
rect(400 - cooldown * 3, 380, 400, 390);
}
if (cooldown <= 0) {
cooldownBar = false;
}
}
//This function is used to make sure the ship that the player controls cannot leave the sceen.
//If the player gets to an edge on the scrren they will not be able to move surther in that direction.
void shipBoundaries() {
//Boundaries for the left of the screen.
if (shipX <= 0 + 20) {
shipX = 0 + 20;
}
//Boundaries for the right of the screen.
if (shipX >= 0 + 380) {
shipX = 0 + 380;
}
//Boundaries for the top of the screen.
if (shipY <= 0 + 20) {
shipY = 0 + 20;
}
//Boundaries for the bottom of the screen.
if (shipY >= 0 + 380) {
shipY = 0 + 380;
}
}
//Draws the ship after all of the movements have been made.
void drawShip() {
fill(40, 40, 120);
triangle(shipX - 20, shipY + 20, shipX + 20, shipY + 20, shipX, shipY - 20);
}
}