0% found this document useful (0 votes)
244 views18 pages

Source Code Game Mobile Kapal Tembak (Stmik Amikbandung)

This document discusses source code for a J2ME game called "Kapal Tempur" (Battleship) created as a final project for a mobile game development course at STMIK AMIK BANDUNG. The game code includes classes for a splash screen, main game screen, and sound playback. The main game screen class handles game logic like player movement, enemy movement, collision detection, and scoring. The goal is to shoot enemies and avoid taking damage.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
244 views18 pages

Source Code Game Mobile Kapal Tembak (Stmik Amikbandung)

This document discusses source code for a J2ME game called "Kapal Tempur" (Battleship) created as a final project for a mobile game development course at STMIK AMIK BANDUNG. The game code includes classes for a splash screen, main game screen, and sound playback. The main game screen class handles game logic like player movement, enemy movement, collision detection, and scoring. The goal is to shoot enemies and avoid taking damage.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

SOURCE CODE GAME KAPAL TEMBAK J2ME NETBEANS

TUGAS AKHIR Diajukan Untuk Memenuhi Ujian Game Mobile di STMIK AMIK BANDUNG Oleh : Zikri Muhammad Ersyad (1002004) Ujang Suryana (1002006 Nuril Pratiwi Sanoi (1002009)

Program Studi Teknik Informatika SEKOLAH TINGGI MANAJEMEN INFORMATIKA DAN KOMPUTER (STMIK) AMIK BANDUNG BANDUNG 2013

Membuat Midlet : Midletgameku.java /* * To change this template, choose Tools | Templates * and open the template in the editor. */

import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.*; import javax.microedition.midlet.*;

/** * @author Nuril Sanoi */ public class Midletgameku extends MIDlet implements CommandListener{

Display display; Command exit = new Command ("Exit",Command.EXIT,0), back = new Command ("Back",Command.OK,0), mulai = new Command("Start",Command.OK,0), menu= new Command("Menu",Command.OK,0), pengaturan=new Command("Level",Command.OK,0), exit1 = new Command ("Exit",Command.EXIT,0), tentang= new Command("About",Command.OK,0);

SplashScreen sc = new SplashScreen(); ClassGame cg;

List list;

public Midletgameku() { display = Display.getDisplay(this); }

public void startApp() {

sc.addCommand(exit); sc.addCommand(mulai); sc.addCommand(tentang); sc.addCommand(pengaturan); sc.setCommandListener(this); display.setCurrent(sc);

public void About(){ TextBox tx= new TextBox("About","Ini adalah Game Pesawat Tempur"+ "Ayo Kalahkan Semua Musuh Musuh kalian",80,TextField.ANY); tx.addCommand(back); tx.setCommandListener(this); display.setCurrent(tx); }

public void pauseApp() { }

public void destroyApp(boolean unconditional) { }

public void commandAction(Command c, Displayable d) {

if (c==exit) { destroyApp(true); notifyDestroyed(); }else if (c==mulai) { display = Display.getDisplay(this); cg = new ClassGame(); cg.start(); cg.addCommand(exit1); display.setCurrent(cg);

}else if (c== tentang){ About(); } else if(c==back){ display.setCurrent(sc); } else if(c==exit1){

try { cg.stop(); }catch (Exception e){ e.printStackTrace();

} } } Membuat Class dengan nama: ClassGame.java

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;

//nuril sanoi

public class ClassGame extends GameCanvas implements Runnable {

boolean loop; int width; int height; int delay = 50; Random rand = new Random();

// Gambar background Image imgBg; // Gambar kapal int xKapal, yKapal; Image imgKapal; Sprite sprtKapal; int indexKapal = 0; // Gambar musuh int xMusuh, yMusuh; Image imgMusuh; Sprite sprtMusuh; // Gambar musuh1 int xMusuh1, yMusuh1; Image imgMusuh1; Sprite sprtMusuh1; // Gambar ledakan int xLedakan, yLedakan; Image imgLedakan; Sprite sprtLedakan; boolean ledakan = false; int indexLedakan = 0; // Gambar Tembakan int xTembakan, yTembakan; Image imgTembakan; Sprite sprtTembakan; boolean tembakan = false; int indexTembakan = 0; Image imgTamat;

int score = 0;

// Constructor dan initialisasi public ClassGame() { super(true); width = getWidth(); height = getHeight(); xKapal = width / 2; yKapal = height / 2; try { // Loading gambar imgKapal = Image.createImage("/00_ship.png"); imgMusuh = Image.createImage("/01_enemy.png"); imgMusuh1 = Image.createImage("/02_enemy.png"); imgLedakan = Image.createImage("/08_explosion.png"); imgTembakan = Image.createImage("/27_yellowshot0.png"); imgBg = Image.createImage("/Background.png"); imgTamat = Image.createImage("/gameover.png"); } catch (Exception ioex) { System.out.println(ioex); } sprtKapal = new Sprite(imgKapal, 29, 14); sprtKapal.setTransform(Sprite.TRANS_MIRROR); sprtMusuh = new Sprite(imgMusuh, 44, 12); sprtMusuh.setTransform(Sprite.TRANS_MIRROR); yMusuh = (int) rand.nextInt(getHeight()); xMusuh = 0 - sprtMusuh.getWidth(); sprtMusuh1 = new Sprite(imgMusuh1, 48, 10);

sprtMusuh1.setTransform(Sprite.TRANS_MIRROR); xMusuh1 = (int) rand.nextInt(getWidth()); yMusuh1 = 0 - sprtMusuh1.getHeight(); sprtLedakan = new Sprite(imgLedakan, 16, 16); sprtTembakan = new Sprite(imgTembakan, 13, 5); sprtTembakan.setTransform(Sprite.TRANS_MIRROR); //xKapal = getWidth()/2; //yKapal = getHeight()/2; }

// membuat otomatis start thread untuk game yang berulang-ulang public void start() { loop = true; Thread t = new Thread(this); t.start(); } // Menyetop thread public void stop() { loop = false; }

// Main Game Loop public void run() { // ambil informasi display Graphics g = getGraphics(); while (loop) { input(); drawScreen(g);

// delay thread try { Thread.sleep(delay); } catch (InterruptedException e) { } } }

// Method untuk menghandle inputan dari keypad private void input() { int key = getKeyStates(); // panah atas if ((key & UP_PRESSED) != 0) { yKapal = Math.max(0, yKapal - 4); } // panah bawah if ((key & DOWN_PRESSED) != 0) { yKapal = Math.min(getHeight() - sprtKapal.getHeight(), yKapal + 4); // panah kiri } if ((key & LEFT_PRESSED) != 0) { xKapal = Math.max(0, xKapal - 4); } // panah kanan if ((key & RIGHT_PRESSED) != 0) { xKapal = Math.min(getWidth() - sprtKapal.getWidth(), xKapal + 4); } // tombol tengah

if ((key &FIRE_PRESSED) != 0) { if ( ! tembakan) { xTembakan = xKapal; yTembakan = yKapal; tembakan = true;

} } }

// Method untuk Display Graphics private void drawScreen(Graphics g) { g.setColor(255, 255, 255); g.fillRect(0, 0, getWidth(), getHeight()); //gambarkan imgLogo ditengah canvas g.drawImage(imgBg, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER); // Gambar kapal sprtKapal.setFrame(indexKapal); sprtKapal.setPosition(xKapal, yKapal); sprtKapal.paint(g); indexKapal++; if (indexKapal > 2) { indexKapal = 0; } // Gambar musuh sprtMusuh.setPosition(xMusuh, yMusuh); sprtMusuh.paint(g);

xMusuh += 5; if (xMusuh > getWidth()) { xMusuh = 0 - sprtMusuh.getWidth(); yMusuh = (int) rand.nextInt(getHeight()); } // Gambar musuh 1 sprtMusuh1.setPosition(xMusuh1, yMusuh1); sprtMusuh1.paint(g); xMusuh1 += 5; if (xMusuh1 > getWidth()) { xMusuh1 = 0 - sprtMusuh1.getWidth(); yMusuh1 = (int) rand.nextInt(getHeight()); } // Gambar ledakan bila player bertubrukan dengan musuh if (sprtKapal.collidesWith(sprtMusuh, true)) { // loop=false; ledakan = true; xLedakan = xKapal; yLedakan = yKapal; /* try { imgTamat = Image.createImage("/gameover.png"); g.setColor(255, 255, 255); g.fillRect(0, 0, getWidth(),getHeight()); g.drawImage(imgTamat, getWidth() /2,getHeight()/2, Graphics.HCENTER | Graphics.VCENTER); g.setColor(0, 0, 0); stop(); ledakan = false; } catch (Exception e) {

}*/ } if (sprtKapal.collidesWith(sprtMusuh1, true)) { // loop = false; ledakan = true; xLedakan = xKapal; yLedakan = yKapal; /*try { imgTamat = Image.createImage("/gameover.png"); g.setColor(255, 255, 255); g.fillRect(0, 0, getWidth(),getHeight()); g.drawImage(imgTamat, getWidth() /2,getHeight()/2, Graphics.HCENTER | Graphics.VCENTER); g.setColor(0, 0, 0); stop(); ledakan = false; } catch (Exception e) { }*/ } // Gambar ledakan if (ledakan) { sprtLedakan.setFrame(indexLedakan); sprtLedakan.setPosition(xLedakan, yLedakan); sprtLedakan.paint(g); indexLedakan++; if (indexLedakan > 2) { indexLedakan = 0; } ledakan = false;

} // Gambar tembakan jika true if (tembakan) { sprtLedakan.setFrame(indexTembakan); sprtTembakan.setPosition(xTembakan, yTembakan); sprtTembakan.paint(g); indexTembakan++; if (indexTembakan > 2) { indexTembakan = 0; } xTembakan -= 5; if (xTembakan > width) { tembakan = false; } } // Peluru bertemu musuh beserta ledakan dan nilainya if (sprtTembakan.collidesWith(sprtMusuh, true)) { ledakan = true; xLedakan = xMusuh; yLedakan = yMusuh; xMusuh = 0 - sprtMusuh.getWidth(); yMusuh = (int) (rand.nextInt() % getWidth()); score = score + 10; } if (sprtTembakan.collidesWith(sprtMusuh1, true)) { ledakan = true; xLedakan = xMusuh1; yLedakan = yMusuh1;

xMusuh1 = 0 - sprtMusuh1.getWidth(); yMusuh1 = (int) (rand.nextInt() % getWidth()); score = score + 10; } // Gambar score g.setColor(255, 255, 255); g.drawString("Score : " + score, 187, 2, Graphics.TOP | Graphics.LEFT); flushGraphics();

} } Membuat Midlet dengan nama : MidletPlayWav.java /* * To change this template, choose Tools | Templates * and open the template in the editor. */

