/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
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;
import javax.microedition.m3g.Graphics3D;
/**
* @author kira
*/
public class Game extends GameCanvas implements Runnable{
Graphics g = getGraphics();
Image bg;
boolean aktif=true;
int posX=100,posY=100,kec=3,bgpos=1,score=0,waktu=60,jalan=0;
Sprite pesawat,ufo,bom,peluru,kar;
private Random r;
private Timer t;
int kiri[]={1,2};
int kanan[]={3,4};
int depan[]={5,6};
int belakang[]={7,8};
public Game(){
super(true);
init();
new Thread(this).start();
}
private void init(){
r = new Random();
t = new Timer();
t.schedule(new TimerTask() {
public void run() {
waktu--;
}
}, 1000, 1000);
try {
bg=Image.createImage("/bg2.png");
pesawat=new Sprite(Image.createImage("/airplane.png"));
ufo=new Sprite(Image.createImage("/ufo.png"));
bom=new Sprite(Image.createImage("/ledakan.png"), 125/5, 25);
peluru=new Sprite(Image.createImage("/peluru.png"));
kar=new Sprite(Image.createImage("/char.png"),420/10,44);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void run() {
while(true){
//looping game
g.setColor(255,0,0);
bgpos++;
if(bgpos>bg.getHeight()){
bgpos=0;
}
g.drawImage(bg, 0, bgpos, 0);
g.drawImage(bg, 0, bgpos-bg.getHeight(), 0);
g.drawString("Score :"+score, 5, 5, 0);
g.drawString("Time :"+waktu, 150, 5, 0);
pesawatX();
ufoX();
peluruX();
bomX();
karX();
pesawatX();
//batas loop game
flushGraphics();
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
private void pesawatX(){
// int tombol = getKeyStates();
// if(tombol==RIGHT_PRESSED){
// posX+=kec;
//
// if(posX>=200)posX=200;
// }
// if(tombol==LEFT_PRESSED){
// posX-=kec;
// if(posX<=0)posX=0;
// }
// if(tombol==DOWN_PRESSED){
// posY+=kec;
// }
// if(tombol==UP_PRESSED){
// posY-=kec;
// }
// if(tombol==FIRE_PRESSED){
// aktif=false;
// }
pesawat.setPosition(posX, posY);
pesawat.paint(g);
}
private void peluruX(){
if(aktif){
peluru.setPosition(pesawat.getX(),pesawat.getY());
peluru.setVisible(false);
}else{
peluru.setVisible(true);
peluru.move(0, -5);
}
if(peluru.getY()<0)aktif=true;
if(peluru.collidesWith(ufo, true)){
aktif=true;
score+=10;
}
peluru.paint(g);
}
private void ufoX(){
ufo.setPosition(10, 50);
ufo.paint(g);
}
private void bomX(){
//bom.setPosition(120, 20);
bom.move(0, 5);
bom.nextFrame();
if(bom.getY()>getHeight()){
bom.setPosition(r.nextInt(getWidth()-bom.getWidth()), 0);
}
bom.paint(g);
}
private void karX(){
//kar.setPosition(100, 200);
int tombol = getKeyStates();
if(tombol==RIGHT_PRESSED){
if(jalan<1){kar.setFrameSequence(kanan);jalan=0;}
kar.nextFrame();
kar.move(3, 0);
}
if(tombol==LEFT_PRESSED){
if(jalan==0){kar.setFrameSequence(kiri);jalan=1;}
kar.nextFrame();
kar.move(-3, 0);
}
if(tombol==DOWN_PRESSED){
kar.setFrameSequence(depan);
kar.nextFrame();
kar.move(0, 3);
}
if(tombol==UP_PRESSED){
kar.setFrameSequence(belakang);
kar.nextFrame();
kar.move(0, -3);
}
kar.paint(g);
}
}
Tidak ada komentar:
Posting Komentar