Final Project PBO C
Anggota Kelompok :
- Muhammad Bagus Istighfar (05111940000049)
- Dewangga Dharmawan - 05111940000029
Nama Proyek : TANK 2D
Penjelasan Proyek
TANK 2D ini adalah sebuah permainan sederhana dimana ada 2 tank saling beradu tembak untuk mengalahkan tank yang lain. Rintangan yang mereka hadapi berupa tembok-tembok di sekelilingnya. Tembok-tembok tersebut ada yang hancur setelah ditembak dan ada yang tidak.
Class-class proyek dan penjelasannya
Penjelasannya juga akan dijelaskan pada video Youtube dibawah
Diagram Class
Class Main
import java.awt.Color;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
Gameplay gamePlay = new Gameplay();
//format layar
frame.setBounds(10, 10, 800, 630);
frame.setTitle("Tank 2D");
frame.setBackground(Color.gray);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //mengatur fungsi tombol exit pada layar
frame.add(gamePlay); // menambah Gameplay pada isi layar
frame.setVisible(true); // menampilkan isi layar
}
}
Class Gameplay
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.Timer;
public class Gameplay extends JPanel implements ActionListener
{
private Dinding din;
private ImageIcon player1;
private int player1X = 200;
private int player1Y = 550;
private boolean player1kanan = false;
private boolean player1kiri = false;
private boolean player1bawah = false;
private boolean player1atas = true; // pertama kali menghadap ke atas
private int player1skor = 0;
private int player1nyawa = 5;
private boolean player1Tembak = false;
private String arahPeluru1 = "";
private ImageIcon player2;
private int player2X = 400;
private int player2Y = 550;
private boolean player2kanan = false;
private boolean player2kiri = false;
private boolean player2bawah = false;
private boolean player2atas = true;
private int player2skor = 0;
private int player2nyawa = 5;
private boolean player2Tembak = false;
private String arahPeluru2 = "";
private Timer timer;
private int delay=8;
private Player1Listener player1Listener;
private Player2Listener player2Listener;
private PeluruPlayer1 peluru1 = null;
private PeluruPlayer2 peluru2 = null;
private boolean play = true;
public Gameplay()
{
din = new Dinding();
player1Listener = new Player1Listener();
player2Listener = new Player2Listener();
setFocusable(true);
//menambahkan listener
addKeyListener(player1Listener);
addKeyListener(player2Listener);
setFocusTraversalKeysEnabled(false);
timer=new Timer(delay,this);
timer.start();
}
public void paint(Graphics g)
{
//background utama
g.setColor(Color.black);
g.fillRect(0, 0, 650, 600);
//sisi kanan background
g.setColor(Color.DARK_GRAY);
g.fillRect(660, 0, 140, 600);
//gambar dinding kuat
din.gambarKuat(this, g);
//gambar dinding lemah
din.gambarLemah(this, g);
if(play) // jika game dimulai
{
//gambar player 1
if(player1atas)
player1 = new ImageIcon("player1_tank_atas.png");
else if(player1bawah)
player1 = new ImageIcon("player1_tank_bawah.png");
else if(player1kanan)
player1 = new ImageIcon("player1_tank_kanan.png");
else if(player1kiri)
player1 = new ImageIcon("player1_tank_kiri.png");
player1.paintIcon(this, g, player1X, player1Y);
//gambar player 2
if(player2atas)
player2 = new ImageIcon("player2_tank_atas.png");
else if(player2bawah)
player2 = new ImageIcon("player2_tank_bawah.png");
else if(player2kanan)
player2 = new ImageIcon("player2_tank_kanan.png");
else if(player2kiri)
player2 = new ImageIcon("player2_tank_kiri.png");
player2.paintIcon(this, g, player2X, player2Y);
//jika peluru muncul dan player1 telah menembak
if(peluru1 != null && player1Tembak)
{
if(arahPeluru1.equals(""))
{
if(player1atas)
{
arahPeluru1 = "atas";
}
else if(player1bawah)
{
arahPeluru1 = "bawah";
}
else if(player1kanan)
{
arahPeluru1 = "kanan";
}
else if(player1kiri)
{
arahPeluru1 = "kiri";
}
}
else
{
peluru1.move(arahPeluru1);
peluru1.gambar(g);
}
//cek peluru menyentuh tank
if(new Rectangle(peluru1.getX(), peluru1.getY(), 10, 10)
.intersects(new Rectangle(player2X, player2Y, 50, 50)))
{
player1skor += 10;
player2nyawa -= 1;
peluru1 = null;
player1Tembak = false;
arahPeluru1 = "";
}
//cek peluru menyentuh dinding lemah atau kuat
if(din.checkCollisionLemah(peluru1.getX(), peluru1.getY())
|| din.checkCollisionKuat(peluru1.getX(), peluru1.getY()))
{
peluru1 = null; // peluru 1 hilang
player1Tembak = false; // reset status player1 tembak
arahPeluru1 = ""; // reset status arah peluru1
}
//jika peluru keluar dari frame, peluru akan hilang
if(peluru1.getY() < 1
|| peluru1.getY() > 580
|| peluru1.getX() < 1
|| peluru1.getX() > 630)
{
peluru1 = null;
player1Tembak = false;
arahPeluru1 = "";
}
}
//jika peluru muncul dan player2 telah menembak
if(peluru2 != null && player2Tembak)
{
if(arahPeluru2.equals(""))
{
if(player2atas)
{
arahPeluru2 = "atas";
}
else if(player2bawah)
{
arahPeluru2 = "bawah";
}
else if(player2kanan)
{
arahPeluru2 = "kanan";
}
else if(player2kiri)
{
arahPeluru2 = "kiri";
}
}
else
{
peluru2.move(arahPeluru2);
peluru2.gambar(g);
}
//cek peluru menyentuh tank
if(new Rectangle(peluru2.getX(), peluru2.getY(), 10, 10)
.intersects(new Rectangle(player1X, player1Y, 50, 50)))
{
player2skor += 10;
player1nyawa -= 1;
peluru2 = null;
player2Tembak = false;
arahPeluru2 = "";
}
//cek peluru menyentuh dinding lemah atau kuat
if(din.checkCollisionLemah(peluru2.getX(), peluru2.getY())
|| din.checkCollisionKuat(peluru2.getX(), peluru2.getY()))
{
peluru2 = null;
player2Tembak = false;
arahPeluru2 = "";
}
//jika peluru keluar dari frame, peluru akan hilang
if(peluru2.getY() < 1
|| peluru2.getY() > 580
|| peluru2.getX() < 1
|| peluru2.getX() > 630)
{
peluru2 = null;
player2Tembak = false;
arahPeluru2 = "";
}
}
}
//tampilan skor
g.setColor(Color.white);
g.setFont(new Font("serif",Font.BOLD, 15));
g.drawString("Skor", 700,30);
g.drawString("Player 1: "+player1skor, 670,60);
g.drawString("Player 2: "+player2skor, 670,90);
g.drawString("Nyawa", 700,150);
g.drawString("Player 1: "+player1nyawa, 670,180);
g.drawString("Player 2: "+player2nyawa, 670,210);
//jika nyawa pemain 1 habis
if(player1nyawa== 0)
{
g.setColor(Color.white);
g.setFont(new Font("serif",Font.BOLD, 60));
g.drawString("Game Over", 200,300);
g.drawString("Player 2 Menang", 130,380);
play = false; // permainan berhenti
g.setColor(Color.white);
g.setFont(new Font("serif",Font.BOLD, 30));
g.drawString("(Tekan Spasi untuk Bermain Lagi)", 130,430);
}
//jika nyawa pemain 2 habis
else if(player2nyawa == 0)
{
g.setColor(Color.white);
g.setFont(new Font("serif",Font.BOLD, 60));
g.drawString("Game Over", 200,300);
g.drawString("Player 1 Menang", 130,380);
g.setColor(Color.white);
g.setFont(new Font("serif",Font.BOLD, 30));
g.drawString("(Tekan Spasi untuk Bermain Lagi)", 130,430);
play = false; // permainan berhenti
}
//menghapus komponen pemain dalam game
g.dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
repaint();
}
//input keyboard dan apa yang akan dilakukannya
private class Player1Listener implements KeyListener
{
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()== KeyEvent.VK_SPACE && (player1nyawa == 0 || player2nyawa == 0))
{
din = new Dinding();
player1X = 200;
player1Y = 550;
player1kanan = false;
player1kiri = false;
player1bawah = false;
player1atas = true;
player2X = 400;
player2Y = 550;
player2kanan = false;
player2kiri = false;
player2bawah = false;
player2atas = true;
player1skor = 0;
player1nyawa = 5;
player2skor = 0;
player2nyawa = 5;
play = true;
repaint();
}
//Player 1 menembak
if(e.getKeyCode()== KeyEvent.VK_F)
{
if(!player1Tembak)
{
if(player1atas)
{
peluru1 = new PeluruPlayer1(player1X + 20, player1Y);
}
else if(player1bawah)
{
peluru1 = new PeluruPlayer1(player1X + 20, player1Y + 40);
}
else if(player1kanan)
{
peluru1 = new PeluruPlayer1(player1X + 40, player1Y + 20);
}
else if(player1kiri)
{
peluru1 = new PeluruPlayer1(player1X, player1Y + 20);
}
player1Tembak = true;
}
}
//player 1 atas
if(e.getKeyCode()== KeyEvent.VK_W)
{
//cek collision player 1 dengan dinding
player1atas = true;
player1kanan = false;
player1kiri = false;
player1bawah = false;
if(!(player1Y < 10))
player1Y-=10;
}
//player1 kiri
if(e.getKeyCode()== KeyEvent.VK_A)
{
player1kanan = false;
player1kiri = true;
player1bawah = false;
player1atas = false;
if(!(player1X < 10))
player1X-=10;
}
//player1 bawah
if(e.getKeyCode()== KeyEvent.VK_S)
{
player1kanan = false;
player1kiri = false;
player1bawah = true;
player1atas = false;
if(!(player1Y > 540))
player1Y+=10;
}
//player1 kanan
if(e.getKeyCode()== KeyEvent.VK_D)
{
player1kanan = true;
player1kiri = false;
player1bawah = false;
player1atas = false;
if(!(player1X > 590))
player1X+=10;
}
}
}
private class Player2Listener implements KeyListener
{
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
//Player 2 menembak
if(e.getKeyCode()== KeyEvent.VK_M)
{
if(!player2Tembak)
{
if(player2atas)
{
peluru2 = new PeluruPlayer2(player2X + 20, player2Y);
}
else if(player2bawah)
{
peluru2 = new PeluruPlayer2(player2X + 20, player2Y + 40);
}
else if(player2kanan)
{
peluru2 = new PeluruPlayer2(player2X + 40, player2Y + 20);
}
else if(player2kiri)
{
peluru2 = new PeluruPlayer2(player2X, player2Y + 20);
}
player2Tembak = true;
}
}
//player2 atas
if(e.getKeyCode()== KeyEvent.VK_UP)
{
player2kanan = false;
player2kiri = false;
player2bawah = false;
player2atas = true;
if(!(player2Y < 10))
player2Y-=10;
}
//player2 kiri
if(e.getKeyCode()== KeyEvent.VK_LEFT)
{
player2kanan = false;
player2kiri = true;
player2bawah = false;
player2atas = false;
if(!(player2X < 10))
player2X-=10;
}
//player2 bawah
if(e.getKeyCode()== KeyEvent.VK_DOWN)
{
player2kanan = false;
player2kiri = false;
player2bawah = true;
player2atas = false;
if(!(player2Y > 540))
player2Y+=10;
}
//player2 kanan
if(e.getKeyCode()== KeyEvent.VK_RIGHT)
{
player2kanan = true;
player2kiri = false;
player2bawah = false;
player2atas = false;
if(!(player2X > 590))
player2X+=10;
}
}
}
}
Class Dinding
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class Dinding {
// array indeks posisi dinding Lemah ==> ada 42 dinding
int dindingLemahDI[] = new int[42];
// array posisi X dinding Lemah
int dindingLemahXPos[] = {50,350,450,550,50,300,350,450,550,150,150,450,550,
250,50,100,150,550,250,350,450,550,50,250,350,550,
50,150,250,300,350,550,50,150,250,350,450,550,50,
250,350,550};
// array posisi Y dinding Kuat
int dindingLemahYPos[] = {50,50,50,50,100,100,100,100,100,150,200,200,200,250,
300,300,300,300,350,350,350,350,400,400,400,400,450,
450,450,450,450,450,500,500,500,500,500,500,550,550,
550,550};
// array posisi X dinding kuat
int dindingKuatXPos[] = {150,350,150,500,450,300,600,400,350,200,0,200,500};
// array posisi Y dinding kuat
int dindingKuatYPos[] = {0,0,50,100,150,200,200,250,300,350,400,400,450};
private ImageIcon dindingKuatImage;
private ImageIcon dindingLemahImage;
// menandai ada dinding lemah di posisi
public Dinding()
{
dindingKuatImage=new ImageIcon("dinding_kuat.jpg");
dindingLemahImage=new ImageIcon("dinding_lemah.jpg");
for(int i=0; i< dindingLemahDI.length; i++)
{
dindingLemahDI[i] = 1; // 1 tanda ada dinding Lemah
}
}
// buat gambar dinding lemah
public void gambarLemah (Component c, Graphics g) {
for (int i=0; i<dindingLemahDI.length; i++) {
if (dindingLemahDI[i] == 1) {
dindingLemahImage.paintIcon(c, g, dindingLemahXPos[i], dindingLemahYPos[i]);
}
}
}
// buat gambar dinding kuat
public void gambarKuat (Component c, Graphics g) {
for (int i=0; i<dindingKuatXPos.length; i++) {
dindingKuatImage.paintIcon(c, g, dindingKuatXPos[i], dindingKuatYPos[i]);
}
}
// cek Collision (menyentuh/tidak)
public boolean checkCollisionLemah(int x, int y)
{
boolean collided = false; // tidak ada yang menyentuh dindingLemah
for(int i=0; i< dindingLemahDI.length;i++)
{
if(dindingLemahDI[i]==1)
{
// apabila ada yang menyentuh dinding lemah maka dinding akan hilang
if(new Rectangle(x, y, 10, 10).intersects(new Rectangle(dindingLemahXPos[i], dindingLemahYPos[i], 50, 50)))
{
dindingLemahDI[i] = 0;
collided = true; // dindingLemah sudah tersentuh
break;
}
}
}
return collided;
}
public boolean checkCollisionKuat(int x, int y)
{
boolean collided = false; // tidak ada yang menyentuh dindingKuat
for(int i=0; i< dindingKuatXPos.length;i++)
{
// apabila ada yang menyentuh dinding kuat maka dinding tidak akan hilang
if(new Rectangle(x, y, 10, 10).intersects(new Rectangle(dindingKuatXPos[i], dindingKuatYPos[i], 50, 50)))
{
collided = true; // dindingKuat sudah tersentuh
break;
}
}
return collided;
}
}
Class PeluruPlayer1
import java.awt.Color;
import java.awt.Graphics;
public class PeluruPlayer1 {
private double x,y;
// posisi peluru player 1 akan ditembakkan
public PeluruPlayer1(double x2, double y2) {
x = x2;
y = y2;
}
// perubahan posisi peluru Player 1 waktu ditembakkan
public void move(String arah) {
if(arah.equals("kanan")) {
x += 5;
}
else if(arah.equals("kiri")) {
x -= 5;
}
else if(arah.equals("atas")) {
y -= 5;
}
else if(arah.equals("bawah")) {
y += 5;
}
}
// ukuran dan warna peluru Player 1
public void gambar(Graphics g) {
g.setColor(Color.yellow);
g.fillOval((int) x, (int) y, 10, 10);
}
// mendapatkan posisi x peluru Player 1 terbaru
public int getX() {
return (int) x;
}
// mendapatkan posisi y peluru Player 1 terbaru
public int getY() {
return (int) y;
}
}
Class PeluruPlayer2
import java.awt.Color;
import java.awt.Graphics;
public class PeluruPlayer2 {
private double x,y;
// posisi peluru player 2 akan ditembakkan
public PeluruPlayer2(double x2, double y2) {
x = x2;
y = y2;
}
// perubahan posisi peluru Player 2 waktu ditembakkan
public void move(String arah) {
if(arah.equals("kanan")) {
x += 5;
}
else if(arah.equals("kiri")) {
x -= 5;
}
else if(arah.equals("atas")) {
y -= 5;
}
else if(arah.equals("bawah")) {
y += 5;
}
}
// ukuran dan warna peluru Player 2
public void gambar(Graphics g) {
g.setColor(Color.red);
g.fillOval((int) x, (int) y, 10, 10);
}
// mendapatkan posisi x peluru Player 2 terbaru
public int getX() {
return (int) x;
}
// mendapatkan posisi y peluru Player 2 terbaru
public int getY() {
return (int) y;
}
}
Komentar
Posting Komentar