Tuesday, 20 January 2015

MEMBUAT GAME PETUALANGAN SI BOY MENGGUNAKAN GREENFOOT

SKENARIO GAME YANG BERJUDUL PETUALANGAN SI BOY

Seorang jagoan yang bernama Boy ingin menyelamatkan putri yang ditawan oleh penjahat yaitu Snake dan game ini akan berakhir apabila si jagoan berhasil menyelamatkan putri dari tawanan penjahat .

v  Aktor yang ada pada game ini yaitu :

-Boy
-putri
-Snake

v  Untuk mengarahkan Boy tersebut gunakan tombol :
-Up       =↑
-Down =↓
-Right   =→
-Left     =←

-Fire     = X

LAMPIRAN SKRIP PROGRAM

World classes
-          World > Dunia

import greenfoot.*;
 // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class dunia here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */

public class dunia extends World

 {   /**
     * Constructor for objects of class dunia.
     *
     */
    {
    Nilai skor = new Nilai("Score: ");
    addObject(skor,50,300);
   
}
  public dunia()
    {   
        // Create a new world with 20x20 cells with a cell size of 10x10 pixels.

         super(200, 100, 5);
           addObject(new boy(),190,50);
           addObject(new snake(),20,50);
           addObject(new snake(),25,70);
           addObject(new snake(),30,90);
           addObject(new snake(),25,30);
           addObject(new snake(),30,10);
           addObject(new snake(),40,50);
           addObject(new snake(),45,70);
           addObject(new snake(),50,90);
           addObject(new snake(),45,30);
           addObject(new snake(),50,10);
           addObject(new snake(),60,38);
           addObject(new snake(),60,58);
           addObject(new snake(),80,50);
           addObject(new Princess(),8,50);
        }
 }


  
          Actor classes

-          Actor > snake

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

/**
 * Write a description of class snake here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class snake  extends Actor
{
    /**
     * Act - do whatever the snake 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.
        setLocation(getX() + 1, getY());
        if(getX()==199)
        {
            setLocation(0,getY());
        }
    }
}

-          Actor > boy

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

/**
 * Write a description of class boy here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class boy  extends Actor
{
    /**
     * Act - do whatever the boy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private dunia pelor;
    public void addedToWorld(World dunia)
        {
            pelor = (dunia) dunia;
        }
    public void act()
    {
        // Add your action code here.
         if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+1,getY());
        }
         if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-1,getY());
        }
         if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(),getY()-1);
        }
         if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(),getY()+1);
        }
       
        snake a=(snake)getOneIntersectingObject(snake.class);
if(a!=null)
{
setImage("tengkorak.png");
Greenfoot.playSound("kalah.wav");
getWorld().removeObject(a);

return;
}
       
        if(Greenfoot.isKeyDown("x"))
        {
            pelor.addObject(new peluru1(), getX(),getY());
            Greenfoot.playSound("glock18-1.wav");
        }
         
    }   
}

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

/**
 * Write a description of class peluru1 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class peluru1  extends Actor
{
    /**
     * Act - do whatever the peluru1 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.
        setLocation(getX()-2,getY());
        if(getX()==199)
{
getWorld().removeObject(this);
return;
}
//jika snake terkena peluru
snake a =(snake)getOneIntersectingObject(snake.class);
if(a!=null)
{
Greenfoot.playSound("headshot2.wav");
getWorld().removeObject(a);
getWorld().removeObject(this);
return;
            }
       }   
   }

-          Actor  > Prinsces

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

/**
 * Write a description of class Princess here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Princess  extends Actor
{
    /**
     * Act - do whatever the Princess 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.
        peluru1 f=(peluru1)getOneIntersectingObject(peluru1.class);
if(f!=null)
{
Greenfoot.playSound("death1.wav");

if(Greenfoot.isKeyDown("x"))
{
Greenfoot.playSound("glock18-1.wav");

}
setImage("smiley3.png");
getWorld().removeObject(f);

       }
    }   
}

-          Actor > Nilai
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Font;
import java.awt.Color;
/**
 * Write a description of class Nilai here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Nilai  extends Actor
{
    /**
     * Act - do whatever the Nilai wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int angka = 0;
private int target = 0;
private String huruf;
private int panjangHuruf;
public Nilai(String depan)
{
huruf=depan;
panjangHuruf = (huruf.length() + 2) * 16;
setImage(new GreenfootImage(panjangHuruf, 24));
GreenfootImage gambar = getImage();
Font jenisHuruf = gambar.getFont();
gambar.setFont(jenisHuruf.deriveFont(24.0F));
gantiGambar();
}
private void gantiGambar()
{
GreenfootImage gambar = getImage();
gambar.clear();
gambar.setColor(Color.RED);
gambar.drawString(huruf + angka, 1, 18);
}
public void tambah (int skor)
{
target +=skor;
}
}

-          Actor > papanskor

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;
/**
 * Write a description of class papanskor here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class papanskor  extends Actor
{
    /**
     * Act - do whatever the papanskor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static final float besarHuruf = 48.0f;
public static final int lebar = 500;
public static final int tinggi = 300;
public papanskor(int skor)
{
bikinGambar("SELAMAT", "Skor Akhir: ", skor);
}
private void bikinGambar(String judul, String depan, int skor)
{
GreenfootImage gambar = new GreenfootImage(lebar, tinggi);
gambar.setColor(new Color(25, 5, 74, 160));
gambar.fillRect(0, 0, lebar, tinggi);
gambar.setColor(new Color(255, 255, 255, 50));
gambar.fillRect(5, 5, 10,10);
Font font = gambar.getFont();
font = font.deriveFont(besarHuruf);
gambar.setFont(font);
gambar.setColor(Color.GREEN);
gambar.drawString(judul, 60, 100);
gambar.drawString(depan + skor, 60, 200);
setImage(gambar);
}
}
  
Download script Link1 / Link2


2 comments: