From 022537c2041f3756ea8f62cf86c4546c89254536 Mon Sep 17 00:00:00 2001
From: Jean Luo <luoj3@mcmaster.ca>
Date: Thu, 17 Nov 2016 09:03:57 -0500
Subject: [PATCH] advance mode modified

---
 Game_Code/Resources/userData.txt            |   3 +-
 Game_Code/src/startGame/GameController.java | 241 ++++++++++++++------
 Game_Code/src/view/GameView.java            |  15 +-
 Game_Code/src/view/Mode.java                |   3 +
 Game_Code/src/view/PongGameDisplay.java     |  30 ++-
 5 files changed, 214 insertions(+), 78 deletions(-)

diff --git a/Game_Code/Resources/userData.txt b/Game_Code/Resources/userData.txt
index b6fc4c6..c977c01 100644
--- a/Game_Code/Resources/userData.txt
+++ b/Game_Code/Resources/userData.txt
@@ -1 +1,2 @@
-hello
\ No newline at end of file
+1
+107.527
diff --git a/Game_Code/src/startGame/GameController.java b/Game_Code/src/startGame/GameController.java
index 0acda63..56b3e17 100644
--- a/Game_Code/src/startGame/GameController.java
+++ b/Game_Code/src/startGame/GameController.java
@@ -63,24 +63,25 @@ public class GameController{
 	private final int SINGLE = 0;
 	private final int ADVANCE = 1;
 	private int gameMode;
-	
+
 	private Ball bomb;
 	private int bombX, bombY, bombSize;
-	
+	private int bombVelX=1, bombVelY=1;
+
 	private Player player;
 	private Player ai;
 	private Timer t;
 	private long startTime;
 	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;
@@ -99,7 +100,7 @@ public class GameController{
 		ballY = frameHeight / 2 - ballSize / 2;
 		b.setPositionX(ballX);
 		b.setPositionY(ballY);
-		
+
 		/**
 		 * Setups for the bomb in the Model
 		 */
@@ -145,21 +146,22 @@ public class GameController{
 		v.tutorialPage(image);
 		tut = v.getTutorial();
 		tut.addListener(new TutorialListener());
+
 		gameFrame = this.v.getGameFrame();								// Game page
 		gameDisplay = this.v.getGame();
-		
+
 		gameDisplay.setTopScore(scoreTop);
 		gameDisplay.setBottomScore(scoreBottom);
 		gameDisplay.setBallSize(ballSize);
 		gameDisplay.setPaddleHeight(padHeight);
 		gameDisplay.setPaddleWidth(padWidth);
 		gameDisplay.setInset(inset);
-		
+
 		gameDisplay.addKeyListener(new GameListener());
 		gameDisplay.setFocusable(true);
 		gameDisplay.setFocusTraversalKeysEnabled(false);
-		
-		
+
+
 		pause = this.v.getPause();
 		resume = this.v.getResume();
 		save = this.v.getSave();
@@ -176,15 +178,15 @@ public class GameController{
 		});
 		save.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
-				// TODO ADD SAVE METHOD
+
 				try{
-				    PrintWriter writer = new PrintWriter("./Resources/userData.txt");
-				    getElapsedTime();
-				    writer.println(scoreBottom);		//first line = lives left
-				    writer.println(timeElapsed);		//second line = time played
-				    writer.close();
+					PrintWriter writer = new PrintWriter("./Resources/userData.txt");
+					getElapsedTime();
+					writer.println(scoreBottom);		//first line = lives left
+					writer.println(timeElapsed);		//second line = time played
+					writer.close();
 				} catch (Exception e1) {
-				 }
+				}
 			}
 		});
 		exit.addActionListener(new ActionListener() {
@@ -194,15 +196,15 @@ public class GameController{
 				gameFrame.dispose();				//close current JFrame(current game)
 			}
 		});
-		
-		
+
+
 		/**
 		 * Initialize the start time and end time for a player
 		 */
 		startTime = 0;
 		endTime = 0;
 		timeElapsed = 0;
