Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//--------------PROG ASSIGNMENT 2---------------------//
//---------------ALEXANDER KEHN-----------------------//
//---------------id# 991368989------------------------//
//-------"SpaceCat in the SeizureZone"----------------//
//---------------------OR-----------------------------//
//-----"How I Said I'd Make Something Super Unique----//
//--------and Made Another Damn Bouncing Ball"--------//



//----------------GLOBAL VARIABLES-------------------//

//planet ball
int ballSize = 20;
float gravity = .1;
float yspeed = 0;
float xspeed = 0;
float xspeedInc = .8;  //increase x speed
float yspeedInc = .8; //increase y speed
float xpos = 75; //x position
float ypos = 0; //y position
float bounce = 7; //how high it bounces

//paddle
float padWidth = 100;
float padHeight = 20;
float padX = mouseX;
float padY = mouseY;
float give = 8; //helps collision

//spacecat
float pX = mouseX; //position of dynamic arm
float pY = mouseY; //used to make gravity effect work

//SETUP AND DRAW-----------------------------------------------//
void setup () {
  size(400, 400);
  background(0);
  noCursor();
}

void draw() {

  background(0);
  stars();
  spaceGrid();
  spaceCat(width/2 + 116, height/2 + 55); //offset from center
  drawPaddle();
  collisions();
  drawBall();
}


//----------------WELCOME--TO--FUNCTION-TOWN----------------------//
//-------------------------POPULATION:----------------------------//
//----------------------------YOU---------------------------------//

//SUPER SPACEY GRID USING FOR LOOPS----------------------------------//
//much respect to Mustafa Kirgul from last year who's source code
//taught me how to make this beauty

void spaceGrid() {
  float RR = random(0, 255); //the variables for random color flashing...is there a way this can be global and still work?
  float RG = random(0, 255);
  float RB = random(0, 255);

  stroke(RR, RG, RB);
  int gridheight = height/2-1; //height is center minus one
  for (int grid=-10; grid<10; grid++) { //for grid -10 upto 10, add x-axis grid
    line((width/2)-(20*grid), height/2, (width/2)-(100*grid)+(width/2)/10, height); //draw pseudo 3D line along x-axis
  }
  for (int grid=0; grid<20; grid++) { //same as above, from 0 upto 20 add y-axis grid
    gridheight = gridheight +int(gridheight*(grid*.005)); //change gridheight to increment exponentially
    line(0, gridheight, width, gridheight); //draw line based on gridheight
  }
}
//---------------------------------------------------------------------//

