Sunday, 14 February 2016

Tutorial Greenfoot - Membuat Game Sederhana ala Feeding Frenzy


Gambaran Umum Game

• Game ini memiliki skenario sederhana yaitu 1 aktor ikan yang lapar, 2 aktor ikan yang dapat dimakan, 1 aktor ikan yang beracun.
• Game terdiri dari 2 scene yaitu scene Level dan Menu.
• Pada Level1, jika berhasil memakan lebih dari 10 ikan, maka pemain dinyatakan menang. Sedangkan jika pemain menyentuh ikan beracun, maka pemain dinyatakan kalah.


Langkah Pembuatan
Langkah Pembuatannya adalah sebagai berikut :

1) Siapkan Resource Image dan Sound untuk project ini.

(Download di SINI )

Letakan gambar berikut di folder images :
*Semua image berformat .png
Letakan file suara berikut di folder sounds :
*Bisa pakai gambar dan sounds dari anda sendiri, tapi jangan lupa nama dan ekstensi disesuaikan dengan coding nantinya.
Buat subclass-subclass berikut :




2) Mari kita Mulai Coding!

Class Levels

 import greenfoot.*;   
 abstract class Levels extends World  
 {  
   public Levels()  
   {      
     super(800, 600, 1);  
   }  
 }  


Class Level1

 import greenfoot.*;   
 public class Level1 extends Levels  
 {  
   public Level1()  
   {    
     super();  
     prepare();  
  }  
  public void prepare()  
   {  
     Crab crab = new Crab();  
     addObject(crab, 506, 668);  
     Crab crab2 = new Crab();  
     addObject(crab2, 657, 602);  
     crab.setLocation(499, 546);  
     crab2.setLocation(652, 567);  
     crab.setLocation(491, 511);  
     crab.setLocation(482, 501);  
     IkanJahat hungryFish = new IkanJahat();  
     addObject(hungryFish, 434, 135);  
     fish fish = new fish();  
     addObject(fish, 212, 262);  
     fish fish2 = new fish();  
     addObject(fish2, 408, 307);  
     fish fish3 = new fish();  
     addObject(fish3, 506, 313);  
     fish fish4 = new fish();  
     addObject(fish4, 517, 268);  
     fish fish5 = new fish();  
     addObject(fish5, 203, 89);  
     fish fish6 = new fish();  
     addObject(fish6, 147, 51);  
     fish fish7 = new fish();  
     addObject(fish7, 102, 169);  
     fish fish8 = new fish();  
     addObject(fish8, 94, 254);  
     fish fish9 = new fish();  
     addObject(fish9, 180, 320);  
     fish fish10 = new fish();  
     addObject(fish10, 264, 342);  
     fish fish11 = new fish();  
     addObject(fish11, 311, 53);  
     fish fish12 = new fish();  
     addObject(fish12, 519, 56);  
     fish fish13 = new fish();  
     addObject(fish13, 266, 217);  
     fish fish14 = new fish();  
     addObject(fish14, 422, 237);  
     fish fish15 = new fish();  
     addObject(fish15, 89, 99);  
     fish fish16 = new fish();  
     addObject(fish16, 57, 353);  
     fish1 fish1 = new fish1();  
     addObject(fish1, 154, 149);  
     fish1 fish17 = new fish1();  
     addObject(fish17, 259, 259);  
     fish1 fish18 = new fish1();  
     addObject(fish18, 25, 42);  
     fish1 fish19 = new fish1();  
     addObject(fish19, 40, 268);  
     fish1 fish110 = new fish1();  
     addObject(fish110, 417, 368);  
     fish1 fish111 = new fish1();  
     addObject(fish111, 524, 251);  
     fish1 fish112 = new fish1();  
     addObject(fish112, 317, 298);  
     fish3 fish32 = new fish3();  
     addObject(fish32, 259, 159);  
     fish3 fish33 = new fish3();  
     addObject(fish33, 40, 148);  
     fish3 fish34 = new fish3();  
     addObject(fish34, 530, 205);  
     fish3 fish35 = new fish3();  
     addObject(fish35, 242, 37);  
     fish3 fish36 = new fish3();  
     addObject(fish36, 145, 355);  
     fish3 fish37 = new fish3();  
     addObject(fish37, 174, 241);  
     fish3 fish38 = new fish3();  
     addObject(fish38, 553, 378);  
     fish111.setLocation(443, 24);  
     fish2.setLocation(574, 127);  
     fish38.setLocation(347, 230);  
     fish110.setLocation(528, 370);  
     fish10.setLocation(395, 371);  
     fish36.setLocation(408, 306);  
     fish.setLocation(257, 382);  
     fish17.setLocation(147, 373);  
     fish37.setLocation(104, 28);  
     fish33.setLocation(33, 376);  
     fish32.setLocation(186, 251);  
     fish34.setLocation(575, 68);  
     fish38.setLocation(501, 219);  
     fish3 fish39 = new fish3();  
     addObject(fish39, 260, 149);  
     fish3 fish310 = new fish3();  
     addObject(fish310, 48, 159);  
     removeObject(fish14);  
     removeObject(fish39);  
     removeObject(fish36);  
     removeObject(fish10);  
     removeObject(fish6);  
     removeObject(fish12);  
     removeObject(fish);  
     removeObject(fish9);  
     fish32.setLocation(206, 328);  
     fish13.setLocation(300, 359);  
     hungryFish.setLocation(425, 143);  
     fish34.setLocation(415, 348);  
     fish35.setLocation(544, 65);  
     fish111.setLocation(215, 31);  
     fish3.setLocation(267, 214);  
     fish7.setLocation(433, 29);  
     fish11.setLocation(200, 250);  
     fish34.setLocation(249, 572);  
     fish110.setLocation(727, 620);  
     fish13.setLocation(934, 322);  
     fish112.setLocation(368, 686);  
     fish32.setLocation(147, 608);  
     fish17.setLocation(597, 551);  
     fish33.setLocation(39, 471);  
     fish16.setLocation(270, 383);  
     hungryFish.setLocation(765, 128);  
     fish4.setLocation(477, 341);  
     fish38.setLocation(637, 437);  
     fish3.setLocation(377, 143);  
     fish11.setLocation(160, 357);  
     fish8.setLocation(891, 515);  
     fish1.setLocation(375, 519);  
     fish5.setLocation(666, 388);  
     fish111.setLocation(263, 216);  
     fish37.setLocation(153, 198);  
     removeObject(fish7);  
     removeObject(fish3);  
     removeObject(fish11);  
     removeObject(fish17);  
     removeObject(fish13);  
     removeObject(fish4);  
     bubble bubble = new bubble();  
     addObject(bubble, 763, 375);  
   }  
 }  


