Cara Membuat Game Di Greenfoot PART 2 - Imangga

Breaking

Wednesday, 30 January 2019

Cara Membuat Game Di Greenfoot PART 2

Yoo lanjut lagi ke PART 2 bagaimana cara membuat game sederhana Air War, setelah kalian membuat subclass di PART 1 waktu nya kita membuat modeling mulai dari desain alien,pesawat background dan lain-lain untuk membuat modeling nya kalian bisa desain sendiri di photoshop atau corel dengan format file PNG untuk ukuran nya sesuaikan saja sesuai kebutuhan, setelah membuat model lanjut untuk membuat sebuah efek suara untuk game untuk efek suara kalian bisa cari di internet atau bisa bikin sendiri, tapi disini mamang coder bakal ngasih efek dan model yang udah mamang coder bikin sendiri bisa download DISINI

Kalo udah download model dan sound nya tinggal di ekstrak aja file nya ke folder game air war kaya gambar di bawah ini

1. Kalo udah di ekstrak langsung yuk mulai coding, kita mulai coding dari subclass awal yang ada di class World caranya klik 2 kali pada subclass awal nanti akan muncul sebuah area untuk memulai coding pada class lalu masukan code berikut :

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Game dibuat oleh :
 *
 * @author (Angga Fantiya Hermawan)
 * follow istagram (punya_angga)
 *@email (anggafantiya@gmail.com)
 */
public class awal extends World
{
    private boolean song=true;
    private GreenfootSound songGame;
 
    /**
     * Constructor for objects of class awal.
     *
     */
    public awal()
    { 
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(854, 480, 1);
 
         songGame = new GreenfootSound("backsound.mp3");
        menu();
    }
    public void act(){

        if(song)
            songGame.play();
    }
    public void started()
    {
        super.started();
        songGame.setVolume(100);
        songGame.playLoop();
    }

    public void stopped()
    {
        super.stopped();
        songGame.pause();
        song=false;
    }
    private void menu()
    {
            addObject(new credit(),600,250);
            addObject(new start(),220,250);
            addObject(new Logo(),100,60);
     

    }
}


2. Jika sudah lalu lanjut ke subclass Area dan masukan source code berikut
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Game dibuat oleh :
 *
 * @author (Angga Fantiya Hermawan)
 * follow istagram (punya_angga)
 *@email (anggafantiya@gmail.com)
 */
public class area extends World
{
        Nilai nilai = new Nilai("Nilai:");
    /**
     * Constructor for objects of class bg.
     *
     */
    public area()
    { 
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(854, 480, 1);
        setPaintOrder(skor.class, ledakan.class, pahlawan.class, musuh.class, Nilai.class, ammoPahlawan.class,darrSlime.class,ammo.class);
        addObject(new pahlawan(),9,220);
        addObject(new musuh(),790,320);
        addObject(nilai,120,450);
    }
    public void act(){
     
        if(Greenfoot.getRandomNumber(100)<1){
            addObject(new musuh(),853,Greenfoot.getRandomNumber(479));
        }
        if(Greenfoot.isKeyDown("space")){
            Greenfoot.stop();
        }
    }
    public void tambah(){
        nilai.add(10);
    }

    /**
     * Called when game is up. Stop running and display score.
     */
    public void selesai()
    {
        addObject(new skor(nilai.getValue()), getWidth()/2, getHeight()/2);
        Greenfoot.stop();
    }
}

3. Jika sudah masuk ke subclass logo yang ada di class Aktor lalu masukan source code berikut
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Game dibuat oleh :
 * 
 * @author (Angga Fantiya Hermawan) 
 * follow istagram (punya_angga)
 *@email (anggafantiya@gmail.com)
 */
public class Logo extends Actor
{
    /**
     * Act - do whatever the Logo wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}

4. Lalu lanjut ke subclass nilai masukan code berikut
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Game dibuat oleh :
 * 
 * @author (Angga Fantiya Hermawan) 
 * follow istagram (punya_angga)
 *@email (anggafantiya@gmail.com)
 */
public class Nilai extends Actor
{
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int value = 0;
    private int target = 0;
    private String text;
    private int stringLength;

    public Nilai()
    {
        this("");
    }

    public Nilai(String prefix)
    {
        text = prefix;
        stringLength = (text.length() + 2) * 16;

        setImage(new GreenfootImage(stringLength, 24));
        GreenfootImage image = getImage();
        Font font = image.getFont();
        image.setFont(font.deriveFont(24.0F));  // use larger font
        
        updateImage();
    }
    
    public void act() {
        if(value < target) {
            value++;
            updateImage();
        }
        else if(value > target) {
            value--;
            updateImage();
        }
    }

    public void add(int score)
    {
        target += score;
    }

    public void subtract(int score)
    {
        target -= score;
    }

    public int getValue()
    {
        return value;
    }

    /**
     * Make the image
     */
    private void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.drawString(text + value, 1, 18);
    }
}

