Posted by : Unknown Friday, 19 June 2015

package vijay;
/**
 *
 * @author VIJAY
 */
import java.awt.*;       
import java.awt.event.*;
import javax.swing.*;
public class SubKillerWithScore extends JPanel 
{
   
    public static void main(String[] args) 
    {
        JFrame window = new JFrame("Sub Killer Game");
        SubKillerWithScore content = new SubKillerWithScore();
        window.setContentPane(content);
        window.setSize(600, 480);
        window.setLocation(100,100);
        window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        window.setResizable(false);  // User can't change the window's size.
        window.setVisible(true);
    }
   
    //------------------------------------------------------------------------

    private Timer timer;        // Timer that drives the animation.

    private int width, height;
    private Boat boat;          // The boat, bomb, and sub objects are defined
    private Bomb bomb;          //    by nested classes Boat, Bomb, and Submarine,
    private Submarine sub;      //    which are defined later in this class.
    private int hits;           // The number of times the user has hit the sub.
    private int misses;         // The number of times the user has missed the sub.
    private Font infoFont = new Font("Monospaced", Font.PLAIN, 16);
                            // A font for displaying the numbers of hits and misses.
    public SubKillerWithScore() 
    {

        setBackground( new Color(0,200,0) );

        ActionListener action = new ActionListener() 
        {
                // Defines the action taken each time the timer fires.
            public void actionPerformed(ActionEvent evt) 
            {
                if (boat != null) 
                {
                    boat.updateForNewFrame();
                    bomb.updateForNewFrame();
                    sub.updateForNewFrame();
                }
                repaint();
            }
        };
        timer = new Timer( 30, action );  // Fires every 30 milliseconds.

        addMouseListener( new MouseAdapter() 
        {
                // The mouse listener simply requests focus when the user
                // clicks the panel.
            public void mousePressed(MouseEvent evt) 
            {
                requestFocus();
            }
        } );

        addFocusListener( new FocusListener() 
       {              
            public void focusGained(FocusEvent evt) 
            {
                timer.start();
                repaint();
            }
            public void focusLost(FocusEvent evt) 
            {
                timer.stop();
                repaint();
            }
        } );

        addKeyListener( new KeyAdapter() 
        {
            public void keyPressed(KeyEvent evt) 
            {
                int code = evt.getKeyCode(); 
                if (code == KeyEvent.VK_LEFT) 
               {
                    boat.centerX -= 15;
                }
                else if (code == KeyEvent.VK_RIGHT) 
                {                        
                    boat.centerX += 15;
                }
                else if (code == KeyEvent.VK_DOWN) 
                {                       
                    if ( bomb.isFalling == false )
                        bomb.isFalling = true;
                }
            }
        } );

    } // end constructor

    public void paintComponent(Graphics g) {

        super.paintComponent(g);  // Fill panel with background color, green.
       
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        if (boat == null) {              
            width = getWidth();
            height = getHeight();
            boat = new Boat();
            sub = new Submarine();
            bomb = new Bomb();
        }

        if (hasFocus())
            g.setColor(Color.CYAN);
        else {
            g.setColor(Color.BLACK);
            g.drawString("CLICK TO ACTIVATE", 20, height - 10);
            g.setColor(Color.GRAY);
        }
        g.drawRect(0,0,width-1,height-1);  // Draw a 3-pixel border.
        g.drawRect(1,1,width-3,height-3);
        g.drawRect(2,2,width-5,height-5);

        boat.draw(g);
        sub.draw(g);
        bomb.draw(g);

        g.setFont(infoFont);
        g.setColor(Color.BLACK);
        g.drawString("Number of hits:   " + hits, 15, 24);
        g.drawString("Number of misses: " + misses, 15, 45);   
       
    } // end paintComponent()
 
