Tuesday 7 April 2009

Chunk 13 2nd Program

Finally finished the 2nd program for chunk 13 - PirateShipArmada - hehe
Code and screenshot below






/**
*
* Example of while statements for chunk 13

* Displays a number of pirate ships that move across the screen at different
* speeds
*
* @author Antony Lees
*/

// wave directions
int UP = 1;
int DOWN = -1;

// pirate ship variables
PirateShip[] ships;
int waveHeight;
int waveDirection;
int shipCount;


void setup() {
// set the size of the window
size(600, 400);

// initialise the variables
shipCount = 10;
waveHeight = 20;
waveDirection = UP;

// create the array of ships using a for statement (section 12)
ships = new PirateShip[shipCount];
for (int i = 0; i < ships.length; i++) {
ships[i] = new PirateShip();
}
// smooth pixels
smooth();
}

void draw() {
// determine of the vwaes should be going up or down
// using a conditional statement (section 10)
if (waveHeight <= 5) {
waveDirection = UP;
}

if (waveHeight >= 20) {
waveDirection = DOWN;
}
// light blue background
background(102, 205, 170);

// waves
noFill();
// wave y coordinate
int waveY = 10;
// while the y coordinate is less than the height of the window
while (waveY <= height) {
// wave x coordinate
int waveX = 0;
// while the x coordinate is less than the width of the window
while(waveX <= width) {
// draw a semi=circle arc
arc(waveX, waveY, 20, waveHeight, 0, PI);
// increase the x coordinate by 20
waveX += 20;
}
// increase the y coordinate by 20
waveY += 20;
}

// move and draw ships while there are more ships to do
int currentShip = 0;
while (currentShip < ships.length) {
ships[currentShip].move();
ships[currentShip].display();
currentShip++;
}
// change the wave height for next time
waveHeight += waveDirection;

}

/**
* Pirate ship
*
* @author Antony Lees
*/
class PirateShip {
// pirate ship variables
float xPosition;
float yPosition;
float speed;
float shipLength;
int sails;
int sailColour;
int[] availableSailColours;
int portholeCount;
float sailSpacing;

/**
* Creates a new pirate ship
*/
PirateShip() {

// set up the sail colours - done like this for processing.js support
availableSailColours = new int[4];
availableSailColours[0] = color(0, 0, 0);
availableSailColours[0] = color(255, 36, 0);
availableSailColours[0] = color(255, 255, 0);
availableSailColours[0] = color(235, 235, 235);
// initialise the ship
initialise();
}

/**
* Initialise the ship
*/
void initialise() {
// initialise the ship at a random-ish location
xPosition = random(600, 700);
yPosition = random(50, 350);
shipLength = random(40, 100);
// determine the speed and number of sails based on the length
speed = shipLength / 20;
sails = min(((int) shipLength / 30), 3);
// select a sail colour from the list
sailColour = availableSailColours[(int) random(0, availableSailColours.length)];
portholeCount = (int) shipLength/12;
sailSpacing = floor(shipLength / 3);
}

/**
* Move the ship by the speed number
*/
void move() {
xPosition -= speed;
}

/**
* Do all the work of displaying the ship
*/
void display() {
// calculate ship coordinates
float frontOfShipXPosition = xPosition - (shipLength / 2);
float backOfShipXPosition = xPosition + shipLength + (shipLength / 3);

// ship hull
// brown lines and fill
stroke(139, 71, 38);
fill(139, 71, 38);
// middle rectangle
rect(xPosition, yPosition, shipLength, 20);
// front and back
triangle(frontOfShipXPosition, yPosition, xPosition, yPosition, xPosition, yPosition + 20);
triangle(xPosition + shipLength, yPosition, backOfShipXPosition, yPosition, xPosition + shipLength, yPosition + 20);

// draw the sails
float initialSailXPosition = xPosition + 15;
// black lines
stroke(0);
for (int i = 0; i < sails; i++) {
// sail
fill(sailColour);
quad(initialSailXPosition + (sailSpacing * i), yPosition - 40, initialSailXPosition + (sailSpacing * i) + 10, yPosition - 40, initialSailXPosition + (sailSpacing * i) + 20, yPosition - 10, initialSailXPosition + (sailSpacing * i), yPosition - 10);
// mast
fill(0);
line(initialSailXPosition + (sailSpacing * i), yPosition - 10, initialSailXPosition + (sailSpacing * i), yPosition);
line(initialSailXPosition + (sailSpacing * i), yPosition - 40, initialSailXPosition + (sailSpacing * i), yPosition - 50);
// flag
fill(0);
rect(initialSailXPosition + (sailSpacing * i), yPosition - 50, 10, 5);
// jolly roger
stroke(255);
line(initialSailXPosition + (sailSpacing * i) + 2, yPosition - 49, initialSailXPosition + (sailSpacing * i) + 8, yPosition - 46);
line(initialSailXPosition + (sailSpacing * i) + 8, yPosition - 49, initialSailXPosition + (sailSpacing * i) + 2, yPosition - 46);
stroke(0);
}

// draw the portholes
for (int i = 0; i < portholeCount; i++) {
// portholes
fill(0);
ellipse(xPosition+(15*i),yPosition+10,5,5);
}

// ship has left the screen
if (backOfShipXPosition <= 0) {
// start again
initialise();
}

}
}

1 comment:

Anonymous said...

I just left a comment on your other blog saying how cool the pseudo console inserts look for displaying the code, unfortunately here quite a bit of the code is clipped off. Now I know I could still copy the code, but it would be nice to see it all on the screen.