// HW 6
// Jennifer Shin
// Section D
// jshin1@andrew.cmu.edu
// Copyright © Jennifer Shin February 2011 Pittsburgh, PA 15221 All Rights Reserved
// Rebeccah Berk
// no one
// Do not alter or delete the next 9 lines of code.
/**********************************
Homework #6
Use the [arrow keys] to "fly" my initials around the screen
and try to hit the moving target.
If you hold down a key, my initials move faster.
Try not to lose my initials.
If you do lose them, press the [space bar].
Have fun.
************************************/
//* ------------ Movie Code - Do Not Alter the next three lines lines ---------/
import processing.video.*;
Capture myCapture;
MovieMaker mm;
float a, deltaA, b, deltaB, w, x, deltaX, y, deltaY, positionX, positionY;
int collisionHits;
void setup( )
{
size ( 400, 400 );
smooth ( );
noStroke ( );
x = .5 * width;
y = .5 * height;
deltaX = 0;
deltaY = 0;
deltaA = random ( 1, 5 );
deltaB = random ( 1, 5 );
w = random ( 1, 4 );
positionX = random ( 0, width );
positionY = random ( 0, height );
collisionHits = 0;
// ------------ 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);
}
void draw( )
{
background ( 0, 0, 0 );
drawBox ( 20, 20 );
keyBox ( );
moveBox ( );
drawInitials ( .1 * width, .1 * height, x + deltaX, y + deltaY );
moveInitials ( );
collisionCheck ( );
numberCollision ( );
offScreen ( );
// ------------ Movie Code #2 - uncomment one linelines ---------/
// mm.addFrame( );
}
void drawBox ( float g, float h )
{
fill ( #FA5849 );
stroke ( #FA5849 );
line ( positionX - g, positionY - .2 * h, positionX - .25 * g, positionY - .2 * h );
line ( positionX - .25 * g, positionY - .2 * h, positionX, positionY - h );
line ( positionX, positionY - h, positionX + .25 * g, positionY - .2 * h );
line ( positionX + .25 * g, positionY - .2 * h, positionX + g, positionY - .2 * h );
line ( positionX + g, positionY - .2 * h, positionX + .45 * g, positionY + .3 * h );
line ( positionX + .45 * g, positionY + .3 * h, positionX + .8 * g, positionY + h );
line ( positionX + .8 * g, positionY + h, positionX, positionY + .6 * h );
line ( positionX, positionY + .6 * h, positionX - .8 * g, positionY + h );
line ( positionX - .8 * g, positionY + h, positionX - .45 * g, positionY + .3 * h );
line ( positionX - .45 * g, positionY + .3 * h, positionX - g, positionY - .2 * h );
}
void moveBox ( )
{
positionX = positionX + w * deltaA ;
positionY = positionY + w * deltaB ;
}
void keyBox( )
{
if ( positionX >= width )
{
deltaA = -deltaA;
}
if ( positionX < 0 )
{
deltaA = -deltaA;
}
if ( positionY >= height )
{
deltaB = -deltaB;
}
if ( positionY < 0 )
{
deltaB = -deltaB;
}
if ( positionX >= width && positionY >= height )
{
deltaA = -deltaA;
deltaB = -deltaB;
}
if ( positionX == 0 )
{
deltaA = -deltaA;
}
if ( positionY == 0 )
{
deltaB = -deltaB;
}
}
void collisionCheck ( )
{
if ( dist ( positionX, positionY, x, y ) < 30 )
{
collisionHits = collisionHits + 1;
positionX = random ( 0, width );
positionY = random ( 0, height );
deltaA = random ( 1, 5 );
deltaB = random ( 1, 5 );
w = random ( 1, 4 );
background ( #49FA9D );
}
}
void offScreen ( )
{
if ( x > width || x < 0 || y > height || y < 0 )
{
offAction ( );
}
}
void offAction ( )
{
fill ( #F71EDE);
line ( x, y, .5 * width, .5 * height );
fill ( #49FA9D );
rect ( .15 * width, .2 * height, .7 * width, .6 * height );
fill ( #49D8FA );
rect ( .2 * width, .25 * height, .6 * width, .5 * height );
fill ( #FA5849 );
textSize ( 15 );
text ( "OH NO! The initials has escaped the premises!", .23 * width, .28 * height, .54 * width, .44 * height );
text ( "Distance from the center: " + dist ( x, y, .5 * width, .5 * height ), .23 * width, .7 * height );
}
void numberCollision ( )
{
textSize ( 13 );
fill ( 150, 150, 150 );
text ( "Number of Collisions: " + collisionHits, .1 * width, .9 * height );
text ( "Horizontal Speed: " + abs ( deltaX ), .1 * width, .1 * height );
text ( "Vertical Speed: " + abs ( deltaY ), .6 * width, .1 * height );
text ( "Time: " + ( millis ( ) / 1000 ), .6 * width, .9 * height );
}
void drawInitials ( float wd, float ht, float x, float y )
{
noStroke ( );
fill ( 44, 232, 171 );
ellipse ( x, y, .75* wd, .75 * ht );
fill ( 0, 0, 0 );
noFill ( );
strokeWeight ( .01 * abs ( width ) );
stroke ( 244, 245, 25 );
beginShape ( );
curveVertex ( x - ( .4125 * wd ), y - ( .4125 * ht ) );
curveVertex ( x - ( .3675 * wd ), y + ( .0375 * ht ) );
curveVertex ( x - ( .3150 * wd ), y + ( .1875 * ht ) );
curveVertex ( x - ( .2625 * wd ), y + ( .2250 * ht ) );
curveVertex ( x - ( .2250 * wd ), y + ( .2250 * ht ) );
curveVertex ( x - ( .1950 * wd ), y + ( .1500 * ht ) );
curveVertex ( x - ( .1875 * wd ), y + ( .0750 * ht ) );
curveVertex ( x - ( .1875 * wd ), y - ( .3000 * ht ) );
curveVertex ( x - ( .1875 * wd ), y - ( .3375 * ht ) );
endShape ( );
stroke ( 255, 105, 5 );
beginShape ( );
curveVertex ( x + ( .2250 * wd ), y - ( .3000 * ht ) );
curveVertex ( x + ( .1350 * wd ), y - ( .3375 * ht ) );
curveVertex ( x , y - ( .3750 * ht ) );
curveVertex ( x - ( .1500 * wd ), y - ( .3000 * ht ) ) ;
curveVertex ( x - ( .0750 * wd ), y - ( .1500 * ht ) );
curveVertex ( x , y - ( .1313 * ht ) );
curveVertex ( x + ( .0750 * wd ), y - ( .1125 * ht ) );
curveVertex ( x + ( .1275 * wd ), y );
curveVertex ( x + ( .1275 * wd ), y + ( .1200 * ht ) );
curveVertex ( x + ( .0900 * wd ), y + ( .3150 * ht ) );
curveVertex ( x , y + ( .3750 * ht ) );
curveVertex ( x - ( .0975 * wd ), y + ( .3450 * ht ) );
curveVertex ( x - ( .1500 * wd ), y + ( .2250 * ht ) );
curveVertex ( x - ( .1688 * wd ), y + ( .0975 * ht ) );
curveVertex ( x - ( .1800 * wd ), y + ( .0750 * ht ) );
endShape ( );
stroke ( 250, 28, 113 );
line ( x + ( .15 * wd ), y - ( .3375 * ht ), x + ( .15 * wd ), y + ( .3375 * ht ) );
beginShape ( );
curveVertex ( x + ( .3375 * wd ), y - ( .2475 * ht ) );
curveVertex ( x + ( .3000 * wd ), y - ( .2100 * ht ) );
curveVertex ( x + ( .2700 * wd ), y - ( .1500 * ht ) );
curveVertex ( x + ( .2025 * wd ), y - ( .0750 * ht ) );
curveVertex ( x + ( .1500 * wd ), y - ( .0375 * ht ) );
curveVertex ( x + ( .1125 * wd ), y - ( .0375 * ht ) );
endShape ( );
beginShape ( );
curveVertex ( x + ( .1125 * wd ), y - ( .0375 * ht ) );
curveVertex ( x + ( .1500 * wd ), y - ( .0375 * ht ) );
curveVertex ( x + ( .2025 * wd ), y - ( .0225 * ht ) );
curveVertex ( x + ( .2400 * wd ), y + ( .0150 * ht ) );
curveVertex ( x + ( .2775 * wd ), y + ( .0825 * ht ) );
curveVertex ( x + ( .3150 * wd ), y + ( .1875 * ht ) );
curveVertex ( x + ( .3375 * wd ), y + ( .2250 * ht ) );
endShape ( );
}
void moveInitials ( )
{
x = x + deltaX;
y = y + deltaY;
}
void keyPressed( )
{
// if (key == 'j')
// {
// saveFrame("HW6.jpg");
// }
//
// // ------------ Movie Code #3 - uncomment next four lines lines ---------/
// else if (key == 'c')
// {
// mm.finish();
// }
if ( key == CODED )
{
if ( keyCode == UP )
{
deltaY = deltaY - 1;
}
else if ( keyCode == DOWN )
{
deltaY = deltaY + 1;
}
else if ( keyCode == LEFT )
{
deltaX = deltaX - 1;
}
else if ( keyCode == RIGHT )
{
deltaX = deltaX + 1;
}
}
if ( keyCode == ' ' )
{
x = .5 * width;
deltaX = 0;
y = .5 * height;
deltaY = 0;
}
}
//
//// ------------ Movie Code #4 - uncomment next four lines lines ---------/
//void captureEvent(Capture myCapture)
//{
// myCapture.read();
//}