Friday, 22 January 2010

2nd Program

For the 2nd program I have gone for a space battle simulation. The target ship moves across the screen, displaying its coordinates. The target sensor can be moved by the mouse and also displays its current coordinates.

Left click to fire
Right click for a new ship





/**
* Chunk 21 program for demonstrating coordinates.
* The program simulates a space battle where the current coordinates
* of the target sensor and the target ship are displayed.
* If, on clicking the left mouse button, the coordinates of the click
are
* approximate to that of the ship, 1 is scored. Click the right mouse
* button for a new ship
* @author Antony Lees
*/

// colours used in the program
int white = color(255);
int black = color(0);
// ship direction
float xDirection;
float yDirection;
// coordinates of the ship
float[] shipCoordinates;
// whether the ship should be shown
boolean showShip;
// current score
int score;

void setup() {
// screen size
size (600,400);
// black background
background(black);
// white lines
stroke(white);
// smooth edges
smooth();
// draw rectangles from the centre
rectMode(CENTER);
// load font for the text
hint(ENABLE_NATIVE_FONTS);
PFont font = loadFont();
textFont(font);
// initialise the ship
initialiseShip();
// set score to 0
score = 0;
}

/**
* Initialise the ship for display
*/
void initialiseShip() {
// choose random direction
xDirection = random(-2, 2);
yDirection = random(-2, 2);
// start in the middle of the screen
shipCoordinates = new float[]{width/2, height/2};
// set the ship to be shown
showShip = true;
}

/**
* Loads a font for the text
* @return the font
*/
PFont loadFont() {
// get all the available font names
String[] fonts = PFont.list();
// try and find Times New Roman
for (int i = 0; i < fonts.length; i++) {
if (fonts[i].equals("Times New Roman")) {
// return the font, size 12
return createFont("Times New Roman", 12);
}
}
// if we got here, Times New Roman wasn't there
// just load the first one, size 12
return createFont(fonts[0], 12);
}

void draw() {
// redraw the background
background(black);
// display the current mouse coordinates
text(mouseX + "," + mouseY, mouseX, mouseY);
// draw the targeting lines so that they leave a space around the mouse pointer
line(0, 0, mouseX - 20, mouseY- 20);
line(width, 0, mouseX + 20, mouseY - 20);
line(0, height, mouseX - 20, mouseY + 20);
line(width, height, mouseX + 20, mouseY + 20);
// display the ship
drawSpaceship();
// display the score
displayScore();
}

/**
* Draws the ship if required, and controls whether it has left the
screen or not
*/
void drawSpaceship() {
// if we should show the ship
if (showShip) {
// draw the ship
rect(shipCoordinates[0], shipCoordinates[1], 5, 5);
line(shipCoordinates[0] - 10, shipCoordinates[1] - 10,
shipCoordinates[0] - 10, shipCoordinates[1] + 10);
line(shipCoordinates[0] + 10, shipCoordinates[1] - 10,
shipCoordinates[0] + 10, shipCoordinates[1] + 10);
line(shipCoordinates[0], shipCoordinates[1], shipCoordinates[0]
- 10, shipCoordinates[1]);
line(shipCoordinates[0], shipCoordinates[1], shipCoordinates[0]
+ 10, shipCoordinates[1]);
// display its coordinates
text((int) shipCoordinates[0] + "," + (int) shipCoordinates[1],
shipCoordinates[0], shipCoordinates[1] + 25);
// update coordinates for next time
shipCoordinates[0] = shipCoordinates[0] + xDirection;
shipCoordinates[1] = shipCoordinates[1] + yDirection;
}
// if current coordinates mean the ship has left the screen
if (shipCoordinates[0] < 0 || shipCoordinates[0] > width ||
shipCoordinates[1] < 0 || shipCoordinates[1] > height) {
// set to not be shown next time
showShip = false;
}

}

/**
* Display the current score
*/
void displayScore() {
// display near the top of the screen
text("Score: " + score, width/2, 10);
}


void mouseClicked() {
// if the left mouse button was clicked
if (mouseButton == LEFT) {
// if the button was pressed in the regiomn of the ship
if (mouseX > (shipCoordinates[0] - 10) && mouseX <
(shipCoordinates[0] + 10)) {
// add 1 to the score
score++;
// set the ship to not be shown
showShip = false;
}
}

// if the right mouse button was clicked
// and we aren't currently displaying the ship
if (mouseButton == RIGHT && showShip == false) {
// create a new ship
initialiseShip();
}
}





No comments:

Post a Comment