Membuat Game Sokocang

In Blogs, Coretan, Tips Triks 584 views

expsokSalah satu game klasik lain adalah Sokoban, hasil ciptaan Hiroyuki Imabayashi. Game yang memenangkan lomba game di Jepang tahun 1980 ini akhirnya resmi dilepas oleh Thinking Rabbit, sebuah perusahaan milik Hiroyuki, dua tahun kemudian. Berbeda dengan Digger, Sokoban lebih mementingkan siasat alias pengaturan strategi. Hal ini karena tujuan Sokoban adalah memindahkan sejumlah boks dalam koridor yang sempit sehingga boks-boks tersebut terletak di tempat targetnya. Anda hanya bisa memindahkan satu boks tiap saat, itu pun hanya mendorong, Anda tidak bisa menarik. Juga hati-hati agar boks-boks tersebut tidak terjebak di sudut ruangan dan tidak bisa dipindahkan lagi. Bisa dilihat, Sokoban lebih mementingkan kecerdasan otak dibandingkan ketangkasan aksi tangan. Dilengkapi dengan puluhan level dengan tingkat kesulitan bervariasi, menamatkan Sokoban atau sekedar bermain hingga puas bisa memakan waktu berjam-jam.

Nah sekarang bagai mana menciptakan game Sokoban versi Sokocang yang dibuat oleh Guru Besar Chen akan Suheri S.T yang juga dosen TKJ Polnep ? sekarang kita kupas tuntas di sini dengan beberapa update Script oleh Bung Fad Say:

Untuk membuat game sokoban, diharapkan teman-teman telah handal dalam menjalankan Aplikasi NetBean disini kita menggunakan versi 6.1 dengan menggunakan beberapa class yaitu:

+src
 -Enter.java
 -Floor.java
 -GameObject.java
 -Goal.java
 -Kanvas.java
 -Package.java
 -Soko.java
 -Space.java
 -SplashScreen.java
 -Wall.java
 -+map
  --screen.1
  --screen.2
  --screen.n
 -+image
  --floor.png
  --goal.png
  --package.png
  --soko_down.png
  --soko_lift.png
  --soko_right.png
  --soko_up.png
  --space.png
  --splash.jpg
  --wall.png
 -+sound
  --soko1.wav
  --soko2.wav
  --soko3.wav
  --open.wav

Script dari beberapa file *.java

Script file Enter.java

import java.awt.Image;

public class Enter extends GameObject {

    public Enter() {
        this.type = ENTER;
    }

    protected Image getImage() {
        return null;
    }
}

Script file Floor.java

import java.awt.Image;
import java.awt.Toolkit;

public class Floor extends GameObject {

    public Floor(Toolkit tkit) {
        this.type = FLOOR;
        this.img = tkit.getImage("src/image/floor.png");
    }

    protected Image getImage() {
        return img;
    }
}

Script dari GameObject.java

import java.awt.Image;

public abstract class GameObject {

    public final static byte FLOOR = 0;
    public final static byte GOAL = 1;
    public final static byte WALL = 2;
    public final static byte PACKAGE = 3;
    public final static byte SOKO_LEFT = 4;
    public final static byte SOKO_RIGHT = 5;
    public final static byte SOKO_UP = 6;
    public final static byte SOKO_DOWN = 7;
    public final static byte SPACE = 8;
    public final static byte ENTER = 9;
    protected byte type;
    protected Image img;

    public GameObject() {
    }

    public byte getType() {
        return type;
    }

    protected abstract Image getImage();
}

Script File Goal.java

import java.awt.Image;
import java.awt.Toolkit;
public class Goal extends GameObject {

    public Goal(Toolkit tkit) {
        this.type = GOAL;
        this.img = tkit.getImage("src/image/goal.png");
    }

    protected Image getImage() {
        return img;
    }
}