5. Lanjut ke subclass ammo dan masukan source code berikut
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Game dibuat oleh :
 * 
 * @author (Angga Fantiya Hermawan) 
 * follow istagram (punya_angga)
 *@email (anggafantiya@gmail.com)
 */
public class ammo extends Actor
{
    /**
     * Act - do whatever the pelor2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        int x=getX();
        int y=getY();
        setLocation(x-32,y);
        if(x==0){
            getWorld().removeObject(this);
            return;
        }
        Actor a = getOneIntersectingObject(pahlawan.class);
        if(a != null){
            getWorld().addObject(new darrSlime(),getX(),getY());
            ((area)getWorld()).selesai();
            getWorld().removeObject(a);
            getWorld().removeObject(this);
            return;
        }
    }    
    
}


6. lanjut ke subclass ammoPahlawan masukan source code berikut
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Game dibuat oleh :
 * 
 * @author (Angga Fantiya Hermawan) 
 * follow istagram (punya_angga)
 *@email (anggafantiya@gmail.com)
 */
public class ammoPahlawan extends Actor
{
    /**
     * Act - do whatever the pelor1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        int x=getX();
        int y=getY();
        setLocation(x+29,y);
        if(x == 853){
            getWorld().removeObject(this);
            return;
        }
        Actor a = getOneObjectAtOffset(0 ,0, musuh.class);
        if(a != null){
            getWorld().addObject(new ledakan(),getX(),getY());
            ((area)getWorld()).tambah();
            getWorld().removeObject(a);
            getWorld().removeObject(this);
            return;
        }
    }    
}



7. Lalu ke subclass credit lalu masukan source code berikut
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Game dibuat oleh :
 * 
 * @author (Angga Fantiya Hermawan) 
 * follow istagram (punya_angga)
 *@email (anggafantiya@gmail.com)
 */
public class credit extends Actor
{
    /**
     * Act - do whatever the credit wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        if(Greenfoot.mouseClicked(this)){
                ((awal)getWorld()).stopped();
                 World a = getWorld();
                 a = new pembuat();
                 Greenfoot.setWorld(a);
       } 
    }    
}

8. Masuk ke subclass darrSlime lalu masukan sourcecode berikut
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Game dibuat oleh :
 * 
 * @author (Angga Fantiya Hermawan) 
 * follow istagram (punya_angga)
 *@email (anggafantiya@gmail.com)
 */
public class darrSlime extends Actor
{
    private final static int IMAGE_COUNT= 8;
    private static GreenfootImage[] images;
    private int size=0;
    private int increment=1;    
    
    public darrSlime() {
        initialiseImages();
        setImage(images[0]);      
        Greenfoot.playSound("fire.wav");
    }     
    
    /** 
     * Create the images for explosion.
     */
    public synchronized static void initialiseImages() {
        if(images == null) {
            GreenfootImage baseImage = new GreenfootImage("darrSlime.png");
            int maxSize = baseImage.getWidth()*5;
            int delta = maxSize / (IMAGE_COUNT+1);
            int size = 0;
            images = new GreenfootImage[IMAGE_COUNT];
            for(int i=0; i < IMAGE_COUNT; i++) {
                size = size + delta;
                images[i] = new GreenfootImage(baseImage);
                images[i].scale(size, size);
            }
        }
    }
    public void act() 
    {
        // Add your action code here.
        setImage(images[size]);

        size += increment;
        if(size>=IMAGE_COUNT) {
            increment = -increment;
            size += increment;
        }
        
        if(size <= 0) {
            getWorld().removeObject(this);
        }
    }    
}


9. Masuk Subclass kembali lalu masukan source code berikut
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Game dibuat oleh :
 * 
 * @author (Angga Fantiya Hermawan) 
 * follow istagram (punya_angga)
 *@email (anggafantiya@gmail.com)
 */
public class kembali extends Actor
{
    /**
     * Act - do whatever the kembali wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        if(Greenfoot.mouseClicked(this)){
                ((pembuat)getWorld()).stopped();
                 World a = getWorld();
                 a = new awal();
                 Greenfoot.setWorld(a);
       } 
    }    
}


10. Lalu buka subclass ledakan lalu masukan sourcecode berikut
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Game dibuat oleh :
 * 
 * @author (Angga Fantiya Hermawan) 
 * follow istagram (punya_angga)
 *@email (anggafantiya@gmail.com)
 */
public class ledakan extends Actor
{
    private final static int IMAGE_COUNT= 8;
    private static GreenfootImage[] images;
    private int size=0;
    private int increment=1;    
    
    public ledakan() {
        initialiseImages();
        setImage(images[0]);      
        Greenfoot.playSound("fire.wav");
    }     
    
