Script Game Bola Pantulan (J2SE)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GambarBolaAnimasi4 extends Canvas
{
int x;
int y;
int dX;
int dY;
boolean move=true;
int posisi;
int point;
String pointanda="";
int level=0;
public GambarBolaAnimasi4()
{
x=200;
y=100;
dX=5;
dY=5;
setBackground(Color.white);
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.drawOval(x,y,10,10);
if(move)
{
x+=dX;
y+=dY;
if(x<0)
{
dX=5+level;
}
else if(x+10>getWidth())
{
dX=-5-level;
}
if(y<0)
{
dY=5+level;
}
else if(y+10>getHeight())
{
dY=-5-level;
}
if(posisi==1 && y>130 && x>150)
{
move=false;
JOptionPane.showMessageDialog(null,"Selamat, Point anda "+
point,"Informasi",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
if(posisi==2 && y>130 && x<150)
{
move=false;
JOptionPane.showMessageDialog(null,"Selamat, Point anda "+
point,"Informasi",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
if((posisi==1 || posisi==2) && y>130)
{
point=point+1;
}
}
String pointanda=Integer.toString(point);
System.out.println(pointanda);
if(point>5)
{
level=5;
}
}
}
class ProsesBolaAnimasi4 extends Thread
{
GambarBolaAnimasi4 picture;
public ProsesBolaAnimasi4(GambarBolaAnimasi4 obyekgambar)
{
this.picture=obyekgambar;
}
public void run()
{
while(true)
{
picture.repaint();
try
{
sleep(50);
}
catch(Exception e)
{
}
}
}
}
public class Animasi4 extends JFrame
{
GambarBolaAnimasi4 gambar=new GambarBolaAnimasi4();
ProsesBolaAnimasi4 proses=new ProsesBolaAnimasi4(gambar);
JButton kiri=new JButton("<");
JButton kanan=new JButton(">");
JLabel lblPoint=new JLabel("");
public Animasi4()
{
super("Game Sederhana dengan Level");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(100,100);
setSize(310,220);
proses.start();
}
void komponenVisual()
{
getContentPane().setLayout(null);
getContentPane().add(gambar);
gambar.setBounds(0,0,300,140);
getContentPane().add(kiri);
kiri.setBounds(0,140,150,25);
getContentPane().add(kanan);
kanan.setBounds(150,140,150,25);
getContentPane().add(lblPoint);
lblPoint.setBounds(150,165,150,20);
setVisible(true);
}
void aksiReaksi()
{
kanan.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent k)
{
if(k.getKeyCode()==k.VK_RIGHT)
{
gambar.move=true;
kiri.setVisible(true);
kiri.requestFocus(true);
kanan.setVisible(false);
gambar.posisi=1;
}
}
});
kiri.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent k)
{
if(k.getKeyCode()==k.VK_LEFT)
{
gambar.move=true;
kanan.setVisible(true);
kanan.requestFocus(true);
kiri.setVisible(false);
gambar.posisi=2;
}
}
});
}
public static void main(String args[])
{
Animasi4 a4=new Animasi4();
a4.komponenVisual();
a4.aksiReaksi();
}
}
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment