Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
///////////Make by Peter Lu in PO3, /////////////////////////////////
///////////Project name Infinite Factory//////////////////////////////
///////////WAD to control the movement of the little guy/////////////
///////////space bar to dodge like tracer/////////////////////////////
///////////left click the mouse to shoot rainbow/////////////////////
///////////try not to kill those shaky cubes//////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////


//Introduce the player
Player main = new Player();
//Introduce first 2 walls
Landscape wall1 = new Landscape();
//Introduce second 2 walls
Landscape wall2 = new Landscape();
//Introduce the enemies
Enemies[] level1Enemies = new Enemies[20];
//Set the X speed of the wall
float wallSpeedX = 0.2;
//Set the Y speed of the wall
float wallSpeedY = 0.2;
//create 4 walls using arrarys
float wallX[] = new float [4];
float wallY[] = new float [4];

void setup()
{
  //inttoduce player the game
  println("This game has no objective, have fun! Press 'w' to become Iron Man, 'Space Bar' to become Tracer, wasd to move and mouseClick to shoot");
  //set the screen size
  size(800, 600);

  //create 20 enemies using x position, y position, speed, and size
  for (int i = 0; i < level1Enemies.length; i++)
  {
    level1Enemies[i] = new Enemies(i*60, 0, 3, 50);
  }

  //Initialize 4 walls. I did not use arrays because the gap between them are hard to control.


  wallX[0] = 350;
  wallX[1] = 450;
  wallX[2] = 300;
  wallX[3] = 400;
  wallY[0] = 0;
  wallY[1] = 150;
  wallY[2] = 300;
  wallY[3] = 450;
}





void draw()
{
  //keep the walls X position fixed and y position moving up and down.
  wallY[0] = wallY[0]-wallSpeedY;
  wallY[1] = wallY[1]+wallSpeedY;
  wallY[2] = wallY[2]-wallSpeedY;
  wallY[3] = wallY[3]+wallSpeedY;
  //set the background color to white
  background(255);
  //set the ground color to light grey
  fill(180);
  //draw the ground
  rect(0, height-50, width, 110);
  //create the player and make it able to move and able to use abilities 
  main.playerMove();
  main.playerShape();
  //Constrain the walls inside the screen, and make them bounce back when touches the edge of the screen
  constrainWalls();
  wallsAndEnemies();
}



void constrainWalls()
{
  //---------------------------------------------------------------------------------------------------------------------------------
  //using for loop to constrain the walls inside the screen
  for (int i = 0; i<4; i++)
  {
    if (wallX[i]>width || wallX[i]<0)
    {
      wallSpeedX = wallSpeedX*-1;
    }
    if (wallY[i]>width || wallY[i]<0)
    {
      wallSpeedY = wallSpeedY*-1;
    }
  }
}




void wallsAndEnemies()
{
  //using for loops to draw enemies and make enemies able to move. 
  for (int i = 0; i < level1Enemies.length; i++)
  {
    level1Enemies[i].drawEnemies();
    level1Enemies[i].moveEnemies();
  }

  //draw the position of first 2 walls
  wall2.drawWallRight(wallX[0], wallY[0]);
  wall2.drawWallLeft(wallX[1], wallY[1]);
  //detect the collision between the walls and the player for the 2 walls in "wall2".
  wall2.detect();

  //detect the collision between the walls and the enemies for the 2 walls in "wall2".
  for (int i = 0; i < level1Enemies.length; i++)
  {
    level1Enemies[i].enemiesDetect();
  }

  //draw the position of second 2 walls
  wall1.drawWallRight(wallX[2], wallY[2]);
  wall1.drawWallLeft(wallX[3], wallY[3]);
  //detect the collision between the walls and the player for the 2 walls in "wall1".
  wall1.detect();

  //detect the collision between the walls and the enemies for the 2 walls in "wall1".
  for (int i = 0; i < level1Enemies.length; i++)
  {
    level1Enemies[i].enemiesDetect();
  }
}




//define the movement/abilities keys

void keyPressed()
{
  if (key == 'd')
  {
    right = true;
  }
  if (key == 'a')
  {
    left = true;
  }
  if (key == 'w')
  {
    up = true;
  }

  if (key == ' ')
  {
    dodge = true;
  }
}

void keyReleased()
{
  if (key == 'd')
  {
    right = false;
  }
  if (key == 'a')
  {
    left = false;
  }
  if (key == 'w')
  {
    speed = 3;
    up = false;
  }
  if (key == 's')
  {
    down = false;
  }
}
class Enemies
{
  
