Beginnings of Classes, Arrays and Object Oriented Programming

In Computational Media, ITP

Last week we we’re introduced to objects, classes and arrays! These are what Space Cannon has been waiting for as I can now treat each bullet as it’s own object. While my sketch is starting to resemble a game, I’ll need to figure out some of the bugs and implement gameState as a next step.


As you can see, there are a couple improvements from the last time:

  • Scrolling background to add motion
  • Array of planets that move through the sketch
  • Ability to shoot multiple bullets
  • keyboard commands for movement are firing ammo

However, there are some issue I’ve encountered along the way:

  • The sketch tends to considerably slow down¬†after it’s been open for a little
  • Sometimes hitmode (the boolean that makes the planet start blinking and fall) does not revert back properly and affects the following planet in the array.
  • Lastly, I can’t seem to get my score to increment simply by 1 when you hit the planet… it behaves very inconsistently.

//code

// Classes

Planet[] myPlanetArray;
int planetNumber = 0;
Ammo[] ammoArray;
int ammoNumber = 0;
Spaceship ship;

// Background variables
float rgbred = 20;
float rgbgreen = 50;
float rgbblue = 70;
float shade = .15;

// Star Dust Variables
float dustx;
float dusty;
float dustPostitionX = 25;
float dustPostitionY = 25;
float smallDustx;
float smallDusty;
float smallDustPostitionX = 37.5;
float smallDustPostitionY = 37.5;

//Planet variables
float planetR = 255;
float planetG = 0;
float planetB = 0;
float planetX = 1000;
float planetY = 80;
float coreWidth = 30;
float coreHeight = 30;
float ringWidth = 50;
float ringHeight =10;
PShape planet, core, ring;
boolean hitmode = false;
// Ammo Variables
float ammoSpeed;

// Spaceship variables
PShape vessel, bubble, disc;
float spaceshipX;
float spaceshipY;
boolean leftArrow = false;
boolean rightArrow = false;
boolean upArrow = false;
boolean downArrow = false;

int scoreCounter= 0;

// SETUP ————————————————————-

void setup() {

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

//Pshape – Planet
planet = createShape(GROUP);
core = createShape(ELLIPSE, 0, 0, coreWidth, coreHeight);
ring = createShape(ARC, -9, 9, ringWidth, ringHeight, radians(-40), radians(228));
planet.addChild(core);
planet.addChild(ring);

size(800, 600);
noCursor();
ellipseMode(CENTER);

PFont scoreFont;
scoreFont = loadFont(“HeroicCondensed-Black-48.vlw”);
textFont(scoreFont);
textAlign(CENTER, CENTER);

spaceshipX = width/2;
spaceshipY = height/2;

myPlanetArray = new Planet[30];
for (int i =0; i < myPlanetArray.length; i++) {
myPlanetArray[i] = new Planet (900, random(40, 560), random(0, 255), random(0, 255), random(0, 255));
}
ship = new Spaceship();

ammoArray = new Ammo [1000];
}

// DRAW ————————————————————-

void draw() {

glowingNightSky();
starDust();

fill(249, 252, 0, 200);
textSize(28);
text(“SCORE”, 40, 30);
fill(249, 252, 0, 200);
textSize(32);
text(“:”, 70, 26);
fill(249, 252, 0, 200);
textSize(28);
text(scoreCounter, 80, 30.22);

for (int i =0; i < myPlanetArray.length; i++) {
Planet thisPlanet = myPlanetArray[i];
thisPlanet.drawIt();
thisPlanet.move ();
}

if (planetX<-30 || planetY>630) {
myPlanetArray [planetNumber] = new Planet(900, random(40, 560), random(0, 255), random(0, 255), random(0, 255));
planetNumber++;
}

for (int i =0; i < ammoArray.length; i++) {
Ammo thisAmmo = ammoArray[i];

if (thisAmmo != null) { //check to see if you put something in this part of the array
thisAmmo.drawIt();
thisAmmo.move();
thisAmmo.resetPlanet();
thisAmmo.checkCollision ();

// if (dist(thisAmmo.ammoX, thisAmmo.ammoY, myPlanetArray.planetX, myPlanetArray.planetY) < 15) {
// coreWidth = coreWidth*.6;
// coreHeight = coreHeight*.6;
// ringHeight = ringHeight*.6;
// ringWidth = ringWidth*.6;
// }
}
}
ship.drawIt();
ship.move();
}
// FUNCTIONS & CLASSES ————————————————————-

