Wednesday 21 September 2016

MEDA102 Assessment 2: Digital coding/Processing Sketch



My processing sketch explores the theme of iteration through the repetition of the animation, the condition checks and the fact that the slime cannot escape the screen. Although not very visible in the sketch, my code is evidence of many layers of iteration.
One of the artworks covered in tutorial was George Perec’s The Art of Asking your Boss for a Raise, this artwork is very relatable to my sketch because many different steps are taken with different decisions made along the way. The sketch iterates back to the same point no matter what choices are made. This is encapsulated by the movement and sequence of animations of the slime as it hops around the screen.
Another artist we covered was Sol LeWitt, the somewhat open ended instructions he used in his conceptual art are similar to how the viewer has no set instructions with what to do with the slime, the falling fruit are also limited yet unlimited by this as they are not given a set location in which to fall from.

Screenshots of the sketch and images used (This is an animated sketch which will require pictures, download link for the sketch with images included here: https://drive.google.com/open?id=0B5PsqQISQHqMWWhDSGJ3Nk54azA ):









Code:
//The vast majority of this code is of my own creation
//Declaring all the various global variables
//Generally bad to use global variables but it's easier for Processing
Animation anim1,anim2,fgIMG,bgIMG,idle1,idle2,fruit0,fruit1,fruit2,fruit3,fruit4; //load all the Animation objects
float xpos = 320;
float ypos = 276;
float rXPos = width;
float rYPos = height / 3;
int iLR = 0;
int numFruit = 10; //Limits number of fruit, prevents memory leaks
int iLives = 5;
boolean bIdleLR = true;
Fruit[] fDrop = new Fruit[numFruit]; //Make "numFruit" amount of fruits. In this case, 10


void setup() //Initializes all the animations, the size of the canvas, sets the frame rate and initializes the fruit rain!
{
  size(640, 360);
  frameRate(21);
  anim1 = new Animation("hopL_",6);
  anim2 = new Animation("hopR_",6);
  fgIMG = new Animation("Foreground_",1);
  bgIMG = new Animation("Background_",1);
  idle1 = new Animation("idleL_",10);
  idle2 = new Animation("idleR_",10);
  fruit0 = new Animation("fruit0_",1);
  fruit1 = new Animation("fruit1_",1);
  fruit2 = new Animation("fruit2_",1);
  fruit3 = new Animation("fruit3_",1);
  fruit4 = new Animation("fruit4_",1);
  for(int i = 0;i < fDrop.length;i++) //For the length of Array "fDrop", loop
  {
    fDrop[i] = new Fruit(); //make new Fruit's and fill up the array!
  } 
}

void draw() //Iterates whatever is inside the {} infinitely.
{
  bgIMG.display(-2,0); //Display the background
  for(int i = 0;i < fDrop.length;i++) //Spawn a fruit and drop it as many times as there are fruits stored in the fDrop array
  {
    fDrop[i].Fall();
  }
  if(iLR == 0) //If idle, run idle animation in whichever direction the slime is facing
  {
    if(bIdleLR == true)
    {
      idle1.display(xpos-idle1.getWidth()/2, ypos);
    }
    if(bIdleLR == false)
    {
      idle2.display(xpos-idle2.getWidth()/2, ypos);
    }
  }
  if(iLR == 1) //Hop left
  {
    anim1.display(xpos-anim2.getWidth()/2, ypos);
    xpos = xpos - 4;
  }
  if(iLR == 2) //Hop right
  {
    anim2.display(xpos-anim2.getWidth()/2, ypos);
    xpos = xpos + 4;
  }
  if(xpos < -23) //If slime goes too far left, put slime on other side of screen
  {
    xpos = 660;
  }
  if(xpos > 663) //If slime goes too far right...
  {
    xpos = -20;
  }
  fgIMG.display(-2,298); //Display the foreground
}

void keyPressed()
{
  if(key == CODED) //Checks for predefined key event
  {                //LEFT and RIGHT are such events
    if(keyCode == LEFT)
    {
      bIdleLR = true;  //If the slime was moving left, change idle animation to face left and then keep moving left
      iLR = 1;
    }
    if(keyCode == RIGHT)
    {
      bIdleLR = false;  //Same as above, opposite direction
      iLR = 2;
    }
  }
}

void keyReleased()  //If no keys are pressed, start playing the idle animation
{
    iLR = 0;
}

class Fruit
{
  int randFruit = (int)random(5); //Provides the switch statement with a random number in order to get random fruit
  float xFruit = random(width - 30); //Gives the fruit a random horizontal starting position
  float yFruit = random(-height); //Sets starting height of fruit, its above the top of the screen
 
  void Fall()
  {
    yFruit = yFruit + 7;  //Moves the fruit down by 7px
 
    switch(randFruit) //Assigns a random fruit sprite to the latest iteration of Fall
    {
      case 0:
        fruit0.display(xFruit,yFruit-fruit0.getHeight());
        break;
      case 1:
        fruit1.display(xFruit,yFruit-fruit1.getHeight());
        break;
      case 2:
        fruit2.display(xFruit,yFruit-fruit2.getHeight());
        break;
      case 3:
        fruit3.display(xFruit,yFruit-fruit3.getHeight());
        break;
      case 4:
        fruit4.display(xFruit,yFruit-fruit4.getHeight());
    }
 
    if(yFruit > height)//If the fruit escapes off of the bottom of the screen, put it back on top
    {
      yFruit = 0;
      xFruit = random(width - 30); //Randomizes the horizontal position once fruit has been relocated
    }
  }
}

class Animation  //This class collates all the separate images and turns them into a single animation, I only wrote the last function
{
  PImage[] images;
  int imageCount;
  int frame;
 
  Animation(String imagePrefix, int count) //This constructor collates a series of images into a single animation, stored in an array
  {
    imageCount = count;
    images = new PImage[imageCount]; //Creating a new array of size "imageCount" to hold the individual frames of the animation

    for (int i = 0; i < imageCount; i++)
    {
      //nf() is used to format i into four digits
      String filename = imagePrefix + nf(i, 4) + ".png"; //Setting the read path to: "prefix", then padding i with 0's to the left and finally the file extension.
      images[i] = loadImage(filename);  //Loading the image from the file path and storing it in the appropriate frame
    }
  }

  void display(float xpos, float ypos) //This function plays and displays an animation at a given location (xpos,ypos)
  {
    frame = (frame+1) % imageCount;
    image(images[frame], xpos, ypos);
  } 
  int getWidth()
  {
    return images[0].width; //Provides the width of the first frame
  }
 
  int getHeight()  //This is the only function that I wrote inside this class
  {
    return images[0].height; //Provides the height of the first frame
  }
}

No comments:

Post a Comment