Minggu, 09 Juni 2013

Tugas Softskill (Pengantar Teknologi Game)

Nama : Parlin Erwin Gunawan
NPM : 54410322
Kelas : 3IA15
Tugas : Pengantar Teknologi Game (Processing)

"SNAKE"


LISTING PROGRAM :

//Codingan Snake:

Snake

//Variables pertaining to the fundamental functioning
final int SCREEN_SIZE = 600; //Size of the screen  (Codingan untuk ukuran size windows)
PFont font; //Font used in the game
int score = 0; //Score of the player
int rainbowFreq = 10, rainbowTimer = rainbowFreq; //Rainbow timers
PVector rainbowColor = new PVector(255, 255, 255); //The color of the rainbow color
boolean pauseState = false; //If the game is paused or not
boolean showInstructionMenu = true; //If the instruction menu should be shown
int superModeTimer = 0, superModeMax = 15, chanceOfSuperMode = 10; //If supermode is on

//Variables pertaining to the segments
ArrayList<Segment> segments = new ArrayList<Segment>(); //Creates an arraylist holding the segments
char currentDirection, lastDirection; //The current direction of the snake head
int segmentSize = 20; //Size of the segment
int snakeMovementTimer = 0, snakeMovementDelay = 4; //The timers for the snake's movement
boolean addSegmentNextUpdate = false; //If a segment should be added at the next update

//Variables pertaining to the food
Food food = new Food(); //Creates a new food
SuperFood superFood = null; //Creates the superfood

void setup() { //Main method
  size(600, 600, JAVA2D); //Creates a screen
  font = loadFont("FranklinGothic-MediumCond-48.vlw"); //Loads the font from the data folder
  textAlign(CENTER); //Aligns text to the center
  textFont(font, SCREEN_SIZE/25); //Sets the text font

  segments.add(new Segment(SCREEN_SIZE/2, SCREEN_SIZE/2)); //Adds the head of the snake
}

void draw() { //Continual loop
  if(!pauseState) rainbowTimer++; //Increments the rainbow color timer
  if(rainbowTimer >= rainbowFreq) { //If the time for color change
    rainbowColor = new PVector(random(255), random(255), random(255)); //Changes it to a random color
    rainbowTimer = 0; //Resets the rainbow timer
  }

  if(!pauseState) snakeMovementTimer++; //Increments the snake movement timer if not paused
  if(snakeMovementTimer == snakeMovementDelay) { //If the time for movement has come
    updateSnake(); //Update the snake
    snakeMovementTimer = 0; //Reset the timer
  }

  if(superModeTimer >= 0) superModeTimer--; //Decrements the super timer
  if(superFood == null && superModeTimer < 0 && !pauseState && !showInstructionMenu)
    if(random(1000) < chanceOfSuperMode) superFood = new SuperFood();

#(Codingan untuk background)
  background(0); //Sets the background to black
  for(int i = 0; i < segments.size(); i++) //Checks each segment
    segments.get(i).renderSegment(); //Renders the segment
  food.renderFood(); //Renders the food
  if(superFood != null) superFood.renderFood(); //Renders superfood if it is not null

  fill(255); //Colors white
  text("Score: " + score, SCREEN_SIZE/2, segmentSize*6/5); //Writes the score for the player

  if(pauseState) { //If the game is paused
    fill(0, 0, 0, 100); //Fills a translucent black
    noStroke(); //Cancels the stroke
    rect(0, 0, SCREEN_SIZE, SCREEN_SIZE); //Cover the screen with a transluscent black
    fill(255); //Colors white
    text("Game Paused", SCREEN_SIZE/2, SCREEN_SIZE/2);
    text("Press 'P' To Continue", SCREEN_SIZE/2, SCREEN_SIZE/2 + segmentSize*6/5);
  } else if(showInstructionMenu) { //If the instruction menu is shown
    text("Use The Arrows To Move", SCREEN_SIZE/2, SCREEN_SIZE*3/4);
    text("Press 'P' To Pause", SCREEN_SIZE/2, SCREEN_SIZE*3/4 + segmentSize*6/5);
  }
}

void keyPressed() { //Called when a key is pressed
  switch(keyCode) { //Checks the key pressed
    case UP: if(segments.size() == 1 || lastDirection != 'S' && !pauseState) currentDirection = 'W'; break; //Changes direction to up
    case DOWN: if(segments.size() == 1 || lastDirection != 'W' && !pauseState) currentDirection = 'S'; break; //Changes direction to down
    case LEFT: if(segments.size() == 1 || lastDirection != 'D' && !pauseState) currentDirection = 'A'; break; //Changes direction to left
    case RIGHT: if(segments.size() == 1 || lastDirection != 'A' && !pauseState) currentDirection = 'D'; break; //Changes direction to right
    case 'P': case 'p': pauseState = !pauseState; break; //Toggles the pause
  }
  showInstructionMenu = false; //Take off the instruction menu
}