-		
+
 	}
 	/**
 	 * @author Pongthusiastics
@@ -219,14 +221,14 @@ public class GameController{
 		 * 			- redirect to view tutorial
 		 * 			- exit the program
 		 * @param e is the action performed on the button
-	     */
+		 */
 		@Override
 		public void actionPerformed(ActionEvent e) {
 			/**
 			 * Save the action performed into a variable
 			 */
 			Object source = e.getSource();
-			
+
 			/**
 			 * - Check for the button pressed
 			 * - Do actions depending on the action performed
@@ -236,7 +238,7 @@ public class GameController{
 				mode.setVisible(true);					// Open the game mode page
 				w.setVisible(false);
 			}else if(source==w.load()){					// If clicked the load button
-				
+
 				//TODO
 				try{									// Read data from a saved record
 					FileReader fr = new FileReader("./Resources/userData.txt");
@@ -245,13 +247,13 @@ public class GameController{
 					scoreBottom = player.getScore();
 					gameDisplay.setBottomScore(scoreBottom);
 					timeElapsed = Double.valueOf((br.readLine()));	//read second line to be time
-					
+
 					w.setVisible(false);
 					gameFrame.setVisible(true);
 					t.start();
 					startTime = System.currentTimeMillis();
 					System.out.println("can load data");
-					
+
 					br.close();
 				}catch(Exception exp){
 					v.cannotLoadMessage();
@@ -286,10 +288,10 @@ public class GameController{
 		 * @details - redirect to game with easy level
 		 * 			- redirect to game with obstacles
 		 * @param e is the action performed on the button
-	     */
+		 */
 		@Override
 		public void actionPerformed(ActionEvent e) {
-			
+
 			/**
 			 * Save the action performed into a variable
 			 */
@@ -301,16 +303,21 @@ 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();
 			} 
-			//else if(source==mode.getAdvance()){
-				//TODO: the obstacle mode
-			//}
+			else if(source==mode.getAdvance()){
+				mode.setVisible(false);
+				gameDisplay.setAdvance();
+				gameFrame.setVisible(true);
+
+				t.start();
+				startTime = System.currentTimeMillis();
+			}
 		}
 	}
