/*Created by Justin Sennema
Goal of the game is to catch the bunny in a trap
catch as many as you can.
originally I wanted 2 sets of moving pegs,
but calculating of the ball hit the first beg at the right angle to
latch to a back peg proved to be too complicated without the use of object
*/
void setup() {
//set up canvas size
size(400, 400);
//set up rec and ellipse modes
rectMode(CORNERS);
ellipseMode(CENTER);
//set framerate to provide best movement
frameRate(144);
background(0);
stroke(255);
//instructions
println("Left and right arrow keys to aim, space bar to fire the trap. Hit the pegs! Not the bunny");
}
//variables, most are self explanitory
float r = random(0.2, 2);
float angle=90;
boolean trapLaunched = false;
boolean hit = false;
float trapX;
float trapY;
float x;
//variables for timer
int last = 0;
int m = 0;
int t=0;
//position of hit peg
int hitPegX=0;
//bunny position x and y
float bpX = 400;
float bpY = 100;
//score
int capturePoints=0;
//array to hold the x position of all the pegs
float[] Pegs = new float[10];
//methed to reset for the next shot
void resetBools(){
trapLaunched = false;
hit = false;
}
//main game loop
void draw() {
background(0);
drawBunny();
updateBunny();
drawArrow(angle);
drawPegs();
//if the trap is launched but hasn't hit
if (trapLaunched && !hit) {
launchBall();//launch the ball
//calulate the balls position based on angle. the '2' is the speed
trapX+=2*cos(radians(angle));
trapY-=2*sin(radians(angle));
//loop to check for a hit on all the pegs
for (int i = 0; i <10; i++) {
if (trapX < Pegs[i]+10 && trapX > Pegs[i]-10 && trapY >= 140 && trapY <= 170) {
hitPegX=(int)Pegs[i];
hit = true;
}
}
}
// checking id the peg hit
if (hit) {
//timer to see how long the trap stays off
//it's based on the framerate which is a bad idea, but should be fine in this case as I've capped the frame rate and the game should never have trouble reaching 144fps
t++;
if (t > 200){
resetBools();
t = 0;
}
//if the bunny passes over the trap, add a point, reset the rabbit, etc
if (bpX-5 <= (int)(hitPegX + 90*cos(radians(angle))) && bpX+5 >= (int)(hitPegX + 90*cos(radians(angle)))){
capturePoints++;
bpX = 400;
resetBools();
t=0;
}
//drawing the trap
fill(255);
strokeWeight(2);
line(hitPegX, 150, (int)(hitPegX + 90*cos(radians(angle))), 50);
} else {
hit = false;
}
fill(255);
rect(0,0, 400, 50);
fill(0);
text("Bunnies Captured: " + capturePoints, 20, 20);
fill(255);
}
//bunny movement. Random every pass
void updateBunny(){
bpX-=r;
if(bpX < -50){
r = random(0.2, 2);
bpX = 400;
}
}
//drawing the bunny //<>//
void drawBunny(){
fill(255);
strokeWeight(0);
ellipse(bpX-20, bpY-20,20,20);
ellipse(bpX-15,bpY-30,7,30);
ellipse(bpX-25,bpY-30,7,30);
ellipse (bpX-2,bpY-15,40,25);
ellipse(bpX-15,bpY-5,4,10);
ellipse(bpX-10,bpY-2,4,10);
ellipse(bpX+3, bpY-2,10,6);
ellipse(bpX+12, bpY-3,12,9);
ellipse(bpX+15, bpY-8,6,10);
ellipse (bpX+19,bpY-19,10,10);
strokeWeight(1);
}
//drawing the pegs
void drawPegs() {
if (!hit) {
//timer for pegs movement
m = millis()-last;
if (millis() > last+5000) {
last = millis();
m=0;
}
}
//for loop to draw them
for (float f = 0; f<500; f+=50) {
Pegs[(int)f/50] = f+m/50;
ellipse(Pegs[(int)f/50]-100, 150, 5, 5);
}
}
//launching the ball
void launchBall() {
ellipse(trapX, trapY, 10, 10);
if (trapX > width || trapX < 0 || trapY < 0) {
trapLaunched = false;
}
}
//drawing the shooting line
void drawArrow(float angle) {
strokeWeight(5);
line(width/2, height, (width/2) + 35*cos(radians(angle)), height - 35 *sin(radians(angle)));
strokeWeight(1);
}
//key release for shooting
void keyReleased() {
if (keyCode == ' ' && trapLaunched == false) {
trapLaunched = true;
trapX = (width/2) + 35*cos(radians(angle));
trapY = height-60+35*sin(radians(angle));
}
}
//keys for changing angle with caps
void keyPressed() {
if (key == CODED && trapLaunched == false) {
if (keyCode == LEFT) {
if (angle < 135) {
angle++;
}
} else if (keyCode == RIGHT) {
if (angle > 45) {
angle--;
}
}
}
}