Introduction: In this project, you will be creating a basic video game using the knowledge of Graphical User Interfaces (GUI) and Object-Oriented programming you have learned so far. You will be giv


Introduction:  

In this project, you will be creating a basic video game using the knowledge of Graphical User Interfaces (GUI) and Object-Oriented programming you have learned so far. You will be given starter code to assist in collision detection as well as a basic framework for the video game. Your goal is to design and fill out the remaining pieces to make the game functional. Furthermore, there will be extra credit assigned based on creativity. That is, if you put in the additional time and effort to create a unique game, you can earn an additional 30 points of extra credit. This will give you an opportunity to improve your projects category grade if you wish to do it. The uniqueness can come from special rules, graphics, etc.  

Video Game:  

Develop a GUI to contain the video game painting and allow the user to play the game. A minimal version of this is displayed below. There are two types of enemies, SmallEnemy and BigEnemy, as well as a Missile to hit the enemies. A JLabel keeps track of the score at the top left of the JFrame. Next, a JButton at the bottom of the JFrame allows us to shoot a Missile. Finally, a Turret is painted at the bottom center of the JFrame to act as the vessel to shoot a Missile.  

More details are in the attached document.

/////////////////////////////////////////////////////////////////////////////////////////////////

here’s the starter code:

Tester.java:

import java.awt.BorderLayout;

import java.awt.GraphicsEnvironment;

import java.awt.Point;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

/**

* The driver class for Project 4.  

*  

*/

@SuppressWarnings(“serial”)

public class Tester extends JFrame {

private static final int WINDOW_WIDTH = 700;

private static final int WINDOW_HEIGHT = 500;

private int score;

private int timer;

private int missilesFired = 0;

private JLabel scoreLabel;

private JButton fireButton;

private GamePanel panel;

/**

 * Default constructor to control the game.

 */

public Tester() {

 // Setup the initial JFrame elements

 setTitle(“Ball Destruction!”);

 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 setSize(WINDOW_WIDTH,WINDOW_HEIGHT);

 setLayout(new BorderLayout());

 panel = new GamePanel();

 add(panel, BorderLayout.CENTER);

 centerFrame(this);

 setVisible(true);

 setTimer();

 // Add the JButton for shooting the bullet

 fireButton = new JButton(“Shoot The Enemy!”);

 fireButton.addActionListener(new ActionListener() {

  @Override

  public void actionPerformed(ActionEvent e) {

   panel.addMissile();

   missilesFired++;

  }

 });

 add(fireButton, BorderLayout.SOUTH);

 // Add the JLabel for the score

 scoreLabel = new JLabel();

 add(scoreLabel, BorderLayout.NORTH);

}

/**

 * This method is called to start the video game which then

 * calls the infinite game loop for the game.

 */

public void start() {

 gameLoop();

}

/**

 * Method contains the game loop to move enemies, manage score,

 * and check if the game is finished.

 */

public void gameLoop() {

 // Game loop

 while(true) {

  pauseGame();  

  panel.detectCollision();

  score = panel.getTotalScore();

  scoreLabel.setText(Integer.toString(score));

  panel.move();

  panel.repaint();

  if(missilesFired > 10) {

   if(score >= 800){

    JOptionPane.showMessageDialog(null, “You Win!”, “Game Finished Message”,  

      JOptionPane.INFORMATION_MESSAGE);

    System.exit(0);

   } else {

    JOptionPane.showMessageDialog(null, “You Lose!”, “Game Finished Message”,  

      JOptionPane.INFORMATION_MESSAGE);

    System.exit(0);

   }

  }

  if(timer == 300) {

   panel.addEnemy();

   setTimer();

  }

  timer++;

 }  

}

/**

 * Pauses the thread for 30ms to control the  

 * speed of the animations.

 */

public void pauseGame() {

 try {

  Thread.sleep(30);

 } catch (InterruptedException e) {

  e.printStackTrace();

 }

}

/**

 * Method centers the frame in the middle of the screen.

 *  

 * @param frame to center with respect to the users screen dimensions.

 */

public void centerFrame(JFrame frame) {    

 int width = frame.getWidth();

 int height = frame.getHeight();

 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

 Point center = ge.getCenterPoint();

 int xPosition = center.x – width/2, yPosition = center.y – height/2;

 frame.setBounds(xPosition, yPosition, width, height);

 frame.validate();

}

/**

 * Randomly assign a value to determine how soon a new Enemy should

 * be created.

 */

public void setTimer() {

 timer = (int)(Math.random() * 100);

}

/**

 * The main method to execute the program.

 *  

 * @param args Any inputs to the program when it starts.

 */

public static void main(String[] args) {

 Tester main = new Tester();

 main.start();

}

}

/////////////////////////////////////////////////////////////////////////

GamePanel.java:

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Rectangle;

import java.util.ArrayList;

import javax.swing.JPanel;

/**

* This class contains the paintable objects such as the enemies,

* turret, and missile. It also keeps track of the  

*  

*/

public class GamePanel extends JPanel {

/**

 * Paints the enemies and missiles when called and also paints

 * the background of the panel White.

 */

@Override

public void paintComponent(Graphics g) {  

 super.paintComponent(g);

 g.setColor(Color.white);  

 g.fillRect(0, 0, this.getWidth(), this.getHeight());  

}  

/**

 * Method detects the collision of the missile and all the enemies. This is done by

 * drawing invisible rectangles around the enemies and missiles, if they intersect, then  

 * they collide.

 */

public void detectCollision() {

 // Create temporary rectangles for every enemy and missile on the screen currently        

 for(int i = 0; i < enemyList.size(); i++) {

  Rectangle enemyRec = enemyList.get(i).getBounds();

  for(int j = 0; j < missileList.size(); j++) {

   Rectangle missileRec = missileList.get(j).getBounds();

   if(missileRec.intersects(enemyRec)) {

    (enemyList.get(i)).processCollision(enemyList, i);

    missileList.remove(j);  

    if(enemyList.get(i) instanceof BigEnemy) {

     totalScore += 100;

    } else {

     totalScore += 150;

    }

   }

  }

 }

}

}