Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*SWING
 In this game, the goal is to swing to the other side. Use left and right to move.
 Hold right to get onto the rope with the right timing. Use down to jump off the 
 rope at the end. If you fall, you die */

float playerPosX = 10;
float playerPosY = 235;
float ropeX = 200 + sin(frameCount/30.0)*100;
float ropeY = 240 + abs(cos(frameCount/30.0)*40);
float cloud1YPos = random(0, 400);
float cloud2YPos = random(0, 400);
float cloud3YPos = random(0, 400);
float cloudSize1X = random(35, 60);
float cloudSize1Y = random(15, 25);
float cloudSize2X = random(35, 60);
float cloudSize2Y = random(15, 25);
float cloudSize3X = random(35, 60);
float cloudSize3Y = random(15, 25);
float cloud1Y = random(-10, 10);
float cloud2Y = random(-10, 10);
float cloud3Y = random(-10, 10);
float moveCloud = 62;
float moveCloud2 = 170;
float moveCloud3 = 255;

void setup() {
  size(400, 400);
}



void draw() {

  //Local Variables
  ropeX = 200 + sin(frameCount/30.0)*100;
  ropeY = 240 + abs(cos(frameCount/30.0)*40);


  //background
  background(252, 177, 58);
  for (float i = 255; i > -20000; i = i - 225) {
    moveCloud = moveCloud + 0.0005;
    drawCloud1( i + moveCloud);
  }
  for (float i = 170; i > -20000; i = i - 275) {
    moveCloud2 = moveCloud2 + 0.001;
    drawCloud2( i + moveCloud2);
  }
  for (float i = 5; i > -20000; i = i - 300) {
    moveCloud3 = moveCloud3 + 0.005;
    drawCloud3( i + moveCloud3);
  }
  //Platforms
  rectMode(CORNER);
  fill(0);
  rect(0, 260, 100, 40);
  rect(300, 260, 100, 40);
  drawRope();


  //Restricting player to screen
  if (playerPosX - 15/2 < 0) {
    playerPosX = 0 + 15/2;
  }
  if (playerPosX + 15/2 > width) {
    playerPosX = width - 15/2;
  }


  //***Swinging Restrictions***
  //Player standing on platforms
  if (playerPosX<100 || playerPosX>300) {
    drawPlayer(playerPosX, playerPosY);
    playerPosY = 235;
    //Player Swinging
  } else if (playerPosX>100 && playerPosX<300 && playerPosX>ropeX-15 && playerPosX<ropeX+15 && playerPosY>ropeY-15 && playerPosY<ropeY+15) {
    drawSwingingPlayer(playerPosX, playerPosY, ropeX, ropeY);
    playerPosX = ropeX - 10;
    playerPosY = ropeY + 2;
  } else {        //player falling
    drawPlayer(playerPosX, playerPosY);
    playerPosY = playerPosY + 6;
  }
  //Player restart when falling off screen
  if (playerPosY>420) {
    playerPosX = 10;
    playerPosY = 235;
  }
}




void drawPlayer(float playerPosX, float playerPosY) {
  stroke(0);
  ellipseMode(CENTER);
  fill(0);
  //body
  line(playerPosX, playerPosY, playerPosX, playerPosY + 15);
  //arms
  line(playerPosX - 6, playerPosY + 10, playerPosX + 6, playerPosY + 10);
  //legs
  line(playerPosX, playerPosY + 15, playerPosX - 3, playerPosY + 25);
  line(playerPosX, playerPosY + 15, playerPosX + 3, playerPosY + 25);
  //head
  ellipse(playerPosX, playerPosY, 15, 15);
}




void drawSwingingPlayer(float playerPosX, float playerPosY, float ropeX, float ropeY) {
  stroke(0);
  fill(0);
  //body
  line(playerPosX, playerPosY, playerPosX, playerPosY + 15);
  //legs
  line(playerPosX, playerPosY + 15, playerPosX - 3, playerPosY + 25);
  line(playerPosX, playerPosY + 15, playerPosX + 3, playerPosY + 25);
  //arms
  line(playerPosX, playerPosY+10, ropeX, ropeY);
  //head
  ellipse(playerPosX, playerPosY, 15, 15);
}




void drawRope() {
  stroke(0);
  //Pole
  ellipseMode(CENTER);
  ellipse(200, 130, 20, 20);

  //rope
  float ropeXP = 200 + sin(frameCount/30.0)*40;
  float ropeYP = 240 + cos(frameCount/30.0)*20;
  noFill();
  curve(200, 130, 200, 130, ropeX, ropeY, ropeXP, ropeYP);
}




//Clouds
void drawCloud1(float cloudX1) {
  ellipseMode(CENTER);
  fill(255, 150);
  noStroke();
  ellipse(cloudX1, cloud1YPos, cloudSize1X, cloudSize1Y);
  ellipse(cloudX1 + 15, cloud1YPos - cloud1Y, cloudSize2X, cloudSize2Y);
}
void drawCloud2(float cloudX2) { 
  ellipse(cloudX2, cloud2YPos, cloudSize3X, cloudSize3Y);
  ellipse(cloudX2 + 12, cloud2YPos + cloud3Y, cloudSize2X, cloudSize2Y);
  ellipse(cloudX2 - 20, cloud2YPos + cloud2Y, cloudSize2X, cloudSize2Y);
}
void drawCloud3(float cloudX3) { 
  ellipse(cloudX3, cloud3YPos, cloudSize1X, cloudSize2Y);
  ellipse(cloudX3 + 12, cloud3YPos + cloud1Y, cloudSize3X, cloudSize1Y);
  ellipse(cloudX3 - 20, cloud3YPos + cloud2Y, cloudSize2X, cloudSize1Y);
}


//***Controls***
void keyPressed() {
  if (key==CODED) {
    if (keyCode==LEFT)
      playerPosX = playerPosX - 3;
  } 
  if (keyCode==RIGHT) {
    playerPosX = playerPosX + 3;
  }
  if (keyCode==DOWN && playerPosX==ropeX-10) {
    playerPosX = playerPosX + 30;
  }
}