//BOATS! A One Button Game
//Press, hold, release spacebar to shoot
Ball cannonball;
Boat sailboat;
Boat pirateboat;
float cannonAngle = 5.4;
float fireAngle;
float cannonInc = 0.008;
float direction = -1;
float xCannon = 1;
float yCannon = -1;
float cannonPower;
float z;
float firePower;
float barPower;
float timePressed = 0;
float timeReleased = 0;
//ocean
float ang = 0;
float scaleVal = 10;
float oceanDirection = -1;
PImage shipImage;
PImage sail;
PImage pirate;
PImage piratedamage1;
PImage piratedamage2;
PImage piratedamage3;
PImage piratesink;
PFont font;
PFont large;
PFont small;
int mode = 1;
int cannonX = 100;
int yOcean = 300; //set height of water
boolean fired = false;
boolean counted = false;
boolean badguy = true;
boolean alreadyPressed = false;
void setup() {
size(600, 400);
noStroke();
noCursor();
smooth();
fill(0);
//images
sail = loadImage("sail.png");
pirate = loadImage("pirate.png");
piratedamage1 = loadImage("piratedamage1.png");
piratedamage2 = loadImage("piratedamage2.png");
piratedamage3 = loadImage("piratedamage3.png");
piratesink = loadImage("piratesink.png");
// fonts
font = loadFont("DIN-Medium-25.vlw");
small = loadFont("DIN-Medium-16.vlw" );
large = loadFont("DIN-Medium-40.vlw");
//cannonball
cannonball = new Ball(firePower*cos(fireAngle), firePower*sin(fireAngle));
//boats
sailboat = new Boat(sail, 50, 205, PI, 0.1, false);
pirateboat = new Boat (pirate, 400, 205, PI/16, 0.6, true);
} //end void setup
void draw() {
background(255);
if (mode == 2) {
if (pirateboat.hit > 0) {
background (255-50*pirateboat.hit);
}
}
//ocean
float ang2 = 0;
for (int j = 0; j < width; j +=5) {
ang2 += PI/50.0;
stroke(69, 135, 220);
strokeWeight(3);
float k = sin(ang + ang2) * scaleVal;
line(j, height, j, yOcean - k);
}
ang += PI/128;
stroke(0);
if (mode == 1) {
sailboat.update();
sailboat.display();
}
println(mode);
// GAME MOADE
if (mode == 2) {
barPower= constrain((millis()-timePressed)/20, 0, 200);
if(keyPressed == true) {
noStroke();
fill(255, 180);
rect(200, 350, barPower, 10);
}
fill(0);
stroke(0);
float m = map(fireAngle, 4.9, 5.8, 20, 10);
float n = map (fireAngle, 4.9, 5.8, 20, 5);
//line(pirateboat.posx - 10, 0, pirateboat.posx - 10, 400);
//line(pirateboat.posx + 120, 0, pirateboat.posx + 120, 400);
//line( 0, yOcean + 10 - m, 600, yOcean + 10 - m);
//line(0, yOcean + 10, 600, yOcean + 10);
counted = false;
//println(int(cannonball.x) + ";" + int(cannonball.y));
//println(counted);
if (cannonball.y > yOcean + 10- m && cannonball.y < yOcean + 10 ) {
println("y");
println(int(pirateboat.posx- 10) + ";" + int(pirateboat.posx +120));
if ((cannonball.x > pirateboat.posx -n) && (cannonball.x < pirateboat.posx + 110 + n)){
println("z");
//println(pirateboat.hit);
if (counted == false) {
pirateboat.hit += 1;
counted = true;
}
}
}
//boats
sailboat.update();
sailboat.display();
pirateboat.update();
pirateboat.display();
}
if (mode == 3) {
sailboat.update();
sailboat.display();
}
} // end void draw
void keyPressed () {
if (key == 32 && alreadyPressed == false) {
if (mode == 1 || mode == 3) {
mode = 2;
}
timePressed = millis();
cannonInc = 0; // stop cannon movement to aim
fireAngle = cannonAngle; // set angle
fired = false; // let it reload
}
alreadyPressed = true;
//test!! delete me after you get collision test
if(key == 'h') {
pirateboat.hit += 1;
pirateboat.wobbleAngle += 0.3;
}
if (key == 'g') {
pirateboat.hit -= 1;
}
} // end void keyPressed
void keyReleased () {
if (key == 32) {
timeReleased = (millis() - timePressed);
cannonPower = constrain(timeReleased/100, 0.5, 15);
firePower = cannonPower; // set magnitude
//println(timePressed);
//println(cos(fireAngle));
//println(millis());
fired = true; // ready to fire
cannonInc = 0.01; //restore cannon movement
println(cannonPower);
cannonball.reload(firePower*cos(fireAngle), firePower*sin(fireAngle));
}
alreadyPressed = false;
} // end void keyReleased
class Ball {
float x = cannonX; //set where cannon is
float y = yOcean;
float vx ; //= 100; //* sin(fireAngle);
float vy ; //= 100; // * cos(fireAngle);
float radius;
float gravity = 0.3;
Ball (float velx, float vely) {
vx = velx;
vy = vely;
} //end Ball
void reload(float velx, float vely) {
// x = cannonX; //set where cannon is
// y = yOcean;
x = 0;
y = 0;
vx = velx;
vy = vely;
} // end void reload
void update() {
vy = vy + gravity;
y += vy;
x += vx;
} // end void update
void display() {
if (y < 0) {
ellipse (x, y, 12, 12);
}
} // end void display
} // end class Ball
class Boat {
float posx;
float posy;
float tilt;
float wobbleAngle;
float speed;
float d = -1;
PImage shipImage;
boolean badguy;
int hit = 0;
Boat (PImage boat, float x, float y, float a, float sp, boolean pirate ){
posx = x;
posy = y;
wobbleAngle = a;
speed = sp;
shipImage = boat;
badguy = pirate;
} //end Boat
void update() {
tilt = cos (wobbleAngle)/16;
wobbleAngle +=0.08;
if (hit ==4) {
z += 0.5;
}
} // end void update
void display () {
pushMatrix();
translate(posx, posy);
rotate(tilt);
int vibrate = 0;
if (hit == 0) {
image(shipImage, 0, 0);
}
else if (hit == 1) {
vibrate = 2;
image(piratedamage1, random(0-vibrate, 0+vibrate), random (0-vibrate, 0+vibrate));
}
else if (hit == 2) {
vibrate = 4;
image(piratedamage2, random(0-vibrate, 0+vibrate), random (-5-vibrate, -5+vibrate));
}
else if (hit == 3) {
vibrate = 6;
image(piratedamage3, random(0-vibrate, 0+vibrate), random (-15-vibrate, -15+vibrate));
}
else if (hit == 4) {
pushMatrix();
translate(60, 60);
rotate(PI/8);
pirateboat.speed = 0;
image(piratesink, 0, -10 + z);
popMatrix();
}
if (mode == 1) {
textFont(font);
textAlign(CENTER);
fill(0);
textFont(large);
//rotate(tilt + PI/16);
text("BOATS: A One Button Game", 250, -55);
textFont(small);
//textAlign(LEFT);
//text ("press spacebar to aim \n hold spacebar to charge \n release spacebar to shoot", 150, 0);
text ("press & hold spacebar to aim & charge, release spacebar to shoot", 246, -27);
textFont(font);
fill(153, 51, 51);
textAlign(CENTER);
text ("press spacebar to begin", 251, 45);
}
if (mode == 2 && z > 80) {
mode = 3;
}
if (mode == 3) {
//if (badguy == false) {
pirateboat.hit = 0;
textFont(font);
fill(153, 51, 51);
text("play again? press spacebar to restart", 322, 53);
z = 0;
println(z);
// }
}
cannonAngle += cannonInc * direction;
if (cannonAngle < 4.9 || cannonAngle > 5.8) { //set range for cannon
direction *= -1;
}
if (badguy == false) {
pushMatrix(); // cannon
translate(60, 100);
rotate(cannonAngle);
stroke(0);
strokeWeight(7);
line(0, 0, 20, 0);
popMatrix();
pushMatrix(); // cannonball
translate(60, 100);
cannonball.update();
cannonball.display();
popMatrix();
} //end sailboat specific
popMatrix();
posx += speed * d;
if ((posx > width - 170) || (posx < width/2)) {
d = -d;
}
}
} // end void display
Tiffany - Project 2
on Sunday, Nov 16, 2008 – 12:36 pm
One Comment
Tiffany, please get the code online. I can’t grade this without it.