From 6f9bf8967e909197516700eee56ba17eba99ef00 Mon Sep 17 00:00:00 2001 From: Jean Luo <luoj3@mcmaster.ca> Date: Mon, 7 Nov 2016 13:16:56 -0500 Subject: [PATCH] partial comments added to codes --- src/model/Ball.java | 75 ++++++++++++++++++----- src/model/GameModel.java | 68 +++++++++++++++++++-- src/model/Paddle.java | 31 +++++++++- src/model/Player.java | 12 ++-- src/startGame/GameController.java | 98 +++++++++++++++++++++++++++---- src/startGame/PongGame.java | 6 ++ src/view/GameView.java | 26 ++++---- src/view/Mode.java | 9 ++- src/view/Welcome.java | 7 +++ 9 files changed, 280 insertions(+), 52 deletions(-) diff --git a/src/model/Ball.java b/src/model/Ball.java index 26e92a0..b050283 100644 --- a/src/model/Ball.java +++ b/src/model/Ball.java @@ -1,43 +1,86 @@ package model; /** - * This class is the model of the ball object. - * It contains all the information for a ball. + * @file Ball.java + * @title Ball * @author Pongthusiastics + * @date 4/11/2016 + * @brief This class represents a ball on the pong game + * @details This class saves the information of a ball, including its position, + * size and the speed. */ public class Ball { + /** + * The X position of a ball on the screen + */ private int positionX; + /** + * The Y position of a ball on the screen + */ private int positionY; + /** + * The size of a ball + */ private final int SIZE = 20; - - //TODO + + // TODO private int speed; - - public Ball(int x, int y){ + + /** + * @brief Constructor for Ball + * @details Constructor accepts the x and y position of the ball + * @param x + * is the x-position + * @param y + * is the y-position + */ + public Ball(int x, int y) { positionX = x; positionY = y; } - - public void setPositionX(int x){ + + /** + * @brief sets the x position of the ball + * @param x-position + * of the ball + */ + public void setPositionX(int x) { positionX = x; } - - public void setPositionY(int y){ + + /** + * @brief sets the y position of the ball + * @param y-position + * of the ball + */ + public void setPositionY(int y) { positionY = y; } - public int getPositionX(){ + /** + * @brief gets the x-position of the ball + * @return positionX + */ + public int getPositionX() { return positionX; } - - public int getPositionY(){ + + /** + * @brief gets the y-position of the ball + * @return positionY + */ + public int getPositionY() { return positionY; } - - public int getSize(){ + + /** + * @brief gets the size of the ball + * @return SIZE + */ + public int getSize() { return SIZE; } - + } diff --git a/src/model/GameModel.java b/src/model/GameModel.java index 4084a40..4378dec 100644 --- a/src/model/GameModel.java +++ b/src/model/GameModel.java @@ -1,11 +1,45 @@ package model; + +/** + * @file GameModel.java + * @title GameModel + * @author Pongthusiastics + * @date 4/11/2016 + * @brief This class represents a ball on the pong game + * @details This class saves the information of a ball, including its position, + * size and the speed. + */ public class GameModel { + /** + * The ball object for the game + */ private Ball b1; + /** + * The two paddle in the game, one for the player and the other for the computer + */ private Paddle p_player, p_computer; + /** + * The two players in the game, one for the user and the other for the computer + */ private Player player, computer; + private int ballX, ballY; + private int playerX, playerY; + private int compX, compY; + + + /** + * @brief Constructor for the game Model + * @details Contains all the data and models for the game, including the player, paddle, and the ball. + * @param ballX is the x-position of the ball + * @param ballY is the y-position of the ball + * @param playerX is the x-position of the user's paddle + * @param playerY is the y-position of the user's paddle + * @param compX is the x-position of the user's paddle + * @param compY is the y-position of the user's paddle + */ public GameModel(int ballX, int ballY, int playerX, int playerY, int compX, int compY){ b1 = new Ball(ballX, ballY); p_player = new Paddle(playerX, playerY); @@ -16,27 +50,53 @@ public class GameModel { computer = new Player(); } - public Ball getBall(){ - return b1; - } - + /** + * @brief sets the x and y positions of a ball + * @param x is the x position of the ball + * @param y is the y position of the ball + */ public void setBall(int x, int y){ b1.setPositionX(x); b1.setPositionY(y); } + + /** + * @brief gets the Ball object + * @return b1 + */ + public Ball getBall(){ + return b1; + } + + /** + * @brief gets the user paddle object + * @return p_player + */ public Paddle getPlayerPaddle(){ return p_player; } + /** + * @brief gets the computer paddle object + * @return p_computer + */ public Paddle getComputerPaddle(){ return p_computer; } + /** + * @brief gets the player object + * @return player + */ public Player getPlayer(){ return player; } + /** + * @brief gets the computer object + * @return computer + */ public Player getComputer(){ return computer; } diff --git a/src/model/Paddle.java b/src/model/Paddle.java index 738086e..660017a 100644 --- a/src/model/Paddle.java +++ b/src/model/Paddle.java @@ -12,35 +12,64 @@ public class Paddle { //TODO private int speed; + public Paddle(int x, int y){ positionX = x; positionY = y; } + /** + * @brief sets the x-position of the paddle + * @param x is the x position of the paddle + */ public void setPositionX(int x){ positionX = x; } + /** + * @brief sets the y-position of the paddle + * @param y is the y position of the paddle + */ public void setPositionY(int y){ positionY = y; } + /** + * @brief returns the x position of the paddle + * @return positionX + */ public int getPositionX(){ return positionX; } + /** + * @brief returns the y position of the paddle + * @return positionY + */ public int getPositionY(){ return positionY; } + /** + * @brief returns the width of the paddle + * @return WIDTH + */ public int getWidth(){ return WIDTH; } - public int getSize(){ + /** + * @brief returns the height of the paddle + * @return HEIGHT + */ + public int getHeight(){ return HEIGHT; } + /** + * @brief returns the inset between the paddle and the screen + * @return INSET + */ public int getInset(){ return INSET; } diff --git a/src/model/Player.java b/src/model/Player.java index 9a77426..7c79bc8 100644 --- a/src/model/Player.java +++ b/src/model/Player.java @@ -9,9 +9,7 @@ public class Player { // Variable declaration for storing the player score private int score; - /** - * Constructor for the player - set the score to be 0 initially - */ + public Player(){ score = LIFE; //TODO: POSITION X, POSITION Y @@ -19,21 +17,21 @@ public class Player { } /** - * Decrease number of life of the player + * @brief decreases number of life of the player */ public void decrementLife(){ score--; } /** - * Increase score for a player + * @brief increases score for a player */ public void incrementScore(){ score++; } /** - * Get the score of a player + * @brief gets the score of a player * @return playerScore - return the score of the player */ public int getScore(){ @@ -41,7 +39,7 @@ public class Player { } /** - * Check whether the player loses the game or not + * @brief checks whether the player loses the game or not * @return - boolean indicating losing or not */ public boolean checkLoss(){ diff --git a/src/startGame/GameController.java b/src/startGame/GameController.java index 47a0248..44c4eaf 100644 --- a/src/startGame/GameController.java +++ b/src/startGame/GameController.java @@ -42,6 +42,9 @@ public class GameController{ this.v = v; this.m = m; + /* + * Setups for the View + */ w = this.v.getWelcome(); w.addListener(new WelcomepageListener()); @@ -59,10 +62,20 @@ public class GameController{ gameDisplay.setFocusable(true); gameDisplay.setFocusTraversalKeysEnabled(false); + /* + * Setups for the Model + */ + frameWidth = v.getFrameWidth(); + frameHeight = v.getFrameHeight(); + + } + /* + * Actionlistener for the welcome page + */ class WelcomepageListener implements ActionListener{ @Override @@ -70,11 +83,13 @@ public class GameController{ // TODO Auto-generated method stub Object source = e.getSource(); + // If clicked the start button if(source==w.getStart()){ mode.setVisible(true); w.setVisible(false); }else if(source==w.load()){ + // If clicked the load button //TODO try{ FileReader fr = new FileReader("./Resources/userData.txt"); @@ -87,6 +102,7 @@ public class GameController{ v.cannotLoadMessage(); } }else if(source==w.highScores()){ + // If clicked the high score button //TODO try{ FileReader fr = new FileReader("./Resources/gameScore.txt"); @@ -99,12 +115,14 @@ public class GameController{ v.noFileAvailMessage(); } }else if(source==w.tutorial()){ + // If clicked the tutorial button //TODO w.setVisible(false); tut.setVisible(true); }else if(source==w.exit()){ + // If clicked the exit button System.exit(0); } @@ -112,6 +130,9 @@ public class GameController{ } + /* + * Actionlistener for the Single-Mode page + */ class ModeListener implements ActionListener{ @Override @@ -120,13 +141,18 @@ public class GameController{ Object source = e.getSource(); if(source == mode.getSingle()){ + // If clicked the basic single mode button mode.setVisible(false); gameFrame.setVisible(true); + t.start(); } } } + /* + * Actionlistener for the tutorial page + */ class TutorialListener implements ActionListener{ @Override @@ -135,6 +161,7 @@ public class GameController{ Object source = e.getSource(); if(source == tut.getBack()){ + // If clicked the back button tut.setVisible(false); w.setVisible(true); } @@ -145,22 +172,19 @@ public class GameController{ class GameListener implements ActionListener, KeyListener{ GameListener(){ - - frameWidth = v.getFrameWidth(); - frameHeight = v.getFrameHeight(); + ballX = gameDisplay.getBallX(); ballY = gameDisplay.getBallY(); t = new Timer(5,this); t.setInitialDelay(1000); // sets initial delay for the movement of the ball - t.start(); bottomPadX = gameDisplay.getBottomX(); bottomPadY = gameDisplay.getBottomY(); -//TODO: MODEL FOR THE PADDLE -System.out.println("x: "+bottomPadX); -System.out.println("y: "+bottomPadY); + + //TODO: MODEL FOR THE PADDLE + } @Override @@ -168,40 +192,77 @@ System.out.println("y: "+bottomPadY); // X-direction if(ballX< 0 || ballX > frameWidth-1.5*ballSize){ - velX = -velX; + /* + * If the ball is trying to go beyond the left/right border of the frame, + * reverse the direction. + */ + velX = -velX; } // Y-direction if(ballY < 0){ + /* + * 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 + */ velY = -velY; ++scoreBottom; gameDisplay.setBottomScore(scoreBottom); } else if(ballY+2.5*ballSize>frameHeight){ + /* + * If the ball is trying to go down beyond the frame + * - reverse the direction + * - the computer gets points + */ velY = -velY; ++scoreTop; gameDisplay.setTopScore(scoreTop); } else if(ballY+2.5*ballSize>frameHeight-inset-2*padHeight && velY > 0 && ballX + ballSize >= bottomPadX && ballX <= bottomPadX + padWidth){ + /* + * If the ball is touching the bottom paddle + * - reverse the direction + */ velY = -velY; } else if(ballY<=inset+2*padHeight && velY < 0 && ballX + ballSize >= topPadX && ballX <= topPadX + padWidth){ + /* + * If the ball is touching the top paddle + * - reverse the direction + */ velY = -velY; } + /* + * Update the ball position by velocity + */ ballX += velX; ballY += velY; gameDisplay.setBall(ballX,ballY); - // pressed keys + /* + * Detect the key pressed by the user on the keyboard + */ if (keys.size() == 1) { - if (keys.contains("LEFT")) { // left key is pressed + if (keys.contains("LEFT")) { + /* + * If the user presses LEFT + * - update the position of the user paddle + * - display the change on the screen + */ if(bottomPadX>0) { //TODO: SPEED bottomPadX-=3; gameDisplay.setBottom(bottomPadX); } } - else if (keys.contains("RIGHT")) { // right key is pressed + else if (keys.contains("RIGHT")) { if(bottomPadX < frameWidth - padWidth){ + /* + * If the user presses RIGHT + * - update the position of the user paddle + * - display the change on the screen + */ //TODO: SPEED bottomPadX+=3; gameDisplay.setBottom(bottomPadX); @@ -209,11 +270,18 @@ System.out.println("y: "+bottomPadY); } } - // AI + /* + * Create actions for the AI paddles + */ double delta = ballX - topPadX; if (delta > 0) { if(topPadX < frameWidth - padWidth){ + /* + * If the AI paddle is trying to reach the right wall + * - move the paddle to the right + * - display the movement on the screen + */ topPadX +=1; gameDisplay.setTop(topPadX); } @@ -221,6 +289,11 @@ System.out.println("y: "+bottomPadY); else if (delta < 0) { if(topPadX>0){ + /* + * If the AI paddle is trying to reach the left wall + * - move the paddle to the left + * - display the movement on the screen + */ topPadX -=1; gameDisplay.setTop(topPadX); } @@ -243,7 +316,6 @@ System.out.println("y: "+bottomPadY); keys.add("RIGHT"); break; } - //gameDisplay.repaint(); } @Override diff --git a/src/startGame/PongGame.java b/src/startGame/PongGame.java index 1ad3c6b..84f064f 100644 --- a/src/startGame/PongGame.java +++ b/src/startGame/PongGame.java @@ -10,10 +10,16 @@ public class PongGame { } public static void main(String[] args){ + /* + * Initialize the model, view, and controller for the game + */ GameView view = new GameView(); GameModel model = new GameModel(1,1,2,2,2,2); GameController controller = new GameController(view, model); + /* + * Invoke the game display from the controller + */ controller.display(); } diff --git a/src/view/GameView.java b/src/view/GameView.java index 8c33ad5..dc84e01 100644 --- a/src/view/GameView.java +++ b/src/view/GameView.java @@ -9,11 +9,17 @@ public class GameView{ private PongGameDisplay ponggame; private Tutorial tutorial; + private final int FRAMEWIDTH = 700; + private final int FRAMEHEIGHT = 500; + private JFrame gameFrame; public GameView(){ - + /* + * Pass in different windows to this view interface + * - will wait for further invocation + */ welcome = new Welcome(); mode = new Mode(); ponggame = new PongGameDisplay(); @@ -47,7 +53,7 @@ public class GameView{ public void createGame(){ gameFrame = new JFrame("FaultInOurPong"); gameFrame.setContentPane(ponggame); - gameFrame.setSize(700,500); + gameFrame.setSize(FRAMEWIDTH,FRAMEHEIGHT); gameFrame.setResizable(false); gameFrame.setLocationRelativeTo(null); gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -57,14 +63,6 @@ public class GameView{ return gameFrame; } - public int getFrameWidth(){ - return gameFrame.getWidth(); - } - - public int getFrameHeight(){ - return gameFrame.getHeight(); - } - // TODO: display a dialogue after successfully saving game records (high score) public void noFileAvailMessage(){ @@ -80,4 +78,12 @@ public class GameView{ public void tutorialPage(ImageIcon img){ tutorial = new Tutorial(img); } + + public int getFrameWidth(){ + return FRAMEWIDTH; + } + + public int getFrameHeight(){ + return FRAMEHEIGHT; + } } \ No newline at end of file diff --git a/src/view/Mode.java b/src/view/Mode.java index 6d645b1..45bf202 100644 --- a/src/view/Mode.java +++ b/src/view/Mode.java @@ -19,11 +19,18 @@ public class Mode extends JFrame{ private JPanel buttonPanel; public Mode(){ + /* + * - Set the header of the window + * - Set the size of the window + */ super("FaultInOurPong"); this.setSize(700,500); this.setResizable(false); this.setLocationRelativeTo(null); - + + /* + * Add buttons on the window + */ buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.Y_AXIS)); diff --git a/src/view/Welcome.java b/src/view/Welcome.java index ce85b6d..76a1cbe 100644 --- a/src/view/Welcome.java +++ b/src/view/Welcome.java @@ -16,11 +16,18 @@ public class Welcome extends JFrame { public Welcome(){ + /* + * - Set the header of the window + * - Set the size of the window + */ super("FaultInOurPong"); this.setSize(700,500); this.setResizable(false); this.setLocationRelativeTo(null); + /* + * Add buttons on the window + */ buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.Y_AXIS)); -- GitLab