Tutorial Game Java Mobile Application

Hai teman-teman 😀 , kali ini saya akan mengajarkan cara membuat game sederhana menggunakan Netbeans berbasis Java Mobile Application, berikut langkah-langkahnya:
Sun-Java-JDK_1

  1. Buat project baru dan beri nama sesuai keinginan,
  2. Setelah membuat project, langkah selanjutnya adalah membuat java midlet beri nama file Midlet, berikut isi sintaks java midlet,
  3. /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    import javax.microedition.lcdui.Display;
    import javax.microedition.midlet.*;
    
    /**
     * @author AdhityaBEe-PC
     */
    public class Midlet extends MIDlet {
    
        Display d;
        Game gm;
    
        public void startApp() {
            d = Display.getDisplay(this);
            gm = new Game(this);
            d.setCurrent(gm);
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
    }
    
  4. Buat class baru dan beri nama Game, isikan sintaks berikut pada class yang telah dibuat,
  5. 
    import java.io.IOException;
    import java.util.Random;
    import java.util.Timer;
    import java.util.TimerTask;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.lcdui.Image;
    import javax.microedition.lcdui.game.GameCanvas;
    import javax.microedition.lcdui.game.Sprite;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    /**
     *
     * @author AdhityaBEe-PC
     */
    public class Game extends GameCanvas implements Runnable {
    
        Graphics g = getGraphics();
        Midlet midlet;
        Sprite space, fire, enemy, fireEnemy, meteor;
        int posSpaceX = 100, posSpaceY = 240, moveSpace = 5;
        int posEnemyX = 100, posEnemyY = 25, move = 5;
        int posMeteorX = 100, posMeteorY = 0;
        boolean shoot = false;
        int score = 0, life = 5;
        Image bg;
        int posBackground = 10;
        Timer t;
    
        public Game(Midlet m) {
            super(true);
            this.midlet = m;
            setFullScreenMode(true);
            try {
                imageSource();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            new Thread(this).start();
        }
    
        public void imageSource() throws IOException {
            bg = Image.createImage("/image/background.png");
            space = new Sprite(Image.createImage("/image/space.png"));
            enemy = new Sprite(Image.createImage("/image/enemy.png"));
            fire = new Sprite(Image.createImage("/image/rocket.png"));
            fireEnemy = new Sprite(Image.createImage("/image/shoot.png"));
            meteor = new Sprite(Image.createImage("/image/meteor.png"));
        }
    
        public void drawSpace() {
            space.setPosition(posSpaceX, posSpaceY);
            space.paint(g);
        }
    
        public void drawEnemy() {
            enemy.setPosition(posEnemyX, posEnemyY);
            if (posEnemyX > 190) {
                move *= -1;
            } else if (posEnemyX < 0) {
                move *= -1;
            }
            posEnemyX += move;
            enemy.paint(g);
        }
    
        public void drawFire() {
            if (shoot) {
                fire.move(0, -10);
                if (fire.getY() < 5) {
                    shoot = false;
                }
            } else {
                fire.setPosition(posSpaceX + 10, posSpaceY + 25);
            }
            fire.paint(g);
        }
    
        public void drawFireEnemy() {
            fireEnemy.move(0, 5);
            if (fireEnemy.getY() > 300) {
                fireEnemy.setPosition(posEnemyX + 20, posEnemyY + 50);
            }
            fireEnemy.paint(g);
        }
    
        public void control() {
            int keyButton = getKeyStates();
            if (keyButton == LEFT_PRESSED) {
                if (posSpaceX >= 5) {
                    posSpaceX -= moveSpace;
                }
            } else if (keyButton == RIGHT_PRESSED) {
                if (posSpaceX <= 200) {
                    posSpaceX += moveSpace;
                }
            } else if (keyButton == FIRE_PRESSED) {
                shoot = true;
            }
        }
    
        public void attack() {
            int x = 0;
            if (fire.collidesWith(enemy, true)) {
                enemy.setVisible(false);
                fireEnemy.setVisible(false);
                shoot = false;
                score += 10;
                x = 1;
            } else if (fire.collidesWith(meteor, true)) {
                meteor.setVisible(false);
                shoot = false;
                score += 5;
                x = 1;
            }
            if (x == 1) {
                t = new Timer();
                t.schedule(new TimerTask() {
    
                    public void run() {
                        enemy.setVisible(true);
                        fireEnemy.setVisible(true);
                        meteor.setPosition(posMeteorX, posMeteorY);
                        meteor.setVisible(true);
                    }
                }, 2000);
                x = 0;
            }
        }
    
        public void attackEnemy() {
            int x = 0;
            if (fireEnemy.collidesWith(space, true)) {
                space.setVisible(false);
                fire.setVisible(false);
                shoot = false;
                life--;
                x = 1;
            } else if (meteor.collidesWith(space, true)) {
                space.setVisible(false);
                fire.setVisible(false);
                shoot = false;
                life--;
                x = 1;
            }
            if (life == 0) {
                int key = getKeyStates();
                space.setVisible(false);
                fire.setVisible(false);
                enemy.setVisible(false);
                fireEnemy.setVisible(false);
                meteor.setVisible(false);
                g.setColor(255, 255, 255);
                g.drawString("Game Over", 90, 150, 0);
                if (key == FIRE_PRESSED) {
                    space.setVisible(true);
                    fire.setVisible(true);
                    enemy.setVisible(true);
                    fireEnemy.setVisible(true);
                    meteor.setVisible(true);
                    score = 0;
                    life = 5;
                }
            } else if (x == 1) {
                t = new Timer();
                t.schedule(new TimerTask() {
    
                    public void run() {
                        space.setVisible(true);
                        fire.setVisible(true);
                    }
                }, 1000);
                x = 0;
            }
        }
    
        public void meteor() {
            Random random = new Random();
            for (int i = 0; i <= 1; i++) {
                int randomMeteor = random.nextInt(100);
                meteor.move(-1, 5);
                if (meteor.getY() > 300) {
                    meteor.setPosition(posMeteorX + randomMeteor, posMeteorY);
                }
                meteor.paint(g);
            }
        }
    
        public void run() {
            while (true) {
                posBackground++;
                if (posBackground > bg.getHeight()) {
                    posBackground = 0;
                }
                g.drawImage(bg, 0, posBackground, 0);
                g.drawImage(bg, 0, posBackground - bg.getHeight(), 0);
                g.setColor(255, 255, 255);
                g.drawString("Score: " + score, 10, 10, 0);
                g.drawString("Life: " + life, 175, 10, 0);
                drawSpace();
                drawEnemy();
                drawFire();
                drawFireEnemy();
                control();
                attack();
                attackEnemy();
                meteor();
                flushGraphics();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
    
  6. Berikut hasil game yang telah dibuat,
  7. Untitled
    Semoga bermanfaat dan selamat mencoba 😀

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s