void updateSnake() { //Updates the snake's position
  if(addSegmentNextUpdate) //If must add a new segment
    segments.add(new Segment((int)segments.get(segments.size() - 1).location.x, (int)segments.get(segments.size() - 1).location.y)); //Adds a segment at the tail
  for(int i = segments.size() - 1; i > 0; i--) { //Checks each segments besides the head
    if(!addSegmentNextUpdate) segments.get(i).location = new PVector(segments.get(i - 1).location.x, segments.get(i - 1).location.y); //Sets the location to the block ahead
    addSegmentNextUpdate = false; //Removes the need to add a new segment
  }

#(Codingan untuk menggerakkan snake)  
  switch(currentDirection) { //Checks the current direction
    case 'W': segments.get(0).location.y -= segmentSize; break; //Moves the snake head up
    case 'S': segments.get(0).location.y += segmentSize; break; //Moves the snake head down
    case 'A': segments.get(0).location.x -= segmentSize; break; //Moves the snake head left
    case 'D': segments.get(0).location.x += segmentSize; break; //Moves the snake head right
  }
  lastDirection = currentDirection; //Sets the last direction used as the current direction

  for(int i = 2; i < segments.size(); i++) //Checks each segment besides the head
    if(superModeTimer < 0 && segments.get(i).location.x == segments.get(0).location.x && segments.get(i).location.y == segments.get(0).location.y) //If a segment hits the head
      startNewGame(); //Starts a new game
   
    if(segments.get(0).location.x >= SCREEN_SIZE || segments.get(0).location.x < 0)
      if(superModeTimer >= 0) segments.get(0).location.x = (segments.get(0).location.x >= SCREEN_SIZE) ? 0 : SCREEN_SIZE - segmentSize;
      else startNewGame();
     
    if(segments.get(0).location.y >= SCREEN_SIZE || segments.get(0).location.y < 0)
      if(superModeTimer >= 0) segments.get(0).location.y = (segments.get(0).location.y >= SCREEN_SIZE) ? 0 : SCREEN_SIZE - segmentSize;
      else startNewGame();
}

#(codingan untuk new game)
void startNewGame() { //Starts a new game
  superFood = null; //Nullifies the super food
  score = 0; //Resets the score
  segments.clear(); //Clears all segments
  segments.add(new Segment(SCREEN_SIZE/2, SCREEN_SIZE/2)); //Adds the head of the snake
  snakeMovementTimer = 0; //Reset the timer
  currentDirection = 'Q'; //Nullifys the current direction
  keyCode = '+'; //Nullifys the keyCode
  food = new Food(); //Resets the food
  showInstructionMenu = true; //Show the instruction menu
}

//Codingan untuk Food:
class Food { //Class for food
  PVector location; //Location of the food

  Food() { //Creates a new food
    this.location = new PVector((int)random(SCREEN_SIZE/segmentSize)*segmentSize, (int)random(SCREEN_SIZE/segmentSize)*segmentSize); //Sets food location to a random location
  }

  void renderFood() { //Renders the food
    fill(255, 0, 0); //Fills red
    stroke(255); //Borders with white
    rect(this.location.x, this.location.y, segmentSize, segmentSize); //Draws the food
 
    if(!pauseState && this.location.x == segments.get(0).location.x && this.location.y == segments.get(0).location.y) { //If the head of the snake is on the food and the game is not paused
      food = new Food(); //Moves the food
      addSegmentNextUpdate = true; //Adds a segment
      score++; //Increments the score
    }
  }

}

//Codingan Untuk Segment:

class Segment { //Class for segments
  PVector location; //Location of the segment

  Segment(int locationX, int locationY) { //Creates a new segment with the specified properties
    this.location = new PVector(locationX, locationY); //Sets the location of the segment
  }

  void renderSegment() { //Renders the segment
    if (superModeTimer < 0) fill(255); //Fills white
    else fill(rainbowColor.x, rainbowColor.y, rainbowColor.z); //Fills rainbow
    stroke(255); //Borders with white
    rect(this.location.x, this.location.y, segmentSize, segmentSize); //Draws the segment onto the screen
  }

}

//Codingan untuk Super Food:

class SuperFood extends Food {
  void renderFood() {
    fill(rainbowColor.x, rainbowColor.y, rainbowColor.z); //Fills rainbow
    stroke(255); //Borders with white
    rect(this.location.x, this.location.y, segmentSize, segmentSize); //Draws the segment onto the screen
 
    if(!pauseState && this.location.x == segments.get(0).location.x && this.location.y == segments.get(0).location.y) { //If the head of the snake is on the food and the game is not paused
      superModeTimer = superModeMax * 60;
      superFood = null;
    }
  }
}


Kesimpulan:
game ini butuh sekali keahlian dalam hal kecepatan tangan, agar snake tidak menabrak diri sendiri sehingga tidak game over dan game ini berpacu dalam waktu untuk mendapatkan score tertinggi.
^_^

Tidak ada komentar:

Posting Komentar