Sebelum kita mulai belajar hayuk doa dulu biar di kasih kemudahan. Bismillah..
Source Code Midlet
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mobile;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.*;
/**
* @author xxx
*/
public class Midlet extends MIDlet {
Display d;
GameAmri ga;
public void startApp() {
d = Display.getDisplay(this);
ga = new GameAmri(this);
d.setCurrent(ga);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
Source Code Class
package mobile;
import java.io.IOException;
import java.util.Random;
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.media.protocol.SourceStream;
/**
*
* @author xxx
*/
public class GameAmri extends GameCanvas implements Runnable{
Random random = new Random();
Graphics g = getGraphics();
Midlet midlet;
Sprite pesawat, musuh, rocket, meteor, raja,roketraja,youwin,youlose; //karakter yang dimainkan dalam game
int pospesX = 100, pospesY = 230, pindah = 10 , pindah1 = 1;//posisi
int posmusX = random.nextInt(((210-0)+1)+0), posmusY = 0;
int posmusXX = random.nextInt(((210-2)+5)+0), posmusYY = 5;
int musuhX = 100, musuhY = 10;
boolean tembak=false;
boolean tembakroketraja = false;
int score =0;
boolean run=true;
Image bg;
int bgPos=10;
public GameAmri(Midlet m) {
super(true);
this.midlet = m;
setFullScreenMode(true);
try {
sourceKu();
} catch (IOException ex) {
ex.printStackTrace();
}
new Thread(this).start();
}
public void drawpesawat() {//manggil posisi pesawat
pesawat.setPosition(pospesX, pospesY);
pesawat.paint(g);
}
public void drawmusuh() {
musuh.setPosition(posmusX, posmusY);
musuh.paint(g);
posmusY++;
}
public void roketraja(){
if (tembak == false){
roketraja.setPosition(musuhX + 20, musuhY + 10);
}
roketraja.paint(g);
}
private void raja(){
if (score>=100){
musuhX +=pindah1;
if (musuhX > 150){
pindah1 = -pindah1;
} else if (musuhX < 10){
pindah1 = -pindah1;
}
raja.setPosition(musuhX, musuhY);
raja.paint(g);
roketraja();
menangkalah();
meteor.setVisible(false);
musuh.setVisible(false);
}
}
public void drawMeteor(){
meteor.setPosition(posmusXX, posmusYY);
meteor.paint(g);
posmusYY++;
}
public void drawYouwin(){
youwin.setPosition(0, 5);
youwin.paint(g);
}
public void drawYoulose(){
youlose.setPosition(0, 5);
youlose.paint(g);
}
public void sourceKu() throws IOException {
bg = Image.createImage("/Gambar/bg2.png");
pesawat = new Sprite(Image.createImage("/Gambar/kapal.png"));
musuh = new Sprite(Image.createImage("/Gambar/2.png"));
rocket = new Sprite(Image.createImage("/Gambar/rocket.png"));
meteor = new Sprite(Image.createImage("/Gambar/3.png"));
raja = new Sprite(Image.createImage("/Gambar/musuhku.png"));
roketraja = new Sprite(Image.createImage("/Gambar/roketmusuh.png"));
youwin = new Sprite(Image.createImage("/Gambar/youwin_1.png"));
youlose = new Sprite(Image.createImage("/Gambar/youlose.png"));
}
protected void control(){
int tombol=getKeyStates();
if(tombol==RIGHT_PRESSED){
if(pospesX<209){
pospesX+=pindah;
}
}
else if(tombol==LEFT_PRESSED){
if(pospesX>0){
pospesX-=pindah;
}
}
else if(tombol==UP_PRESSED){
if(pospesY>2){
pospesY-=pindah;
}
}
else if(tombol==DOWN_PRESSED){
if(pospesY<250){
pospesY+=pindah;
}
}else if(tombol==FIRE_PRESSED){
tembak=true;
tembakroketraja = true;
}
if (tembakroketraja) {
roketraja.move(0, 20);
if (roketraja.getY() < 4){
tembakroketraja = false;
}
}
}
public void fungsiTembakdanTambahScore(){
if(rocket.collidesWith(musuh, true)|| rocket.collidesWith(meteor, true)){
musuh.setVisible(false);
meteor.setVisible(false);
tembak=false;
score+=10;
}
}
public void tembak(){
if(tembak){
rocket.move(0, -7);
if(rocket.getY() <3 ){
tembak=false;
}
}else {
rocket.setPosition(pospesX+10, pospesY+30);
}
}
public void tabrakan(){
if(rocket.collidesWith(musuh, true) ){
posmusX = random.nextInt((210 - 0) + 1) + 0;
posmusY=0;
drawmusuh();
tembak=false;
score+=10;
}else if(posmusY>=320){
posmusX = random.nextInt((210 - 0) + 1) + 0;
posmusY=0;
drawmusuh();
// score-=10;
}else if(rocket.collidesWith(meteor, true)){
posmusXX = random.nextInt(((210-2)+1)+0);
posmusYY = 5;
drawMeteor();
tembak=false;
score+=10;
} else if(posmusYY>=320){
posmusXX = random.nextInt(((210-2)+1)+0);
posmusYY = 5;
drawMeteor();
// score-=10;
}else if (pesawat.collidesWith(meteor, tembak)||pesawat.collidesWith(musuh, tembak)){
run=false;
drawYoulose();
}
}
public void menangkalah(){
if(rocket.collidesWith(raja, true)){
run=false;
drawYouwin();
}else if(roketraja.collidesWith(pesawat, true)){
run=false;
drawYoulose();
// }else if (pesawat.collidesWith(meteor, tembak)||pesawat.collidesWith(musuh, tembak)){
// run=false;
// drawYoulose();
}
}
// public void jalankebawah(){
// while(jalanbawah){
// posmusYY++;
// posmusY++;
// }
// }
public void run() {
while (run) {
bgPos++;
if(bgPos > bg.getHeight()){
bgPos=0;
}
g.drawImage(bg, 0, bgPos, 0);
g.drawImage(bg, 0, bgPos - bg.getHeight(), 0);
g.setColor(255,255,255);
g.drawString("Score : " +score, 30, 5, 0);
raja();
control();
drawpesawat();
tembak();
rocket.paint(g);
// rocket.paint(g);
drawMeteor();
//posmusYY++;
drawmusuh();
//posmusY++;
//jalankebawah();
tabrakan();
fungsiTembakdanTambahScore();
flushGraphics();
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
Mohon maaf kalo ada kesalahan, dan mohon koreksi. Maaf kalo kurang maksimal karena sayanya masih dalam kondisi kurang fit. Sudah seminggu -.-.
0 komentar:
Posting Komentar