/////////////////////////////////////*BEE BOUNCE!- Assignment 2: Interactive Toy*/////////////////////////////////////
////*Purpose and Functionality: Jump accross the flower field by jumping on flowers, try not to miss and fall! *////
///////////////////////////////////*Controls: Use RIGHT Key to Bounce on Flowers*///////////////////////////////////
//////////////////////////////////////////////////*By: Sima Naseem*/////////////////////////////////////////////////
//BEE variables
float beeX = 60;
float beeY = 10;
float beeW = 50;
float beeH = 40;
float speedY = 1;
float sadBX=180;
float sadBY=300;
float leeway=6;
//FLOWER variables
float x, y;
float flowerX1=30;
float flowerY1=200;
float flowerX2=230;
float flowerY2=200;
float flowerX3=230;
float flowerY3=200;
//GRASS variables
float grassX=-10;
float grassY=400;
//SCORE variables
int score = 0;
int highscore = 0;
//POLLEN variables (credits:Marty Daniels)
float px, py;
float ellipseMin = 1;
float ellipseMax = 14;
float ellipseSize = ellipseMin;
float dotBrightness = 255;
float dotBrightnessMax = 200;
void setup() {
size(400, 400);
smooth();
noStroke();
ellipseMode(CORNER);
rectMode(CORNER);
}
void draw() {
drawBackground();
flowers();
drawBee();
updateBee();
updateFlower();
drawPollen();
}
/////*FUNCTIONS*/////
//DRAW BACKGROUND
void drawBackground() {
background(140, 195, 201);
//draw rectangles in the sky --loop
for (int i=0; i<400; i+=200) {
fill(255, 255, 255, 40);
rect(0, i, 400, 100);
}
//controls drawn on screen so people know how to play
fill(255);
textSize (20);
text("Press -> to Bounce!", 190, 30);
//draw grass triangles --loop
for (int i=0; i<400; i+=30) {
fill(100, 150, 40);
triangle(grassX+i, grassY, grassX+i+15, grassY-40, grassX+i+30, grassY);
}
}
/*FLOWER OBSTACLES*/
//CALL FLOWERS ->function used to call the drawFlowers function
//this makes it easier to input 3 variables instead of making 3 different functions with different dimensions for each flower
void flowers() {
drawFlowers(flowerX1, flowerY1);
drawFlowers(flowerX2, flowerY2);
drawFlowers(flowerX3, flowerY3);
}
//DRAW FLOWERS
//inputs x and y variables and draws a flower in that location
void drawFlowers(float flowerX, float flowerY) {
//x and y contain the x and y coordinates of the new flower
x=flowerX;
y=flowerY;
//DRAW FLOWER
//stem
fill(100, 100, 40);
rect(x+45, y+10, 10, 200);
//leaves
quad(x+15, y+80, x+35, y+90, x+25, y+110, x+15, y+100);
quad(x+25, y+110, x+35, y+115, x+50, y+110, x+35, y+90);
quad(x+50, y+90, x+70, y+90, x+85, y+80, x+60, y+70);
quad(x+60, y+70, x+85, y+80, x+100, y+50, x+70, y+60);
//middle
fill(240, 220, 0);
ellipse(x+20, y-10, 60, 40);
//petals
fill(250, 86, 127);
ellipse(x-5, y-10, 30, 20);
ellipse(x, y+3, 20, 20);
ellipse(x+15, y+4, 20, 30);
ellipse(x+30, y+9, 25, 30);
ellipse(x+50, y+9, 25, 30);
ellipse(x+70, y+4, 20, 30);
ellipse(x+75, y-10, 30, 20);
ellipse(x+80, y+3, 20, 20);
//COLLISIONS FOR ALL FLOWERS AND BEE
//If the beeX is in between the x boundaries of the flower it will bounce back up
if ((x-35<=beeX&&beeX+beeW<=x+100+35)) {
//if beeY is lower than flower y
if (beeY > y - beeH) {
// set the position to be directly above flower y
beeY = y- beeH;
// bee moves in the opposite direction
speedY = -1*speedY;
//score increments every time the bee hits the flower (bounces)
score++;
//print everytime the bee bounces
if (score==1)println("Bee bounced "+score+" time!");
else println("Bee bounced "+score+" times!");
//pollen dust credz:Marty Daniels (this was done for decorative/aesthetic purposes)
ellipseSize = ellipseMax; // make the circles pulse
dotBrightness = dotBrightnessMax; // make the circles bright
}
//if beeY is less than 10 reverse speed direction
else if (beeY <= 10) {
//switch the direction and make it bounce off
speedY = -1*speedY;
}
}
//GAME OVER SCREEN
else if (beeY>=height) {
background(99, 149, 85);
//draw text for end screen
textSize(60);
fill(255);
text("GAME OVER", 20, 60);
textSize(35);
text("Refresh to Play Again!!", 07, 250);
textSize(30);
//show amount of bounces player got
text("Bee Bounced "+score+" Times", 40, 160);
//graphics
//grass
fill(23, 85, 40);
rect(0, 335, 400, 200);
for (int i=0; i<400; i+=30) {
triangle(grassX+i, grassY-65, grassX+i+15, grassY-90, grassX+i+30, grassY-65);
}
//sad bee on ground
drawSadBee();
}
}
/*UPDATE FLOWER*/
//when RIGHT key is pressed, flowers will scroll to the left and bee will be able to bounce on them
void updateFlower() {
if (keyPressed) {
if (keyCode==RIGHT) {
flowerX1-=10;
}
}
if (keyPressed) {
if (keyCode==RIGHT) {
flowerX2-=10;
}
}
if (keyPressed) {
if (keyCode==RIGHT) {
flowerX3-=10;
}
}
//when flower's x value is less than -100 bring it back to 400 + a random distance between 0 and 100
//flowers may be randomly placed in front of another but its okay because that happens in nature so its realistic :D
if (flowerX1<=-100) {
int r=(int)(random(0, 100));
flowerX1=400+r;
}
if (flowerX2<=-100) {
int r=(int)(random(0, 100));
flowerX2=400+r;
}
if (flowerX3<=-100) {
int r=(int)(random(0, 100));
flowerX3=400+r;
}
}
//DRAW BEE
void drawBee() {
//bee
noStroke();
//stinger
fill(125, 154, 216);
triangle(beeX+2, beeY+2, beeX+8, beeY+10, beeX+3, beeY+15);
//back wing
ellipse(beeX+22, beeY+3, 10, 20);
//body
fill(0);
arc(beeX, beeY, beeW, beeH, 0, PI);
ellipse(beeX, beeY+10, 20, 20);
ellipse(beeX+30, beeY+10, 20, 20);
//eye and smile
fill(255);
ellipse(beeX+35, beeY+17, 3, 3);
noFill();
strokeWeight(1);
stroke(255);
arc(beeX+42, beeY+13, 15, 10, (PI)/2, PI);
//legs
stroke(0);
arc(beeX+35, beeY+30, 20, 20, (3*PI)/2, (15*PI)/8);
arc(beeX+32, beeY+33, 20, 20, (3*PI)/2, (15*PI)/8);
arc(beeX-6, beeY+28, 20, 20, PI, (3*PI)/2);
arc(beeX-2, beeY+31, 20, 20, PI, (3*PI)/2);
//antenna
arc(beeX+40, beeY+3, 20, 20, PI, (3*PI)/2);
ellipse(beeX+47, beeY+2, 3, 3);
noStroke();
//stripes
stroke(252, 231, 69);
strokeWeight(3);
line(beeX+30, beeY+20, beeX+40, beeY+34);
line(beeX+25, beeY+20, beeX+30, beeY+38);
line(beeX+22, beeY+20, beeX+17, beeY+38);
line(beeX+20, beeY+20, beeX+7, beeY+32);
line(beeX+17, beeY+17, beeX+1, beeY+25);
noStroke();
//front wing
fill(193, 213, 255);
ellipse(beeX+17, beeY+3, 10, 20);
}
//UPDATE BEE
void updateBee() {
//add gravity to speed
speedY = speedY + 0.5;
//add speed to the bee's Y coordinates
//bee goes up or down
beeY = beeY + speedY;
}
/*POLLEN DUST*/
//credits to pulsing elllipse from Marty Daniel's code
//added this effect to make the bounce seem more realistic
//functions created to duplicate pollen dust in different locations
void drawPollen() {
//pollen variables
float pX1 = 30;
float pY1 = 182;
float pX2 = 40;
float pY2 = 170;
float pX3 = 60;
float pY3 = 190;
float pX4 = 100;
float pY4 = 192;
float pX5 = 120;
float pY5 = 175;
float pX6 = 145;
float pY6 = 185;
//calls pollen function
pollen(pX1, pY1);
pollen(pX2, pY2);
pollen(pX3, pY3);
pollen(pX4, pY4);
pollen(pX5, pY5);
pollen(pX6, pY6);
}
//everytime the bee hits the flower, dust appears
void pollen(float pollenX, float pollenY) {
px=pollenX;
py=pollenY;
//draw pollen
fill(240, 220, 55, dotBrightness);
ellipse(px, py+ sin(px + millis()/60)*2, ellipseSize, ellipseSize);
//if ellipse is bigger than minimum it shrinks and disappears
if (ellipseSize > ellipseMin) {
ellipseSize -= (ellipseSize-ellipseMin)/10;
}
dotBrightness -= 3;
}
//DRAW SAD BEE
//FOR GAME OVER SCREEN
//just changes bee's smile to a frown (extra stuff added towards the end to make it more fun)
void drawSadBee() {
//bee
noStroke();
//stinger
fill(125, 154, 216);
triangle(sadBX+2, sadBY+2, sadBX+8, sadBY+10, sadBX+3, sadBY+15);
//back wing
ellipse(sadBX+22, sadBY+3, 10, 20);
//body
fill(0);
arc(sadBX, sadBY, beeW, beeH, 0, PI);
ellipse(sadBX, sadBY+10, 20, 20);
ellipse(sadBX+30, sadBY+10, 20, 20);
//eye and sad face
fill(255);
ellipse(sadBX+35, sadBY+17, 3, 3);
noFill();
strokeWeight(1);
stroke(255);
arc(sadBX+42, sadBY+13, 10, 15, PI, (3*PI)/2);
//legs
stroke(0);
arc(sadBX+35, sadBY+30, 20, 20, (3*PI)/2, (15*PI)/8);
arc(sadBX+32, sadBY+33, 20, 20, (3*PI)/2, (15*PI)/8);
arc(sadBX-6, sadBY+28, 20, 20, PI, (3*PI)/2);
arc(sadBX-2, sadBY+31, 20, 20, PI, (3*PI)/2);
//antenna
arc(sadBX+40, sadBY+3, 20, 20, PI, (3*PI)/2);
ellipse(sadBX+47, sadBY+2, 3, 3);
noStroke();
//stripes
stroke(252, 231, 69);
strokeWeight(3);
line(sadBX+30, sadBY+20, sadBX+40, sadBY+34);
line(sadBX+25, sadBY+20, sadBX+30, sadBY+38);
line(sadBX+22, sadBY+20, sadBX+17, sadBY+38);
line(sadBX+20, sadBY+20, sadBX+7, sadBY+32);
line(sadBX+17, sadBY+17, sadBX+1, sadBY+25);
noStroke();
//front wing
fill(193, 213, 255);
ellipse(sadBX+17, sadBY+3, 10, 20);
}