diff --git a/src/model/Ball.java b/src/model/Ball.java index 6ce1d0c89532be84c330f5d19ef95db3b724088d..26e92a01ca7ce519b9a9bcd1e53f85e579d36539 100644 --- a/src/model/Ball.java +++ b/src/model/Ball.java @@ -1,5 +1,11 @@ package model; +/** + * This class is the model of the ball object. + * It contains all the information for a ball. + * @author Pongthusiastics + */ + public class Ball { private int positionX; diff --git a/src/model/GameModel.java b/src/model/GameModel.java index b59dda0679e20dace394a300fd830f3014f3daba..4084a408290f8d47032c6f0163f51e671392db39 100644 --- a/src/model/GameModel.java +++ b/src/model/GameModel.java @@ -20,6 +20,11 @@ public class GameModel { return b1; } + public void setBall(int x, int y){ + b1.setPositionX(x); + b1.setPositionY(y); + } + public Paddle getPlayerPaddle(){ return p_player; } diff --git a/src/startGame/GameController.java b/src/startGame/GameController.java index 9c58b97f71efb81249fa496c63af965bbc68b521..47a0248492203eabefc092d17867fec2678cb777 100644 --- a/src/startGame/GameController.java +++ b/src/startGame/GameController.java @@ -4,8 +4,11 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import java.io.BufferedReader; +import java.io.FileReader; import java.util.HashSet; +import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.Timer; @@ -18,6 +21,7 @@ public class GameController{ private GameModel m; private Welcome w; private Mode mode; + private Tutorial tut; private HashSet<String> keys = new HashSet<String>(); @@ -27,7 +31,7 @@ public class GameController{ private int velX=1, velY=1; private int padWidth = 80, padHeight = 10; - private int bottomPadX, topPadX; + private int bottomPadX, bottomPadY, topPadX, topPadY; private int ballX, ballY, ballSize=20; private int scoreTop, scoreBottom; private int inset; @@ -44,16 +48,19 @@ public class GameController{ mode = this.v.getmode(); mode.addListener(new ModeListener()); - //game = this.v.getGame(); - //game.addListener(new GameListener()); + ImageIcon image = new ImageIcon("./Resources/tutorial.png"); + v.tutorialPage(image); + tut = v.getTutorial(); + tut.addListener(new TutorialListener()); gameFrame = this.v.getGameFrame(); gameDisplay = this.v.getGame(); - //gameDisplay.addListener(new GameListener()); gameDisplay.addKeyListener(new GameListener()); gameDisplay.setFocusable(true); gameDisplay.setFocusTraversalKeysEnabled(false); + + } class WelcomepageListener implements ActionListener{ @@ -67,8 +74,39 @@ public class GameController{ mode.setVisible(true); w.setVisible(false); - } -// else if() + }else if(source==w.load()){ + //TODO + try{ + FileReader fr = new FileReader("./Resources/userData.txt"); + BufferedReader br = new BufferedReader(fr); + + System.out.println("can load data"); + + br.close(); + }catch(Exception exp){ + v.cannotLoadMessage(); + } + }else if(source==w.highScores()){ + //TODO + try{ + FileReader fr = new FileReader("./Resources/gameScore.txt"); + BufferedReader br = new BufferedReader(fr); + + System.out.println("can display high score"); + + br.close(); + }catch(Exception exp){ + v.noFileAvailMessage(); + } + }else if(source==w.tutorial()){ + //TODO + + w.setVisible(false); + tut.setVisible(true); + + }else if(source==w.exit()){ + System.exit(0); + } } @@ -76,7 +114,6 @@ public class GameController{ class ModeListener implements ActionListener{ - @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub @@ -88,82 +125,72 @@ public class GameController{ } } + } + + class TutorialListener implements ActionListener{ + + @Override + public void actionPerformed(ActionEvent e) { + // TODO Auto-generated method stub + Object source = e.getSource(); + + if(source == tut.getBack()){ + tut.setVisible(false); + w.setVisible(true); + } + } } class GameListener implements ActionListener, KeyListener{ GameListener(){ - bottomPadX = gameDisplay.getBottom(); + 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); } @Override public void actionPerformed(ActionEvent e) { // X-direction - if(ballX< 0 || ballX > frameWidth-ballSize){ + if(ballX< 0 || ballX > frameWidth-1.5*ballSize){ velX = -velX; } // Y-direction if(ballY < 0){ -System.out.println("touch top border"); velY = -velY; ++scoreBottom; - } else if(ballY+ballSize>frameHeight){ -System.out.println("touch bottom border"); + gameDisplay.setBottomScore(scoreBottom); + } else if(ballY+2.5*ballSize>frameHeight){ velY = -velY; ++scoreTop; - } else if(ballY+ballSize>frameHeight-inset-padHeight && velY > 0 && ballX + ballSize >= bottomPadX && ballX <= bottomPadX + padWidth){ -System.out.println("touch bottom pad"); + gameDisplay.setTopScore(scoreTop); + } else if(ballY+2.5*ballSize>frameHeight-inset-2*padHeight && velY > 0 && ballX + ballSize >= bottomPadX && ballX <= bottomPadX + padWidth){ velY = -velY; - } else if(ballY+ballSize<=ballSize+inset+padHeight && velY < 0 && ballX + ballSize >= topPadX && ballX <= topPadX + padWidth){ -System.out.println("touch top pad"); + } else if(ballY<=inset+2*padHeight && velY < 0 && ballX + ballSize >= topPadX && ballX <= topPadX + padWidth){ velY = -velY; } -/* - // side walls - if (ballX < 0 || ballX > frameWidth - ballSize) { // making sure ball is horizontally within the frame (left&right) - velX = -velX; // reverse the ball position if trying to go out - } - // top / down walls - if (ballY < 0) { // similarly, for vertical position - velY = -velY; // reverse the ball position vertically - ++ scoreBottom; - System.out.println("bottom: "+scoreBottom); - gameDisplay.setBottomScore(scoreBottom); - } - - if (ballY + ballSize > frameHeight - ballSize) { // similarly, for the vertical position of bottom paddle - velY = -velY; - ++ scoreTop; // points are incremented if the paddle is missed by opponent -System.out.println("top: "+scoreTop); - gameDisplay.setTopScore(scoreTop); - } - // bottom pad - if (ballY + ballSize>= frameHeight - padHeight - inset && velY > 0) // similar to the previous parts - if (ballX + ballSize >= bottomPadX && ballX <= bottomPadX + padWidth) - velY = -velY; - - // top pad - if (ballY <= padHeight + inset && velY < 0) - if (ballX + ballSize >= topPadX && ballX <= topPadX + padWidth) - velY = -velY; -*/ ballX += velX; ballY += velY; gameDisplay.setBall(ballX,ballY); - // pressed keys if (keys.size() == 1) { if (keys.contains("LEFT")) { // left key is pressed @@ -240,6 +267,8 @@ System.out.println("top: "+scoreTop); } - + public void display(){ + v.display(); + } } diff --git a/src/startGame/PongGame.java b/src/startGame/PongGame.java index b28114d112205212b0b0ebbbc6af197977aee4e7..1ad3c6bcd1890fc4013cf9bfe8a55abb4293cfec 100644 --- a/src/startGame/PongGame.java +++ b/src/startGame/PongGame.java @@ -14,7 +14,7 @@ public class PongGame { GameModel model = new GameModel(1,1,2,2,2,2); GameController controller = new GameController(view, model); - view.display(); + controller.display(); } } diff --git a/src/view/GameView.java b/src/view/GameView.java index b8a4bcf5247510b2bab0656f7709144ddcaf6023..8c33ad52c32d3d235bb886aad8f1277177f9ff95 100644 --- a/src/view/GameView.java +++ b/src/view/GameView.java @@ -1,6 +1,6 @@ package view; -import javax.swing.JFrame; +import javax.swing.*; public class GameView{ @@ -17,7 +17,6 @@ public class GameView{ welcome = new Welcome(); mode = new Mode(); ponggame = new PongGameDisplay(); - tutorial = new Tutorial(); createGame(); } @@ -43,6 +42,8 @@ public class GameView{ return tutorial; } + + //TODO: ADD PANEL FOR OPTIONS IN THE GAME public void createGame(){ gameFrame = new JFrame("FaultInOurPong"); gameFrame.setContentPane(ponggame); @@ -64,5 +65,19 @@ public class GameView{ return gameFrame.getHeight(); } + // TODO: display a dialogue after successfully saving game records (high score) + + public void noFileAvailMessage(){ + JFrame errorFrame = new JFrame("Error"); + JOptionPane.showMessageDialog(errorFrame, "No record available!"); + } + + public void cannotLoadMessage(){ + JFrame errorFrame = new JFrame("Error"); + JOptionPane.showMessageDialog(errorFrame, "The record is either damaged or not available, please start a new game!"); + } + public void tutorialPage(ImageIcon img){ + tutorial = new Tutorial(img); + } } \ No newline at end of file diff --git a/src/view/PongGameDisplay.java b/src/view/PongGameDisplay.java index 16f7c7f74df8ceeab29c7abdcdb3b21c3b75289f..0711c493f80157f59c8beec1d2178d0fa155200b 100644 --- a/src/view/PongGameDisplay.java +++ b/src/view/PongGameDisplay.java @@ -78,19 +78,7 @@ public class PongGameDisplay extends JPanel{ g2d.drawString(scoreB, 10, frameHeight / 2); // printing the score of the bottom paddle in the screen g2d.drawString(scoreT, frameWidth - 50, frameHeight / 2); // printing the score of the top paddle in the screen } - - -/* - public void addListener(ActionListener listener){ - //ballX = 40; - //ballY = 40; - //System.out.println("keylistener"); - repaint(); - } -*/ - - public void setBall(int x, int y){ ballX = x; ballY = y; @@ -104,10 +92,14 @@ public class PongGameDisplay extends JPanel{ topPadX = x; } - public int getBottom(){ + public int getBottomX(){ return bottomPadX; } + public int getBottomY(){ + return bottomPadY; + } + public void setTopScore(int s){ scoreTop=s; } @@ -116,4 +108,11 @@ public class PongGameDisplay extends JPanel{ scoreBottom = s; } + public int getBallX(){ + return ballX; + } + + public int getBallY(){ + return ballY; + } } diff --git a/src/view/Tutorial.java b/src/view/Tutorial.java index 70d03e60ca8ca0b73273591c0c3575733d547877..171243454635c053c2db3d918fe756651761a5fa 100644 --- a/src/view/Tutorial.java +++ b/src/view/Tutorial.java @@ -1,8 +1,35 @@ package view; -public class Tutorial { +import java.awt.event.ActionListener; - public Tutorial(){ +import javax.swing.ImageIcon; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; + +public class Tutorial extends JFrame{ + + private JButton back; + + public Tutorial(ImageIcon img){ + super("FaultInOurPong - Tutorial"); + this.setSize(700,500); + this.setResizable(false); + this.setLocationRelativeTo(null); + + this.add(new JLabel(img)); + back = new JButton("Back"); + //this.add(back); + + this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + + public JButton getBack(){ + return back; + } + + public void addListener(ActionListener listener){ + back.addActionListener(listener); } } diff --git a/src/view/Welcome.java b/src/view/Welcome.java index cb0a2b97bcc06ca5b5b42b9e4cbd2002e6beb95c..ce85b6df66ada96471876d2618828095480ec75d 100644 --- a/src/view/Welcome.java +++ b/src/view/Welcome.java @@ -42,6 +42,22 @@ public class Welcome extends JFrame { return start; } + public JButton load(){ + return load; + } + + public JButton highScores(){ + return highScores; + } + + public JButton tutorial(){ + return tutorial; + } + + public JButton exit(){ + return exit; + } + public void addButton(JButton x) { x.setMaximumSize(start.getPreferredSize()); x.setAlignmentY(CENTER_ALIGNMENT);