Friday, April 5, 2019

Cara membuat bola pantul dari greenfoot

Cara Menmbuat Bola Pantul Dari GreenFoot

Tutorial Greenfoot kali ini saya akan membuat game Bola Pantul.
  1. Buatlah project baru beri nama Bola pantul/Bounce Ball
  2. Buat class World baru dengan cara klik kanan>>new subclass>>masukkan nama class, begitu juga dengan class actor caranya sama. Tampilannya akan seperti di bawah.greenfoot.png
  3. Masukkan kode di bawah pada class bg (nama subclass world)import greenfoot.*;
    import javax.swing.JOptionPane;
    /**
    * Write a description of class bg here.
    *
    * @author Aldio
    * @version trial
    */
    public class bg extends World
    {
    counter counter = new counter(“Skor: “);
    skor skor = new skor();
    player player = new player();
    ball ball = new ball();
    tembok tembok = new tembok();
    tembok tembok1 = new tembok();
    tembok tembok2 = new tembok();
    tembok tembok3 = new tembok();
    tembok tembok4 = new tembok();
    /**
    * Constructor for objects of class bg.
    *
    */
    public bg()
    {
    // Create a new world with 600×400 cells with a cell size of 1×1 pixels.
    super(600, 400, 1);
    setPaintOrder(counter.class, player.class, skor.class, tembok.class, ball.class);
    addObject(counter,155,380);
    //meltakkan posisi caption skor
    addObject(player,321,360);
    //meletakkan posisi tembok pemantul
    addObject(ball,320,335);
    //meletakkan posisi bola
    addObject(tembok,100,35);
    //meletakkan posisi tembok
    addObject(tembok1,170,75);
    //meletakkan posisi tembok
    addObject(tembok2,200,135);
    //meletakkan posisi tembok
    addObject(tembok3,400,135);
    //meletakkan posisi tembok
    addObject(tembok4,500,35);
    //meletakkan posisi tembok
    prepare();
    }

    public void act(){
    if(Greenfoot.getRandomNumber(1000)<4){
    addObject(new tembok(),Greenfoot.
    getRandomNumber(100),Greenfoot.
    getRandomNumber(170)); //menampilkan class tembok agar tampil secara acak
    }

    if(Greenfoot.isKeyDown(“space”))
    {//menggunakan tombol spasi untuk menghentikan sementara game
    Greenfoot.stop();
    }
    }
    public void tambah(){
    counter.add(5);
    }
    /**
    * Called when game is up. Stop running and display score
    */
    public void selesai()
    {
    addObject(new skor(counter.getValue()), getWidth()/2, getHeight()/2);
    //menampilkan pesan “Game Over dan skor terakhir yg diperoleh”
    }

    /**
    * Prepare the world for the start of the program. That is: create the initial
    * object and add them to the world
    */
    private void prepare()
    {
    }
    }
    Lalu klick compile
  4. Masukkan juga kode di bawah ini pada class ball agar bola dapat bergerak dan memantul pada dindingimport greenfoot.*;
    /**
    * Write a description of class ball here.
    *
    * @author Aldio
    * @version trial
    */
    public class ball extends Actor
    {
    int x=3;
    int y=3;
    /**
    * Act – do whatever the ball 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.
    gerak();
    kanan();
    kiri();
    atas();
    player();
    }
    public void gerak()
    {
    setLocation(getX()+x,getY()+y);
    }
    public void kanan()
    {
    if(getX()>=getWorld().getWidth()-getImage().getWidth()/2){
    x=x-1;
    }
    }
    public void kiri()
    {
    if(getX()<=getImage().getWidth()/2){
    x=x+1;
    }
    }
    public void atas()
    {
    Actor tembok=getOneIntersectingObject(tembok.class);
    if(getY()<=getImage(). getHeight()/2){
    y=y+1;
    }
    if(tembok !=null){
    ((bg)getWorld()).tambah();
    //jika subclass ball berhasil menabrak tembok dan pecah, akan ditambahkan poinnya
    getWorld(). removeObject(tembok);
    }
    }
    public void player()
    {
    Actor box=getOneIntersectingObject(player.class);
    if(box !=null){
    y=y-1;
    }
    if(getY()>=getWorld().getHeight()-getImage().getHeight()/2){
    Greenfoot.stop();
    ((bg)getWorld()).selesai();
    }
    }
    }
  5. Masukkan juga kode di bawah ini pada class counterimport greenfoot.*;
    import java.awt.Font; //Tambahkan Font
    /**
    * Write a description of class counter here.
    *
    * @author Aldio
    * @version trial
    */
    public class counter extends Actor
    {
    private int value = 0;
    private int target = 0;
    private String text;
    private int stringLength;
    public counter()
    {
    this(“”);
    }
    public counter (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);
    }
    }
  6. Masukkan juga kode di bawah ini pada class skorimport greenfoot.*;
    import java.awt.Color;
    import java.awt.Font;
    import java.util.Calendar;
    /**
    * Write a description of class skor here.
    *
    * @author Aldio
    * @version trial
    */
    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 = 300;
    /**
    * Create a score board with dummy result fot testing.
    */
    public skor() {
    this (25);
    }
    /**
    * Create a score board for the final result.
    */
    public skor(int score) {
    makeImage(“GAME OVER”, “Skor Anda: “, 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(14, 14, 89, 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.
    }
    }
  7. Masukkan juga kode di bawah ini pada class playerimport greenfoot.*;
    /**
    * Write a description of class player here.
    *
    * @author Aldio
    * @version trial
    */
    public class player extends Actor
    {
    /**
    * Act – do whatever the player wants to do. This method is called whenever
    * the ‘Act’ or ‘Run’ button gets pressed in the environment.
    */
    public player()
    {
    // Add your action code here.
    GreenfootImage img=getImage();
    img.scale(img.getWidth()+30,img.getHeight()-60);
    setImage(img);
    }
    public void act(){
    if(Greenfoot.isKeyDown(“left”)){
    move(-3);
    }
    if(Greenfoot.isKeyDown(“right”)){
    move(3);
    }
    if(Greenfoot.mouseMoved(null)){
    MouseInfo mouse = Greenfoot.getMouseInfo();
    setLocation(mouse.getX(),360);
    }
    }
    }
setelah semuanya di compile dan hasilnya seperti gambar di bawahgreenfoot.png

Cara membuat bola pantul dari greenfoot

Cara Menmbuat Bola Pantul Dari GreenFoot Tutorial Greenfoot kali ini saya akan membuat game Bola Pantul. Buatlah project baru beri na...