12

TUTORIAL-Game Snake Shenia DI Processing

Sunday, June 4, 2017
Share this Article on :
Salam Sejahtra buat kita semua , Pertama tama marilah kita ucapkan Puji syukur atas kehadirat Tuhan Yang Maha Esa yang mana masih memberikan Rahmat dan limpahannya .
Pernah gak terpikir bagaimana sih membuat Game sederhana Yang di hp hp jadul hehehehe. Baiklah saya di sini akan memberikan Tutorial membuat Game Ular Snake Shenia langsung saja ya

Pertama tama Buka Aplikasi Processing

tinggal Copy Source Code di bawah ini dan masukkan di Processing.





snake test;
food food1;
int highScore;

void setup(){
  size(1000, 600);
  frameRate(14);
  test = new snake();
  food1 = new food();
  rectMode(CENTER);
  textAlign(CENTER, CENTER);
 
  highScore = 0;
}



void draw(){
  background(255, 255, 255);
  drawScoreboard();
 
  test.move();
  test.display();
  food1.display();
 
 
  if( dist(food1.xpos, food1.ypos, test.xpos.get(0), test.ypos.get(0)) < test.sidelen ){
    food1.reset();
    test.addLink();
  }
 
  if(test.len > highScore){
    highScore= test.len;
  }
}


void keyPressed(){
  if(key == CODED){
    if(keyCode == LEFT){
      test.dir = "left";
    }
    if(keyCode == RIGHT){
      test.dir = "right";
    }
    if(keyCode == UP){
      test.dir = "up";
    }
    if(keyCode == DOWN){
      test.dir = "down";
    }
  }
}


void drawScoreboard(){
  // All of the scode for code and title
 
  fill(0, 24, 133);
  textSize(65);
  text( "GAME ULAR", width/2, 80);
 
 
  // draw scoreboard
  stroke(179, 140, 198);
  fill(0,0,255);
  rect(90, 70, 160, 80);
  fill(255, 255, 255);
  textSize(17);
  text( "Score: " + test.len, 70, 50);
 
  fill(255,255,255);
  textSize(17);
  text( "High Score: " + highScore, 70, 70);
}

class food{
 
  // define varibles
  float xpos, ypos;
 
 
 
  //constructor
  food(){
    xpos = random(100, width - 100);
    ypos = random(100, height - 100);
  }
 
 
  // functions
 void display(){
  
   fill(11,0,83);
   ellipse(xpos, ypos,17,17);
 }


 void reset(){
    xpos = random(100, width - 100);
    ypos = random(100, height - 100);
 }  
}

class snake{
 
  //define varibles
  int len;
  float sidelen;
  String dir;
  ArrayList xpos, ypos;
 
  // constructor
  snake(){
    len = 1;
    sidelen = 17;
    dir = "right";
    xpos = new ArrayList();
    ypos = new ArrayList();
    xpos.add( random(width) );
    ypos.add( random(height) );
  }
 
  // functions
 
 
  void move(){
   for(int i = len - 1; i > 0; i = i -1 ){
    xpos.set(i, xpos.get(i - 1));
    ypos.set(i, ypos.get(i - 1)); 
   }
   if(dir == "left"){
     xpos.set(0, xpos.get(0) - sidelen);
   }
   if(dir == "right"){
     xpos.set(0, xpos.get(0) + sidelen);
   }
  
   if(dir == "up"){
     ypos.set(0, ypos.get(0) - sidelen);
 
   }
  
   if(dir == "down"){
     ypos.set(0, ypos.get(0) + sidelen);
   }
   xpos.set(0, (xpos.get(0) + width) % width);
   ypos.set(0, (ypos.get(0) + height) % height);
  
    // check if hit itself and if so cut off the tail
    if( checkHit() == true){
      len = 1;
      float xtemp = xpos.get(0);
      float ytemp = ypos.get(0);
      xpos.clear();
      ypos.clear();
      xpos.add(xtemp);
      ypos.add(ytemp);
    }
  }
 
 
 
  void display(){
    for(int i = 0; i
      stroke(179, 140, 198);
      fill(0, 111, 191, map(i-1, 0, len-1, 250, 50));
      rect(xpos.get(i), ypos.get(i), sidelen, sidelen);
    } 
  }
 
 
  void addLink(){
    xpos.add( xpos.get(len-1) + sidelen);
    ypos.add( ypos.get(len-1) + sidelen);
    len++;
  }
 
   boolean checkHit(){
    for(int i = 1; i < len; i++){
     if( dist(xpos.get(0), ypos.get(0), xpos.get(i), ypos.get(i)) < sidelen){
       return true;
     }
    }
    return false;
   }
}



 Ahkir kata saya ucapkan Terima Kasih



Artikel Terkait:

12 comments:

Muhammad Rifai said...

Oke izin copas yaa

Koplo Pop said...

ijin coba ya min

yogo turnandes said...

nggk jalan gan

Kise Ryota said...

Ijin coba gan

Vanrezky Sadewa said...

coba aja dulu

Vanrezky Sadewa said...

oke

Rangga Saputra said...

Itu bahasanya pke pemrograman Java ya Gan?

Muhai Ni said...

Gan itu bisa di buat ke playstore ga? Mau aku bikin game disana

Isfan Fajar said...

mantap nih gan :)

Vanrezky Sadewa said...

Belum saya coba. Nanti saya coba dulu

Vanrezky Sadewa said...

Processing gan

Vanrezky Sadewa said...

Makasih

Post a Comment