  //set the enemies position
  PVector enemiesPos = new PVector(0, 0);
  //set and initialize the enemies speed
  float enemiesSpeed = 0;
  //set the enemies size
  float enemiesSize;
  //temp variable
  float tempEnemiesSize;
  //temp variable
  float tempEnemiesSpeed;
  //temp variable
  float r1;
  float g1;
  float b1;


  //Initialize all variables for enemies
  Enemies(float enemiesPosX, float enemiesPosY, float tempEnemiesSpeed, float tempEnemiesSize)
  {
    enemiesSize = tempEnemiesSize;
    enemiesPos.x = enemiesPosX;
    enemiesPos.y = enemiesPosY;
    enemiesSpeed = tempEnemiesSpeed;
  }


  //draw enemies
  void drawEnemies()
  {
    //using user defined function to draw enemies(see bottom) 
    enemiesShape();
  }

  //function to define the movement of the enemies
  void moveEnemies()
  {
    //set the horizontal movement
    enemiesPos.x = enemiesPos.x + enemiesSpeed;
    //set the vertical movement(simulate gravity)
    enemiesPos.y = enemiesPos.y + speed;
    //if touches the edges, bounce back
    if (enemiesPos.x+enemiesSize>width || enemiesPos.x<0)
    {
      enemiesSpeed = enemiesSpeed*-1;
    }
    //constrain enemies inside the screen
    enemiesPos.y = constrain(enemiesPos.y, 0, height-enemiesSize);
  }

  //detect the collision between enemies and the walls and the players.
  void enemiesDetect()
  {
    //if entering right side wall, keep enemies outside the walls
    if (enemiesPos.x>wall.x && enemiesPos.x<wall.x+widthWall && enemiesPos.y+enemiesSize>wall.y && enemiesPos.y<wall.y+20)
    {
      enemiesPos.y = wall.y-enemiesSize;
    }
    //if entering left side wall, keep enemies outside the walls
    if (enemiesPos.x>walll.x && enemiesPos.x<widthWalll && enemiesPos.y+enemiesSize>walll.y  && enemiesPos.y<walll.y+20)
    {
      enemiesPos.y = walll.y-enemiesSize;
    }





    //if player hit the enemies, then enemies' size reduce to 0 and stop shooting procedure
    if (pos.x+shootWidth>enemiesPos.x && pos.x+shootWidth<enemiesPos.x+enemiesSize && pos.y+20>enemiesPos.y && pos.y+20+shootHeight<enemiesPos.y+enemiesSize && shoot == true)
    {
      shoot = false;
      enemiesSize = 0;
    }

    //if enemies get to the right bottom corner, then respawn it at the top and increase its size and random its speed.
    if (enemiesPos.y == height-enemiesSize && enemiesPos.x > width-enemiesSize-1)
    {
      int i = 0; 
      i = i+1;
      r1 = random(0, 255);
      g1 = random(0, 255);
      b1 = random(0, 255);
      enemiesPos.x=random(0);
      enemiesPos.y =random(0);
      enemiesSize = enemiesSize + i*10;
      enemiesSpeed = random(1, 10);
    }
  }













































  void enemiesShape()
  {
    fill(r1, g1, b1);
    rectMode(CORNER);
    rect(enemiesPos.x, enemiesPos.y, enemiesSize, enemiesSize);
    fill(0);
    rect( enemiesPos.x+(enemiesSize/5), 5*sin(frameCount)+enemiesPos.y+(enemiesSize/5), enemiesSize/5, enemiesSize/5);
    rect( enemiesPos.x+(3*enemiesSize/5), 5*sin(frameCount)+enemiesPos.y+(enemiesSize/5), enemiesSize/5, enemiesSize/5);
    rect( enemiesPos.x+(enemiesSize/5), 5*sin(frameCount)+enemiesPos.y+(3*enemiesSize/5), -2*sin(frameCount)+enemiesSize/2, enemiesSize/5);
  }
}
//set the variables for walls
PVector wall=new PVector();
PVector walll=new PVector();
float widthWall;
float widthWalll;

class Landscape
{

  //initialize variables
  public Landscape()
  {
    wall = new PVector(300, 300);
    walll = new PVector(0, 0);
  }

  //Functions for drawing the walls which are on the right side of the screen
  void drawWallRight(float tempWallx, float tempWally)
  {
    wall.x = tempWallx;
    wall.y = tempWally;
    //set the width of the wall extend all the way against the right side of the screen
    widthWall = width - wall.x;
    //draw the rectangle that represents the wall.
    rect(wall.x, wall.y, widthWall, 20);
  }

  //Functions for drawing the walls which are on the left side of the screen
  void drawWallLeft(float tempwalllX, float tempwalllY)
  {
    widthWalll = tempwalllX;
    walll.y = tempwalllY;
    //draw the rectangle
    rect(walll.x, walll.y, widthWalll, 20);
  }


