package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import java.util.Random;
public class PinBang {
//define table size
private final int TABLE_WIDTH=300;
private final int TABLE_HEIGHT=400;
//define ball size
private final int BALL_SIZE=16;
//define packet size
private final int PACKET_WIDTH=60;
private final int PACKET_HEIGHT=20;
//define packet vertical position
private final int PACKET_Y=340;
Random rand=new Random();
//vertical speed
private int ySpeed=10;
// create an rate
private double rate=rand.nextDouble()-0.5;
//calculate horizontal speed
private int xSpeed=(int)(ySpeed*rate*2);
//ball position
private int ballX=rand.nextInt(200)+20;
private int ballY=rand.nextInt(10)+20;
//packet position
private int packetX=rand.nextInt(200)+20;
// is Game Over?
private boolean isLose=false;
// Frame
private Frame f=new Frame("PinBang");
//Canvas
private Canva c=new Canva();
//Timer
private Timer timer;
//restart
private Button btnRestart=new Button("Restart");
class Canva extends Canvas
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
if(isLose==true)
{
g.setColor(new Color(255,0,0));
g.setFont(new Font("TIMES",Font.BOLD,30));
g.drawString("Game Over", 50, 200);
restartGame();
}
else
{
g.setColor(new Color(240,240,80));
g.fillOval(ballX, ballY, BALL_SIZE, BALL_SIZE);
g.setColor(new Color(80,80,200));
g.fillRect(packetX, PACKET_Y, PACKET_WIDTH, PACKET_HEIGHT);
}
}
}
public void iniGame()
{
c.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));
f.add(c);
KeyAdapter keyProcessor=new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
//super.keyPressed(e);
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
if(packetX>0)
{
packetX-=10;
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
if(packetX<=TABLE_WIDTH-PACKET_WIDTH)
{
packetX+=10;
}
}
}
};
// add key event listener
f.addKeyListener(keyProcessor);
c.addKeyListener(keyProcessor);
ActionListener taskPerformer=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(ballX<=0||ballX>=TABLE_WIDTH-BALL_SIZE)
{
xSpeed=-xSpeed;
}
if(ballY>PACKET_Y&&(ballXpacketX+PACKET_WIDTH))
{
timer.stop();
isLose=true;
c.repaint();
}
if(ballY<=0||
(ballY>PACKET_Y&&ballX>packetX&&ballX
{
ySpeed=-ySpeed;
}
ballY+=ySpeed;
ballX+=xSpeed;
c.repaint();
}
};
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
//super.windowClosing(e);
System.exit(0);
}
});
timer=new Timer(100, taskPerformer);
timer.start();
//System.out.println("Here");
f.add(btnRestart,BorderLayout.SOUTH);
btnRestart.setEnabled(false);
f.pack();
f.setVisible(true);
}
public void restartGame()
{
btnRestart.setEnabled(true);
btnRestart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new PinBang().iniGame();
//timer.stop();
//isLose=false;
//ballX=150;
//ballY=20;
//iniGame();
}
});
}
public static void main(String[] args)
{
new PinBang().iniGame();
}
}