    private class Boat {
        int centerX, centerY;  // Current position of the center of the boat.
        Boat() { // Constructor centers the boat horizontally, 80 pixels from top.
            centerX = width/2;
            centerY = 80;
        }
        void updateForNewFrame() { // Makes sure boat has not moved off screen.
            if (centerX < 0)
                centerX = 0;
            else if (centerX > width)
                centerX = width;
        }
        void draw(Graphics g) {  // Draws the boat at its current location.
            g.setColor(Color.BLUE);
            g.fillRoundRect(centerX - 40, centerY - 20, 80, 40, 20, 20);
        }
    } // end nested class Boat

    private class Bomb {
        int centerX, centerY; // Current position of the center of the bomb.
        boolean isFalling;   
        Bomb() { // Constructor creates a bomb that is initially attached to boat.
            isFalling = false;
        }
        void updateForNewFrame() {  // If bomb is falling, take appropriate action.
            if (isFalling) {
                if (centerY > height) {
                    isFalling = false;
                    misses++;   // USER HAS MISSED THE SUB
                }
                else if (Math.abs(centerX - sub.centerX) <= 36 &&
                        Math.abs(centerY - sub.centerY) <= 21) {
                    sub.isExploding = true;
                    sub.explosionFrameNumber = 1;
                    isFalling = false;  // Bomb reappears on the boat.
                    hits++;   // USER HAS HIT THE SUB
                }
                else {
                    centerY += 10;
                }
            }
        }
        void draw(Graphics g) { // Draw the bomb.
            if ( ! isFalling ) { 
                centerX = boat.centerX;
                centerY = boat.centerY + 23;
            }
            g.setColor(Color.RED);
            g.fillOval(centerX - 8, centerY - 8, 16, 16);
        }
    } // end nested class Bomb

    private class Submarine {
        int centerX, centerY; // Current position of the center of the sub.
        boolean isMovingLeft; // Tells whether the sub is moving left or right
        boolean isExploding;  // Set to true when the sub is hit by the bomb.
        int explosionFrameNumber; 
        Submarine() {  // Create the sub at a random location 40 pixels from bottom.
            centerX = (int)(width*Math.random());
            centerY = height - 40;
            isExploding = false;
            isMovingLeft = (Math.random() < 0.5);
        }
        void updateForNewFrame() { // Move sub or increase explosionFrameNumber.
            if (isExploding) {
                explosionFrameNumber++;
                if (explosionFrameNumber == 15) {
                    centerX = (int)(width*Math.random());
                    centerY = height - 40;
                    isExploding = false;
                    isMovingLeft = (Math.random() < 0.5);
                }
            }
            else { // Move the sub.
                if (Math.random() < 0.04) { 
                    isMovingLeft = ! isMovingLeft;
                }
                if (isMovingLeft) {
                    centerX -= 5; 
                    if (centerX <= 0) { 
                        centerX = 0;
                        isMovingLeft = false;
                    }
                }
                else {
                    centerX += 5;        
                    if (centerX > width) { 
                        centerX = width;  
                        isMovingLeft = true;
                    }
                }
            }
        }
        void draw(Graphics g) {  // Draw sub and, if it is exploding, the explosion.
            g.setColor(Color.BLACK);
            g.fillOval(centerX - 30, centerY - 15, 60, 30);
            if (isExploding) {
                g.setColor(Color.YELLOW);
                g.fillOval(centerX - 4*explosionFrameNumber,
                        centerY - 2*explosionFrameNumber,
                        8*explosionFrameNumber,
                        4*explosionFrameNumber);
                g.setColor(Color.RED);
                g.fillOval(centerX - 2*explosionFrameNumber,
                        centerY - explosionFrameNumber/2,
                        4*explosionFrameNumber,
                        explosionFrameNumber);
            }
        }
    } // end nested class Submarine   
} // end class SubKiller

Output:

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Welcome to My Blog

Translate

Popular Post

Total Pageviews

Blog Archive

- Copyright © Learning Java Program - Powered by Blogger -