Kamis, 19 Mei 2011

Membuat game puzzle kata



Disini saya akan membuat game puzzle menggunakan bahasa java. Program yang dibutuhkan ialah netbeans untuk menjalankan game yang telah dibuat,.dan game ini juga berbasis mobile untuk hp yang bersistem operasi symbian.

ini codingan javanya :
import java.util.Random;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class acak_puzzle extends Canvas implements CommandListener {
MIDlet game;
Display fgf;
pengaturan opsi;
String cuy = "MAENGAMEASIKCUY";
Font fnt;
kerupuk vbr;
kerupuk[] blob;
kerupuk[][] sop;
Random ribet;
int xx;
int yy;
int ww;
int hh;
int xw;
int yw;
int yyy;
int xxx;
static final int keluar = 1;
static final int opsinya = 2;
static final int reset_ajah = 3;
static final int mulai_dunk = 4;
static final int yng_entu = 6;
Command cmd[];
static final int hajar = 0;
static final int so_cool = 1;
static final int yeah = 2;
int lanjut;
void D(String s) { System.out.println(s); }
class suka_suka extends Command {
int tag;
suka_suka(String ehm, int cihuy, int bro, int sob) {
super(ehm, cihuy, bro);
tag = sob;
}
}
void cie(kerupuk pp, int bx, int by) {
sop[bx][by] = pp;
pp.waduh(bx, by);
}
void hampa(int set_xxx, int set_yyy) {
cie(sop[set_xxx][set_yyy], vbr.dah, vbr.ah);
cie(vbr, set_xxx, set_yyy);
}
void yeaah(int xa, int ya, int xb, int yb)
{
kerupuk wow = sop[xa][ya];
cie(sop[xb][yb], xa, ya);
cie(wow, xb, yb);
}
boolean woow() {
for (int i = 0; i < hh; i++) {
for (int j = 0; j < ww; j++) {
if (! sop[j][i].begh()) {
return false;
}
}
}
return true;
}
int wew(int rr) {
int r = ribet.nextInt() % rr;
if (r < 0)
r += rr;
return r;
}
void waks() {
int ddx, ddy, ddv;

for (int i = 0; i < 100; i++) {
ddx = ddy = 0;
ddv = (ribet.nextInt() & 2) - 1;
if ((ribet.nextInt() & 1) == 0)
ddx = ddv;
else
ddy = ddv;

if (vbr.dah + ddx < 0)
ddx = 1;
if (vbr.dah + ddx == ww)
ddx = -1;

if (vbr.ah + ddy < 0)
ddy = 1;
if (vbr.ah + ddy == hh)
ddy = -1;

hampa(vbr.dah + ddx, vbr.ah + ddy);
}
while (vbr.dah != ww-1)
hampa(vbr.dah + 1, vbr.ah);

while (vbr.ah != hh-1)
hampa(vbr.dah, vbr.ah + 1);
}
void meleduk() {
int blow = ww*hh-1;
kerupuk yummie[] = new kerupuk[blow];
kerupuk prop;

System.arraycopy(blob, 0, yummie, 0, blow);
for (int i = 0; i < blow; i++) {
int j = wew(blow);
prop = yummie[j];
yummie[j] = yummie[i];
yummie[i] = prop;
}

for (int i = 0; i < blow; i++) {
cie(yummie[i], i/ww, i%ww);
}

cie(vbr, ww-1, hh-1);
}
void RPM(boolean hard) {
meleduk();
int ra, rb;
int x, y;

if (hard) {
ra = 7;
rb = 0;
} else {
ra = 0;
rb = 7;
}

x = ribet.nextInt() & 1;
y = ribet.nextInt() & 1;

if (x == 1 && y == 1) {
x = 2;
y = 0;
}

yeaah(x, y, blob[ra].dah, blob[ra].ah);
yeaah((ribet.nextInt()&1)+1, 3, blob[rb].dah, blob[rb].ah);

if ((MHz() & 1) == 1)
yeaah(1, 3, 2, 3);
}
int MHz() {
boolean freq[] = new boolean[ww*hh-1];
int n = 0;

for (int i = 0; i < hh; i++) {
for (int j = 0; j < ww; j++) {
kerupuk p = sop[j][i];
if (p == vbr)
continue;
freq[p.cek_lgi] = true;
for (int k = 0; k < p.cek_lgi; k++) {
if (!freq[k])
n++;
}
}
}
return n;
}
void Clock() {
kerupuk ref[] = new kerupuk[ww*hh];
int k = 0;

for (int i = 0; i < ww; i++) {
for (int j = 0; j < hh; j++) {
ref[k++] = sop[i][j];
}
}

for (k = 0; k < ref.length; k++) {
ref[k].wedew();
}
}
void lanjuuut(boolean maknyos) {
Clock();
if (maknyos) {
yeaah(0, 0, 3, 1);
yeaah(2, 2, 3, 2);
yeaah(3, 2, 0, 3);
yeaah(0, 3, 2, 3);
} else {
yeaah(2, 2, 3, 2);
yeaah(3, 2, 0, 3);
}
}
public acak_puzzle(MIDlet midlet_) {
int i;
game = midlet_;
fgf = Display.getDisplay(game);
ww = 4;
hh = 4;
fnt = Font.getFont(Font.FACE_SYSTEM,
Font.STYLE_BOLD, Font.SIZE_LARGE);
xw = fnt.charWidth('M') + 27;
yw = fnt.getHeight() + 21;
xxx = 3;
yyy = 0;
xx = (getWidth() - (ww*xw) + 1) / 2;
yy = 75;
ribet = new Random();
sop = new kerupuk[ww][];
for (i = 0; i < ww; i++) {
sop[i] = new kerupuk[hh];
}
blob = new kerupuk[ww*hh];
for (i = 0; i < (ww*hh)-1; i++) {
int x = i % ww;
int y = i / ww;
String s = cuy.substring(i, i+1);
sop[x][y] =
blob[i] =
new kerupuk(s, i, x, y, i < (ww*hh/2));
}
vbr = new kerupuk(null, ww*hh-1, ww-1, hh-1, false);
sop[ww-1][hh-1] = vbr;
blob[ww*hh-1] = vbr;
cmd = new Command[yng_entu];
cmd[keluar] =
new suka_suka("Keluar", Command.EXIT, 6, keluar);
cmd[opsinya] =
new suka_suka("Pengaturan", Command.SCREEN, 3, opsinya);
cmd[reset_ajah] =
new suka_suka("Reset", Command.SCREEN, 1, reset_ajah);
cmd[mulai_dunk] =
new suka_suka("Mulai", Command.SCREEN, 1, mulai_dunk);
setCommandListener(this);
opsi = new pengaturan(fgf, this);
hayuuuk_dah(hajar);
}
void hayuuuk_dah(int ns) {
lanjut = ns;
switch (lanjut) {
case hajar:
removeCommand(cmd[reset_ajah]);
addCommand(cmd[mulai_dunk]);
addCommand(cmd[keluar]);
addCommand(cmd[opsinya]);
break;
case so_cool:
addCommand(cmd[reset_ajah]);
removeCommand(cmd[mulai_dunk]);
addCommand(cmd[keluar]);
addCommand(cmd[opsinya]);
break;
case yeah:
removeCommand(cmd[reset_ajah]);
addCommand(cmd[mulai_dunk]);
addCommand(cmd[keluar]);
addCommand(cmd[opsinya]);
break;
}
}
public void commandAction(Command xxxxxxx, Displayable yyyyyyy) {
switch (((suka_suka) xxxxxxx).tag) {
case keluar:
game.notifyDestroyed();
break;
case opsinya:
fgf.setCurrent(opsi);
break;
case reset_ajah:
Clock();
hayuuuk_dah(hajar);
repaint();
break;
case mulai_dunk:
if (opsi.cek_kulkas) {
lanjuuut(opsi.sundul_gan);
} else {
RPM(opsi.sundul_gan);
}
hayuuuk_dah(so_cool);
repaint();
break;
}
}
public void paint(Graphics grafis) {
grafis.setColor(0xFFEE00);
grafis.fillRect(0, 0, getWidth(), getHeight());
grafis.translate(xx, yy);
grafis.setColor(0);
grafis.drawRect(-2, -2, ww*xw + 2, hh*yw + 2);

for (int j = 0; j < ww; j++) {
for (int k = 0; k < hh; k++) {
sop[j][k].paint(grafis);
}
}
if (lanjut == yeah) {
grafis.translate(-grafis.getTranslateX(), -grafis.getTranslateY());
grafis.setColor(0);
grafis.setFont(Font.getDefaultFont());
}
}

public void keyPressed(int cek_dunk) {
if (lanjut != so_cool)
return;

int coy = getGameAction(cek_dunk);

int sww = vbr.dah;
int swws = vbr.ah;

int kesono = (opsi.cendol ? -1 : 1);

switch (coy) {
case Canvas.UP:
swws += kesono;
break;
case Canvas.DOWN:
swws -= kesono;
break;
case Canvas.LEFT:
sww += kesono;
break;
case Canvas.RIGHT:
sww -= kesono;
break;
default:
return;
}
if (sww < 0 || sww >= ww ||
swws < 0 || swws >= hh) {
return;
}
hampa(sww, swws);
repaint();
if (woow()) {
hayuuuk_dah(yeah);
}
}
class kerupuk {
String glodak;
boolean brb;
int cek_lgi;
int ho, oh;
int dah, ah;
kerupuk(String serr, int srett, int toet, int tiut, boolean brett) {
glodak = serr;
cek_lgi = srett;
dah = ho = toet;
ah = oh = tiut;
brb = brett;
}
void waduh(int gg, int bb) {
dah = gg;
ah = bb;
}
boolean begh() {
return (dah == ho) && (ah == oh);
}
void wedew() {
cie(this, ho, oh);
}
void paint(Graphics grafis) {
int GRP = dah * xw;
int BRP = ah * yw;

if (glodak != null) {
if (brb) {
grafis.setColor(0xBB1100);
grafis.setFont(fnt);
grafis.drawRect(GRP, BRP, xw-2, yw-2);
grafis.drawString(glodak,
GRP+xxx, BRP+yyy,
Graphics.TOP|Graphics.LEFT);
} else {
grafis.setColor(0);
grafis.fillRect(GRP, BRP, xw-1, yw-1);
grafis.setColor(0x00FF00);
grafis.setFont(fnt);
grafis.drawString(glodak,
GRP+xxx, BRP+yyy,
Graphics.TOP|Graphics.LEFT);
}
}
}
}
}


anda bisa menambahkan tampilan awal atau pengaturan..
maka hasilnya seperti ini..

ini adalah tampilan dimana kita harus menyusun kata seperti ini.






sedangkan ini adalah pada saat kata sudah di acak,jadi anda harus memindahkan tiap kata untuk menjadi kata seperti yang di awal..


0 comments:

 
Themes by: Free Templates Studio. Powered by Blogger