    /** 
     * Create the images for explosion.
     */
    public synchronized static void initialiseImages() {
        if(images == null) {
            GreenfootImage baseImage = new GreenfootImage("ledakan.png");
            int maxSize = baseImage.getWidth()*2;
            int delta = maxSize / (IMAGE_COUNT+1);
            int size = 0;
            images = new GreenfootImage[IMAGE_COUNT];
            for(int i=0; i < IMAGE_COUNT; i++) {
                size = size + delta;
                images[i] = new GreenfootImage(baseImage);
                images[i].scale(size, size);
            }
        }
    }
    public void act() 
    {
        // Add your action code here.
        setImage(images[size]);

        size += increment;
        if(size>=IMAGE_COUNT) {
            increment = -increment;
            size += increment;
        }
        
        if(size <= 0) {
            getWorld().removeObject(this);
        }
    }    
}

11. Lalu masuk ke subclass musuh dan masukan source code berikut
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Game dibuat oleh :
 * 
 * @author (Angga Fantiya Hermawan) 
 * follow istagram (punya_angga)
 *@email (anggafantiya@gmail.com)
 */
public class musuh extends Actor
{
    /**
     * Act - do whatever the ufo wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   
    public void act() 
    {
        // Add your action code here.
        int x=getX();
        int y=getY();
        setLocation(x-2,y);
        if(x==0){
            getWorld().removeObject(this);
        }
        if(Greenfoot.mouseMoved(null)){
            MouseInfo mouse=Greenfoot.getMouseInfo();
            if(mouse.getY()>(y-7) && mouse.getY()<(y+7)){
                getWorld().addObject(new ammo(),getX(),getY());
                Greenfoot.playSound("ammo.wav");
            }
        }
    }    
}


12. lalu masuk ke subclass pahlawan lalu masukan source code berikut
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Game dibuat oleh :
 * 
 * @author (Angga Fantiya Hermawan) 
 * follow istagram (punya_angga)
 *@email (anggafantiya@gmail.com)
 */
public class pahlawan extends Actor
{
    /**
     * Act - do whatever the ps wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private area pl;
    public void addedToWorld(World area){
        pl=(area)area;
    }
    public void act() 
    {
        // Add your action code here.
        
        if(Greenfoot.mouseMoved(null)){
            MouseInfo mouse = Greenfoot.getMouseInfo();
            setLocation(mouse.getX(), mouse.getY());
        }
        if(Greenfoot.mouseClicked(null)) {
            pl.addObject(new ammoPahlawan(),getX()+getImage().getHeight(),getY());
            Greenfoot.playSound("ammoPahlawan.wav");
        }
        Actor a=getOneObjectAtOffset(0,0,musuh.class);
        if(a!=null){
            getWorld().addObject(new darrSlime(),getX(),getY());
            ((area)getWorld()).selesai();
            getWorld().removeObject(a);
            getWorld().removeObject(this);
        }    
    }
}


13. Lalu masuk ke subclass skor lalu masukan source code beriktu
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


import java.util.Calendar;
/**
 * Game dibuat oleh :
 * 
 * @author (Angga Fantiya Hermawan) 
 * follow istagram (punya_angga)
 *@email (anggafantiya@gmail.com)
 */
public class skor extends Actor
{
    /**
     * Act - do whatever the skor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static final float FONT_SIZE = 48.0f;
    public static final int WIDTH = 400;
    public static final int HEIGHT = 250;
    
    /**
     * Create a score board with dummy result for testing.
     */
    public skor(){
        this(100);
    }

    /**
     * Create a score board for the final result.
     */
    public skor(int score){
        makeImage("Game Over", "Nilai: ", score);
    }

    /**
     * Make the score board image.
     */
    private void makeImage(String title, String prefix, int score){
        GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);

        image.setColor(new Color(0, 0, 0, 160));
        image.fillRect(0, 0, WIDTH, HEIGHT);
        image.setColor(new Color(255, 255, 255, 100));
        image.fillRect(5, 5, WIDTH-10, HEIGHT-10);
        Font font = image.getFont();
        font = font.deriveFont(FONT_SIZE);
        image.setFont(font);
        image.setColor(Color.WHITE);
        image.drawString(title, 60, 100);
        image.drawString(prefix + score, 60, 200);
        setImage(image);
    }
    public void act() 
    {
        // Add your action code here.
        
    }    
}

14.Langkah Akhir masuk ke subclass start dan masukan source code berikut
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Game dibuat oleh :
 * 
 * @author (Angga Fantiya Hermawan) 
 * follow istagram (punya_angga)
 *@email (anggafantiya@gmail.com)
 */
public class start extends Actor
{
    /**
     * Act - do whatever the start wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
       if(Greenfoot.mouseClicked(this)){
                ((awal)getWorld()).stopped();
                 World a = getWorld();
                 a = new area();
                 Greenfoot.setWorld(a);
       } 
    }    
}


jika langkah langkah di atas sudah sesuai langsung klik run saja pada aplikasi greenfoot dengan tools greenfoot ini kita juga bisa langsung export filenya menjadi APK, untuk cara exportnya akan saya posting di postingan berikutnya jadi terus pantengin aja hehe, kalo ada yang males buat dari awal boleh langsung download aja file yang udah jadi DISINI

1 comment: