diff --git a/Game_Code/src/startGame/GameController.java b/Game_Code/src/startGame/GameController.java index e05f3b0fae55be45031f674957e60b64f8d14d74..0e438664dc8851dcf7ea5cb46690c9fc295a0a52 100644 --- a/Game_Code/src/startGame/GameController.java +++ b/Game_Code/src/startGame/GameController.java @@ -9,7 +9,9 @@ import java.io.FileReader; import java.util.HashSet; import javax.swing.ImageIcon; +import javax.swing.JButton; import javax.swing.JFrame; +import javax.swing.JPanel; import javax.swing.Timer; import model.*; @@ -82,6 +84,14 @@ public class GameController{ private long endTime; private double timeElapsed; + + private JButton pause; + private JButton resume; + private JButton save; + private JButton exit; + + + public GameController(GameView v, GameModel m){ this.v = v; this.m = m; @@ -164,6 +174,47 @@ public class GameController{ gameDisplay.addKeyListener(new GameListener()); gameDisplay.setFocusable(true); gameDisplay.setFocusTraversalKeysEnabled(false); + + + pause = this.v.getPause(); + resume = this.v.getResume(); + save = this.v.getSave(); + exit = this.v.getExit(); + pause.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + t.stop(); + } + }); + + resume.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + t.start(); + + } + }); + + save.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + // TODO ADD SAVE METHOD +/* try{ + PrintWriter writer = new PrintWriter("SavedGame.txt"); + writer.println("The first line"); //first line = lives left + writer.println("The second line"); //second line = time played + writer.close(); + } catch (Exception e1) { + }*/ + } + }); + + exit.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + w.setVisible(true); //show WelcomePage + t.stop(); + gameFrame.dispose(); //close current JFrame(current game) + } + }); + + /** * Initialize the start time and end time for a player @@ -203,6 +254,7 @@ public class GameController{ * - Do actions depending on the action performed */ if(source==w.getStart()){ // If clicked the start button + resetGame(); mode.setVisible(true); // Open the game mode page w.setVisible(false); }else if(source==w.load()){ // If clicked the load button @@ -272,6 +324,7 @@ public class GameController{ if(source == mode.getSingle()){ // If clicked the basic single mode button mode.setVisible(false); // Start the game with single mode gameFrame.setVisible(true); + t.start(); startTime = System.currentTimeMillis(); } @@ -329,6 +382,8 @@ public class GameController{ GameListener(){ t = new Timer(5,this); t.setInitialDelay(1000); + + resetGame(); } /** @@ -344,7 +399,7 @@ public class GameController{ * - x direction * - y direction */ - if(ballX< 0 || ballX > frameWidth-1.5*ballSize){ + if(ballX< 0 || ballX > frameWidth-8.5*ballSize){ /** * - X-direction * - If the ball is trying to go beyond the left/right border of the frame, @@ -388,13 +443,13 @@ public class GameController{ gameDisplay.setBottomScore(scoreBottom); ai.decrementLife(); - } else if(ballY+2.5*ballSize>frameHeight-inset-padHeight && velY > 0 && ballX + ballSize >= bottomPadX && ballX <= bottomPadX + padWidth){ + } else if(ballY+2.5*ballSize>frameHeight-inset-padHeight && velY > 0 && ballX + 3*ballSize >= bottomPadX && ballX <= bottomPadX + padWidth){ /** * If the ball is touching the bottom paddle * - reverse the direction */ velY = -velY; - } else if(ballY<=inset+padHeight && velY < 0 && ballX + ballSize >= topPadX && ballX <= topPadX + padWidth){ + } else if(ballY<=inset+padHeight && velY < 0 && ballX + 2.5*ballSize >= topPadX && ballX <= topPadX + padWidth){ /** * If the ball is touching the top paddle * - reverse the direction @@ -554,6 +609,8 @@ public class GameController{ } + + /** * @brief sets the display * @details opens a window @@ -594,12 +651,20 @@ public class GameController{ endTime = System.currentTimeMillis(); timeElapsed = (endTime-startTime)/1000.0; + + System.out.println(timeElapsed); } private void resetGame(){ player.resetScore(); ai.resetScore(); + + scoreTop = player.getScore(); + scoreBottom = player.getScore(); + + gameDisplay.setBottomScore(scoreBottom); + gameDisplay.setTopScore(scoreTop); } diff --git a/Game_Code/src/view/GameView.java b/Game_Code/src/view/GameView.java index 6381021662ffeee69463406b6f16322b6d28a506..c99633195e9683632e9f5d872700f60bf40376d4 100644 --- a/Game_Code/src/view/GameView.java +++ b/Game_Code/src/view/GameView.java @@ -1,5 +1,12 @@ package view; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.PrintWriter; + import javax.swing.*; /** @@ -11,7 +18,7 @@ import javax.swing.*; * @details This class import all different windows for display. */ public class GameView{ - + /** * Variable declarations to store different pages * - welcome page @@ -24,13 +31,23 @@ public class GameView{ private PongGameDisplay ponggame; private Tutorial tutorial; private JFrame gameFrame; - + /** * Constant declarations for the view */ - private final int FRAMEWIDTH = 700; - private final int FRAMEHEIGHT = 500; + private final int FRAMEWIDTH = 700; //700 + private final int FRAMEHEIGHT = 500; //500 + /** + * Set up buttons on the game panel + */ + private JButton pause; + private JButton resume; + private JButton save; + private JButton exit; + private JPanel gameOptions; + + /** * @brief Constructor for the view * @details declares all other windows @@ -43,10 +60,10 @@ public class GameView{ welcome = new Welcome(); mode = new Mode(); ponggame = new PongGameDisplay(); - + createGame(); } - + /** * @brief displays the welcome page. * @details sets the visibility of the window to be true. @@ -54,7 +71,7 @@ public class GameView{ public void display(){ welcome.setVisible(true); } - + /** * @brief gets welcome page window * @return welcome page object @@ -62,7 +79,7 @@ public class GameView{ public Welcome getWelcome(){ return welcome; } - + /** * @brief gets game mode page window * @return game mode page object @@ -70,7 +87,7 @@ public class GameView{ public Mode getmode(){ return mode; } - + /** * @brief gets game window * @return game window object @@ -78,7 +95,7 @@ public class GameView{ public PongGameDisplay getGame(){ return ponggame; } - + /** * @brief gets tutorial page window * @return tutorial page object @@ -87,24 +104,80 @@ public class GameView{ return tutorial; } - - //TODO: ADD PANEL FOR OPTIONS IN THE GAME /** * @brief create the game for display * @details create a frame under set dimension for the game */ public void createGame(){ gameFrame = new JFrame("FaultInOurPong"); - gameFrame.setContentPane(ponggame); - - + gameFrame.setLayout(new BorderLayout()); + + + gameOptions = new JPanel(); + gameOptions.setLayout(new BoxLayout(gameOptions, BoxLayout.Y_AXIS)); + pause = new JButton("Pause Game"); + resume = new JButton("Resume Game"); + save = new JButton("Save Game"); + exit = new JButton("Exit to Main Page"); + + gameOptions.add(Box.createVerticalGlue()); + addButton(gameOptions, pause, exit); + addButton(gameOptions, resume, exit); + addButton(gameOptions, save, exit); + addButton(gameOptions, exit, exit); + gameOptions.add(Box.createVerticalGlue()); + + gameOptions.setFocusable(false); + gameFrame.add(gameOptions, BorderLayout.LINE_START); + ponggame.setBorder(BorderFactory.createLineBorder(Color.black)); + ponggame.setFocusable(true); + gameFrame.add(ponggame, BorderLayout.CENTER); gameFrame.setSize(FRAMEWIDTH,FRAMEHEIGHT); - gameFrame.setResizable(false); + gameFrame.setResizable(true); gameFrame.setLocationRelativeTo(null); gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } + + public JButton getPause(){ + return pause; + } + public JButton getResume(){ + return resume; + } + + public JButton getSave(){ + return save; + } + + public JButton getExit(){ + return exit; + } + +/* + public void addListener(ActionListener listener){ + pause.addActionListener(listener); + resume.addActionListener(listener); + save.addActionListener(listener); + exit.addActionListener(listener); + } +*/ + public JPanel getGameOptionPanel(){ + return gameOptions; + } + + + public void addButton(JPanel panel, JButton button, JButton prefer) { + button.setAlignmentX(Component.CENTER_ALIGNMENT); + button.setAlignmentY(Component.CENTER_ALIGNMENT); + button.setMaximumSize(prefer.getPreferredSize()); +System.out.println(prefer.getPreferredSize()); + panel.add(button); + panel.add(Box.createVerticalStrut(20)); + } + + /** * @brief gets game object * @return game object @@ -112,7 +185,7 @@ public class GameView{ public JFrame getGameFrame(){ return gameFrame; } - + // TODO: display a dialogue after successfully saving game records (high score) /** * @brief display message for error loading game record @@ -122,7 +195,7 @@ public class GameView{ JFrame errorFrame = new JFrame("Error"); JOptionPane.showMessageDialog(errorFrame, "No record available!"); } - + /** * @brief display message for error loading game * @details create a frame for display @@ -131,13 +204,13 @@ public class GameView{ JFrame errorFrame = new JFrame("Error"); JOptionPane.showMessageDialog(errorFrame, "The record is either damaged or not available, please start a new game!"); } - + /** * @brief display message for game over * @param whichplayer is the indicator for the player */ public void gameOver(int whichplayer){ - + /** * - If the computer wins, display winning message for the computer * - If the player wins, display winning message for the player @@ -152,9 +225,9 @@ public class GameView{ } gameFrame.setVisible(false); welcome.setVisible(true); - + } - + /** * @brief create tutorial page * @param img is the image for display @@ -162,7 +235,7 @@ public class GameView{ public void tutorialPage(ImageIcon img){ tutorial = new Tutorial(img); } - + /** * @brief gets width of the window * @return FRAMEWIDTH @@ -170,7 +243,7 @@ public class GameView{ public int getFrameWidth(){ return FRAMEWIDTH; } - + /** * @brief gets height of the window * @return FRAMEHEIGHT diff --git a/Game_Code/src/view/PongGameDisplay.java b/Game_Code/src/view/PongGameDisplay.java index 7b7b7c279bfec851011592c9cf7f250d229ddac6..9610229048e0ec2213fef5cba1b748776e23a7c0 100644 --- a/Game_Code/src/view/PongGameDisplay.java +++ b/Game_Code/src/view/PongGameDisplay.java @@ -3,14 +3,9 @@ package view; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; -import java.awt.event.ActionListener; -import java.awt.event.KeyListener; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; - -import javax.swing.JFrame; import javax.swing.JPanel; -import javax.swing.Timer; /** * @file PongGameDisplay.java