Balloon hitting game Program Plan: Create an applet panel and set the gun inside it Create a balloon in that panel as random position Press up-arrow key to fire a small ball from the gun If the ball misses the balloon, the ball disappears once it hits the boundary of the panel. » » »
Program *Compile Rurn : javac HittingBalloons.java : java HittingBalloons t/ // header files import java. awt.Graphics; import java. awt. Point; import java. awt. event. ActionEvent import java. awt.event. ActionListener; import java. awt.event. Key Adapter import java. awt. event. KeyEvent; import java. util.Random; import javax.swing. JErame; import javax.swing. JPanel; import javax.swing.Timer; public class HittingBalloons extends JFrame private Game board; public HittingBalloons (int width, int height) board new Game (wi dth, height);
add ( board ); board. setFocusable ( true clas s Game extends JPanel //instance variables private int ballons Hit= 0; private int baïïsshot = 0; private int xCoordinate-0 private Point shootingErom; private Boolean shooting = false; Timer timer; private Point balloonLocation; //determines how big the gun is vertically private int gunSize - 25; //ensures that a balloon is drawn without appearing cutoff (over the edge) private int padding = 25; //flag to let paint Component ) know to draw the shatter visual private boolean isshatte red;
//this timer will end the shatter visual private Timer stopShatter; The current distance ofthe mini-balloons from the shatter point slowly increases as shatter visual executes private int shatterDistance The bulletTilt helps guide the bullet in a diagonal path both are needed as you can change the tit of the gun while a bullet is flying which require a constant tilt private int gunTiït 90; private int bulletTilt; //Make the radius one final size, don't use balloonRadius 4 //Also lets make the balloon a little bigger private int balloonRadius=25; private int bulletRadius 5; public Game (int width, int height) You must set the size of this panel so that you can give xCoordinate a value, otherwise getWidth0 will equal 0. Since xCoordinate represents the x-coord of the gun, set it to the middle of the board
this.setSize (width, height); xCoordinate this.getWidth)/2-1;
Don't try this line before the constructor, because it depends on the size of the panel which isn't set before setSize is called. balloonLocation - set RandomBalloonLocation ); timer = new Time r ( 25, new ActionListener 0 public void actionPerformed ( ActionEvent e) t repaint ) //repaint board every 25 milliseconds timer.start ; //initiate shatter timer. stopshatter = new Timer (2000, new ShatterTerminator )) addKeyListener( new KeyAdapter ) public void keyPressed KeyEvent e)
switch e. getKeyCode ) case KeyEvent. VK_UP: shootBall); bulletTilt gunTİlt; break; case KeyEvent. VK_LEFT: turnLeft ); break; case KeyEvent. VK RIGHT: turnRight ); break; public Point set RandomBalloonLocation () Use java. util. Random to generate random location Make x between 1 and the x coordinate of the right hand edge of the JPanel paddingd so that the ball doesn't appear in cut off
Make y between 1 and half of the y-co ordinate of the lower hand edge of the JPanel Random rand new Random Point location - new Point ( rand. nextInt (this.getX ()+this. getWidth () padding)+1, (rand.nextInt (this.gety O +this.getHeight 0)/2)+1) ret urn ї0cation; public void shootBall () if shooting) shootingtrue; shootingE rom - new Point ( xCoordinate, qetHeiqht () - qunSize) else system.out.println "The ball is already being shot!"
public void turnLeft () xCoordinate - 1; gunTiltt5; public void turnRight () Coordinate gunTilt 1; 5 Biggest issue here is, the use of constants (hard-coded values). Use all variables, because some variables will be used in several computations, e.g. balloonLocation, ballonRadius. protected void paintComponent Graphics g) super.paint component g)i int = (int) ( Math.cos ( Math. to Radians ( gunTilt int y - (int) ( Math.sin ( Math.toRadians ( gunTilt) )gunSize ) ) * gunSize );
//Draw gun //turnLeft ) and turnRight will control it g.drawLine ( getWidth / 2 + x, getHeight- y, getMidth) / 2, getHeight; //Display mini-balloons if the balloon was hit, otherwise display balloon if (isShattered) 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+ 5 //move mini-balloons else g. drawOval balloonLocation. x, balloonLocation.y, balloonRadius, balloonRadius) :
//Draw bullet if (shooting) g. drawoval shootingFrom. x, shootingFrom.y, bulletRadius, bulletRadius) //check if the bullet hit balloon //bullet is between the left and right boundaries of balloon boundaries of balloon shooting?rom.x <- (balloonLocation. x+ (balloonRadius))) shootingF rom . У <- (balloonLocation.yt (balloonRadius ))) //and bullet is between the lower and upper && (shootingF rom . y >_ balloonLo cation. y && system. out. print1n("Ballon Hit!"); shatterDistance 0; isShattered true stopShatter.start ); shooting- false; //Detect edge collision //Hitting the left and right edge aren't even possible, since
//bullets shoot straight up if (shootingFron.y <= 0 //hits top shootingFrom. x < 0 //hits left edge shootingFrom.x >- this.getX ()+this. getWidth () //hits right edge System, out . printin ("Hİt Boundaryl"); shootingfalse //continue to move bullet straight up //To shoot diagonal, you set shootingF rom.x accordingly shootingFrom. -(bulletTilt/3); shooting?rom.y -20; private class ShatterTerminator implements ActionListener public void actionPerformed( ActionEvent e) isshattered -false; balloonLocation -setRandomBalloonLocation); stopShatter.stop
lic static void main( Stringl] args) int width 300, height 300 HittingBalloons frame - new HittingBalloons (width, height); frame. setSize( width, heiqht
frame. setLocationRelativeTo ( null frame.setDefaultcloseoperation( JFrame. EXIT_ON_CLOSE frame. setvisible ( true ) Sample output:
Hit Boundary Hit Boundary! The ball is already being shot! Hit Boundary! Hit Boundary Hit Boundary! Hit Boundary Ballon Hit!