0% found this document useful (0 votes)
18 views5 pages

Laporan 1 Membuat Pingpong Game Tujuan 1. Membuat Sebuah Game Sederhana. Langkah-Langkah Percobaan

1. The document describes steps to create a simple ping pong game in Java. It includes creating a new project, making a PingPongGame class, and writing code for the game logic, graphics, and handling mouse input. 2. The code controls the movement and collision detection of a ball bouncing around the screen and a paddle that the user controls with their mouse to hit the ball back. 3. The game ends when the ball passes the paddle and hits the bottom of the screen, displaying "GAME OVER" text.

Uploaded by

Andi Kurniawan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Laporan 1 Membuat Pingpong Game Tujuan 1. Membuat Sebuah Game Sederhana. Langkah-Langkah Percobaan

1. The document describes steps to create a simple ping pong game in Java. It includes creating a new project, making a PingPongGame class, and writing code for the game logic, graphics, and handling mouse input. 2. The code controls the movement and collision detection of a ball bouncing around the screen and a paddle that the user controls with their mouse to hit the ball back. 3. The game ends when the ball passes the paddle and hits the bottom of the screen, displaying "GAME OVER" text.

Uploaded by

Andi Kurniawan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

LAPORAN 1

MEMBUAT PINGPONG GAME


TUJUAN
1. Membuat sebuah Game sederhana.
Langkah-langkah percobaan
1. Membuat Project Baru dengan cara klik kanan pada javagame
Java Class.

2. Membuat Class dengan nama PingPongGame.

3. Kemudian mengetik program

New

package pingponggame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class PingPongGame extends JFrame implements
MouseMotionListener{
int x,y, mx, my, bx, by;
double kcbx, kcby;
boolean mouseDiDalamKotak,md,PermainanSelesai = false;
private Image dbImage;
private Graphics dbg;
public PingPongGame (){
setTitle("Java Game");
setSize(700,500);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(new Mouse ());
addMouseMotionListener ((MouseMotionListener) this);
bx =40;
by =40;
kcbx =1;
kcby =1;
}
public class Mouse extends MouseAdapter {
public void Mousepressed(MouseEvent e){
mx = e.getX ();
my = e.getY();
//x =mx - 20;
//y = my -20;
///System.out.print(mx);
}
public void mouseReleased(MouseEvent e){
md = false;
//System.out.print("ANDA MELEPAS MOUSE");
}
public void mouseEntered(MouseEvent e){
mouseDiDalamKotak = true;
//System.out.print("ANDA MELEPAS MOUSE");
}

public void mouseExited(MouseEvent e){


mouseDiDalamKotak = false;
//System.out.print("ANDA MELEPAS MOUSE");
}
}
public void paint(Graphics g){
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintBola (dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paintBola (Graphics g){
bx += kcbx;
by += kcby;
if (bx >= getWidth ()){
kcbx = -1;
}
if (bx <= 0){
kcbx = 1;
}
if (by >= getHeight ()){
kcby = 0;
PermainanSelesai = true;
}
if (by <= 0){
kcby = 1;
}
Rectangle b1 = new Rectangle (bx, by, 8, 8);
Rectangle P = new Rectangle (x, getHeight() -10,140,6);
g.setColor(Color.BLACK);
if (PermainanSelesai){
g.setFont(new Font ("Brush Script MT",Font.BOLD,34));
g.drawString("GAME SELESAI!!!", 200, 300);
}
g.setColor(Color.black);
g.fillOval(b1.x,b1.y,b1.width,b1.height);
g.setColor(Color.red);
g.fillOval(b1.x,b1.y,b1.width,b1.height);
if (md){
g.setColor(Color.BLUE );
g.fillRect(P.x,P.y,P.width,P.height);
g.setColor(Color.lightGray);
if(b1.intersects(P)){
kcby = -1;
//g.setColor(Color.red);
///g.drawString("TABRAKAN TERJADI!!!", x, y -40);
}
}

repaint();
}
public static void main(String[] args) {
PingPongGame mi = new PingPongGame();
}
@Override
public void mouseDragged(MouseEvent e){
mx = e.getX();
my = e.getY();
//x = mx;
///y = my;
md = true;
//System.out.print(mx);
e.consume();
}
@Override
public void mouseMoved(MouseEvent e){
mx = e.getX();
my = e.getY();
x=mx;
y=my;
md=true;
e.consume();
}
}
4. Setelah selesai kemudian menjalankan program dengan cara klik kanan pada
PingPongGame.java
Run File. Seperti gambar berikut.

5. Tampilan sesudah program di jalankan.

You might also like