-	
+
 	/**
 	 * @author Pongthusiastics
 	 * @date 13/11/2016
@@ -321,10 +328,10 @@ public class GameController{
 		/** 
 		 * @brief opens the tutorial window with game instruction displayed.
 		 * @param e is the action performed on the button
-	     */
+		 */
 		@Override
 		public void actionPerformed(ActionEvent e) {
-			
+
 			/**
 			 * Save the action performed into a variable
 			 */
@@ -349,18 +356,18 @@ public class GameController{
 		/** 
 		 * @brief Constructor for the action listener class
 		 * @details Set up a timer to start the game
-	     */
+		 */
 		GameListener(){
 			t = new Timer(5,this);  
 			t.setInitialDelay(1000);
-			
+
 			resetGame();
 		}
 		/** 
 		 * @brief update the ball, paddles, and player information
 		 * @details update the ball positions, paddle positions, key pressed actions, and player score.
 		 * @param e is the action performed on the keyboard
-	     */
+		 */
 		@Override
 		public void actionPerformed(ActionEvent e) {
 			/**
@@ -387,12 +394,18 @@ public class GameController{
 				 */
 				velY = -velY;
 				--scoreTop;
-				checkGameOver();
+				
 				/**
 				 * Update model and view
 				 */
 				gameDisplay.setTopScore(scoreTop);
 				player.decrementLife();
+				
+				/**
+				 * Check whether the game ends.
+				 */
+				checkGameOver();
+				
 			} else if(ballY+2.5*ballSize>frameHeight){
 				/**
 				 * If the ball is trying to go down beyond the frame
@@ -402,12 +415,18 @@ public class GameController{
 				 */
 				velY = -velY;
 				--scoreBottom;
-				checkGameOver();
+				
 				/**
 				 * Update model and view
 				 */
 				gameDisplay.setBottomScore(scoreBottom);
 				ai.decrementLife();
+				
+				/**
+				 * Check whether the game ends
+				 */
+				checkGameOver();
+				
 			} 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
@@ -421,7 +440,7 @@ public class GameController{
 				 */
 				velY = -velY;
 			}
-			
+
 			/**
 			 * Update the ball position by velocity 
 			 */
@@ -433,6 +452,98 @@ public class GameController{
 			gameDisplay.setBall(ballX,ballY);
 			b.setPositionX(ballX);
 			b.setPositionY(ballY);
+			
+			/**
+			 * Advance mode actions
+			 */
+			long currentTime = System.currentTimeMillis();
+			if((currentTime-startTime)/1000>2){
+				gameDisplay.timeForBomb();
+				
+				/**
+				 * Update the velocity/direction of the Ball
+				 * - x direction
+				 * - y direction
+				 */
+				if(bombX< 0 || bombX > frameWidth-8.5*bombSize){
+					/**
+					 * - X-direction
+					 * - If the ball is trying to go beyond the left/right border of the frame, 
+					 * reverse the direction.
+					 */
+					bombVelX = -bombVelX;			
+				}
+				if(bombY < 0){
+					/**
+					 * - Y-direction
+					 * 
+					 * If the ball is trying to go up above the frame, 
+					 * - reverse the direction
+					 * - user gets points because the ball hits the border of the computer side
+					 * - check game over or not
+					 */
+					bombVelY = -bombVelY;
+
+				} else if(bombY+2.5*bombSize>frameHeight){
+					/**
+					 * If the ball is trying to go down beyond the frame
+					 * - reverse the direction
+					 * - the computer gets points
+					 * - check game over or not
+					 */
+					bombVelY = -bombVelY;
+				} else if(bombY+2.5*bombSize>frameHeight-inset-padHeight && velY > 0 && bombX + 3*bombSize >= bottomPadX && bombX <= bottomPadX + padWidth){
+					/**
+					 * If the ball is touching the bottom paddle
+					 * - reverse the direction
+					 */
+					bombVelY = -bombVelY;
+					
+					//TODO
+					--scoreBottom;
+					/**
+					 * Update model and view
+					 */
+					gameDisplay.setBottomScore(scoreBottom);
+					player.decrementLife();	
+					checkGameOver();
+
+									
+				} else if(bombY<=inset+padHeight && velY < 0 && bombX + 2.5*bombSize >= topPadX && bombX <= topPadX + padWidth){
+					/**
+					 * If the ball is touching the top paddle
+					 * - reverse the direction
+					 */
+					bombVelY = -bombVelY;
+					//TODO
+					--scoreTop;
+					
+					/**
+					 * Update model and view
+					 */
+					gameDisplay.setTopScore(scoreTop);
+					ai.decrementLife();
+					checkGameOver();
+
+					
+				}
+				
+				/**
+				 * Update the ball position by velocity 
+				 */
+				bombX += bombVelX;
+				bombY += bombVelY;
+
+				/**
+				 * Update the view and model
+				 */
+				gameDisplay.setBomb(bombX,bombY);
+				bomb.setPositionX(bombX);
+				bomb.setPositionY(bombY);
+				
+			}
+			
+			
 			/**
 			 * Detect the key pressed by the user on the keyboard
 			 */
@@ -510,14 +621,14 @@ public class GameController{
 		/** 
 		 * @brief detect which key is pressed on the keyboard
 		 * @param e is the action performed on keyboard
-	     */
+		 */
 		@Override
 		public void keyPressed(KeyEvent e) {
 			/**
 			 * Declare a variable to store the mouse click event
 			 */
 			int code = e.getKeyCode();				
-			
+
 			/**
 			 * - Detect which key is pressed and perform corresponding actions
 			 * - Save the action into a hashString
@@ -534,14 +645,14 @@ public class GameController{
 		/** 
 		 * @brief detects which key is released
 		 * @param e is the action performed on keyboard
-	     */
+		 */
 		@Override
 		public void keyReleased(KeyEvent e) {
 			/**
 			 * Declare a variable to store the mouse click event
 			 */
 			int code = e.getKeyCode();				
-			
+
 			/**
 			 * - Detect which key is pressed and perform corresponding actions
 			 * - Delete the action from the hashString
@@ -558,20 +669,20 @@ public class GameController{
 		@Override
 		public void keyTyped(KeyEvent e) {}
 	}
-	
+
 	/** 
 	 * @brief sets the display
 	 * @details opens a window
-     */
+	 */
 	public void display(){
 		v.display();
 	}
 	/** 
 	 * @brief checks whether the game ends
 	 * @details check the number of life for both the player and the ai is 0.
-     */
+	 */
 	private void checkGameOver(){
-		
+
 		/**
 		 * - If the number of life for the ai is 0, the player wins
 		 * - If the number of life for the player is 0, the ai wins.
@@ -581,37 +692,39 @@ public class GameController{
 			getElapsedTime();
 			v.gameOver(0, timeElapsed);
 			resetGame();
-			
-			
+
+
 		} else if(scoreTop==0){			
 			getElapsedTime();
 			v.gameOver(1, timeElapsed);
 			resetGame();
-			
-			
+
+
 		}
 	}
-	
+
 	private void getElapsedTime(){
 		endTime = System.currentTimeMillis();
 		timeElapsed = timeElapsed + (endTime-startTime)/1000.0;
-		
-		
-		
-		System.out.println(timeElapsed);
+
+
+
+//System.out.println(timeElapsed);
 	}
-	
+
 	private void resetGame(){
 		player.resetScore();
 		ai.resetScore();
-		
+
 		scoreTop = player.getScore();		
 		scoreBottom = player.getScore();
-		
+
 		gameDisplay.setBottomScore(scoreBottom);
 		gameDisplay.setTopScore(scoreTop);
+		
+		gameDisplay.noBomb();
 	}
-	
-	
-	
+
+
+
 }
\ No newline at end of file
diff --git a/Game_Code/src/view/GameView.java b/Game_Code/src/view/GameView.java
index c9510cd..5f378ca 100644
--- a/Game_Code/src/view/GameView.java
+++ b/Game_Code/src/view/GameView.java
@@ -35,8 +35,8 @@ public class GameView{
 	/**
 	 * Constant declarations for the view
 	 */
-	private final int FRAMEWIDTH = 700; //700
-	private final int FRAMEHEIGHT = 500; //500
+	private final int FRAMEWIDTH = 700; 
+	private final int FRAMEHEIGHT = 500;
 	
 	/**
 	 * Set up buttons on the game panel
@@ -154,15 +154,7 @@ public class GameView{
 	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;
 	}
@@ -172,7 +164,6 @@ public class GameView{
 		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));
 	}
diff --git a/Game_Code/src/view/Mode.java b/Game_Code/src/view/Mode.java
index 55d8147..2ee1e73 100644
--- a/Game_Code/src/view/Mode.java
+++ b/Game_Code/src/view/Mode.java
@@ -93,4 +93,7 @@ public class Mode extends JFrame{
 		return single;
 	}
 	
+	public JButton getAdvance(){
+		return sObstacle;
+	}
 }
diff --git a/Game_Code/src/view/PongGameDisplay.java b/Game_Code/src/view/PongGameDisplay.java
index 9610229..2540fce 100644
--- a/Game_Code/src/view/PongGameDisplay.java
+++ b/Game_Code/src/view/PongGameDisplay.java
@@ -1,5 +1,6 @@
 package view;
 
+import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.Rectangle;
@@ -31,6 +32,7 @@ public class PongGameDisplay extends JPanel{
 	private int scoreTop, scoreBottom;
 	
 	private int ballX, ballY;
+	private int bombX, bombY;
 	private int bottomPadX, bottomPadY;
 	private int topPadX, topPadY;
 	private boolean first;
@@ -41,6 +43,7 @@ public class PongGameDisplay extends JPanel{
 	private int gameMode;
 	private final int SINGLE=0;
 	private final int ADVANCE=1;
+	private boolean startBomb;
 	
 	/**
 	 * @brief Constructor for PongGameDisplay
@@ -49,6 +52,7 @@ public class PongGameDisplay extends JPanel{
 	public PongGameDisplay(){
 		first = true;
 		gameMode = SINGLE;
+		startBomb=false;
 	}
 	
 	/**
@@ -89,10 +93,20 @@ public class PongGameDisplay extends JPanel{
 		/**
 		 * Draw the ball by passed in values
 		 */
-		// ball
 		Ellipse2D ball = new Ellipse2D.Double(ballX, ballY, 20, 20);  // creating the ball object for the game
 		g2d.fill(ball);
 		
+		/**
+		 * Draw the bomb if the mode is the advance mode
+		 */
+		if(gameMode == ADVANCE && startBomb==true){
+			
+			Ellipse2D bomb = new Ellipse2D.Double(bombX, bombY, 20, 20); 
+			g2d.setPaint(new Color(255,0,0));
+			g2d.fill(bomb);
+		}
+		
+		
 		/**
 		 * Draw scores on the screen by passed in values
 		 */
@@ -113,6 +127,20 @@ public class PongGameDisplay extends JPanel{
 		ballY = y;
 	}
 	
+	public void setBomb(int x, int y){
+		bombX = x;
+		bombY = y;
+	}
+	
+	public void timeForBomb(){
+		startBomb = true;
+	}
+	
+	public void noBomb(){
+		startBomb = false;
+	}
+	
+	
 	/**
 	 * @brief sets the size of the ball
 	 * @param s is the ball size
-- 
GitLab