Script File Kanvas.java

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Kanvas extends Frame {
    private byte data, widthMatrix;
    private InputStream fin = null;
    private int jmlKarakter,posx,posy,index;
    private Toolkit tkit;
    private int npacket, indexPusher, level,
            width, height;
    private boolean win;
    private Soko soko;
    private GameObject[] mFGSWE,mPackage,mSoko;
    private JFileChooser jfc; //mendeklarasi file untuk menambahakan menu SAVE
    private Image offImg; //mendeklarasikan Graphics

    public Kanvas() {
        super("Sokoban V1.0");
        Dimension dim = Toolkit.getDefaultToolkit().
                getScreenSize();
        setSize(new Dimension(dim.width,dim.height));
        tkit = getToolkit();
        setLevel(0);
        bacaMap();
        jfc = new JFileChooser(".");

        MenuBar mBar = new MenuBar();
        Menu mFile = new Menu("File");

        MenuItem miOpen = new MenuItem("Open");
        miOpen.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                try {
                    int result = jfc.showOpenDialog(Kanvas.this);
                    if (result == jfc.APPROVE_OPTION){
                        BufferedReader buf = new BufferedReader(new FileReader(jfc.getSelectedFile()));
                        String line = null;
                        while ((line = buf.readLine()) !=null) {
                            level = Integer.parseInt(line);
                        }
                        buf.close();
                        setLevel(level);
                        bacaMap();
                        repaint();
                    }
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(Kanvas.this, "Error Loading File");
                    }
            }
        });

       mFile.add(miOpen);

        MenuItem miSave = new MenuItem ("Save");
       miSave.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt){
               try {
                   int result = jfc.showSaveDialog(Kanvas.this);
                   if (result == jfc.APPROVE_OPTION){
                       PrintWriter p = new PrintWriter(new FileWriter(jfc.getSelectedFile()));
                       p.print(level);
                       p.println();
                       p.close();
                   }
               } catch (Exception ex) {
                   JOptionPane.showMessageDialog(Kanvas.this, "Error Saving Coy");
               }
           }
       });
       mFile.add(miSave);

        MenuItem miPilihLevel = new
                MenuItem("Pilih Level");
        miPilihLevel.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                pilihLevelActionPerformed(evt);
            }
        });
        mFile.add(miPilihLevel);

        MenuItem miNextLevel = new MenuItem("Level Berikutnya");
        miNextLevel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if (level<33) {
                    level++;
                    setLevel(level);
                    bacaMap();
                    repaint();
                } else {
                    JOptionPane.showMessageDialog(
                            Kanvas.this,"Level maksimum adalah "+
                            level,"Konfirmasi",
                            JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        mFile.add(miNextLevel);

        MenuItem miPrevLevel = new MenuItem("Level Sebelumnya");
        miPrevLevel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if (level>0) {
                    level--;
                    setLevel(level);
                    bacaMap();
                    repaint();
                } else {
                    JOptionPane.showMessageDialog(Kanvas.this,
                            "Level minimum adalah "+
                            level,"Konfirmasi",
                            JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        mFile.add(miPrevLevel);

        MenuItem miRestart = new MenuItem("Restart");
        miRestart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                bacaMap();
                repaint();
            }
        });

        mFile.add(miRestart);

        MenuItem miUndo = new MenuItem("Undo");
        miUndo.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                undoMove(evt);
            }
        });
        mFile.add(miUndo);

        Menu mInfo = new Menu("Info");
        MenuItem miAbout = new MenuItem("Tentang Programmer");
        miAbout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JOptionPane.showMessageDialog(Kanvas.this,
                        "GAME SOKOBAN KHATULISTIWA" +
                        "Real Time Strategy (RTS).n"+
                        "Created by Suheri, S.T. Editing by bangfad " +
                        "Copyright © 2009 www.pontianak.web.idn"+
                        "Teknisi Komputer & Jaringan " +
                        "Politenik Negeri Pontianak",
                        "About Sokoban v1.1",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        });
        mInfo.add(miAbout);

        MenuItem miHelp = new MenuItem("Bantuan");
        miHelp.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JOptionPane.showMessageDialog(Kanvas.this,
                        "Game Sokoban merupakan salah satu jenis game Real Time Strategy (RTS)." +
                        "nKetika pertama kali dijalankan, pemain dapat langsung bermain padan" +
                        "level 0 dari 34 level yang ada. Terdapat dua menu utama, yaitu : menu File dan Info.n" +
                        "Menu File dibagi lagi menjadi beberapa sub menu, yaitu :n" +
                        "- Pilih Level, digunakan untuk memilih level yang akan dimainkan.n" +
                        "- Level Berikutnya, digunakan untuk memainkan level berikutnya.n" +
                        "- Level Sebelumnya, digunakan untuk memainkan level sebelumnya.n" +
                        "- Restart, digunakan untuk mengulang permainan pada level yang bersangkutan.n" +
                        "- Undo, digunakan untuk mengembalikan posisi pemain pada posisi sebelumnya.nn" +
                        "Menu Info dibagi lagi menjadi beberapa sub menu, yaitu :n" +
                        "- Tentang Programmer, digunakan untuk menampilkan informasi tentang programmer.n" +
                        "- Bantuan, digunakan untuk menampilkan informasi cara bermain.nn" +
                        "Cara Bermain : Gunakan tombol keyboardn" +
                        "- Left untuk melangkah ke kirin" +
                        "- Right untuk melangkah ke kanann" +
                        "- Up untuk melangkah ke atasn" +
                        "- Down untuk melangkah ke bawahn" +
                        "Tujuannya adalah mendorong paket yang telah disediakan ke tempat tujuannya.",
                        "Bantuan",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        });
        mInfo.add(miHelp);

        mBar.add(mFile);
        mBar.add(mInfo);
        setMenuBar(mBar);

        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent evt){
                       JOptionPane.showMessageDialog(Kanvas.this,
                        "GAME SOKOBAN KHATULISTIWA.n" +
                        "Real Time Strategy (RTS).n"+
                        "Created by Suheri, S.T. Editing by bangfad.n" +
                        "Copyright © 2009 www.pontianak.web.idn"+
                        "Teknisi Komputer & Jaringan " +
                        "Politenik Negeri Pontianak",
                        "About Sokoban v1.1",
                        JOptionPane.INFORMATION_MESSAGE);
                System.exit(0);
            }
        });

        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (!win) {
                    int keyCode = e.getKeyCode();
                    if (keyCode == e.VK_LEFT) {
                        soko.setType(soko.SOKO_LEFT);
                    } else if (keyCode == e.VK_RIGHT) {
                        soko.setType(soko.SOKO_RIGHT);
                    } else if (keyCode == e.VK_UP) {
                        soko.setType(soko.SOKO_UP);
                    } else if (keyCode == e.VK_DOWN) {
                        soko.setType(soko.SOKO_DOWN);
                    }
                    if (sizeMove(keyCode) != 0) {
                        indexPusher = soko.moveXY(mSoko,mFGSWE,mPackage,
                                indexPusher,sizeMove(keyCode));
                        repaint();
                        if (npacket == soko.getNPacketed()) {
                            win = true;
                            try {
                                String output = "Jumlah Langkah = "+
                                        soko.getMoves()+
                                        "nJumlah Langkah Mendorong Paket = "+
                                        soko.getPushes()+
                                        "nLevel berikutnya?";
                                int choice = JOptionPane.showConfirmDialog
                                        (Kanvas.this, output,
                                        "Score Anda",JOptionPane.YES_NO_OPTION,
                                        JOptionPane.INFORMATION_MESSAGE);
                                if (choice==0) {
                                    level++;
                                    if (level<=34) {
                                        setLevel(level);
                                        bacaMap();
                                        repaint();
                                    } else {
                                        level--;
                                    }
                                }
                            } catch (NumberFormatException nfe){
                            }
                        }
                    }
                }
            }
        });

        setVisible(true);
    }

    private void pilihLevelActionPerformed(ActionEvent evt){
        try {
            String choice = JOptionPane.showInputDialog(
                    Kanvas.this,"Level","Input Level",
                    JOptionPane.INFORMATION_MESSAGE);
            if ((Integer.parseInt(choice)>=0)&&(
                    Integer.parseInt(choice)<=34)){
                setLevel(Integer.parseInt(choice));
                bacaMap();
                repaint();
            }
        } catch (NumberFormatException nfe){
        }
    }

    private void undoMove(ActionEvent evt) {
        indexPusher = soko.moveXY(mSoko,mFGSWE,mPackage,
                indexPusher,soko.undoMove());
        repaint();
    }

    private void setLevel(int level) {
        this.level = level;
        setTitle("Sokoban v1.1 - Level "+level);
    }

    private int getLevel() {
        return this.level;
    }

    private int sizeMove(int move) {
        switch (move) {
            case KeyEvent.VK_LEFT :
                return -1;
            case KeyEvent.VK_RIGHT :
                return 1;
            case KeyEvent.VK_UP :
                return -widthMatrix;
            case KeyEvent.VK_DOWN :
                return widthMatrix;
        }
        return 0;
    }

    private void bacaMap() {
        try {
            fin = new FileInputStream("src/map/screen."+
                    Integer.toString(level));
            jmlKarakter = fin.available();
            index=0; npacket=0; widthMatrix=0; win=false;
            mFGSWE = new GameObject[jmlKarakter];
            mPackage = new GameObject[jmlKarakter];
            mSoko = new GameObject[jmlKarakter];
            do {
                data = (byte)fin.read();
                switch ((char)data) {
                    case '#' :
                        mFGSWE[index] = new Wall(tkit);
                        index++;
                        break;
                    case ' ' :
                        mFGSWE[index] = new Floor(tkit);
                        index++;
                        break;
                    case '$' :
                        mPackage[index] = new Package(tkit);
                        mFGSWE[index] = new Floor(tkit);
                        index++;
                        npacket++;
                        break;
                    case '.' :
                        mFGSWE[index] = new Goal(tkit);
                        index++;
                        break;
                    case '@' :
                        soko = new Soko(tkit,widthMatrix);
                        mSoko[index] = soko;
                        mFGSWE[index] = new Floor(tkit);
                        indexPusher = index;
                        index++;
                        break;
                    case 's' :
                        mFGSWE[index] = new Space(tkit);
                        index++;
                        break;
                    case 'n' :
                        mFGSWE[index] = new Enter();
                        mPackage[index] = new Enter();
                        mSoko[index] = new Enter();
                        index++;
                        if (widthMatrix == 0) {
                            widthMatrix = (byte)index;
                        }
                        break;
                    default :
                        break;
                }
            } while (data != -1);
        } catch (FileNotFoundException e) {
            System.out.println("Map tidak ditemukan");
        } catch (IOException e) {
            System.out.println("Eksepsi tidak ditemukan : "+
                    e);
        }finally {
            if (fin != null) {
                try {
                    fin.close();
                } catch (IOException err) {
                    System.out.println("Eksepsi tidak " +
                            "diketahui : "+err);
                }
            }
        }
    }