Class IkanJahat

 import greenfoot.*;   
 public class IkanJahat extends Actor  
 {  
   public int totalPoisonEat = 0;  
   public int totalFishEat = 0;  
   Score score_field = new Score("Score:" + totalFishEat);  
   public void act()   
   {  
     keyBoardControl();  
     eat();  
     checkScore();  
   }    
   public void checkScore()  
   {  
     if(totalPoisonEat >= 1)   
     {  
       setImage("fishMainDead.png");   
       loose alert_loose = new loose();  
       getWorld().addObject(alert_loose, 370, 300);     
       Greenfoot.delay(350);  
       Greenfoot.setWorld(new MenuScreen());  
     }  
     if(totalFishEat >= 10)   
     {  
       win alert_win = new win();  
       getWorld().addObject(alert_win, 370, 300);   
        Menus.titleSound.stop();  
       Greenfoot.playSound("fanfare.wav");  
       //Greenfoot.stop();  
       Greenfoot.delay(350);  
       Greenfoot.setWorld(new MenuScreen());  
     }  
   }  
   public void keyBoardControl()  
   {   
     if (Greenfoot.isKeyDown("left") )  
     {  
       setImage("fishMain2left.png");      
       move (-3) ;  
       if (Greenfoot.isKeyDown("down"))  
         turn (-3) ;  
       if (Greenfoot.isKeyDown("up"))  
         turn (3);     
     }     
     if (Greenfoot.isKeyDown("right"))  
     {   
       setImage("fishMain2.png");  
       move (3);  
       if (Greenfoot.isKeyDown("down"))  
         turn (3) ;  
       if (Greenfoot.isKeyDown("up"))  
         turn (-3);  
     }  
   }  
   public void eat()  
   {  
     Actor Score;  
     Actor fish;  
     fish = getOneObjectAtOffset(4, 4, fish.class);  
     getWorld().addObject(score_field, 90, 30);  
     if (fish != null)  
     {  
       World Sea;  
       Sea = getWorld() ;  
       Sea.removeObject(fish);  
       Greenfoot.playSound("eating.wav");  
       totalFishEat++;  
       score_field.setText("Score: " +totalFishEat);  
       hitafish();  
     }  
     Actor fish1;  
     fish1 = getOneObjectAtOffset(4, 4, fish1.class);  
     if (fish1 != null)  
     {  
       World world;  
       world = getWorld() ;  
       world.removeObject(fish1);  
       Greenfoot.playSound("eating.wav");  
       totalFishEat++;  
       score_field.setText("Score: " +totalFishEat);  
       hitafish();  
     }  
     Actor fish3;  
     fish3 = getOneObjectAtOffset(4, 4, fish3.class);  
     if (fish3 != null)  
     {  
       World world;  
       world = getWorld() ;  
       world.removeObject(fish3);  
       Menus.titleSound.stop();  
       Greenfoot.playSound("loose.mp3");  
       totalPoisonEat++;  
       hitafish();  
     }  
   }  
   private void hitafish()  
   {  
     Levels SeaWorld = (Levels) getWorld();  
   }  
 }  

