From d290e1c18f37edfb673369858ec6c2d9506574cd Mon Sep 17 00:00:00 2001 From: Jean Luo <luoj3@mcmaster.ca> Date: Sat, 22 Oct 2016 11:46:15 -0400 Subject: [PATCH] MVC model created for redevelopment --- Game_Code/src/Game.java | 156 ++++++++++++++ Game_Code/src/original_code/Game.java | 155 ++++++++++++++ Game_Code/src/original_code/Main.java | 23 +++ Game_Code/src/redevelopedCode/Ball.java | 22 ++ Game_Code/src/redevelopedCode/Players.java | 73 +++++++ .../src/redevelopedCode/Pong_controller.java | 71 +++++++ Game_Code/src/redevelopedCode/Pong_model.java | 39 ++++ Game_Code/src/redevelopedCode/Pong_view.java | 191 ++++++++++++++++++ 8 files changed, 730 insertions(+) create mode 100644 Game_Code/src/Game.java create mode 100644 Game_Code/src/original_code/Game.java create mode 100644 Game_Code/src/original_code/Main.java create mode 100644 Game_Code/src/redevelopedCode/Ball.java create mode 100644 Game_Code/src/redevelopedCode/Players.java create mode 100644 Game_Code/src/redevelopedCode/Pong_controller.java create mode 100644 Game_Code/src/redevelopedCode/Pong_model.java create mode 100644 Game_Code/src/redevelopedCode/Pong_view.java diff --git a/Game_Code/src/Game.java b/Game_Code/src/Game.java new file mode 100644 index 0000000..2845be4 --- /dev/null +++ b/Game_Code/src/Game.java @@ -0,0 +1,156 @@ + +import java.awt.*; +import java.awt.event.*; +import java.awt.geom.Ellipse2D; +import java.awt.geom.Rectangle2D; +import java.util.HashMap; +import java.util.HashSet; + +import javax.swing.*; + + +public class Game extends JPanel implements KeyListener, ActionListener { + + private int height, width; // the height and width of the screen + private Timer t = new Timer(1, this); // the 't' variable makes sure that there is a initial delay before the same starts off + private boolean first; //game state (starting/playing) + + private HashSet<String> keys = new HashSet<String>(); + + // pad + private final int SPEED = 1; //the speed of the paddles + private int padH = 10, padW = 40; // paddle width/height + private int bottomPadX, topPadX; // these represent the top and the bottom paddles in the game + private int inset = 10; // this helps determine the distance between the paddle + // - and the top and bottom screen boundaries + + // ball + private double ballX, ballY, velX = 1, velY = 1, ballSize = 20; //ball position, ball velocity, ball size + + // score + private int scoreTop, scoreBottom; // keep track of game record + + public Game() { + addKeyListener(this); // it helps to read the commands given through the keyboard + setFocusable(true); // keylisterer knows that it needs to look for the movement through keys + setFocusTraversalKeysEnabled(false); // since the argument is set to false, it moves the focus away from tab and shift keys + first = true; // sets the game state to true to start playing + t.setInitialDelay(100); // sets initial delay for the movement of the ball + t.start(); // set the delay for every movement of the ball + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; // create an object for graphics (rectangles) + height = getHeight(); // get game frame/screen size + width = getWidth(); + + // initial positioning + if (first) { + bottomPadX = width / 2 - padW / 2; // setups for the paddles positions - in the middle of the screen + topPadX = bottomPadX; + ballX = width / 2 - ballSize / 2; // setups for the ball positions - in the middle of the screen + ballY = height / 2 - ballSize / 2; + first = false; // setup completed + } + + // bottom pad + Rectangle2D bottomPad = new Rectangle(bottomPadX, height - padH - inset, padW, padH); // creating the object for bottom paddle + g2d.fill(bottomPad); + + // top pad + Rectangle2D topPad = new Rectangle(topPadX, inset, padW, padH); // creating paddle object for the top paddle + g2d.fill(topPad); + + // ball + Ellipse2D ball = new Ellipse2D.Double(ballX, ballY, ballSize, ballSize); // creating the ball object for the game + g2d.fill(ball); + + // scores + String scoreB = "Bottom: " + new Integer(scoreBottom).toString(); // saving the score of the bottom paddle + String scoreT = "Top: " + new Integer(scoreTop).toString(); // saving the score of the top paddle + g2d.drawString(scoreB, 10, height / 2); // printing the score of the bottom paddle in the screen + g2d.drawString(scoreT, width - 50, height / 2); // printing the score of the top paddle in the screen + } + + @Override + public void actionPerformed(ActionEvent e) { + // side walls + if (ballX < 0 || ballX > width - 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; + } + + if (ballY + ballSize > height) { // similarly, for the vertical position of bottom paddle + velY = -velY; + ++ scoreTop; // points are incremented if the paddle is missed by opponent + } + // bottom pad + if (ballY + ballSize >= height - padH - inset && velY > 0) // similar to the previous parts + if (ballX + ballSize >= bottomPadX && ballX <= bottomPadX + padW) + velY = -velY; + + // top pad + if (ballY <= padH + inset && velY < 0) + if (ballX + ballSize >= topPadX && ballX <= topPadX + padW) + velY = -velY; + + ballX += velX; + ballY += velY; + + // pressed keys + if (keys.size() == 1) { + if (keys.contains("LEFT")) { // left key is pressed + bottomPadX -= (bottomPadX > 0) ? SPEED : 0; // move the bottom paddle to the left + } + else if (keys.contains("RIGHT")) { // right key is pressed + bottomPadX += (bottomPadX < width - padW) ? SPEED : 0; + } + } + + // AI + double delta = ballX - topPadX; + if (delta > 0) { // move right if ball is to the right of the paddle + topPadX += (topPadX < width - padW) ? SPEED : 0; + } + else if (delta < 0) { // move left if ball is to the left of the paddle + topPadX -= (topPadX > 0) ? SPEED : 0; + } + + repaint(); + } + + @Override + public void keyTyped(KeyEvent e) {} + + @Override + public void keyPressed(KeyEvent e) { + int code = e.getKeyCode(); // get which key is pressed + switch (code) { + case KeyEvent.VK_LEFT: + keys.add("LEFT"); + break; + case KeyEvent.VK_RIGHT: + keys.add("RIGHT"); + break; + } + } + + @Override + public void keyReleased(KeyEvent e) { + int code = e.getKeyCode(); // get which key is released + switch (code) { + case KeyEvent.VK_LEFT: + keys.remove("LEFT"); + break; + case KeyEvent.VK_RIGHT: + keys.remove("RIGHT"); + break; + } + } +} diff --git a/Game_Code/src/original_code/Game.java b/Game_Code/src/original_code/Game.java new file mode 100644 index 0000000..cc3b5b6 --- /dev/null +++ b/Game_Code/src/original_code/Game.java @@ -0,0 +1,155 @@ +package original_code; +import java.awt.*; +import java.awt.event.*; +import java.awt.geom.Ellipse2D; +import java.awt.geom.Rectangle2D; +import java.util.HashMap; +import java.util.HashSet; + +import javax.swing.*; + + +public class Game extends JPanel implements KeyListener, ActionListener { + + private int height, width; + private Timer t = new Timer(5, this); + private boolean first; + + private HashSet<String> keys = new HashSet<String>(); + + // pad + private final int SPEED = 1; + private int padH = 10, padW = 40; + private int bottomPadX, topPadX; + private int inset = 10; + + // ball + private double ballX, ballY, velX = 1, velY = 1, ballSize = 20; + + // score + private int scoreTop, scoreBottom; + + public Game() { + addKeyListener(this); + setFocusable(true); + setFocusTraversalKeysEnabled(false); + first = true; + t.setInitialDelay(100); + t.start(); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + height = getHeight(); + width = getWidth(); + + // initial positioning + if (first) { + bottomPadX = width / 2 - padW / 2; + topPadX = bottomPadX; + ballX = width / 2 - ballSize / 2; + ballY = height / 2 - ballSize / 2; + first = false; + } + + // bottom pad + Rectangle2D bottomPad = new Rectangle(bottomPadX, height - padH - inset, padW, padH); + g2d.fill(bottomPad); + + // top pad + Rectangle2D topPad = new Rectangle(topPadX, inset, padW, padH); + g2d.fill(topPad); + + // ball + Ellipse2D ball = new Ellipse2D.Double(ballX, ballY, ballSize, ballSize); + g2d.fill(ball); + + // scores + String scoreB = "Bottom: " + new Integer(scoreBottom).toString(); + String scoreT = "Top: " + new Integer(scoreTop).toString(); + g2d.drawString(scoreB, 10, height / 2); + g2d.drawString(scoreT, width - 50, height / 2); + } + + @Override + public void actionPerformed(ActionEvent e) { + // side walls + if (ballX < 0 || ballX > width - ballSize) { + velX = -velX; + } + // top / down walls + if (ballY < 0) { + velY = -velY; + ++ scoreBottom; + } + + if (ballY + ballSize > height) { + velY = -velY; + ++ scoreTop; + } + // bottom pad + if (ballY + ballSize >= height - padH - inset && velY > 0) + if (ballX + ballSize >= bottomPadX && ballX <= bottomPadX + padW) + velY = -velY; + + // top pad + if (ballY <= padH + inset && velY < 0) + if (ballX + ballSize >= topPadX && ballX <= topPadX + padW) + velY = -velY; + + ballX += velX; + ballY += velY; + + // pressed keys + if (keys.size() == 1) { + if (keys.contains("LEFT")) { + bottomPadX -= (bottomPadX > 0) ? SPEED : 0; + } + else if (keys.contains("RIGHT")) { + bottomPadX += (bottomPadX < width - padW) ? SPEED : 0; + } + } + + // AI + double delta = ballX - topPadX; + if (delta > 0) { + topPadX += (topPadX < width - padW) ? SPEED : 0; + } + else if (delta < 0) { + topPadX -= (topPadX > 0) ? SPEED : 0; + } + + repaint(); + } + + @Override + public void keyTyped(KeyEvent e) {} + + @Override + public void keyPressed(KeyEvent e) { + int code = e.getKeyCode(); + switch (code) { + case KeyEvent.VK_LEFT: + keys.add("LEFT"); + break; + case KeyEvent.VK_RIGHT: + keys.add("RIGHT"); + break; + } + } + + @Override + public void keyReleased(KeyEvent e) { + int code = e.getKeyCode(); + switch (code) { + case KeyEvent.VK_LEFT: + keys.remove("LEFT"); + break; + case KeyEvent.VK_RIGHT: + keys.remove("RIGHT"); + break; + } + } +} diff --git a/Game_Code/src/original_code/Main.java b/Game_Code/src/original_code/Main.java new file mode 100644 index 0000000..5b5ea9c --- /dev/null +++ b/Game_Code/src/original_code/Main.java @@ -0,0 +1,23 @@ +package original_code; +import java.awt.BorderLayout; + +import javax.swing.JFrame; + + +public class Main { + + /** + * @param args + */ + public static void main(String[] args) { + JFrame frm = new JFrame(); + frm.setTitle("Pong"); + Game g = new Game(); + frm.setContentPane(g); + frm.setSize(300, 700); + frm.setResizable(false); + frm.setVisible(true); + frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + +} diff --git a/Game_Code/src/redevelopedCode/Ball.java b/Game_Code/src/redevelopedCode/Ball.java new file mode 100644 index 0000000..df8598b --- /dev/null +++ b/Game_Code/src/redevelopedCode/Ball.java @@ -0,0 +1,22 @@ +package redevelopedCode; + +public class Ball { + + private int positionX; + private int positionY; + + public Ball(int x, int y){ + positionX = x; + positionY = y; + } + + public void setPosition(int x, int y){ + positionX = x; + positionY = y; + } + + public int[] getPosition(){ + int[] temp = {positionX,positionY}; + return temp; + } +} diff --git a/Game_Code/src/redevelopedCode/Players.java b/Game_Code/src/redevelopedCode/Players.java new file mode 100644 index 0000000..ab31458 --- /dev/null +++ b/Game_Code/src/redevelopedCode/Players.java @@ -0,0 +1,73 @@ +package redevelopedCode; + +public class Players { + + // Constant declaration for number of life + private final int LIFE = 3; + private final int NOLIFE = 0; + + // Variable declaration that store the player score + private int playerScore; + private int paddlePositionX; + private int paddlePositionY; + + /** + * Constructor for the player - set the score to be 0 initially + */ + public Players(){ + playerScore = LIFE; + //TODO: POSITION X, POSITION Y + + } + + /** + * Decrease number of life of the player + */ + public void decrementLife(){ + playerScore--; + } + + /** + * Increase score for a player + */ + public void incrementScore(){ + playerScore++; + } + + /** + * Get the score of a player + * @return playerScore - return the score of the player + */ + public int getScore(){ + return playerScore; + } + + /** + * Save the paddle positions + * @param x - horizontal position of the paddle + * @param y - vertical position of the paddle + */ + public void setPosition(int x, int y){ + paddlePositionX = x; + paddlePositionY = y; + } + + /** + * Get the paddle positions + * @return - horizontal and vertical positions in an array + */ + public int[] getPosition(){ + int[] temp = {paddlePositionX,paddlePositionY}; + return temp; + } + + /** + * Check whether the player loses the game or not + * @return - boolean indicating losing or not + */ + public boolean checkLoss(){ + if(playerScore==NOLIFE){ return true;} + else{ return false; } + } + +} diff --git a/Game_Code/src/redevelopedCode/Pong_controller.java b/Game_Code/src/redevelopedCode/Pong_controller.java new file mode 100644 index 0000000..3d9d2de --- /dev/null +++ b/Game_Code/src/redevelopedCode/Pong_controller.java @@ -0,0 +1,71 @@ +package redevelopedCode; + +import java.io.*; + +public class Pong_controller { + + public Pong_model model; + public static Pong_view viewer; + + public Pong_controller(){ + model = new Pong_model(); + + } + + public static void main(String[] args){ + Pong_controller controller = new Pong_controller(); + viewer = new Pong_view(controller); + } + + + public void loadGame(){ + + try{ + FileReader fr = new FileReader("savedScore.txt"); + BufferedReader br = new BufferedReader(fr); + + int score1 = Integer.parseInt(br.readLine()); + int score2 = Integer.parseInt(br.readLine()); + + + + // TODO + + br.close(); + } catch(Exception e){ + e.printStackTrace(); + } + + } + + public void saveGame(){ + + try{ + FileWriter fw = new FileWriter("savedScore.txt"); + BufferedWriter bw = new BufferedWriter(fw); + + bw.write(model.player1.getScore()); + bw.write(model.player2.getScore()); + + // TODO + + bw.close(); + } catch(Exception e){ + e.printStackTrace(); + } + + } + + public int gameOver(){ + if(model.player1.getScore()==0){ + return 1; + } + else if(model.player2.getScore()==0){ + return 2; + } + else{ + return -1; + } + } + +} diff --git a/Game_Code/src/redevelopedCode/Pong_model.java b/Game_Code/src/redevelopedCode/Pong_model.java new file mode 100644 index 0000000..0455df8 --- /dev/null +++ b/Game_Code/src/redevelopedCode/Pong_model.java @@ -0,0 +1,39 @@ +package redevelopedCode; + +public class Pong_model{ + + + + private int score1; + private int score2; + public Players player1; + public Players player2; + + public Pong_model(){ + + player1 = new Players(); + player2 = new Players(); + score1 = player1.getScore(); + score2 = player2.getScore(); + + } + + public void changeSpeed(int newSpeed){ + + } + + public String readTutorial(){ + String content = ""; + + return content; + } + + public boolean createObstacle(boolean gameMode){ + if(gameMode==true){ return true;} + else{ return false; } + } + + + + +} \ No newline at end of file diff --git a/Game_Code/src/redevelopedCode/Pong_view.java b/Game_Code/src/redevelopedCode/Pong_view.java new file mode 100644 index 0000000..55d73ed --- /dev/null +++ b/Game_Code/src/redevelopedCode/Pong_view.java @@ -0,0 +1,191 @@ +package redevelopedCode; + +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.geom.Ellipse2D; +import java.awt.geom.Rectangle2D; +import java.util.HashSet; + +import javax.swing.*; + + +public class Pong_view extends JFrame{ + + private Pong_controller controller; + private JButton start = new JButton("Start New Game"); + private JButton load = new JButton("Load Game"); + private JButton highScores = new JButton("High Scores"); + private JButton tutorial = new JButton("Tutorial"); + private JButton exit = new JButton("Exit"); + private JButton single = new JButton("Single Player Mode"); + private JButton sObstacle = new JButton("Advanced Single Player Mode"); + private JButton multi = new JButton("Multiplayer Mode"); + + private JPanel buttonPanel = new JPanel(); + + public Pong_view(Pong_controller c){ + super("FaultInOurPong"); + controller = c; + initializeFrame(); + } + + private void initializeFrame(){ + + this.setSize(700,500); + this.setResizable(false); + + buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.Y_AXIS)); + + buttonPanel.add(Box.createVerticalGlue()); + addButton(start); + addButton(load); + addButton(highScores); + addButton(tutorial); + addButton(exit); + buttonPanel.add(Box.createVerticalGlue()); + + start.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + start(); + } + }); + + load.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + // TODO: + //load(); + + } + }); + + highScores.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + // TODO: + //highScores(); + } + }); + + tutorial.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + // TODO: + //tutorial(); + } + }); + + exit.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + System.exit(0); + } + }); + + add(buttonPanel); + + this.setDefaultCloseOperation(EXIT_ON_CLOSE); + this.setLocationRelativeTo(null); + this.setVisible(true); + } + + public void addButton(JButton x) { + x.setMaximumSize(sObstacle.getPreferredSize()); + x.setAlignmentY(CENTER_ALIGNMENT); + x.setAlignmentX(CENTER_ALIGNMENT); + buttonPanel.add(x); + buttonPanel.add(Box.createVerticalStrut(20)); + } + + public void start() { + buttonPanel.removeAll(); + this.getContentPane().removeAll(); + this.repaint(); + + buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.Y_AXIS)); + + buttonPanel.add(Box.createVerticalGlue()); + addButton(single); + addButton(sObstacle); + buttonPanel.add(Box.createVerticalGlue()); + + add(buttonPanel); + this.setVisible(true); + + single.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + // TODO: + single(); + } + }); + + } + + + private int height, width; // the height and width of the screen + //private Timer t = new Timer(1, this); // the 't' variable makes sure that there is a initial delay before the same starts off + private boolean first=true; //game state (starting/playing) + private HashSet<String> keys = new HashSet<String>(); + // pad + private final int SPEED = 1; //the speed of the paddles + private int padH = 10, padW = 40; // paddle width/height + private int bottomPadX, topPadX; // these represent the top and the bottom paddles in the game + private int inset = 10; // this helps determine the distance between the paddle + // - and the top and bottom screen boundaries + // ball + private double ballX, ballY, velX = 1, velY = 1, ballSize = 20; //ball position, ball velocity, ball size + // score + private int scoreTop, scoreBottom; // keep track of game record + + public void single(){ + buttonPanel.removeAll(); + this.remove(buttonPanel); + + JPanel game_panel = new JPanel(){ + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; // create an object for graphics (rectangles) + + height = getHeight(); // get game frame/screen size + width = getWidth(); + + // initial positioning + if (first) { + bottomPadX = width / 2 - padW / 2; // setups for the paddles positions - in the middle of the screen + topPadX = bottomPadX; + ballX = width / 2 - ballSize / 2; // setups for the ball positions - in the middle of the screen + ballY = height / 2 - ballSize / 2; + first = false; // setup completed + } + + // bottom pad + Rectangle2D bottomPad = new Rectangle(bottomPadX, height - padH - inset, padW, padH); // creating the object for bottom paddle + g2d.fill(bottomPad); + + // top pad + Rectangle2D topPad = new Rectangle(topPadX, inset, padW, padH); // creating paddle object for the top paddle + g2d.fill(topPad); + + // ball + Ellipse2D ball = new Ellipse2D.Double(ballX, ballY, ballSize, ballSize); // creating the ball object for the game + g2d.fill(ball); + + // scores + String scoreB = "Bottom: " + new Integer(scoreBottom).toString(); // saving the score of the bottom paddle + String scoreT = "Top: " + new Integer(scoreTop).toString(); // saving the score of the top paddle + g2d.drawString(scoreB, 10, height / 2); // printing the score of the bottom paddle in the screen + g2d.drawString(scoreT, width - 50, height / 2); // printing the score of the top paddle in the screen + + + } + }; + + this.add(game_panel); + this.revalidate(); + this.repaint(); + + + } + + + +} -- GitLab