import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class Game extends JPanel {
//instance variables
private int xCoordinate = 0;
private Point shootingFrom;
private boolean shooting = false;
private Point balloonLocation;
private int gunSize = 25;
private int padding = 35;
private boolean isShattered;
private Timer stopShatter;
private int shatterDistance;
private int gunTilt = 90;
private int bulletTilt;
private int balloonRadius = 20;
private int bulletRadius = 5;
private static int ballonsHit;
private static int s = 60;
private static int k = 0;
private Timer timer;
private int x;
private int y;
public Game(int width, int height) {
JOptionPane.showMessageDialog (null," Welcome to my Balloon Game! " + "\n You have 60 seconds to shoot and break as many balloons as you can." +
"\n\nDirections to play this game." + "\nLeft arrow key moves the gun left." + "\nRight arrow key moves the gun right." + "\nUp arrow key shoots the gun.");
final JLabel time = new JLabel();
this.add(time);
ActionListener actionListener =
new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
if(s > k) {
s--;
time.setText("Time left: " + s +
" Balloons hit: " + ballonsHit);
}
if(s == 0){
time.setText("Time is up!" +
" Balloons hit: " + ballonsHit);
}
}
} ;
Timer countDownTimer = new Timer(1000, actionListener);
countDownTimer.start();
this.setSize(width, height);
xCoordinate = this.getWidth()/2-1;
balloonLocation = setRandomBalloonLocation();
timer = new Timer( 15,
new ActionListener() {
public void actionPerformed(ActionEvent e) {
repaint();
}
} );
//repaint board every 25 milliseconds
timer.start();
//initiate shatter timer.
stopShatter = new Timer(1500, new ShatterTerminator());
addKeyListener(
new KeyAdapter() {
public void keyPressed(KeyEvent e) {
switch ( e.getKeyCode() ) {
case KeyEvent.VK_UP:
shootBall();
bulletTilt = gunTilt;
break;
case KeyEvent.VK_LEFT:
turnLeft();
break;
case KeyEvent.VK_RIGHT:
turnRight();
break;
}
}
});
}
public Point setRandomBalloonLocation() {
//use java.util.Random to generate random location
Random rand = new Random();
Point location = new Point(rand.nextInt(this.getX()+this.getWidth()-padding)+1, (rand.nextInt(this.getY()+this.getHeight())/2)+1);
return location;
}
public void shootBall() {
if (!shooting) {
shooting = true;
shootingFrom = new Point(getWidth() / 2 + x - bulletRadius, getHeight() - y);
}
}
public void turnLeft() {
xCoordinate -= 1;
gunTilt += 5;
}
public void turnRight() {
xCoordinate += 1;
gunTilt -= 5;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = (int) (Math.cos(Math.toRadians(gunTilt)) * gunSize);
int y = (int)(Math.sin(Math.toRadians(gunTilt)) * gunSize);
//turnLeft() and turnRight will control it
g.drawLine( getWidth() / 2 + x, getHeight() - y, getWidth() / 2, getHeight() );
//Display mini-balloons if the balloon was hit, otherwise display balloon
if(isShattered) {
g.setColor(Color.red);
g.drawOval(balloonLocation.x+shatterDistance, balloonLocation.y, balloonRadius/2, balloonRadius/2);
g.drawOval(balloonLocation.x-shatterDistance, balloonLocation.y, balloonRadius/2, balloonRadius/2);
g.drawOval(balloonLocation.x, balloonLocation.y+shatterDistance, balloonRadius/2, balloonRadius/2);
g.drawOval(balloonLocation.x, balloonLocation.y-shatterDistance, balloonRadius/2, balloonRadius/2);
shatterDistance+=2;
}
else {
g.setColor(Color.red);
g.drawOval( balloonLocation.x, balloonLocation.y, balloonRadius*2, balloonRadius*2);
}
//Draw bullet
if (shooting) {
g.setColor(Color.black);
shootingFrom.x += (int)(Math.cos(Math.toRadians(gunTilt)) * gunSize);
g.fillOval( shootingFrom.x, shootingFrom.y, bulletRadius, bulletRadius);
if((shootingFrom.x >= balloonLocation.x && shootingFrom.x <= (balloonLocation.x+(balloonRadius*3)))
&& (shootingFrom.y >= balloonLocation.y && shootingFrom.y <= (balloonLocation.y+(balloonRadius*3)))
) {
ballonsHit++;
shatterDistance = 0;
isShattered = true;
stopShatter.start();
shooting = false;
}
//checks if the ball hits the edge of the screen
if(shootingFrom.y <= 0 || shootingFrom.x <= 0 || shootingFrom.x >= this.getX()+this.getWidth()) {
shooting = false;
}
shootingFrom.y -= (int)(Math.sin(Math.toRadians(gunTilt)) * gunSize);
}
}
public class ShatterTerminator implements ActionListener {
public void actionPerformed(ActionEvent e) {
isShattered = false;
balloonLocation = setRandomBalloonLocation();
stopShatter.stop();
}
}
}