Skip to content
16 changes: 16 additions & 0 deletions raindropGameCode/building.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Building {
float h, w, x; //height, width, x coordinate

Building() {
h = random(40, 120); //height varies between 40 and 120
w = random(10, 20); //width varies betweeen 10 and 20
x = random(width); //building can have any x coordinate on the canvas
}

void build() {
noStroke(); //no stroke
fill(100, 200, 100); //greenish fill
rectMode(CORNERS); //draw from bottom left corner
rect(x, height, x+w, height-h); //building with specified h/w/x on the bottom of canvas
}
}
25 changes: 25 additions & 0 deletions raindropGameCode/missile_class.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Missile {
PVector loc, vel; //location and velocity
float diam, locx, locy; //diameter, x, y

Missile(float locx, float locy) { //x & y coordinates are parameters
loc = new PVector(locx, locy); //pvector at locx, locy
vel = new PVector((random(-3, 3)), random(6, 8)); //velocity is random and has a greater y magnitude than x magnitude
diam = 10; //diameter of 10
}

void display() {
noStroke(); //no stroke
fill(230, 230, 255); //light blue
ellipse(loc.x, loc.y, diam, diam); //missiles are little circles
}

void fire() {
loc.add(vel); //move at vel
}

void deflected() {
vel.y = -vel.y; //missile bounces
display();
}
}
93 changes: 74 additions & 19 deletions raindropGameCode/raindropGameCode.pde
Original file line number Diff line number Diff line change
@@ -1,26 +1,81 @@
PVector mouse; //declare a P
Raindrop r; //declare a new Raindrop called r

// On your own, create an array of Raindrop objects instead of just one
// Use the array instead of the single object
// You can start out by just using the single Raindrop as you test

ArrayList<Missile> m = new ArrayList<Missile>(); //creates array list of missiles
ArrayList<Building> city = new ArrayList<Building>(); //creates array list of buildings
Shield s = new Shield(mouseX, 400); //creates shield
float runtime; //these two variables deal with the score
float score;

void setup() {
size(1200, 800);
mouse = new PVector(); //initialize mouse PVector. value is irrelevant since it will be set at the start of void draw(){}
r = new Raindrop(random(width), 0); //Initialize r. The parameters used are the initial x and y positions
size(1200, 600); //canvas 1200 by 600
m.add(new Missile(random(width), 0));
score = 0; //these two lines ensure score starts at zero
runtime = 0;
while (city.size() <= 20) { //start game with 20 buildings
city.add(new Building());
}
}

void draw() {
mouse.set(mouseX, mouseY); //set value of mouse as mouseX,mouseY
background(0, 200, 255);
r.fall(); //make the raindrop fall. It should accelerate as if pulled towards the ground by earth's gravity
r.display(); //display the raindrop
if (r.isInContactWith(mouse)) { //check to see if the raindrop is in contact with the point represented by the PVector called mouse
r.reset(); //if it is, reset the raindrop
background(0); //black background
runtime += frameRate/1000; //score is based on frame rate and will increase consistent with time
score += runtime;
fill(255); //white fill
textSize(20); //size 20 text
text("Surviving Buildings:", 0, 25); //displays how many buildings remain
text(city.size(), 0, 50);
text("Score:", width-100, 25); //displays score
text(score, width-100, 50);
s.show(); //draws shield
for (int i = 0; i <city.size(); i++) {
Building b = city.get(i); //build the buildings from the array list
b.build();
}
if (r.loc.y > height + r.diam/2) { //check to see if the raindrop goes below the bottom of the screen
r.reset(); //if it does, reset the raindrop
for (int i = m.size()-1; i >= 0; i--) {
Missile miss = m.get(i);
if (m.size() <=10) {
m.add(new Missile(random(width), 0)); //ensures there are always 10 missiles on screen
}
miss.display(); //draw missile
miss.fire(); //fire (move) missile
if (miss.loc.x > s.loc.x-s.notdiam/2 && miss.loc.x < s.loc.x+s.notdiam/2 && miss.loc.y > s.loc.y-s.notdiam1/2 && miss.loc.y < 400) {
miss.deflected(); //missile bounces off shield if it hits it
s.isHit(); //shield flashes red when hit
}
if (miss.loc.y < 0 || miss.loc.y >=height) {
m.remove(i); //if the missile gets to the bottom, it's removed
}
for (int j = city.size()-1; j >= 0; j--) {
Building target = city.get(j);
if (miss.loc.y >= height-target.h && miss.loc.x >= target.x && miss.loc.x <= target.x+target.w) {
city.remove(j); //building is destroyed if it gets hit by a missile
score -= 500000/millis(); //penalty for losing a building
}
}
}
}
if (city.size() <= 5 && city.size() > 1) { //shield shrinks once only 5 buildings remain
s.notdiam = 75;
s.notdiam1 = 50;
}
if (city.size() == 1) { //with one building left, it gets way harder
s.notdiam = 50;
s.notdiam1 = 25;
}
if (city.size() == 0) { //if all the buildings have been hit
runtime*=0; //score should stop increasing (not sure why it still does???)
background(0); //black background
fill(255, 0, 0); //red fill
textSize(50); //size 50 text
text("game over they ded", width/2, height/2); //helpfully inform the player that the entire city has died a horrible death
textSize(10); //size 10 text
text("press 'r' key to restart", width/2, height-100); //tell player to restart
textSize(20); //size 20 text
text("Final score:", 0, 25); //show the player their score
text(score, 0, 50);
if (keyPressed && key == 'r') { //if the player wants to embark on this quixotic mission again
score = 0; //score resets to 0
runtime = 0;
while (city.size() <= 20) { //city is built again
city.add(new Building());
}
}
}
}
26 changes: 26 additions & 0 deletions raindropGameCode/shield_class.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Shield {
PVector loc; //location vector
float notdiam, notdiam1, locx, locy; //diameters and coordinates

Shield(float locx, float locy) { //x & y coordinates are parameters
loc = new PVector(locx, locy); //location is at locx, locy
notdiam = 200; //horizontal diameter is 200
notdiam1 = 100; //vertical diameter is 100
}

void show() {
fill(0); //shield is just an arc
loc.set(mouseX, 400); //location is always at mouse x and y of 400
stroke(200, 200, 255); //light blue
strokeWeight(5); //stroke weight of 5
arc(loc.x, loc.y, notdiam, notdiam1, PI, TWO_PI); //180 degree arc
}

void isHit(){
fill(0); //shield is still just an arc
loc.set(mouseX, 400); //location does not change
stroke(255, 0, 0); //shield flashes bright red
strokeWeight(5); //stroke weight does not change
arc(loc.x, loc.y, notdiam, notdiam1, PI, TWO_PI); //shape does not change
}
}