//// Project 1c -- birds, bombs, hits -- use functions with input args. Object Oriented Project 3 april 15 //// B.A.Martin, 2015 Mar 11 ///////////////////////////////////////// DO NOT COPY ANY OF THIS CODE ////////////////////////////////// String title= "Igor chases Vlad (plus birds)"; String author= "Joshua Jack 2015 april 15"; String news=""; float helpX, helpY; int score=0; float sunX=100, sunY=100; float horizon; float houseX, houseY, houseW=100, houseH=60; float goldX, goldY; float vladX, vladY, vladW=50, vladH=80; float frankX, frankY, frankW=70, frankH=90; float frankStep; // Wobbly walk. int step; float look; float bird1X, bird1Y=20, bomb1Y=1; // ++ Move Globals into objects,=BirdBomb and bird need to be part of same object// float bird2X, bird2Y=40, bomb2Y=1; float bird3X, bird3Y=60, bomb3Y=1; float bird4X, bird4Y=80, bomb4Y=1; Bird one, two, three, four; void setup() { /* DO NOT COPY */ //// setup //// size( 800,600 ); horizon= height/4; sunY= horizon/2; houseX= width - 2*houseW; houseY= horizon-houseH; score=0; reset(); } void reset() { //Start AT OPPOSITE SIDES // Start at opposite sides. // if (random(1) > 0.5) { vladX= width-30; frankX=30; } else { frankX= width-30; vladX=30; } vladY= random( horizon,height-60 ); frankY= random( horizon,height-60 ); goldX= random( width/4, width*3/4); goldY= random(horizon+200,height-20); mouseY=height; //RESET THE BIRDS every time it apears one= new Bird (255,255,0); one.y=110; two = new Bird (0,255,0,255); two.y=140; three = new Bird(0,255,127); three.y=60; four = new Bird(255,0,255); four.y=80; } void draw() { // next frame // scene(); if (key == '?') { help(); return; } gold(); hero(); monster(); birds(); hits(); // Check for collisions. // // Different step, every 15 frames. // step = step+1; if (step % 15 == 0) frankStep= random( -15, 15 ); // Title, etc. fill(0); text( title, width/3, 20 ); text( author, 20, height-15 ); fill(255,255,0); text( "SCORE= " + score, width-200, 25 ); text( news, 20, 25 ); } void scene() { /* DO NOT COPY */ // sky, sun, etc. background( 200,200,255 ); fill(255,255,0); // yellow sun ellipse(sunX,sunY, 50,50); fill( 200,255,200); // grass rectMode( CORNER ); rect(0,horizon, width,height*3/4); // sun moves across sky. (-10 each day.) // if (sunX > width) { sunX=0; sunY= random( 10, horizon-20); score= score-10; } sunX= sunX+2; fill(0); text( sunX, sunX, sunY+40); //// Trees. for (float x=20; xwidth+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); } if (bird4X>width+55) { bird4X= 80; bird4Y= random(20,horizon); } // draw birds & bombs // /* DO NOT COPY */ fill( 255,0,0 ); // bird 1: red drawBird( bird1X, bird1Y+5-random(5) ); bomb1Y= updateBomb( bomb1Y ); drawBomb( bird1X, bomb1Y ); one.show(); two.show(); three.show(); four.show(); fill( 255,155,0 ); drawBird( bird2X, bird2Y ); bomb2Y= updateBomb( bomb2Y ); drawBomb( bird2X, bomb2Y ); fill( 255,255,0 ); drawBird( bird3X, bird3Y ); bomb3Y= updateBomb( bomb3Y ); drawBomb( bird3X, bomb3Y ); fill( 0,255,255 ); drawBird( bird4X, bird4Y ); bomb4Y= updateBomb( bomb4Y ); drawBomb( bird4X, bomb4Y ); } void drawBird( float x, float y ) { /* DO NOT COPY */ //// draw one bird ///// triangle( x,y, x-60,y-10, x-60,y+10 ); // 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; Wing( x, y, flap ); } void Wing( float yup ) { /* DO NOT COPY */ // Draw the wing (up or down). triangle( x-20,y, x-40,y, x-40,y-yup ); } float updateBomb( float y ) { /* DO NOT COPY */ //// Calculate new Y value for bomb: add 5 if >0 and height) return 0; return y + 1 + y/50; // Approximate gravity by using y/50. } void drawBomb( float x, float y ) { /* DO NOT COPY */ //// draw a bomb IF y > 0 //// if (y<=0) return; ellipse( x,y, 20,30 ); stroke(255,0,0); fill( 255,0,0, 50 ); // Red glow surrounds bomb. ellipse( x,y, 50,50 ); stroke(0); fill(0); triangle( x,y+3, x-15,y-12, x+15,y-12 ); } void scoring() { } //////// Collisions //////// void hits() { /* DO NOT COPY */ //// Check for collisions //// // Monster catches hero. // if ( dist( vladX,vladY, frankX,frankY ) < 50 ) { eat(); vladX= houseX; vladX= houseY-houseH; news= "The hero was EATEN by the monster."; } // Monster gets the gold. // if ( dist( goldX,goldY, frankX,frankY ) < 50 ) { goldX= 50; goldY= random(horizon+200,height); news= "SORRY! The monster got the gold!"; } //// Hero gets the gold! // if ( dist( vladX,vladY, goldX,goldY ) < 50 ) { score += random( 100,200 ); background(255,255,255); reset(); news= "HOORAY! The hero found the gold."; houseX= random( 100, width-100 ); vladX= houseX; vladY= houseY-houseH/2; } // Bombs (starting after 200 frames) if (step<200) return; if ( dist( frankX,frankY, bird1X,bomb1Y ) < 50 ) frankHit(); if ( dist( frankX,frankY, bird2X,bomb2Y ) < 50 ) frankHit(); if ( dist( frankX,frankY, bird3X,bomb3Y ) < 50 ) frankHit(); if ( dist( frankX,frankY, bird4X,bomb4Y ) < 50 ) frankHit(); } void frankHit() { /* DO NOT COPY */ //// Monster has been bombed. //// score += 10; background(0); // Flash black frankX=0; frankY= random(horizon,height); news= "Monster was hit by a bomb!"; } void eat() { //// Monster eats hero!!!! Deduct 200 from score. score= score - 200; background(255,0,0); reset(); } void mousePressed() { //// Click. //// news=""; goldX= mouseX; goldY= mouseY; score= score - 50; frankX=0; frankY= random(20,height-20); news= "Gold is now at ("+ int(goldX) +","+ int(goldY) +")" ; } void keyPressed() { //// Handle keys. //// news=""; if (key == 'q') exit(); if (key == 'r') reset(); // 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 == '4') bomb4Y= bird4Y; if (key == '*') { bomb1Y= bird1Y; // All boimbs away!! bomb2Y= bird2Y; bomb3Y= bird3Y; bomb4Y= bird4Y; } } void help() { // Instructions // news=""; helpX= 100; helpY =horizon+20; say( "I N S T R U C T I O N S" ); say( "" ); say( "Hero chases gold -- +100 points." ); say( "Monster chases hero -- lose points if eaten!" ); say( "Click to mov1Ge gold (but lose 50)." ); say( "(Also lose 10 points per day.)" ); say( "r key to reset (monster to left, hero to right, gold in the middle." ); say( "q key to quit" ); } void say( String s ) { //// Display one line of text (on next available line). //// text( s, helpX, helpY ); helpY += 12; // Next line. } // New Class to Define a Bird object ++// class Bird { float x=0, y=120; float slow, fast; // two random values/variables. float bombY; float flap; int r,b,g; //CONTRUCTORS// Bird ( float y, int r, int g, int b) { //local variable// this.r= int (random(r-50,r+50) ); this.g= g; this.b= b; this.y= y; } //METHODS// void move (float slow, float fast){ //move the bird //++++STUB++++ x += random(slow,fast); if (x>width+500) { x=80; y= random(120, horizon = 100); } } void show () { fill (r, g, b); void drawBird( float x, float y ) { /* DO NOT COPY */ //// draw one bird ///// triangle( x,y, x-60,y-10, x-60,y+10 ); // 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; Wing( ); } void Wing( ) { /* DO NOT COPY */ // Draw the wing (up or down). triangle( x-20,y, x-40,y, x-40,y-yup ); } // Display Bird and Bomb } }