  //functions to detect the collision between the players and the walls
  void detect()
  {
    //if player enters the wall zone, keep player outside of the wall. (for the right walls)
    if (pos.x>wall.x && pos.x<wall.x+widthWall && pos.y+60>wall.y && pos.y<wall.y+20 && up == true)
    {
      pos.y = wall.y+20;
    }
    if (pos.x+20>wall.x && pos.x<wall.x+widthWall && pos.y+60>wall.y && pos.y<wall.y+20 && faceRight == true && up == true)
    {
      pos.x = wall.x-20;
    }
    if (pos.x>wall.x && pos.x<wall.x+widthWall && pos.y+60>wall.y && pos.y<wall.y+20 && up == false)
    {
      pos.y = wall.y - 60;
    }

    //if player enters the wall zone, keep player outside of the wall. (for the left walls)
    if (pos.x>=walll.x && pos.x<walll.x+widthWalll && pos.y+60>walll.y && pos.y<walll.y+20 && up == true)
    {
      pos.y = walll.y+20;
    }
    if (pos.x>=walll.x && pos.x<walll.x+widthWalll && pos.y+60>walll.y && pos.y<walll.y+20 && up == false)
    {
      pos.y = walll.y - 60;
    }
  }
}
//I did not put these into the class is because it will be needed in other class and in main code.

PVector pos=new PVector();//Position of the player
float speed;//speed of the player
float gravity=2;
boolean right;//movement triggers
boolean left;//
boolean up;//^
boolean down;//^
boolean dodge;//triggers dodge 
boolean faceRight;//to represent when player is faacing right
boolean faceLeft;//to represent when player is faacing left
float shootSpeed;//set bullet speed
boolean shoot;//shoot bullet
float shootWidth;//bullet width
float shootHeight;//bullet height
float fadeSpeed = 0.5;

class Player
{





  PVector feetPosL;//left foot position
  PVector feetPosR;//right foot position



  //initialize all the variables
  public Player()
  {
    feetPosL=new PVector();
    feetPosR=new PVector();
    pos = new PVector(200, 200);
    speed = 3;
    right = false;
    left = false;
    up = false;
    down = false;
    dodge = false;
    faceLeft = false;
    faceRight = false;
    shoot = false;
    shootHeight = 10;
    shootWidth = 50;
    shootSpeed = 8;
    gravity = 2;
  }

  //functions to draw the player
  void playerShape()
  {


    if (faceRight == false && faceLeft == false)
    {
      fill(0);
      noStroke();
      //--------------Head parts------------------
      rect(pos.x, pos.y, 20, 10);//the upper hair
      rect(pos.x, pos.y+10, 10, 10);//the lower hair
      fill(255, 187, 160);//change face color to light red
      rect(pos.x+10, pos.y+10, 10, 10); //the face
      //---------------Body parts-----------------
      fill(155, 8, 0);//change body color to dark red
      rect(pos.x+5, pos.y+20, 10, 20);//the body
      fill(155, 103, 0);//change the pants color to dark brown
      //---------------feet parts-----------------
      rect(feetPosL.x+7.5, feetPosL.y, 5, 10);//the pants
      rect(feetPosR.x+7.5, feetPosR.y, 5, 10);//the pants
      fill(0, 0, 0);//change the boots' color to black
      rect(feetPosL.x+7.5, feetPosL.y+10, 5, 10);//the boots
      rect(feetPosR.x+7.5, feetPosR.y+10, 5, 10);//the boots
    }
    //in player is facing right, then draw the rightside of the body
    if (faceRight == true)
    {
      fill(0);
      noStroke();
      //--------------Head parts------------------
      rect(pos.x, pos.y, 20, 10);//the upper hair
      rect(pos.x, pos.y+10, 10, 10);//the lower hair
      fill(255, 187, 160);//change face color to light red
      rect(pos.x+10, pos.y+10, 10, 10); //the face
      //---------------Body parts-----------------
      fill(155, 8, 0);//change body color to dark red
      rect(pos.x+5, pos.y+20, 10, 20);//the body
      fill(155, 103, 0);//change the pants color to dark brown
      //---------------feet parts-----------------
      rect(feetPosL.x+7.5, feetPosL.y, 5, 10);//the pants
      rect(feetPosR.x+7.5, feetPosR.y, 5, 10);//the pants
      fill(0, 0, 0);//change the boots' color to black
      rect(feetPosL.x+7.5, feetPosL.y+10, 5, 10);//the boots
      rect(feetPosR.x+7.5, feetPosR.y+10, 5, 10);//the boots
    }
    //in player is facing left, then draw the leftside of the body
    if (faceLeft == true)
    {
      fill(0);
      noStroke();
      //--------------Head parts------------------
      rect(pos.x, pos.y, 20, 10);//the upper hair
      rect(pos.x+10, pos.y+10, 10, 10);//the lower hair
      fill(255, 187, 160);//change face color to light red
      rect(pos.x, pos.y+10, 10, 10); //the face
      //---------------Body parts-----------------
      fill(155, 8, 0);//change body color to dark red
      rect(pos.x+5, pos.y+20, 10, 20);//the body
      fill(155, 103, 0);//change the pants color to dark brown
      //---------------feet parts-----------------
      rect(feetPosL.x+7.5, feetPosL.y, 5, 10);//the pants
      rect(feetPosR.x+7.5, feetPosR.y, 5, 10);//the pants
      fill(0, 0, 0);//change the boots' color to black
      rect(feetPosL.x+7.5, feetPosL.y+10, 5, 10);//the boots
      rect(feetPosR.x+7.5, feetPosR.y+10, 5, 10);//the boots
    }
  }