//diperlukan untuk membuat menu save graphics
    public void update(Graphics g){
    paint(g);
}
    public void paintOffScreen(Graphics g){
        g.setColor(getBackground());
        g.fillRect(0, 0, getSize().width, getSize().height);
        g.setColor(Color.BLACK);
          g.setColor(Color.BLACK);
        g.drawString("Jumlah Paket = " + npacket,10,60);
        g.drawString("Jumlah Paket Yang Telah Sampai = " +
                soko.getNPacketed(),10,75);
        g.drawString("Jumlah Langkah = " + soko.getMoves(),
                250,60);
        g.drawString("Jumlah Langkah Mendorong Paket = " +
                soko.getPushes(),250,75);

        posx=0; posy=100; width=height=0;
        for (int i=0; i

Script file Package.java

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.*;

public class Package extends GameObject {
  //  private AudioInputStream ngomong;
  //  private Clip clip;

  /*  private void nyampai() {
        try {
            ngomong = AudioSystem.getAudioInputStream(new File("src/sound/soko2.wav"));
            clip = AudioSystem.getClip();
            clip.open(ngomong);
            clip.start();
        } catch (LineUnavailableException lue) {
        } catch (IOException ioe){
        } catch (UnsupportedAudioFileException uaf) {
        } catch (NullPointerException npe) {
        }
    }
      */
    public Package(Toolkit tkit) {
        this.type = PACKAGE;
        this.img=tkit.getImage("src/image/package.png");
    }

    protected Image getImage() {
        return img;
    }

    private boolean isGoal(GameObject[] mFGSWE,int index) {
        if (mFGSWE[index].getType() == GOAL) {
            return true;
        }
        return false;
    }

    private void changeArrayPosisi(
            GameObject[] mPackage, int index, int move) {
        mPackage[index + move] = mPackage[index];
        mPackage[index] = null;
    }

    public int moveXY(GameObject[] mPackage,
            GameObject[] mFGSWE, int index, int move) {

        if (isGoal(mFGSWE,index)) {

            changeArrayPosisi(mPackage, index, move);
            if (isGoal(mFGSWE,index + move)) {
                return 0;
            }
            return -1;

        }
        if (isGoal(mFGSWE,index + move)) {
            changeArrayPosisi(mPackage, index, move);
       //     nyampai();
            return 1;
        }

        changeArrayPosisi(mPackage, index, move);

        return 0;
    }

}

Script file Soko.java

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
/*import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
*/
public class Soko extends GameObject {
    private static final byte MOVEPACKET = 3;
    private byte[] recordMove;
    private byte widthMatrix;
    private int moves, pushes, npacketed;
    private boolean undo;
    private Image[] img;
 //   private AudioInputStream ngomong;
 //   private Clip clip;

    public Soko(Toolkit tkit,byte widthMatrix) {

        this.type = SOKO_LEFT;
        img = new Image[4];
        img[0] = tkit.getImage("src/image/soko_left.png");
        img[1] = tkit.getImage("src/image/soko_right.png");
        img[2] = tkit.getImage("src/image/soko_up.png");
        img[3] = tkit.getImage("src/image/soko_down.png");
        recordMove = new byte[200];
        moves = 0;
        pushes = 0;
        npacketed = 0;
        this.widthMatrix=widthMatrix;
        undo = false;

    }

/*    private void mendorongPaket() {
         try {
            ngomong = AudioSystem.getAudioInputStream(new File("src/sound/Neutral.mp3"));
            clip = AudioSystem.getClip();
            clip.open(ngomong);
            clip.start();
        } catch (LineUnavailableException lue) {
        } catch (IOException ioe){
        } catch (UnsupportedAudioFileException uaf) {
        } catch (NullPointerException npe) {
        }
    }

     private void ngomonglewat() {
         try {
            ngomong = AudioSystem.getAudioInputStream(new File("src/sound/Enemy.pm3"));
            clip = AudioSystem.getClip();
            clip.open(ngomong);
            clip.start();
        } catch (LineUnavailableException lue) {
        } catch (IOException ioe){
        } catch (UnsupportedAudioFileException uaf) {
        } catch (NullPointerException npe) {
        }
    }
 */

    public void setType(byte type) {
        this.type = type;
    }

    protected Image getImage() {
        switch (type) {
            case SOKO_LEFT :
                return img[0];
            case SOKO_RIGHT :
                return img[1];
            case SOKO_UP :
                return img[2];
            case SOKO_DOWN :
                return img[3];
        }
        return null;
    }

    private boolean isWall(
            GameObject [] mFGSWE, int index, int move) {
        if (mFGSWE[index + move].getType() == WALL) {
            return true;
        }
        return false;
    }

    private boolean isPackage(
            GameObject [] mPackage, int index, int move) {
        if (mPackage[index + move] != null) {
            if (mPackage[index + move].getType() ==
                    PACKAGE) {
                return true;
            }
        }
        return false;
    }

    public int getMoves() {
        return moves;

    }

    public int getPushes() {
        return pushes;
    }

    public int getNPacketed() {
        return npacketed;
    }

    public void setWidthMatrix(byte width) {
        widthMatrix = width;
    }

    public int getWidthMatrix() {
        return widthMatrix;
    }

    private void saveMove(int move) {
        if (moves >= recordMove.length) {
            byte[] n = new byte[recordMove.length + 200];
            System.arraycopy(recordMove, 0, n, 0,
                    recordMove.length);
            recordMove = n;
        }
        recordMove[moves] = (byte)move;
    }

    public int undoMove() {
        if (moves > 0) {
            undo = true;
            return recordMove[--moves];
        }
        return 0;
    }

    private void changeArrayPosisi(GameObject[] mSoko,
            int index, int move) {
        mSoko[index + move] = mSoko[index];
        mSoko[index] = null;
    }

    private int checkPushes(GameObject[] mFGSWE,
            GameObject[] mPackage, int index, int move) {
        if ((move-MOVEPACKET) == -1) {
            npacketed += ((Package)mPackage[index + 1]).
                    moveXY(mPackage,mFGSWE,index + 1,-1);
            move-=MOVEPACKET;
            pushes--;
        } else if ((move-MOVEPACKET) == 1) {
            npacketed += ((Package)mPackage[index - 1 ]).
                    moveXY(mPackage,mFGSWE,index - 1,1);
            move-=MOVEPACKET;
            pushes--;
        } else if ((move-MOVEPACKET) == -widthMatrix) {
            npacketed += ((Package)mPackage[index +
                    widthMatrix]).moveXY(mPackage,mFGSWE,
                    index + widthMatrix,-widthMatrix);
            move-=MOVEPACKET;
            pushes--;
        } else if ((move-MOVEPACKET) == widthMatrix) {
            npacketed += ((Package)mPackage[index -
                    widthMatrix]).moveXY(mPackage,mFGSWE,
                    index - widthMatrix,widthMatrix);
            move-=MOVEPACKET;
            pushes--;

        }

        return move;

    }

    public int moveXY(GameObject[] mSoko,
            GameObject[] mFGSWE, GameObject[] mPackage,
            int index, int move) {
        move = checkPushes(mFGSWE,mPackage,index,move);
        if (move != 0) {
            if (isWall(mFGSWE,index,move)) {
                return index;
            }

            if (isPackage(mPackage,index,move)) {
                if (isWall(mFGSWE,index + move,move)) {
                    return index;
                }
                if (isPackage(mPackage,index + move,move)){
                    return index;
                }
        //        mendorongPaket();
                npacketed += ((Package)mPackage[index + move]).
                        moveXY(mPackage,mFGSWE,index + move,move);
                changeArrayPosisi(mSoko, index, move);
                if (undo == false) {
                    saveMove((-move) + MOVEPACKET);
                    moves++;
                    pushes++;
                } else {
                    undo = false;
                }
                return index + move;
            }
   //     ngomonglewat();
            changeArrayPosisi(mSoko, index, move);
            if (undo == false) {
                saveMove(-move);
                moves++;
            } else {
                undo = false;
            }
            return index + move;
        }
        return index;
    }

}

Script file Space.java

import java.awt.Image;
import java.awt.Toolkit;
public class Space extends GameObject {

    public Space(Toolkit tkit) {
        this.type = SPACE;
        this.img = tkit.getImage("src/image/space.png");
    }

    protected Image getImage() {
        return img;
    }
}

Script file SplashScreen.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.border.LineBorder;

public class SplashScreen extends JWindow {
    private Dimension dim1 = Toolkit.getDefaultToolkit()
    .getScreenSize();
    private JLabel label = new JLabel(
            new ImageIcon("src/image/splash.jpg"));
    private JProgressBar pro = new JProgressBar();
    private int time1 = 0;
    private Timer timer1;
    private AudioInputStream ngomong;
    private Clip clip;

    public SplashScreen() {
        try {
            ngomong = AudioSystem.getAudioInputStream(
                    new File ("src/sound/open.mp3"));
            clip = AudioSystem.getClip();
            clip.open(ngomong);
            clip.start();
        } catch (LineUnavailableException lue) {
        } catch (IOException ioe) {
        } catch (UnsupportedAudioFileException uaf) {
        }
        Color warna = Color.BLUE;
        pro.setValue(0);
        pro.setPreferredSize(new Dimension(240,15));
        pro.setBackground(Color.WHITE);
        pro.setForeground(Color.RED);
        pro.setStringPainted(true);
        pro.setBorder(new LineBorder(warna,1));
        label.setBorder(new LineBorder(warna,1));
        getContentPane().add(label,BorderLayout.NORTH);
        getContentPane().add(pro,BorderLayout.CENTER);
        timer1 = new Timer(1,new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                time1++;
                pro.setValue(time1);
                if (pro.getPercentComplete()==1.0) {
                    timer1.stop();
                    setVisible(false);
                    new Kanvas();
                }
            }
        });
        timer1.start();
        pack();
        setLocation(dim1.width/2-getWidth()/2,
                dim1.height/2-getHeight()/2);
        setVisible(true);
    }

    public static void main (String[] args) {
        new SplashScreen();
    }
}

