Cara membuat Game “Ular
Terbang” menggunakan Netbeans
Untuk memenuhi tugas softskill pengantar teknologi game.
Kami dari salah satu kelompok 3IA06
Bagas Anugrah Waluyo -51416293
Raditya Pratama - 55416930
Muhammad Kahfi -54416971
Sebelum masuk ke kodingan siapkan dulu beberapa file
dukungan seperti gambarnya dll. Bisa download di link berikut Download
Langkah-Langkah Cara Membuat Game Ular Terbang di Java
1. Buka Aplikasi Netbeand IDE di Launchpad lalu klik
Netbeans, atau bisa juga di finder, lalu pada folder Application > Netbeans
(ini untuk Sistem operasi Macintosh yang saya gunakan).
2. Setelah Netbeans terbuka, membuat project baru dengan
cara klik menu File > New Project atau bisa dengan tombol Shift+Command+N
pada keyboard.
3. Pada Kotak dialog choise Project Anda pilih kategory Java
dengan Tipe project Java Application. Setelah itu klik tombol Next, seperti
gambar di bawah.
4. Pada kotak dialog Name and Location, centang pada Create
main Class, dan set as main project. Lalu memberikan Nama dengan game, setelah
itu mengklik tombol finish.
5. Setelah itu pada project game yang ada di sidebar
netbeans, klik game > sources Package > game > game.java.
6. Kemudian mengetikkan Source program di dalam editor
netbeans, Source programnya adalah :
Game.java
/*
* To change this template, choose Tools |
Templates
* and open the template in the editor.
*/
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;
/**
*
* @author user
*/
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("hutan.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("ular.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;
}
}
}
}
Selanjutnya buat java class makanan.java
/*
* To change this template, choose Tools |
Templates
* and open the template in the editor.
*/
package game;
import
java.awt.Graphics;
import
java.awt.Image;
import
java.awt.Rectangle;
import
java.util.Random;
import
javax.swing.ImageIcon;
/**
*
* @author user
*/
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("tikus.png")).getImage();
g.drawImage(makanan, x, y, null);
}
Rectangle getHitbox() {
return new Rectangle(x, y, 10, 10);
}
}
Kemudian Selanjunyabuat class musuh.java
/*
* To change this template, choose Tools |
Templates
* and open the template in the editor.
*/
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;
/**
*
* @author user
*/
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);
}
}
Untuk hasilnya akan seperti ini ketika di play
Untuk lebih jelasnya silahkan download file game netbeansnya Download
EmoticonEmoticon