You are on page 1of 4

/* * To change this template, choose Tools | Templates * and open the template in the editor.

*/ package lat; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.game.GameCanvas; public class Game1 extends GameCanvas implements Runnable { private private private private int int int int posisikotak1_x; posisikotak1_y; lebarkotak; panjangkotak;

//property kotakdorong private int posisikotak2_x; private int posisikotak2_y; private int target_x; private int target_y; private int target1_x; private int target1_y; //properti score && nyawa private int score; private int nyawa; private int geser; private int a = 255; private int b = 0; private int c = 0; private int a1 = 255; private int b1 = 0; private int c1 = 0; Graphics g; public Game1() { super(false); setFullScreenMode(true); g = getGraphics(); } public void run() { inisialisasi(); while (nyawa>0) { gambarBackground(); inputUser(); updatePosisi(); update_xy(); draw(); flushGraphics(); try { Thread.sleep(1000 / 30);

} catch (Exception ex) { ex.printStackTrace(); } } gameover(); } private void inisialisasi() { panjangkotak = 20; //sesuaikan lebarkotak = 20; //sesuaikan posisikotak1_x = getWidth()/2 - panjangkotak; posisikotak1_y = getHeight()/2 - lebarkotak; posisikotak2_x = getWidth()/2 - panjangkotak; posisikotak2_y = getHeight() - lebarkotak; target_x=(getWidth()/2)-3*panjangkotak; target_y=(getHeight()/2)-(2*lebarkotak); target1_x=(getWidth()/2)+3*panjangkotak; target1_y=(getHeight()/2)+(2*lebarkotak); geser = 1; //sesuaikan........ score = 0; nyawa = 10; a=255; b=0; c=0; } private void gambarBackground() { g.setColor(255, 255, 255); g.fillRect(0, 0, getWidth(), getHeight()); } private void inputUser() { //System.out.println("baca input dari user"); int keyState = getKeyStates(); if ((keyState & UP_PRESSED) != 0) { int temppoint; int temppoint2; temppoint=posisikotak2_x + panjangkotak; temppoint2=posisikotak2_x; if(posisikotak2_y==posisikotak1_y+panjangkotak){ if ((temppoint >= posisikotak1_x) && (temppoint2 <= (posisikotak 1_x+lebarkotak))){ posisikotak1_y -= geser; } } posisikotak2_y -= geser; } if ((keyState & DOWN_PRESSED) != 0) { if ((posisikotak2_y + panjangkotak) == posisikotak1_y) { if (((posisikotak2_x+lebarkotak) >= posisikotak1_x) && (posisiko tak2_x)<=posisikotak1_x+lebarkotak){ posisikotak1_y += geser;

} } posisikotak2_y += geser; } if ((keyState & LEFT_PRESSED) != 0) { if (posisikotak2_x == posisikotak1_x+panjangkotak) { if ((posisikotak2_y+panjangkotak >= posisikotak1_y) && (posisiko tak2_y)<=posisikotak1_y+panjangkotak){ posisikotak1_x -= geser; } } posisikotak2_x -= geser; } if ((keyState & RIGHT_PRESSED) != 0) { if (posisikotak2_x+panjangkotak == posisikotak1_x) { if ( (posisikotak2_y <= posisikotak1_y+lebarkotak) && (posisikot ak2_y+lebarkotak>=posisikotak1_y)){ posisikotak1_x += geser; } } posisikotak2_x += geser; } } private void updatePosisi() { if (posisikotak1_x >= posisikotak1_y) { //int tempPoint = posisibola_x + radiusbola/2; int tempPoint = posisikotak1_x; if ((tempPoint > posisikotak1_x) && (tempPoint < posisikotak1_x + panjangkotak)) { //kecepatanbola_y = Math.abs(kecepatanbola_y) * -1; score += 2; } } } public void update_xy(){ if((posisikotak1_x==target_x)&&(posisikotak1_y==target_y)){ g.setColor(255,255,0); //ganti........... g.fillRect(target_x, target_y, panjangkotak, lebarkotak); g.setColor(255,255,0); //ganti........... g.fillRect(posisikotak1_x, posisikotak1_y, panjangkotak, lebarkotak) ; g.setColor(0,0,0); //ganti........... g.fillRect(posisikotak2_x, posisikotak2_y, panjangkotak, lebarkotak) ; } }

private void draw() { if((posisikotak1_x==target_x)&&(posisikotak1_y==target_y)){ g.setColor(255,0,0); //ganti........... g.fillRect(target1_x, target1_y, panjangkotak, lebarkotak); a=255; b=255; c=136; g.setColor(255,255,136); //ganti........... g.fillRect(target_x, target_y, panjangkotak, lebarkotak); g.setColor(255,255,136); //ganti........... g.fillRect(posisikotak1_x, posisikotak1_y, panjangkotak, lebarkotak) ; g.setColor(0,0,0); //ganti........... g.fillRect(posisikotak2_x, posisikotak2_y, panjangkotak, lebarkotak) ; }else if((posisikotak1_x==target1_x)&&(posisikotak1_y==target1_y)){ g.setColor(255,0,0); //ganti........... g.fillRect(target_x, target_y, panjangkotak, lebarkotak); a1=255; b1=255; c1=136; g.setColor(255,255,136); //ganti........... g.fillRect(target1_x, target1_y, panjangkotak, lebarkotak); g.setColor(255,255,136); //ganti........... g.fillRect(posisikotak1_x, posisikotak1_y, panjangkotak, lebarkotak) ; g.setColor(0,0,0); //ganti........... g.fillRect(posisikotak2_x, posisikotak2_y, panjangkotak, lebarkotak) ; } else{ g.setColor(a,b,c); //ganti........... g.fillRect(target_x, target_y, panjangkotak, lebarkotak); g.setColor(a1,b1,c1); //ganti........... g.fillRect(target1_x, target1_y, panjangkotak, lebarkotak); g.setColor(0,0,0); //ganti........... g.fillRect(posisikotak1_x, posisikotak1_y, panjangkotak, lebarkotak); g.setColor(0,0,0); //ganti........... g.fillRect(posisikotak2_x, posisikotak2_y, panjangkotak, lebarkotak); }} public void start() { Thread runner = new Thread(this); runner.start(); } private void gameover(){ g.setColor(0xff00ff); g.drawString("Game Over !", getWidth()/2-50, getHeight()/2-15, 0); flushGraphics(); } }

You might also like