Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //David Down
- //Section D
- //Down.DavidR@gmail.com
- //Copyright © David Down February 2011 Pittsburgh, PA, 15232 All Rights Reserved
- //
- //
- // Do not alter or delete the next 9 lines of code.
- /**********************************
- <B> Homework #6</B><BR>
- Use the [arrow keys] to "fly" my initials around the screen<BR>
- and try to hit the moving target.<BR>
- If you hold down a key, my initials move faster.<BR>
- Try not to lose my initials.<BR>
- If you do lose them, press the [space bar].<BR>
- Have fun.<BR>
- ************************************/
- //* ------------ Movie Code - Do Not Alter the next three lines lines ---------/
- import processing.video.*;
- Capture myCapture;
- MovieMaker mm;
- void setup( )
- {
- size ( 400, 400 );
- background (10,76,250);
- ht=.1*height;
- wd=.1*width;
- loadFont("SansSerif-18.vlw");
- textAlign(CENTER);
- // ------------ Movie Code #1 - uncomment next two lines ---------/
- // myCapture = new Capture(this, width, height, 30);
- // mm = new MovieMaker(this, width, height, "hw6.mov", 30, MovieMaker.ANIMATION, MovieMaker.HIGH);
- }
- float boxx=0;
- float boxy=0;
- float boxdx=random(8);
- float boxdy=random(8);
- float initialsx=200;
- float initialsy=200;
- float initialsdx=random(1);
- float initialsdy=random(1);
- float ht;
- float wd;
- int hit;
- void draw( )
- {
- offscreen();
- drawBox();
- drawInitials();
- collisionDetect();
- indicators();
- // ------------ Movie Code #2 - uncomment one linelines ---------/
- // mm.addFrame( );
- }
- void keyPressed()
- {
- if (keyCode==keyEvent.VK_SPACE)
- {
- initialsx=200;
- initialsy=200;
- initialsdx=0;
- initialsdy=0;
- }
- if (key==CODED)
- {
- if (keyCode==UP)
- {
- initialsdy=initialsdy-1;
- }
- else if (keyCode==DOWN)
- {
- initialsdy=initialsdy+1;
- }
- else if (keyCode==LEFT)
- {
- initialsdx=initialsdx-1;
- }
- else if (keyCode==RIGHT)
- {
- initialsdx=initialsdx+1;
- }
- }
- // ------------ Movie Code #3 - uncomment next four lines lines ---------/
- //else if (key == 'c')
- //{
- // mm.finish
- //}
- }
- // ------------ Movie Code #4 - uncomment next four lines lines ---------/
- //void captureEvent(Capture myCapture)
- //{
- //myCapture.read();
- //}
- void drawBox()
- {
- //rectMode(CORNER);
- fill(random(255),random(255),random(255));
- rect(boxx,boxy, width*.05, height*.05);
- boxx=boxx+boxdx;
- boxy=boxy+boxdy;
- if(boxx>400)
- {
- boxdx=(-1*random(8));
- }
- if (boxx<0)
- {
- boxdx=random(8);
- }
- if(boxy>400)
- {
- boxdy=(-1*random(8));
- }
- if (boxy<0)
- {
- boxdy=random(8);
- }
- fill(10,76,250,100);
- rect(0,0,width,height);
- }
- void drawInitials ()
- {
- smooth();
- fill(21,95,5);
- ht=wd;
- initialsx=initialsx+initialsdx;
- initialsy=initialsy+initialsdy;
- noStroke();
- ellipse(initialsx,initialsy,wd,ht);
- noFill();
- strokeWeight(.03*wd);
- stroke(255,11,3);
- beginShape();
- curveVertex(initialsx-(.6*wd),initialsy-(.6*ht));
- curveVertex(initialsx-(.3*wd),initialsy-(.4*ht));
- curveVertex(initialsx,initialsy);
- curveVertex(initialsx-(.3*wd),initialsy+(.4*ht));
- curveVertex(initialsx-(.6*wd),initialsy+(.6*ht));
- endShape();
- beginShape();
- curveVertex(initialsx-(.2*wd),initialsy-(.55*ht));
- curveVertex(initialsx-(.3*wd),initialsy-(.4*ht));
- curveVertex(initialsx-(.38*wd),initialsy);
- curveVertex(initialsx-(.3*wd),initialsy+(.4*ht));
- curveVertex(initialsx-(.2*wd),initialsy+(.55*ht));
- endShape();
- stroke(255,100,3);
- beginShape();
- curveVertex(initialsx-(.1*wd),initialsy+(.55*ht));
- curveVertex(initialsx-(.15*wd),initialsy+(.475*ht));
- curveVertex(initialsx-(.2*wd), initialsy);
- curveVertex(initialsx-(.15*wd),initialsy-(.475*ht));
- curveVertex(initialsx-(.1*wd),initialsy-(.55*ht));
- endShape();
- beginShape();
- curveVertex(initialsx-(.3*wd),initialsy-(.4*ht));
- curveVertex(initialsx-(.15*wd),initialsy-(.475*ht));
- curveVertex(initialsx+(.2*wd),initialsy-(.3*ht));
- curveVertex(initialsx+(.17*wd),initialsy);
- curveVertex(initialsx-(.2*wd), initialsy);
- curveVertex(initialsx-(.38*wd),initialsy-(.1*ht));
- endShape();
- beginShape();
- curveVertex(initialsx-(.1*wd),initialsy-(.1*ht));
- curveVertex(initialsx,initialsy);
- curveVertex(initialsx+(.15*wd),initialsy+(.475*ht));
- curveVertex(initialsx+(.13*wd),initialsy+(.6*ht));
- endShape();
- stroke(255,230,3);
- beginShape();
- curveVertex(initialsx+(.2*wd),initialsy-(.5*ht));
- curveVertex(initialsx+(.3*wd),initialsy-(.4*ht));
- curveVertex(initialsx+(.37*wd),initialsy);
- curveVertex(initialsx+(.3*wd), initialsy+(.4*ht));
- curveVertex(initialsx+(.2*wd),initialsy+(.5*ht));
- endShape();
- beginShape();
- curveVertex(initialsx+(.2*wd),initialsy-(.5*ht));
- curveVertex(initialsx+(.3*wd),initialsy-(.4*ht));
- curveVertex(initialsx+(.5*wd),initialsy);
- curveVertex(initialsx+(.3*wd), initialsy+(.4*ht));
- curveVertex(initialsx+(.2*wd),initialsy+(.5*ht));
- endShape();
- }
- void collisionDetect()
- {
- if(-.05*width<=initialsx-boxx&&initialsx-boxx<=.05*width+.05*width)
- {
- if(-.05*height<=initialsy-boxy&&initialsy-boxy<=.05*height+.05*height)
- {
- boxx=random(400);
- boxy=random(400);
- hit=hit+1;
- fill(255,0,0,100);
- rect(0,0,width,height);
- }
- }
- }
- void indicators()
- {
- fill(0,0,0);
- text("X Coordinate: ("+int(initialsx)+")",80,100);
- text("Y Coordinate: ("+int(initialsy)+")",80,130);
- text("X Speed: ("+int(initialsdx)+")",80,160);
- text("Y Speed: ("+int(initialsdy)+")",80, 190);
- text("Hits: ("+hit+")",80,220);
- }
- void offscreen()
- {
- if(initialsx<0)
- {
- fill(255,0,0);
- text("The Initials Are Offscreen!",200,300);
- stroke(255,0,0);
- strokeWeight(4);
- line(200,200,initialsx, initialsy);
- }
- if(initialsx>400)
- {
- fill(255,0,0);
- text("The Initials Are Offscreen!",200,300);
- stroke(255,0,0);
- strokeWeight(4);
- line(200,200,initialsx, initialsy);
- }
- if(initialsy<0)
- {
- fill(255,0,0);
- text("The Initials Are Offscreen!", 200,300);
- stroke(255,0,0);
- strokeWeight(4);
- line(200,200,initialsx, initialsy);
- }
- if(initialsy>400)
- {
- fill(255,0,0);
- text("The Initials Are Offscreen!",200,300);
- stroke(255,0,0);
- strokeWeight(4);
- line(200,200,initialsx, initialsy);
- }
- }
Add Comment
Please, Sign In to add comment