Thursday, October 30, 2014

Interactive Processing Exercise

For this exercise our team made a basic snake game using Processing.  To make the controls interactive, we used Makey-makey with a wooden snake.  The idea was that if you turned the snake right or left, the snake on the screen would also turn right or left.  This interaction didn't work out perfectly, but we did come up with more ideas to make it work better in the future.

As an additional feature, we included sound and color.  The snake begins black, and slowly becomes more colorful as the player acquires more of the "fruits". Additionally, past a certain point, the intro to "Sandstorm" by Darude starts playing on loop.  When the snake reaches complete white color, the snake starts flashing rainbow and Sandstorm plays at full force.  During this time the snake is invincible, and cannot die by running into themselves. The song plays for several seconds, and then stops, with the snake black again.  The process then repeats until the player loses the game.

With Jason Hilegmeier, Nicholas Smith, and Dean Zhu



Processing Code:

import ddf.minim.*;
Minim minim;

sound buildUp;
sound buildUpLoop;
sound drop;
PImage img;

boolean up, down, right, left;
int posX = 0;
int posY = 0;
ArrayList snake;
int time = 0;
int snakeInc = 4;
point fruit = new point(0,0);
int score = 0;
int red = 5;
int green = 5;
int blue = 5;
int backgroundValue = 200;
boolean godMode = false;
boolean start = false;
boolean loop = false;
int timer = 0;

void setup(){
  up = true;
  fruit.x = 0;
  fruit.y = 0;
  moveFruit();
  minim = new Minim(this);
  img = loadImage("Terrbear.jpg");
  buildUp = new sound("loopStart.wav");
  buildUpLoop = new sound("loopContinue.wav");
  drop = new sound("He-man.mp3");
  smooth();
  size(500, 400);
  posX = width/2;
  posY = height/2;
  snake = new ArrayList();
  snake.add(new point(posX, posY));
  snake.add(new point(posX, posY - 5));
  snake.add(new point(posX, posY - 10));
}

void keyPressed(){
 if (keyCode == RIGHT){
  if(up){
   right = true; up = false; left = false; down = false;
  }
  else if(right){
   right = false; up = false; left = false; down = true;
  }
  else if(left){
   right = false; up = true; left = false; down = false;
  }
  else if(down){
   right = false; up = false; left = true; down = false;
  }
 }

 else if (keyCode == LEFT){
  if(down){
   right = true; up = false; left = false; down = false;
  }
  else if(left){
   right = false; up = false; left = false; down = true;
  }
  else if(right){
   right = false; up = true; left = false; down = false;
  }
  else if(up){
   right = false; up = false; left = true; down = false;
  }
 }
}

void moveFruit(){
 fruit.x = 5*(int(random(10, (width - 10)/5)));
 fruit.y = 5*(int(random(10, (height - 10)/5))); 

}

void checkCollide(){
  if ((posX == fruit.x || posX == fruit.x + 5 || posX == fruit.x + 10) && (posY == fruit.y || posY == fruit.y + 5 || posY == fruit.y + 10)){
  snakeInc = 0;
  score +=100;
  red += 30;
  if(backgroundValue > 75) backgroundValue -= 10;
  if(red >= 255) green +=30;
  if(green >= 255) blue +=30;
  moveFruit();
  }
  if(!godMode){
  for(int i = 1; i < snake.size()-1; ++i){
    point point = snake.get(i);
    if (posX == point.x && posY == point.y) { print("Score: ", score); exit();}
  }
  }
  //if(posX == width || posX == 0 || posY == height || posY == 0){ print("Score: ", score); exit();}
  if(posX == width) posX = 5;
  else if(posX == 0) posX = width - 5;
  else if(posY == height) posY = 5;
  else if(posY == 0) posY = height - 5;
}
  

void draw() {
  time++;
  if(time % 1 == 0){
  if(godMode) timer++;
  if(timer > 1000){
    timer = 0;
    godMode = false;
   drop.song.pause();
    red = 5; blue = 5; green = 5;
    backgroundValue = 200;
  }
  checkCollide();
  background(backgroundValue, backgroundValue, backgroundValue); 
  fill(255, 255, 255);
  rect(fruit.x, fruit.y, 15, 15);
    stroke (red, green, blue);
  fill (red, green, blue); 
  
  if(!buildUp.song.isPlaying() && !buildUpLoop.song.isPlaying() && loop == true){
    buildUp.song.rewind();
    buildUpLoop.song.loop();
  }
  
  if(blue >= 150 && !buildUp.song.isPlaying() && !buildUpLoop.song.isPlaying()) {
    buildUp.song.play();
    loop = true;
  }

  
   if(red >=255 && green >= 255 && blue >=255) {
    godMode = true;
    buildUpLoop.song.pause();
    buildUpLoop.song.rewind();
    drop.song.play();
    image(img, random(0, width), random(0, height));
    int r = int(random(0,255));
    int g = int(random(0,255));
    int b = int(random(0, 255));
    stroke(r, g, b);
    fill(r, g, b);
  }


  if(up){
    posY -= 5;
    snake.add(new point(posX, posY));
    if(snakeInc < 3) snakeInc++;
    else{ snake.remove(0);}
  }
  
  if(right){
    posX += 5;
    snake.add(new point(posX, posY));
    if(snakeInc < 3) snakeInc++;
    else{ snake.remove(0);}
  }
  
  if(down){
    posY += 5;
    snake.add(new point(posX, posY));
    if(snakeInc < 3) snakeInc++;
    else{ snake.remove(0);}
  }
  
  if(left){
    posX -= 5;
    snake.add(new point(posX, posY));
    if(snakeInc < 3) snakeInc++;
    else{ snake.remove(0);}
  }
  for(int i = 0; i
    point point = snake.get(i);
    rect(point.x, point.y, 5, 5);
  }
  }
}

class sound {
   AudioSnippet song;
   sound (String filename) {
    // load "dingdong.wav" into a new AudioPlayer
    song = minim.loadSnippet(filename);
  }
}

class point{
  point(int tempx, int tempy){x = tempx; y = tempy;}
  int x;
  int y;
}

No comments:

Post a Comment