//THE SPACECAT----------------------------------------------------------//
void spaceCat(int x, int y) {
  //LOCAL VARIABLES AND STUFF
  float ranColR = random(0, 255); //the variables for random color flashing...is there a way this can be global and still work?
  float ranColG = random(0, 255);
  float ranColB = random(0, 255);
  float zg = sin(millis()/200)*4; //zero gravity wobble effect
  y = y + int(zg); //takes y parameter and adds wobble effect
  rectMode(CORNER); //resets to default since the paddle screws with it
  stroke(ranColR, ranColG, ranColB); //set stroke to flash random
  fill(0); //fill black

  //BODY
  triangle(x - 70, y + 110, x - 74, y + 82, x - 30, y + 80); //builds the left leg curve
  ellipse(x - 80, y + 100, 20, 60);//left leg

  triangle(x + 10, y + 80, x + 51, y + 82, x + 50, y + 110); //builds right leg curve
  ellipse(x + 60, y + 100, 20, 60); //right leg

  ellipse(x - 10, y + 80, 160, 40); //rounds bottom of body
  rect(x - 90, y - 40, 160, 120, 7); //main body rect

  //HELMET
  // !!add another bezier here for the bottom lip of helmet *cosmetic*
  noFill();
  bezier(x - 90, y - 40, x - 44, y - 210, x + 60, y - 140, x + 70, y - 40); //helmet glass
  fill(0);
  ellipse(x - 10, y - 40, 160, 40); //base of helmet, add perspective

  //HEAD
  bezier(x - 80, y - 30, x - 40, y - 160, x + 40, y - 120, x + 60, y - 30); //head outline
  triangle(x - 50, y - 90, x - 48, y - 113, x - 29, y - 108); //L ear
  triangle(x + 15, y - 105, x + 36, y - 113, x + 32, y - 90);//R ear

  //FACE
  ellipse(x - 30, y - 70, 6, 30); //R eye
  ellipse(x - 30, y - 80, 4, 10); //R eye pupil
  ellipse(x, y - 70, 6, 30); //L eye
  ellipse(x, y - 80, 4, 10); //L eye pupil
  line(x - 28, y - 49, x - 48, y - 53); //L whiskers
  line(x - 28, y - 45, x - 48, y - 43); //L whiskers
  line(x - 3, y - 49, x + 20, y - 53); //R whiskers
  line(x - 3, y - 45, x + 20, y - 43); //R whiskers
  line(x - 15, y - 44, x - 25, y - 34);//L mouth
  line(x - 15, y - 44, x - 5, y - 34);
  triangle(x - 22, y - 50, x - 9, y - 50, x - 15, y - 44); //nose

  //ASTROSUIT DETAILS
  ellipse(x - 60, y, 24, 25); //oxygen gauge
  line(x - 61, y, x - 60, y - 12.5); //gauge detail
  line(x - 61, y, x - 50, y - 7); //gauge detail
  ellipse (x - 40, y - 7, 5, 5);
  ellipse (x - 40, y + 2, 5, 5);
  line (x - 43, y + 8, x - 38, y + 8);
  line (x - 43, y + 11, x - 38, y + 11);

  //STATIC ARM (LEFT)
  beginShape();
  vertex(x - 89, y - 20);
  bezierVertex(x - 110, y - 25, x - 130, y + 10, x - 120, y + 30);
  bezierVertex(x - 107, y + 40, x - 117, y + -5, x - 89, y - 8);
  endShape();

  //LINE CLEAN-UP
  //these hide some layering junk to clean up the shape of everything
  strokeWeight(2);
  stroke(0);
  line(x - 49, y - 90, x - 31, y - 107);//cleans up L ear
  line(x + 17, y - 103, x + 31, y - 91);//cleans up R ear
  line(x - 70, y + 108, x - 70, y + 86);//cleans up L leg triangle
  line(x + 51, y + 92, x + 50, y + 108);//cleans up R leg triangle
  triangle(x - 87, y + 109, x - 87, y + 63, x - 40, y + 90);//cleans up L leg
  triangle(x - -68, y + 111, x - -66, y + 63, x - -22, y + 90);//cleans up R leg
  strokeWeight(5);
  line(x - 87, y + 78, x + 67, y + 78);//cleans up BODY
  strokeWeight(1);//resets weight after clean-up
  stroke(ranColR, ranColG, ranColB);//resets stroke after clean-up

  //DYANMIC ARM (RIGHT)


  quad(x - -70, y - 20, x - -68, y - 6, pX + 75, pY+5, pX + 75, pY-5);
  pX += (mouseX - pX)*gravity;
  pY += (mouseY - pY)*gravity;
}
//----------------------------------------------------------------------//

//THE BALL--------------------------------------------------------------//
void drawBall() {
  noStroke();
  float RR = random(0, 255); //the variables for random color flashing...is there a way this can be global and still work?
  float RG = random(0, 255);
  float RB = random(0, 255);

  fill(RR, RG, RB);
  ellipseMode(CENTER);
  ellipse(xpos, ypos, ballSize, ballSize);

  yspeed += gravity; //y axis speed moves based on gravity
  ypos += yspeed; //y axis movement, default starts at zero
  xpos += xspeed; //same as above but x axis
}
//----------------------------------------------------------------------//