void glowingNightSky() {
background(rgbred, rgbgreen, rgbblue);
rgbred = rgbred – shade;
rgbgreen = rgbgreen – shade;
rgbblue = rgbblue – shade;

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

void starDust() {

for (float smallDusty = smallDustPostitionY; smallDusty < height; smallDusty = smallDusty + 50) {
for (float smallDustx = smallDustPostitionX; smallDustx < width; smallDustx = smallDustx + 50) {
fill(250, 255, 221, 200);
noStroke();
strokeWeight(1);
ellipse(smallDustx, smallDusty, 1, 1);
}
}
smallDustPostitionX=smallDustPostitionX -.6;
for (float dusty = dustPostitionY; dusty < height; dusty = dusty + 25) {
for (float dustx = dustPostitionX; dustx < width; dustx = dustx + 25) {
fill(250, 255, 221, random(100, 200));
stroke(0);
strokeWeight(1);
ellipse(dustx, dusty, 3, 3);
}
}
dustPostitionX=dustPostitionX-1.5;
}

void keyPressed() {
if (keyCode==32) {
ammoArray[ammoNumber] = new Ammo (spaceshipX, spaceshipY);
ammoNumber++;
}

if (keyCode==37) leftArrow = true;
if (keyCode==38) upArrow = true;
if (keyCode==39) rightArrow = true;
if (keyCode==40) downArrow = true;
}

void keyReleased() {
if (keyCode == 39) rightArrow = false;
if (keyCode == 37) leftArrow = false;
if (keyCode == 38) upArrow = false;
if (keyCode == 40) downArrow = false;
}

class Planet {
Planet(float _x, float _y, float _r, float _b, float _g) {
planetX = _x;
planetY = _y;
planetR = _r;
planetB = _b;
planetG = _g;
}

void drawIt() {

smooth();
core.setStroke(false);
core.setFill(color(planetR, planetB, planetG));
ring.setStroke(color(249, 252, 0));
ring.setStrokeWeight(3);
ring.setFill(false);
shape(planet, planetX, planetY);

if (hitmode==true) {
planetR = random(0, 255);
planetG = random(0, 255);
planetB = random(0, 255);
planetY=planetY+.2;
}
}

void move () {
planetX = planetX – .18;
}

}
class Spaceship {

void drawIt () {
bubble.setFill(color(202, 75, 195));
disc.setFill(color(107, 48, 188));
shape(vessel, spaceshipX, spaceshipY);
}

void move () {
if (leftArrow) spaceshipX = spaceshipX-5.5;
if (upArrow) spaceshipY = spaceshipY-5.5;
if (rightArrow)spaceshipX = spaceshipX+5.5;
if (downArrow) spaceshipY = spaceshipY+5.5;
}
}

class Ammo {
float ammoSpeed=16;
float ammoXDir = 1;
float ammoX;
float ammoY;
float ammoSize =6;
boolean ammoLive = true;

Ammo (float _x, float _y) {
ammoX = _x;
ammoY = _y;
}

void move () {
ammoX = ammoX + ammoXDir*ammoSpeed;
}

void drawIt () {
if (ammoLive) {
fill(249, 252, 0);
noStroke();
ellipse (ammoX, ammoY, ammoSize, ammoSize);
}
}

void checkCollision () {

if (ammoX-ammoSize/2>= (planetX-ammoSize/2+25)) {
if (ammoY>planetY-coreHeight/2+15 && ammoY<planetY+coreHeight/2+15) {

scoreCounter++;
hitmode = true;
ammoLive = false;

}
}
}

void resetPlanet () {
if (planetX<-5 || planetY>605 || planetX>800) {
hitmode =false;
}
}

}

Submit a comment