Home > Pemprograman > Membuat Game Snake Cube Sederhana pada Platform J2ME

Membuat Game Snake Cube Sederhana pada Platform J2ME

Pada postingan kali ini saya akan membahasa cara membuat game snake cube sederhana. Mengapa dinamakan cube? dikarenakan bentuk snake nya cube😀

berikut cuplikannya :

Jika snake mengenai dinding luar (kanan,kiri,atas,bawah) maka permainan akan berhenti

Score akan terus update

Berikut source codenya :

Midlet.Java


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

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

/**
* @author octav
*/
public class Midlet extends MIDlet {
private Display display;
public Midlet()
{
display = Display.getDisplay(this);
}
public void startApp() {
Kanvas kv = new Kanvas(this);
display.setCurrent(kv);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
}

Kanvas.Java

import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

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

/**
*
* @author octav
*/
public class Kanvas extends Canvas{
private int x = getWidth()/2;
private int y = getHeight()/2;
private boolean kondisi = false;
private boolean arah = false;
private Midlet midlet;
private int score = 0;
Timer t;
public Kanvas(Midlet midlet)
{
this.midlet = midlet;
setFullScreenMode(true);
t = new Timer();
t.schedule(new TimerTask() {

public void run() {
score ++;
if((kondisi==false)&&(arah==false))
{
y -= 5 ;
}
if((kondisi==false)&&(arah==true))
{
y += 5 ;
}
if((kondisi==true)&&(arah==false))
{
x -= 5 ;
}
if((kondisi==true)&&(arah==true))
{
x += 5 ;
}
}
}, 50, 50);
}
protected void paint(Graphics g) {
//drawing Background
g.setColor(0x000000);
g.fillRect(0, 0, getWidth(), getHeight());
//drawing cube snake
g.setColor(0xffffff);
g.fillRoundRect(x, y, 20, 20, 5, 5);
//drawing garis luar
g.setColor(82,133,190);
g.fillRect(0, 0, getWidth(), 5);
g.fillRect(0, getHeight()-5, getWidth(), 5);
g.fillRect(0, 0, 5, getHeight());
g.fillRect(getWidth()-5, 0, 5, getHeight());
if((x>=getWidth()-10)||(x<5)||(y>=getHeight()-5)||(y<5))
midlet.destroyApp(true);
g.setColor(142,255,17);
g.drawString("Score : "+score, 8, 7, Graphics.TOP|Graphics.LEFT);
repaint();
}

protected void keyPressed(int keyCode) {
System.out.println(keyCode);
switch(keyCode)
{
case -1:
kondisi = false;
arah = false;
break;
case -2:
kondisi = false;
arah = true;
break;

case -3:
kondisi = true;
arah = false;
break;
case -4:
kondisi = true;
arah = true;
break;
}
repaint();
}
}

Categories: Pemprograman
  1. January 15, 2013 at 5:25 am

    Gan, ane boleh minta source code game gatot koco yang di pdf ini http://ilmukomputer.org/wp-content/uploads/2012/08/octav-GameDevelopment.pdf .??
    please gan,, kirim email ya,, thanks,,

  2. January 19, 2013 at 10:45 am

    Bang, ane udah komen, minta source code game gatot koco. .
    kok malah dihapus.??
    please bang ane butuh banget source code nya. .

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s