Script file Wall.java

import java.awt.Image;
import java.awt.Toolkit;
public class Wall extends GameObject {

    public Wall(Toolkit tkit) {
        this.type = WALL;
        this.img = tkit.getImage("src/image/wall.png");
    }

    protected Image getImage() {
        return img;
    }
}

Setelah semua script telah selesai, silakan menjalankan menu RUN dari NetBeans dan source code diatas dapat download disini (Next Title “MEMBUAT_FILE_EXECUTABLE_EXE_DARI_FILE_JAVA_ARCHIVE_JAR”…)

Tags: #Download Game #Game Java #Game RTS #Polnep #Programer #Sokoban #TJK

author
Author: 

Bangfad – Bukan Blogger Profesional, hanya mencoba berbagi pengalaman dan cerita. Belajar, Belajar dan Belajar untuk menjadi seorang penulis profesional dengan hobi Menulis, Ngoprek dan Mancing.

Pelatihan Web Blog dan Citizen Jurnalist
Pelatihan Web Blog dan Citizen Jurnalist
Shares/Shares on facebookTweet on twitter Setelah sekian
Cara Reset HP LG K8 4G K350N Factory Reset
Cara Reset HP LG K8 4G K350N Factory Reset
Shares/Shares on facebookTweet on twitter Pengalaman adalah
Tangisan pertamanya di 6 Maret 2017
Tangisan pertamanya di 6 Maret 2017
Shares/Shares on facebookTweet on twitterAlhamdulilah segala puji
Pengenalan Aplikasi Pemetaan dengan DevInfo
Pengenalan Aplikasi Pemetaan dengan DevInfo
Shares/Shares on facebookTweet on twitterBeberapa hari yang
  1. author

    ugo9 years ago

    bagus sekali brooooooo..tanks boleh di peraktekin kan……:)

    Reply
  2. author

    Saia8 years ago

    Salut untuk guru besar Chen aka Suheri, ST. Maju terus ya ……

    Reply

Leave a reply "Membuat Game Sokocang"

 

Must read×

Top