Senin, 30 April 2012

Script


Splashscreen
public class splashscreen extends Canvas {
    Image splash;
public splashscreen(){
    setFullScreenMode(true);
        try {
            splash=Image.createImage("/Gambar/stitchangel.jpg");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    protected void paint(Graphics g) {
        g.setColor(255,255,255);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(splash,getWidth()/2, getHeight()/2, Graphics.HCENTER | Graphics.VCENTER);
         }
Midlet
public class MidletWinda extends MIDlet {
    Display tampilan;
    splashscreen splash;
    MidletWinda midlet;
    Menu gambarmenu;
    about about;
    Main main;
    GamePlay gy;//deklarasi kelas GamePlay
   
    public void startApp() {
        try {
            //method untuk memulai aplikasi
           
tampilan=Display.getDisplay(this);//inisiasi variabel display
splash=new splashscreen();
gy=new GamePlay();//inisiasi kelas GamePlay
gambarmenu=new Menu(this);
tampilan.setCurrent(gy);

Thread.sleep(1000);

//tampilan.setCurrent(gambarmenu);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
   
    public void pauseApp() {
    }
   
    public void destroyApp(boolean unconditional) {
    System.gc();
    notifyDestroyed();
    }

    void pilihMenu(int pilih) {
        switch(pilih){
            case 1:
                break;
            case 2:
                about=new about();
                tampilan.setCurrent(about);
                break;
            case 3:
                destroyApp(true);
                break;
                default:
    }
    }
}
Menu
public class Menu extends GameCanvas implements Runnable{
    MidletWinda midlet;
    Image gambarmenu;
    Image play,about,exit,playoff,aboutoff,exitoff;
    Graphics g=getGraphics();
    int pilih;
   
    public Menu(MidletWinda mh) {
        super(true);
        midlet=mh;
        try {
            gambarmenu=Image.createImage("/Gambar/background.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        this.midlet=mh;
        pilih=1;
        mulai();
        new Thread(this).start();
    }
   
    public void paint(){
        g.fillRect(0,0, getWidth(), getHeight());
        g.drawImage(gambarmenu, getWidth()/2, getHeight()/2, Graphics.HCENTER | Graphics.VCENTER);
         if(pilih==1) {
        g.drawImage(play,120,90, Graphics.HCENTER | Graphics.VCENTER);
        } else {
        g.drawImage(playoff,120,90, Graphics.HCENTER | Graphics.VCENTER);
         }if(pilih==2) {
        g.drawImage(about,120,155, Graphics.HCENTER | Graphics.VCENTER);
        } else {
        g.drawImage(aboutoff,120,155, Graphics.HCENTER | Graphics.VCENTER);
         }if(pilih==3) {  
        g.drawImage(exit,120,220, Graphics.HCENTER | Graphics.VCENTER);
        } else {
        g.drawImage(exitoff,120,220, Graphics.HCENTER | Graphics.VCENTER);
    }
 }
    public void mulai(){
        try {
            setFullScreenMode(true);
            play=Image.createImage("/Gambar/play_on.png");
            about=Image.createImage("/Gambar/about_on.png");
            exit=Image.createImage("/Gambar/quit_on.png");
            playoff=Image.createImage("/Gambar/play_off.png");
            aboutoff=Image.createImage("/Gambar/about_off.png");
            exitoff=Image.createImage("/Gambar/quit_off.png");   
            
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
   public void pencet() {
      
       int pencet=getKeyStates();
       if(pencet==DOWN_PRESSED) {
           pilih++;
       if(pilih>3) {
           pilih=1;
       }
          
       }
       if(pencet==UP_PRESSED) {
           pilih--;
       if(pilih<1) {
           pilih=3;
       }
          
   }
       if(pencet==FIRE_PRESSED) {
          
          midlet.pilihMenu(pilih);
          
       }
   }
   public void run() {
       
            while(true){
            try {
                pencet();
            } catch (Exception ex){
                ex.printStackTrace();
            }
            paint();
            flushGraphics();
            try {
                Thread.sleep(250);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
       }
   }
}
About
public class about extends Canvas {
    Image splash;
   
public about(){
    setFullScreenMode(true);
        try {
            splash=Image.createImage("/Gambar/aboutyes.png");
        } catch (IOException ex) {
            ex.printStackTrace();     
        }
    }
    protected void paint(Graphics g) {
        g.setColor(255,255,255);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(splash,getWidth()/2, getHeight()/2, Graphics.HCENTER | Graphics.VCENTER);
    }
}
GamePlay
public class GamePlay extends GameCanvas implements Runnable {
    Image gblatar;//deklarasi gambar background
    private Sprite player, enemy;
    private boolean stop;
    Graphics g=getGraphics();
    int pospX,pospY,posEx,posEy,skor;
    int posgbY;
   
    public GamePlay() {
        super(true);
        inisiasi();
        new Thread(this).start();//jalankan thread      
    }
    public void inisiasi() {
        try {
            gblatar=Image.createImage("/Gambar/jalan.png");
            //inisiasi variabel gblatar
            player=new Sprite(Image.createImage("/Gambar/player2.png"));
            //inisiasi variabel player
            pospX=120;
            pospY=230;
            enemy=new Sprite(Image.createImage("/Gambar/enemy.png"));
            posEx=75;
            posEy=140;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void paint() {
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(gblatar,getWidth()/2,getHeight()/2,Graphics.HCENTER | Graphics.VCENTER);
        player.setPosition(pospX,pospY);
        enemy.setPosition(posEx, posEy);
        player.paint(g);
        enemy.paint(g);
    }
   
    public void tambahskor() {
        if(player.collidesWith(enemy, true)){
            skor+=100000;
            enemy.setVisible(false);
        }
    }
   
    public void press() {
        int pos=getKeyStates();
       
        if((pos & RIGHT_PRESSED) !=0) {
        player.setPosition(pospX+=3, pospY);
        if(pospX>120) {
            pospX=120;
        }
        }
        if((pos & LEFT_PRESSED) !=0) {
        player.setPosition(pospX-=3, pospY);
        if(pospX<75) {
            pospX=75;
        }
        }
        if((pos & UP_PRESSED) !=0) {
        player.setPosition(pospX, pospY-=3);
        if(pospY<0) {
            pospY=0;
        }
        }
        if((pos & DOWN_PRESSED) !=0) {
        player.setPosition(pospX, pospY+=3);
        if(pospY>240) {
            pospY=240;
        }
        }
    }
    public void run() {
        while(!stop) {
        try {
            paint();//memanggil method paint
            press();
            tambahskor();
            g.drawString("Score:"+skor, 0, 0, 0);
            flushGraphics();//membersihkan tampilan layar
            Thread.sleep(50);//menghentikan thread selama 1/50000 detik
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
    }       
}