  //move the player
  void playerMove()
  {
    //set the feet position
    feetPosL = new PVector(pos.x, pos.y+40);
    feetPosR = new PVector(pos.x, pos.y+40);

    //go right and do animation using sine when d is pressed
    if (right == true)
    {
      faceRight = true;
      faceLeft = false;
      pos.x = pos.x + speed;
      feetPosL.x = 3*sin(frameCount/5)+pos.x;
      feetPosR.x = 3*-sin(frameCount/5)+pos.x;
    }
    //go left and do animation using sine when a is pressed
    if (left == true)
    {
      faceLeft = true;
      faceRight = false;
      pos.x = pos.x - speed;
      feetPosL.x = 3*sin(frameCount/5)+pos.x;
      feetPosR.x = 3*-sin(frameCount/5)+pos.x;
    }
    //go up and don't do animation when w is pressed
    if (up == true)
    {
      speed = speed + 0.1;
      speed = constrain(speed, 0, 10);
      pos.y = pos.y-speed;
      fill(255, 0, 0, 100);
      ellipse(-sin(frameCount)+pos.x+10, pos.y+60, 10, 10);
      fill(255, 0, 0, 50);
      ellipse(sin(frameCount)+pos.x+10, pos.y+60, 20, 20);
      fill(255, 100, 0);
      triangle(feetPosL.x+7.5, feetPosL.y+20, feetPosL.x+12.5, feetPosL.y+20, 3*sin(frameCount)+feetPosL.x+10, feetPosL.y+50);
    }





    //constrain the player inside the screen
    pos.x = constrain(pos.x, 0, width-20);
    pos.y = constrain(pos.y, 0, height-80);



    //if dodge, then teleport player ahead for 200 pixels/frame and then turn dodge to false so the player can stop.
    if (dodge == true && right == true)
    {
      pos.x = pos.x + 200;
      fill(255, 0, 0);
      triangle(pos.x, pos.y+60, pos.x, pos.y, pos.x-200, pos.y+30);
      dodge = false;
    }

    if (dodge == true && left == true)
    {
      pos.x = pos.x - 200;
      fill(255, 0, 0);
      triangle(pos.x, pos.y+60, pos.x, pos.y, pos.x+200, pos.y+30);
      dodge = false;
    }


    //if shoot and face left, draw a rectangle at the left side of the player and make it height decline by 0.5 pixel per frame and its width increase.
    if (shoot == true && faceLeft == true)
    {
      fill((random (0, 255)), (random (0, 255)), (random (0, 255)));
      rect(pos.x+10, pos.y+20, shootWidth, shootHeight);
      shootHeight = shootHeight - fadeSpeed;
      shootWidth  = shootWidth - shootSpeed;
      shootHeight = constrain(shootHeight, 0, 10);
      shootWidth = constrain(shootWidth, -1000, 1000);
    }

    //if shoot and face right, draw a rectangle at the right side of the player and make it height decline by 0.5 pixel per frame and its width increase.
    if (shoot == true && faceRight == true)
    {
      fill((random (0, 255)), (random (0, 255)), (random (0, 255)));
      rect(pos.x+10, pos.y+20, shootWidth, shootHeight);
      shootHeight = shootHeight - fadeSpeed;
      shootWidth  = shootWidth + shootSpeed;
      shootHeight = constrain(shootHeight, 0, 10);
      shootWidth = constrain(shootWidth, -1000, 1000);
    }

    //simulate gravity on player
    pos.y = pos.y + gravity;

  }
}


//if mouseClicked, then shoot.
void mousePressed()
{
  shoot = true;
}

//reset variables
void mouseReleased()
{
  shoot = false;
  shootWidth = 10;
  shootHeight = 10;
}