//THE PADDLE-------------------------------------------------------------//
void drawPaddle() {
  float RR = random(0, 255); //the variables for random color flashing...is there a way this can be global and still work?
  float RG = random(0, 255);
  float RB = random(0, 255);

  fill(0);
  stroke(RR, RG, RB);
  ellipseMode(CENTER);
  rectMode(CENTER);
  ellipse(padX, padY, padWidth, padHeight);
  rect(padX + 60, padY, 30, 7);

  padX += (mouseX - padX)*gravity; //move with mouse and add heavy gravity
  padY += (mouseY - padY)*gravity; //same as above on y axis
}
//------------------------------------------------------------------------//

//COLLISION JUNK//FUN STUFF-----------------------------------------------//
void collisions() {
  if (ypos > padY && ypos < padY + (yspeed * 2) //if ballY and padY are the same
    && xpos < padX + (padWidth/2) + give //and ballX and padX are the same (with give)
    && xpos > padX - (padWidth/2) - give) {
    xspeed += xspeedInc; //increase x speed
    yspeed = -bounce; // bounce ball opposite direction
  } else if (ypos > height) { //if ball hits the ground
    ypos = height;
    yspeed = -8; //turn around
    xspeed = 0; //reset x speed
  }

  if (xpos > width || xpos <0) { //if ball hits a wall
    xspeed *= -1; //reverse direction
    xspeedInc *= -1; //invert acceleration
    if (xspeed <0) { 
      xpos = (width - (xpos-width));
    } else if (xspeed > 0) {
      xpos = -xpos;
    } else { //if ball gets stuck somehow, reset it
      xpos = 100;
    }
  }
}
//---------------------------------------------------------------------//

//GLOBAL VARIABLES FOR STARS TO DRAW RANDOMLY
float sX1 = random(0, 400);
float sY1 = random(0, 200);
float sX2 = random(0, 400);
float sY2 = random(0, 200);
float sX3 = random(0, 400);
float sY3 = random(0, 200);
float sX4 = random(0, 400);
float sY4 = random(0, 200);
float sX5 = random(0, 400);
float sY5 = random(0, 200);
float sX6 = random(0, 400);
float sY6 = random(0, 200);
float sX7 = random(0, 400);
float sY7 = random(0, 200);
float sX8 = random(0, 400);
float sY8 = random(0, 200);
float sX9 = random(0, 400);
float sY9 = random(0, 200);
float sX10 = random(0, 400);
float sY10 = random(0, 200);
float sX11 = random(0, 400);
float sY11 = random(0, 200);
float sX12 = random(0, 400);
float sY12 = random(0, 200);
float sX13 = random(0, 400);
float sY13 = random(0, 200);
float sX14 = random(0, 400);
float sY14 = random(0, 200);
float sX15 = random(0, 400);
float sY15 = random(0, 200);
float sX16 = random(0, 400);
float sY16 = random(0, 200);
float sX17 = random(0, 400);
float sY17 = random(0, 200);
float sX18 = random(0, 400);
float sY18 = random(0, 200);
float sX19 = random(0, 400);
float sY19 = random(0, 200);
float sX20 = random(0, 400);
float sY20 = random(0, 200);

//STARS--------------------------------------------------------------//
void stars() {
  float RR = random(0, 255); 
  float RG = random(0, 255);
  float RB = random(0, 255);
  stroke(RR, RG, RB);
//draw points based on random star variables
  point(sX1, sY1);
  point(sX2, sY2);
  point(sX3, sY3);
  point(sX4, sY4);
  point(sX5, sY5);
  point(sX6, sY6);
  point(sX7, sY7);
  point(sX8, sY8);
  point(sX9, sY9);
  point(sX10, sY10);
  strokeWeight(2); //some have a heavier weight
  point(sX11, sY11);
  point(sX12, sY12);
  point(sX13, sY13);
  point(sX14, sY14);
  point(sX15, sY15);
  point(sX16, sY16);
  point(sX17, sY17);
  point(sX18, sY18);
  point(sX19, sY19);
  point(sX20, sY20);
  strokeWeight(1); //reset weight
}