As computing becomes more ubiquitous in our objects, designers need to be more aware of how to design meaningful interactions into electronically enhanced objects. At the University of Washington, a class of junior Interaction Design majors is exploring this question. These pages chronicle their efforts.

Tuesday, April 9, 2013

Randy // Processing: Mario


Collect all the coins before time runes out, and without getting hit by bullets.

//SET NUMBER OF COINS!
int numberCoins = 25;

// Images.
PImage mario;
PImage coin;
PImage background;
PImage grass;
PImage win;
PImage lose;
PImage bullet;

// Fonts.
PFont font;

// Window size.
int windowSizeX = 640;
int windowSizeY = 360;

// Image size;
int sizeX = 16;
int sizeY = 32;

// Starts the image being controlled at top left.
int myX = 568;
int myY = 180;

// Keep count for your coins.
String coinImage;
int coinCount = 0;
int coinRotate = 0;

// Position of coins.
int[] coinsPosX = new int[numberCoins];
int[] coinsPosY = new int[numberCoins];

// Bullet.
int bulletNumber = 0;
int[] bulletX = new int[numberCoins];
int[] bulletY = new int[numberCoins];
int[] bulletPathX = new int[numberCoins];
int[] bulletPathY = new int[numberCoins];
int[] bulletTime = new int[numberCoins];
boolean unleashedBullet = false;

// Win.
int winX = 0;
int winY = -360;
boolean hasWon = false;

// Lose.
int loseX = 0;
int loseY = -360;
boolean hasLost = false;

// Timer
int now = second();
int time = numberCoins;

void setup() {
  coinImage = "coin1.png";
  mario = loadImage("marioRight.png");
  background = loadImage("background.png");
  coin = loadImage(coinImage);
  grass = loadImage("grass.png");
  bullet = loadImage("bulletLeft.png");
  font = loadFont("MyriadPro-Light-30.vlw");
  textFont(font, 30);
  textAlign(RIGHT);
  win = loadImage("win.png");
  lose = loadImage("lose.png");
  size(640, 360);
  fill(204);

  for (int i = 0; i < numberCoins; i++) {
    coinsPosX[i] = ((int)random(32) * 16 + 56);
  }

  for (int i = 0; i < numberCoins; i++) {
    coinsPosY[i] = ((int)random(7) * 32 + 84);
  }

  for (int i = 0; i < numberCoins; i++) {
    bulletX[i] = -40;
    bulletY[i] = -32;
    bulletPathX[i] = 0;
    bulletPathY[i] = (int)random(7) * 32 + 84;
    bulletTime[i] = (int)random(numberCoins);
  }
}

void draw() {
  background(background);
  fill(0);
  image(mario, myX, myY);
  for (int i = 0; i < numberCoins; i++) {
    image(coin, coinsPosX[i], coinsPosY[i]);
  }
  image(win, winX, winY);
  image(lose, loseX, loseY);

  for (int i = 0; i < numberCoins; i++) {
     image(bullet, bulletX[i], bulletY[i]);
  }

  // Keep track of time in variable "time".
  int then = second();
  if (now != then) {
    // If bullet time, unleash bullet.
    for (int i = 0; i < numberCoins; i++) {
      if (bulletTime[i] == time) {
        bulletY[bulletNumber] = bulletPathY[i];
        bulletNumber++;
      }
    }
    time -= 1;
    now = then;
 
    for (int i = 0; i < numberCoins; i++) {
      if (bulletNumber >= i) {
        bulletX[i] += 32;
      }
   
      if (time % 3 == 0) {
        coinImage = "coin1.png";
      } else if (time % 3 == 1) {
        coinImage = "coin2.png";
      } else {
        coinImage = "coin3.png";
      }
    }
  }

  for (int i = 0; i < numberCoins; i++) {
    if (bulletX[i] == myX && bulletY[i] == myY) {
      loseX = 0;
      loseY = 0;
      hasLost = true;
    }
 
    if (bulletX[i] + 16 == myX && bulletY[i] == myY && hasWon == false) {
      loseX = 0;
      loseY = 0;
      hasLost = true;
    }
  }

  // If player wins, stop countdown.
  if (time > 0 && hasWon == false && hasLost == false) {
    text(time, 633, 30);
  }

  // If time runs out, display lose message.
  if (time < 0 && hasWon == false) {
    loseX = 0;
    loseY = 0;
    hasLost = true;
  }
}

void keyPressed() {
  char buttonPressed = (key);

  if (buttonPressed == 'w') {
    mario = loadImage("marioUp.png");
    if (myY > 84) {
      image(mario, myX, myY - 32);
      myY -= 32;
    }
  }

  if (buttonPressed == 's') {
    mario = loadImage("marioDown.png");
    if (myY < 276) {
      image(mario, myX, myY + 32);
      myY += 32;
    }
  }

  if (buttonPressed == 'a') {
    mario = loadImage("marioLeft.png");
    if (myX > 56) {
      image(mario, myX - 16, myY);
      myX -= 16;
    }
  }

  if (buttonPressed == 'd') {
    mario = loadImage("marioRight.png");
    if (myX < 568) {
      image(mario, myX + 16, myY);
      myX += 16;
    }
  }

  for (int i = 0; i < numberCoins; i++) {
    if (coinsPosX[i] == myX && coinsPosY[i] == myY) {
      coinsPosX[i] = 5 + (coinCount * 16);
      coinsPosY[i] = 0;
      coinCount++;
    }
  }

  if (numberCoins <= coinCount && hasLost == false) {
    winX = 0;
    winY = 0;
    hasWon = true;
  }

  if (buttonPressed == 'r') {
    // Starts the image position.
    myX = 568;
    myY = 180;
 
    // Keep count for your coins.
    coinCount = 0;
    coinRotate = 0;
 
    // Position of coins.
    coinsPosX = new int[numberCoins];
    coinsPosY = new int[numberCoins];
 
    // Bullet.
    bulletNumber = 0;
    bulletX = new int[numberCoins];
    bulletY = new int[numberCoins];
    bulletPathX = new int[numberCoins];
    bulletPathY = new int[numberCoins];
    bulletTime = new int[numberCoins];
    unleashedBullet = false;
 
    // Win.
    winX = 0;
    winY = -360;
    hasWon = false;
 
    // Lose.
    loseX = 0;
    loseY = -360;
    hasLost = false;
 
    // Timer
    now = second();
    time = numberCoins;
    setup();
  }
}

No comments:

Post a Comment