Kali ini saya akan memberikan contoh tentang sebuah game java sederhana yang pernah saya buat dan ini merupakan game pertama yang saya buat dengan menggunakan bahasa pemograman Java. Mungkin bagi temen - temen yang ingin mengembangkan game saya ini menjadi lebih baik lagi silakan saja.
Berikut screenshootnya
Logika game ini adalah :
- Player dengan karakter kecoa (Kecoa ato kumbang saya jg ga tau) harus memakan ulat yang berwarna hijau sebanyak mungkin untuk mendapatkan POINT sebanyak-banyaknya.
- Player harus menghindari musuh supaya terhindar dari kematian :D
- Game ini terdiri dari 3 class yaitu class Game, musuh, dan Makanan.
Baca Juga :
Berikut Script java nya :
Class Game
package game;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Game extends JFrame implements Runnable {
private int x;
private int y;
private int xArah;
private int yArah;
private ArrayList<musuh> musuhs;
private ArrayList<Makanan> makanann;
private boolean Pemain;
Image gambar;
private Image background;
private Image background2;
Dimension size;
AudioClip soundTrack;
AudioClip soundTrack2;
int skor = 0;
private boolean message
public void run() {
while (true) {
move()
for (int i = 0; i < makanann.size(); i++) {
Rectangle makananHitbox = makanann.get(i).getHitbox();
Rectangle playerHitbox = getPlayerHitbox();
if (makananHitbox.intersects(playerHitbox))
makanann.remove(i);
if(makananHitbox.intersects(playerHitbox)){
skor +=10;
if(makananHitbox.intersects(playerHitbox)){
soundTrack2 = Applet.newAudioClip(getClass().getResource("lick-02.wav"));
soundTrack2.play();
}
}
}
for (int i = 0; i < musuhs.size(); i++) {
Rectangle musuhHitbox = musuhs.get(i).getHitbox();
Rectangle playerHitbox = getPlayerHitbox();
if (musuhHitbox.intersects(playerHitbox))
Pemain = false ;
}
try {
Thread.sleep(5);
} catch (InterruptedException ex) {
System.err.println("Error: Thread Interrupted.");
}
}
}
private void move() {
x += xArah;
y += yArah;
if (x < 0)
x = 0;
if (y < 20)
y = 20;
if (x > 580)
x = 580;
if (y > 440)
y = 440;
}
Game() {
setPreferredSize(size);
setSize(640, 480);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.WHITE);
setVisible(true);
setTitle("Iron Cockroach");
x = 320;
y = 240;
xArah = 0;
yArah = 0;
addKeyListener(new KeyListener());
musuhs = new ArrayList<musuh>();
makanann = new ArrayList<Makanan>();
Pemain = true;
size = new Dimension();
background = new ImageIcon(this.getClass().getResource("papan.jpg")).getImage();
size.width = background.getWidth(null);
size.height = background.getHeight(null);
setPreferredSize(size);
soundTrack = Applet.newAudioClip(getClass().getResource("lick-01.wav"));
soundTrack.loop();
}
public void paint(Graphics g) {
Image dbImg = createImage(getWidth(), getHeight());
Graphics dbg = dbImg.getGraphics();
draw(dbg);
g.drawImage(dbImg, 0, 0, this);
}
public void draw(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(background, 0, 0, null);
if (Pemain) {
Image serangga = new ImageIcon(this.getClass().getResource("bugg.png")).getImage();
g.drawImage(serangga, x, y, this);
}
for (int i = 0; i < musuhs.size(); i++) {
musuh e = musuhs.get(i);
e.draw(g);
}
for (int i = 0; i < makanann.size(); i++) {
Makanan e = makanann.get(i);
e.draw(g);
}
repaint();
g2d.setFont(new Font("Arial",Font.BOLD,18));
g2d.setColor(Color.WHITE);
g2d.drawString("SCORE : " + Integer.toString(skor),20,50);
g2d.setFont(new Font("Arial",Font.PLAIN,10));
g2d.setColor(Color.GRAY);
}
public static Image loadImage(String fileName) {
ImageIcon icon = new ImageIcon(fileName);
return icon.getImage();
}
private void Lawanmusuh() {
musuh e = new musuh();
musuhs.add(e);
Thread t = new Thread(e);
t.start();
}
private void munculMakanan() {
Makanan e = new Makanan();
makanann.add(e);
Thread t = new Thread(e);
t.start();
}
private Rectangle getPlayerHitbox() {
return new Rectangle(x, y, 69, 42);
}
public static void main(String[] args) {
Game gh = new Game();
Thread t = new Thread(gh);
t.start();
while (true) {
if (gh.musuhs.size() < 15) {
gh.Lawanmusuh();
}
if (gh.makanann.size() < 15) {
gh.munculMakanan();
}
try {
Thread.sleep(2500);
} catch (InterruptedException ex) {
System.err.println("Error: Thread Interrupted.");
}
}
}
private class KeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
yArah = -2;
break;
case KeyEvent.VK_DOWN:
yArah = 2;
break;
case KeyEvent.VK_LEFT:
xArah = -2;
break;
case KeyEvent.VK_RIGHT:
xArah = 2;
break;
default:
break;
}
}
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
yArah = 0;
break;
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
xArah = 0;
break;
default:
break;
}
}
}
}
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Game extends JFrame implements Runnable {
private int x;
private int y;
private int xArah;
private int yArah;
private ArrayList<musuh> musuhs;
private ArrayList<Makanan> makanann;
private boolean Pemain;
Image gambar;
private Image background;
private Image background2;
Dimension size;
AudioClip soundTrack;
AudioClip soundTrack2;
int skor = 0;
private boolean message
public void run() {
while (true) {
move()
for (int i = 0; i < makanann.size(); i++) {
Rectangle makananHitbox = makanann.get(i).getHitbox();
Rectangle playerHitbox = getPlayerHitbox();
if (makananHitbox.intersects(playerHitbox))
makanann.remove(i);
if(makananHitbox.intersects(playerHitbox)){
skor +=10;
if(makananHitbox.intersects(playerHitbox)){
soundTrack2 = Applet.newAudioClip(getClass().getResource("lick-02.wav"));
soundTrack2.play();
}
}
}
for (int i = 0; i < musuhs.size(); i++) {
Rectangle musuhHitbox = musuhs.get(i).getHitbox();
Rectangle playerHitbox = getPlayerHitbox();
if (musuhHitbox.intersects(playerHitbox))
Pemain = false ;
}
try {
Thread.sleep(5);
} catch (InterruptedException ex) {
System.err.println("Error: Thread Interrupted.");
}
}
}
private void move() {
x += xArah;
y += yArah;
if (x < 0)
x = 0;
if (y < 20)
y = 20;
if (x > 580)
x = 580;
if (y > 440)
y = 440;
}
Game() {
setPreferredSize(size);
setSize(640, 480);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.WHITE);
setVisible(true);
setTitle("Iron Cockroach");
x = 320;
y = 240;
xArah = 0;
yArah = 0;
addKeyListener(new KeyListener());
musuhs = new ArrayList<musuh>();
makanann = new ArrayList<Makanan>();
Pemain = true;
size = new Dimension();
background = new ImageIcon(this.getClass().getResource("papan.jpg")).getImage();
size.width = background.getWidth(null);
size.height = background.getHeight(null);
setPreferredSize(size);
soundTrack = Applet.newAudioClip(getClass().getResource("lick-01.wav"));
soundTrack.loop();
}
public void paint(Graphics g) {
Image dbImg = createImage(getWidth(), getHeight());
Graphics dbg = dbImg.getGraphics();
draw(dbg);
g.drawImage(dbImg, 0, 0, this);
}
public void draw(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(background, 0, 0, null);
if (Pemain) {
Image serangga = new ImageIcon(this.getClass().getResource("bugg.png")).getImage();
g.drawImage(serangga, x, y, this);
}
for (int i = 0; i < musuhs.size(); i++) {
musuh e = musuhs.get(i);
e.draw(g);
}
for (int i = 0; i < makanann.size(); i++) {
Makanan e = makanann.get(i);
e.draw(g);
}
repaint();
g2d.setFont(new Font("Arial",Font.BOLD,18));
g2d.setColor(Color.WHITE);
g2d.drawString("SCORE : " + Integer.toString(skor),20,50);
g2d.setFont(new Font("Arial",Font.PLAIN,10));
g2d.setColor(Color.GRAY);
}
public static Image loadImage(String fileName) {
ImageIcon icon = new ImageIcon(fileName);
return icon.getImage();
}
private void Lawanmusuh() {
musuh e = new musuh();
musuhs.add(e);
Thread t = new Thread(e);
t.start();
}
private void munculMakanan() {
Makanan e = new Makanan();
makanann.add(e);
Thread t = new Thread(e);
t.start();
}
private Rectangle getPlayerHitbox() {
return new Rectangle(x, y, 69, 42);
}
public static void main(String[] args) {
Game gh = new Game();
Thread t = new Thread(gh);
t.start();
while (true) {
if (gh.musuhs.size() < 15) {
gh.Lawanmusuh();
}
if (gh.makanann.size() < 15) {
gh.munculMakanan();
}
try {
Thread.sleep(2500);
} catch (InterruptedException ex) {
System.err.println("Error: Thread Interrupted.");
}
}
}
private class KeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
yArah = -2;
break;
case KeyEvent.VK_DOWN:
yArah = 2;
break;
case KeyEvent.VK_LEFT:
xArah = -2;
break;
case KeyEvent.VK_RIGHT:
xArah = 2;
break;
default:
break;
}
}
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
yArah = 0;
break;
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
xArah = 0;
break;
default:
break;
}
}
}
}
Class musuh
package game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.Random;
import javax.swing.ImageIcon;
public class musuh implements Runnable {
private int x;
private int y;
private int xArah;
private int yArah;
Image musuh;
musuh() {
x = 320;
y = 50;
Random rng = new Random();
xArah = -1 + rng.nextInt(3);
yArah = -1 + rng.nextInt(3);
if (xArah == 0 && yArah == 0) {
xArah = 1;
yArah = 1;
}
}
public void run() {
while (true) {
move();
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
System.err.println("Error: Thread Interrupted.");
}
}
}
private void move() {
x += xArah;
y += yArah;
if (x < 0)
xArah = 1;
if (x > 630)
xArah = -1;
if (y < 0)
yArah = 1;
if (y > 470)
yArah = -1;
}
public void draw(Graphics g) {
Image musuh = new ImageIcon(this.getClass().getResource("musuh.png")).getImage();
g.drawImage(musuh, x, y, null);
}
Rectangle getHitbox() {
return new Rectangle(x, y, 10, 10);
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.Random;
import javax.swing.ImageIcon;
public class musuh implements Runnable {
private int x;
private int y;
private int xArah;
private int yArah;
Image musuh;
musuh() {
x = 320;
y = 50;
Random rng = new Random();
xArah = -1 + rng.nextInt(3);
yArah = -1 + rng.nextInt(3);
if (xArah == 0 && yArah == 0) {
xArah = 1;
yArah = 1;
}
}
public void run() {
while (true) {
move();
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
System.err.println("Error: Thread Interrupted.");
}
}
}
private void move() {
x += xArah;
y += yArah;
if (x < 0)
xArah = 1;
if (x > 630)
xArah = -1;
if (y < 0)
yArah = 1;
if (y > 470)
yArah = -1;
}
public void draw(Graphics g) {
Image musuh = new ImageIcon(this.getClass().getResource("musuh.png")).getImage();
g.drawImage(musuh, x, y, null);
}
Rectangle getHitbox() {
return new Rectangle(x, y, 10, 10);
}
}
Class makanan
package game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.Random;
import javax.swing.ImageIcon;
public class Makanan implements Runnable {
private int x;
private int y;
private int xArah;
private int yArah;
Image makanan;
Makanan() {
x = 360;
y = 50;
Random rng = new Random();
xArah = -1 + rng.nextInt(3);
yArah = -1 + rng.nextInt(3);
if (xArah == 0 && yArah == 0) {
xArah = 1;
yArah = 1;
}
}
public void run() {
while (true) {
move();
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
System.err.println("Error: Thread Interrupted.");
}
}
}
private void move() {
x += xArah;
y += yArah;
if (x < 0)
xArah = 1;
if (x > 630)
xArah = -1;
if (y < 0)
yArah = 1;
if (y > 470)
yArah = -1;
}
public void draw(Graphics g) {
Image makanan = new ImageIcon(this.getClass().getResource("ulat.png")).getImage();
g.drawImage(makanan, x, y, null);
}
Rectangle getHitbox() {
return new Rectangle(x, y, 10, 10);
}
}
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.Random;
import javax.swing.ImageIcon;
public class Makanan implements Runnable {
private int x;
private int y;
private int xArah;
private int yArah;
Image makanan;
Makanan() {
x = 360;
y = 50;
Random rng = new Random();
xArah = -1 + rng.nextInt(3);
yArah = -1 + rng.nextInt(3);
if (xArah == 0 && yArah == 0) {
xArah = 1;
yArah = 1;
}
}
public void run() {
while (true) {
move();
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
System.err.println("Error: Thread Interrupted.");
}
}
}
private void move() {
x += xArah;
y += yArah;
if (x < 0)
xArah = 1;
if (x > 630)
xArah = -1;
if (y < 0)
yArah = 1;
if (y > 470)
yArah = -1;
}
public void draw(Graphics g) {
Image makanan = new ImageIcon(this.getClass().getResource("ulat.png")).getImage();
g.drawImage(makanan, x, y, null);
}
Rectangle getHitbox() {
return new Rectangle(x, y, 10, 10);
}
}
Note : jangan lupa masukkan gambar background, gambar karakter dan sound (tulisan berwarna biru) ke package game sesuai yang sobat inginkan. Terus jangan lupa dikembangin lagi game nya seperti untuk game over nya.
Terimakasih telah mengunjungi blog saya ini.
Pengunjung yg baik selalu ninggalin jejak :)
Bagi yang ingin scriptnya klik tombol download di bawah ini.
Mas.... itu bisa gk bisa d buka.a.... scriptnya...
ReplyDeleteMaksudnya gimana mas?
Deleteijin copas scriptnya ya gan
ReplyDeletewww.smartkiosku.com
silakan gan ...
DeleteTerimakasih mas sangat bermanfaat
ReplyDeletesama-sama mas semoga bermanfaat
Deletegan,, langkah" nya ada gk
ReplyDeletegan langkah" nya dong
ReplyDeleteitu penerapan async ya mas ?
ReplyDeletemas ..itu pakai aplikasi Netbeand ya untuk buat game nya ??
ReplyDeletemohon di jawab mas
mas itu pakai aplikasi Netbeand ya untuk buat game nya ??
ReplyDeletesaya masih awam mas mohon bantuanya
iya #bantujawan
Deletepake android studio bang
Deletekak aku ud nyoba tapi kok pas kena musuh g bisa game over? kumbangnya hilang tapi gamenya jalan terus. mohon pencerahannya kak..
ReplyDeleteizin copas n download ya, makasih ^_^
ReplyDeletesilakan
DeleteCara measukkan gambarnya gi mana ya ?
ReplyDeletekalo pake eclipse gimana ya hasil nya ?
ReplyDeletesama aja
Deletemas, ini emang gak bisa ya klo player kena musuh terus keluar game overnya gtu ??
ReplyDeleteiya emang gak bisa
DeleteThis comment has been removed by the author.
ReplyDeletetampilannya kok jadi putih ya kak, gak muncul gambarnya. kira2 cara masukin gambar gmn ya kak?
ReplyDeletecoba lebih teliti lagi mas
DeleteAda game kartu remi poker dan black jack mas di satu projek ?
ReplyDeletemaaf gak punya mbak
Deleteada tutorialnya gak? dari awal pembuatan bukan script doang. maksih
ReplyDeletepakai aplikasi apa work nya bg
ReplyDeletebang izin copas ya bang ?
ReplyDeleteizin copas ya bang ?
ReplyDeleteterima kasih mas. sangat membant.
ReplyDeleteini aplikasi java apa yah gan,, soalnya yang ane java eclipse
ReplyDeleteIjin donwload script nya 🙏🏻
ReplyDeletethanks gan sudah share
ReplyDeleteSolder uap
ReplyDeletewah akhirnya ad web yang bisa membantu saya dalam hal membuat wordpress, artikelnya menarik dan memanjakan mata untuk di baca
perkenalkan nama saya Titannia yulanda NIM 1822500065 dari ISB Atma Luhur