For Loops, If Statements and a Couple Booleans Too

In Computational Media, ITP

I’ve continued on my mission to build out Space Cannon. Using the concepts we learned in Week 3, I added some event based animation using for loops and if statements. Booleans also became my best friend.


 

SpaceCannon_V2_fullgif

Now you can see that when the space ship fires a bullet at the planet, it bounces off. If you hit it in the middle, it will bounce directly back in your direction; however if you hit it at the top, the bullet will bounce in an upward direction. The opposite goes if you hit it on the bottom of the planet.

When the bullet hits the planet, a couple things occur:

  • The star dust environment goes into the chaotic war mode
  • an enemy space ship appears
  • the planet changes color

Lastly, I removed the cursor which was an eye soar. I look forward to implementing some object oriented programming next week so I can individualize the properties of the enemy space ships and bullets.

// COde

// Background variables
float rgbred = 20;
float rgbgreen = 50;
float rgbblue = 70;
float shade = .15;
float dustx;
float dusty;
float dustPostitionX = 20;
float dustPostitionY = 20;
boolean warMode = false;

//Planet variables
float planetR = 255;
float planetG = 0;
float planetB = 0;
float planetX = 700;
float planetY = 80;
float planetWidth = 30;
float planetHeight = 30;

// Ammo Variables
float ammospeed = 0;
float ammoangle = 0;
float ammosize = 4;
float ammoX =0;
float ammoY=0;
float startX;
float startY;

// Mouse Click Variables
boolean ammoactivated = false;

// Direction variable
boolean ammoRight = true;

// Spaceship variables
PShape spaceship, bubble, ship;
boolean enemyShip = false;
float enemyStartX = 800;
float enemyStartY = 500;

void setup() {
size(800, 600);
noCursor();
ellipseMode(CENTER);

//Pshape – spaceship
spaceship = createShape(GROUP);
bubble = createShape(ARC, -10, -12, 20, 25, -PI, 0, CHORD);
ship = createShape(ELLIPSE, -25, -10, 50, 20);
spaceship.addChild(ship);
spaceship.addChild(bubble);
}

void draw() {

//Glowing Night Sky
background(rgbred, rgbgreen, rgbblue);

rgbred = rgbred – shade;
rgbgreen = rgbgreen – shade;
rgbblue = rgbblue – shade;

if (rgbred < 1 || rgbred > 25) {
shade = shade * -1;
}

//Star Dust

for (float dusty = dustPostitionX; dusty < height; dusty = dusty + 20) {
for (float dustx = dustPostitionY; dustx < width; dustx = dustx + 20) {
fill(250, 255, 221, random(100, 200));
stroke(0);
strokeWeight(1);
ellipse(dustx, dusty, 3, 3);
}
}

//Planet

noStroke();
fill(planetR, planetG, planetB);
ellipse(planetX, planetY, planetWidth, planetHeight); // planet
smooth();
stroke(249, 252, 0);
strokeWeight(3);
noFill();
arc(700, 80, 50, 10, radians(-51), radians(231));

//Ammo

if (ammoactivated) {
ammoX = startX + ammospeed;
ammoY = startY + ammoangle;
if (ammoRight == true) {
ammospeed = ammospeed + 16 ;
} else if (ammoRight == false) {
ammospeed = ammospeed – 16;

if (ammoY>planetY) {
ammoangle = ammoangle + 2;
}

if (ammoY<planetY) {
ammoangle = ammoangle – 2;
}
}
fill(249, 252, 0);
ellipse (ammoX, ammoY, ammosize, ammosize);
}

if (enemyShip==true) {
enemyStartX = enemyStartX – 12;
enemyStartY = enemyStartY +2;
bubble.setFill(color(random(0, 255), random(0, 255), random(0, 255)));
ship.setFill(color(random(0, 255), random(0, 255), random(0, 255)));
shape(spaceship, enemyStartX, enemyStartY);
}

if (warMode==true) {
dustPostitionX = random(-20, 0) + dustPostitionX;
dustPostitionY = random(-20, 0) + dustPostitionY;
}
if (ammoX+ammosize/2>= (planetX-planetWidth)) {
if (ammoY>planetY-planetHeight/2 && ammoY<planetY+planetHeight/2) {
ammoRight = false;

planetR = random(0, 255);
planetG = random(0, 255);
planetB = random(0, 255);

enemyShip = true;
warMode = true;
}
}

if (ammoX>width || ammoX<0) {
ammoactivated = false;
ammoRight = true;
ammoangle = 0;
ammoX = 0;
ammoY= mouseY;
ammospeed = 0;
}

if (enemyStartX>width || enemyStartX<0) {
dustPostitionX = 20;
dustPostitionY = 20;
warMode = false;

}

// My Spaceship
bubble.setFill(color(202, 75, 195));
ship.setFill(color(107, 48, 188));
shape(spaceship, mouseX, mouseY);
}

void mouseClicked() {
ammoactivated = true;
ammoRight = true;
enemyShip = false;
warMode = false;
enemyStartX = 800;
enemyStartY = 250;
dustPostitionX = 20;
dustPostitionY = 20;
ammoangle = 0;
ammoX = 0;
ammospeed = 0;
startX = mouseX;
startY = mouseY;
}

Submit a comment