import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.*; import javax.microedition.lcdui.Displayable; import javax.microedition.media.Manager; import javax.microedition.media.Player; import javax.microedition.media.PlayerListener; import javax.microedition.media.control.VolumeControl; import javax.microedition.midlet.*;

/** * @author PERSONAL KOMPUTER */ public class MidletPlayWav extends MIDlet implements PlayerListener, CommandListener {

private Display display; private static Player player; private VolumeControl vc; private List list; private Command cmExit, cmStop;

public MidletPlayWav() { display = Display.getDisplay(this); }

public void startApp() { try { playMedia("wav.wav", "wav"); } catch (Exception e) { e.printStackTrace(); } }

public void pauseApp() { }

public void destroyApp(boolean unconditional) { }

public void playerUpdate(Player player, String event, Object eventData) { }

public void commandAction(Command c, Displayable d) { }

public void exitMIDlet() { destroyApp(false); notifyDestroyed(); }

private void playMedia(String file, String p) throws Exception { player = Manager.createPlayer(getClass().getResourceAsStream(file), "audio/x-wav"); player.addPlayerListener(this); player.setLoopCount(-1); player.prefetch(); player.realize(); vc = (VolumeControl) player.getControl("VolumeControl"); if (vc != null) { vc.setLevel(100); } player.start(); }

Membuat Class dengan nama : SplashScreen.java import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.*;

public class SplashScreen extends Canvas{ private Image imgLogo; protected void paint(Graphics g) { try { imgLogo = Image.createImage("/LOGO STMIK AMIKBANDUNG.png"); }catch (Exception a) { System.out.println("File image tidak ditemukan!"); } //hapus canvas g.setColor(255,255,255); g.fillRect(0,0,getWidth(),getHeight()); //gambarkan imgLogo ditengah canvas g.drawImage(imgLogo,getWidth()/2,getHeight()/2, Graphics.HCENTER|Graphics.VCENTER);

} }

You might also like