Class Menus

 import greenfoot.*;   
 abstract class Menus extends World  
 {  
   public static GreenfootSound titleSound;  
   public Menus()  
   {    
     super(720, 480, 1);  
     if (this.getClass().getName().equalsIgnoreCase("MenuScreen"))  
     {  
       addObject(new StartBtn(), 80, 100);  
       addObject(new StoryBtn(), 100, 220);  
       addObject(new HelpBtn(), 80, 310);  
       titleSound = new GreenfootSound("title-theme.mp3");  
       titleSound.stop();  
             titleSound.setVolume(70);  
             titleSound.playLoop();  
     }  
     else  
     {  
       addObject(new BackBtn(), 100, 400);  
     }  
   }  
 }  

Class Button

 import greenfoot.*;   
 public class Buttons extends Actor  
 {  
   public void act(String buttonName)   
   {  
     if (Greenfoot.mouseMoved(this)) {  
       setImage(buttonName + "_over.png");  
     } else if (Greenfoot.mouseMoved(null)) {  
       setImage(buttonName + ".png");  
     }  
     if (Greenfoot.mousePressed(this)) {  
       getImage().scale((int)Math.round(getImage().getWidth() * 0.9), (int)Math.round(getImage().getHeight() * 0.9));  
     }  
     if (Greenfoot.mouseClicked(null) || Greenfoot.mouseDragEnded(null)) {  
       setImage(buttonName + ".png");   
     }  
     if (Greenfoot.mouseClicked(this)) {  
       setImage(buttonName + ".png");  
       Greenfoot.delay(5);   
       if   (buttonName.equalsIgnoreCase("StartBtn")) Greenfoot.setWorld(new Level1());  
       else if (buttonName.equalsIgnoreCase("StoryBtn")) Greenfoot.setWorld(new StoryScreen());  
       else if (buttonName.equalsIgnoreCase("HelpBtn")) Greenfoot.setWorld(new HelpScreen());  
       else if (buttonName.equalsIgnoreCase("BackBtn")) Greenfoot.setWorld(new MenuScreen());  
     }  
   }  
 }  


Pada setiap class button (story, start, help), letakan statement ini (pada method act)

 public void act()   
   {    super.act(this.getClass().getName());  
   }  


Untuk membuat Kepiting (crab) bergerak, letakkan kode berikut pada class Crab


 import greenfoot.*;  
 public class Crab extends Actor  
 {  
     public void act()   
   {  
      setLocation(getX() - 2, getY());  
      if (getX() <= 0)  
      {  
          setLocation(getWorld().getWidth() +getX() , getY());  
      }  
   }    
 }   


Untuk membuat gelembung (bubble) bergerak, letakkan kode berikut pada class bubble

 import greenfoot.*;  
 public class bubble extends Actor  
 {  
   public void act()   
   {  
     setLocation(getY() - 5, getX());  
      if (getY() <= 0)  
      {  
          setLocation(getWorld().getHeight() + 20 , Greenfoot.getRandomNumber(600));  
      }  
   }     
 }  


Aktor crab dan bubble hanyalah aktor pendukung, tidak ada pengaruhnya pada jalannya game. Hanya sebagai animasi tambahan supaya game lebih menarik.

Pada kelas fish (ikan yang dimakan / ikan musuh) kode dasarnya adalah sebagai berikut


Class Fish


 import greenfoot.*;  
 public class fish extends Actor  
 {  
   public void act()   
   {  
     move (2) ;  
     if (Greenfoot.getRandomNumber (20) < 10)  
   {  
     turn (Greenfoot.getRandomNumber (50) - 20) ;  
   }   
   if (getX () <= 5 || getX() >= getWorld().getWidth () - 5)  
   {  
   turn (180) ;  
   }  
   if (getY () <= 5 || getY() >= getWorld().getHeight () - 5)  
   {  
     turn (180) ;  
   }  
   }     
 }  


Listing Program ini dipakai juga untuk class fish1 dan fish3. Untuk melakukan variasi terhadap gerakan ikan, maka kita dapat mengubah nilai move() dan turn() diatas sesuai dengan keinginan.

Pada Class Score, kode nya adalah sebagai berikut

Class Score

 import greenfoot.*;   
 import java.awt.Color;  
 import java.applet.*;  
 import java.awt.*;  
 public class Score extends Actor  
 {  
 Font font1=new Font("Helvetica",Font.BOLD,35);  
  public Score(String text)  
  {  
    GreenfootImage img_field = new GreenfootImage(140,50);  
    img_field.setFont(font1);  
    img_field.setColor(Color.white);  
    img_field.drawString(text, 0, 45);  
    setImage(img_field);  
   }  
  public void setText(String text)  
   {  
     GreenfootImage img_field = getImage();  
     img_field.clear();  
     img_field.drawString(text, 0, 45);  
   }  
 }  

Related Posts

Tutorial Greenfoot - Membuat Game Sederhana ala Feeding Frenzy
4/ 5
Oleh

1 comments:

22 September 2016 at 15:58 delete

wew keq gtu ya logic/algoritmanya
mantab gan thx

nonton film korea

Reply
avatar