//Valondine Jeudy //October 12, 2014 //Prof. Martin //Project 2 String title="Birds, Bombs, Heroes and Monsters"; String author="Valondine Jeudy"; //global variables float sunX= 85, sunY=85; float horizon; float heroX, heroY; float evilX = 50, evilY = 400; float goldX, goldY; int score; int step; float bird1X, bird1Y =25, bomb1Y=1; float bird2X, bird2Y =45, bomb2Y=1; float bird3X, bird3Y =65, bomb3Y=1; float bodyW = 25; float bodyH = 30; void setup() { //setup// score = 0; size(800, 600); horizon= height * 1/4; } void draw() { //next frame// scene(); score(); gold(); hero(); evil(); hits(); step+=1; //has something to do with the bird wing. birds(); } void reset() { // Start at opposite sides. // if (random(1) > 0.5) { heroX= width-30; evilX=30; } else { evilX= width-30; heroX=30; } heroY= random( horizon, height-60 ); evilY= random( horizon, height-60 ); goldX= random( width/4, width*3/4); goldY= random(horizon+20, height-20); } void scene() { //grass fill(0, 255, 0); background(134, 241, 0); //sky fill(0, 191, 255); rectMode(CORNER); rect(0, 0, width, horizon); //sun fill(255, 255, 0); ellipse(sunX, sunY, 55, 55); sunX = sunX+1; if (sunX>width) { //sun moves sunX=0; sunY= sunY; } textSize(16); text(title, width/3, 30 ); textSize(12); fill(0); text(author, width/2-375, height-50 ); } void hero() { //moves with the mouse heroX= heroX + (mouseX-heroX)/60; heroY= heroY + (mouseY-heroY)/60; if (heroYwidth+500) { bird1X= 80; bird1Y= random(20, horizon); } if (bird2X>width+500) { bird2X= 80; bird2Y= random(20, horizon); } if (bird3X>width+500) { bird3X= 80; bird3Y= random(20, horizon); } // drawing of birds // fill( 255, 0, 0 ); //first bird drawBird( bird1X, bird1Y+5-random(5), bodyW, bodyH); bomb1Y= updateBomb( bomb1Y ); drawBomb( bird1X, bomb1Y ); fill( 255, 155, 0 ); //color of second bird. drawBird( bird2X, bird2Y+2-random(1), bodyW, bodyH ); bomb2Y= updateBomb( bomb2Y ); drawBomb( bird2X, bomb2Y ); fill( 255, 255, 0 ); //color of third bird. drawBird( bird3X, bird3Y, bodyW+5, bodyH+5-random(6) ); bomb3Y= updateBomb( bomb3Y ); drawBomb( bird3X, bomb3Y ); } float updateBomb( float y ) { //// Calculate new Y value for bomb: add 5 if >0 and height) return 0; return y + 1 + y/50; // bomb going down with a 9.50 gravity. } void drawBomb( float x, float y ) { //// draw a bomb IF y > 0 //// if (y<=0) return; ellipse( x, y, 20, 20 ); //size of the bomb. stroke(255, 0, 0); fill( 255, 0, 0, 15 ); // Red glow surrounds bomb. ellipse( x, y, 50, 50 ); stroke(0); } void drawBird( float x, float y, float bodyW, float bodyH) { //// draw one bird ///// ellipse( x, y, bodyW, bodyH-15); // body // Make the wings flap (every 1/2 second). float flap; if ( step/30 % 4 == 0 ) flap= 20; else if ( step/30 % 4 == 1 ) flap= -10; else if ( step/30 % 4 == 2 ) flap= -20; else flap= +10; birdWing( x, y, flap ); } void birdWing( float x, float y, float yup ) { // Draw the wing (up or down). triangle( x-10, y, x+7, y, x+8, y-yup ); } void hits() { //if evil is 30 feet from the hero. eat if (dist(heroX, heroY, evilX, evilY) <=40) { eat(); /* //if the hero touches the gold. they will get 25 pts. if (dist(goldX, goldY, heroX, heroY) <50) { score+=25; background(255, 0, 255); reset(); } if (dist(evilX, evilY, goldX, goldY)<50) { score-=25; reset(); } // */ if ( dist( evilX, evilY, bird1X, bomb1Y ) < 50 ) evilHit(); if ( dist( evilX, evilY, bird2X, bomb2Y ) < 50 ) evilHit(); if ( dist( evilX, evilY, bird3X, bomb3Y ) < 50 ) evilHit(); } } void evilHit() { //evil is hit by a bomb. score+=25; background(255, 255, 255); evilX = 0; evilY = random(horizon, height); } //body parts of hero and evil void headHero(float x, float y, float w, float h) { //head ellipse(x, y, w, h); } void headEvil(float x, float y, float w, float h) { //head of evil rect(x, y, w, h); } void body(float x, float y, float w, float h) { //bodies of hero and evil rect(x, y, w, h); } void arm(float x, float y, float w, float h) { //function of arm rect(x, y, w, h); } void birdBody(float x, float y, float w, float h) { //body of the bird ellipse(x, y, w, h); } void score() { //score box textSize(15); text("Score:", width-100, 95); text( score, width-50, 95); } void eat() { //if the hero touches evil. it will lose 30 pts. score-= 30; evilX= random(0,width); evilY= random(horizon,height); } void gold() { //treasure fill(255, 255, 0); ellipse(goldX+36, goldY+500, 35, 35); //if the hero touches the gold. they will get 25 pts. if (dist(goldX, goldY, heroX, heroY) <50) { score+=25; background(255, 0, 255); goldX = random(0,width); goldY = random(horizon,height); } } void keyPressed() { //handle the key if (key == 'q') exit(); // Drop bomb when # 1,2,3,4 key is pressed. if (key == '1') bomb1Y= bird1Y; if (key == '2') bomb2Y= bird2Y; if (key == '3') bomb3Y= bird3Y; if (key == '0') { bomb1Y= bird1Y; // all bombs goes down bomb2Y= bird2Y; bomb3Y= bird3Y; } }