From 53832b52c7f9373bbd7aa809730e6a6e1d5c978a Mon Sep 17 00:00:00 2001 From: Jean Luo <luoj3@mcmaster.ca> Date: Thu, 10 Nov 2016 15:46:45 -0500 Subject: [PATCH] bug fixed for ball and paddle positions --- Game_Code/html/index.html | 98 ++++++++++++++ Game_Code/src/model/Ball.java | 6 +- Game_Code/src/model/GameModel.java | 8 +- Game_Code/src/model/Paddle.java | 6 +- Game_Code/src/model/Player.java | 2 - Game_Code/src/startGame/GameController.java | 137 +++++++++++++++----- Game_Code/src/startGame/PongGame.java | 2 +- Game_Code/src/view/GameView.java | 18 ++- Game_Code/src/view/PongGameDisplay.java | 9 +- 9 files changed, 235 insertions(+), 51 deletions(-) create mode 100644 Game_Code/html/index.html diff --git a/Game_Code/html/index.html b/Game_Code/html/index.html new file mode 100644 index 0000000..497322e --- /dev/null +++ b/Game_Code/html/index.html @@ -0,0 +1,98 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml"> +<head> +<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> +<meta http-equiv="X-UA-Compatible" content="IE=9"/> +<meta name="generator" content="Doxygen 1.8.12"/> +<meta name="viewport" content="width=device-width, initial-scale=1"/> +<title>FaultInOurPong: Main Page</title> +<link href="tabs.css" rel="stylesheet" type="text/css"/> +<script type="text/javascript" src="jquery.js"></script> +<script type="text/javascript" src="dynsections.js"></script> +<link href="navtree.css" rel="stylesheet" type="text/css"/> +<script type="text/javascript" src="resize.js"></script> +<script type="text/javascript" src="navtreedata.js"></script> +<script type="text/javascript" src="navtree.js"></script> +<script type="text/javascript"> + $(document).ready(initResizable); +</script> +<link href="search/search.css" rel="stylesheet" type="text/css"/> +<script type="text/javascript" src="search/searchdata.js"></script> +<script type="text/javascript" src="search/search.js"></script> +<link href="doxygen.css" rel="stylesheet" type="text/css" /> +</head> +<body> +<div id="top"><!-- do not remove this div, it is closed by doxygen! --> +<div id="titlearea"> +<table cellspacing="0" cellpadding="0"> + <tbody> + <tr style="height: 56px;"> + <td id="projectalign" style="padding-left: 0.5em;"> + <div id="projectname">FaultInOurPong +  <span id="projectnumber">1</span> + </div> + </td> + </tr> + </tbody> +</table> +</div> +<!-- end header part --> +<!-- Generated by Doxygen 1.8.12 --> +<script type="text/javascript"> +var searchBox = new SearchBox("searchBox", "search",false,'Search'); +</script> +<script type="text/javascript" src="menudata.js"></script> +<script type="text/javascript" src="menu.js"></script> +<script type="text/javascript"> +$(function() { + initMenu('',true,false,'search.php','Search'); + $(document).ready(function() { init_search(); }); +}); +</script> +<div id="main-nav"></div> +</div><!-- top --> +<div id="side-nav" class="ui-resizable side-nav-resizable"> + <div id="nav-tree"> + <div id="nav-tree-contents"> + <div id="nav-sync" class="sync"></div> + </div> + </div> + <div id="splitbar" style="-moz-user-select:none;" + class="ui-resizable-handle"> + </div> +</div> +<script type="text/javascript"> +$(document).ready(function(){initNavTree('index.html','');}); +</script> +<div id="doc-content"> +<!-- window showing the filter options --> +<div id="MSearchSelectWindow" + onmouseover="return searchBox.OnSearchSelectShow()" + onmouseout="return searchBox.OnSearchSelectHide()" + onkeydown="return searchBox.OnSearchSelectKey(event)"> +</div> + +<!-- iframe showing the search results (closed by default) --> +<div id="MSearchResultsWindow"> +<iframe src="javascript:void(0)" frameborder="0" + name="MSearchResults" id="MSearchResults"> +</iframe> +</div> + +<div class="header"> + <div class="headertitle"> +<div class="title">FaultInOurPong Documentation</div> </div> +</div><!--header--> +<div class="contents"> +</div><!-- contents --> +</div><!-- doc-content --> +<!-- start footer part --> +<div id="nav-path" class="navpath"><!-- id is needed for treeview function! --> + <ul> + <li class="footer">Generated by + <a href="http://www.doxygen.org/index.html"> + <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.12 </li> + </ul> +</div> +</body> +</html> diff --git a/Game_Code/src/model/Ball.java b/Game_Code/src/model/Ball.java index b050283..dcccf5a 100644 --- a/Game_Code/src/model/Ball.java +++ b/Game_Code/src/model/Ball.java @@ -36,9 +36,9 @@ public class Ball { * @param y * is the y-position */ - public Ball(int x, int y) { - positionX = x; - positionY = y; + public Ball() { + positionX = 0; + positionY = 0; } /** diff --git a/Game_Code/src/model/GameModel.java b/Game_Code/src/model/GameModel.java index 4378dec..0cb7a87 100644 --- a/Game_Code/src/model/GameModel.java +++ b/Game_Code/src/model/GameModel.java @@ -40,10 +40,10 @@ public class GameModel { * @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); - p_computer = new Paddle(compX, compY); + public GameModel(){ + b1 = new Ball(); + p_player = new Paddle(); + p_computer = new Paddle(); //TODO player = new Player(); diff --git a/Game_Code/src/model/Paddle.java b/Game_Code/src/model/Paddle.java index 660017a..ed27007 100644 --- a/Game_Code/src/model/Paddle.java +++ b/Game_Code/src/model/Paddle.java @@ -13,9 +13,9 @@ public class Paddle { private int speed; - public Paddle(int x, int y){ - positionX = x; - positionY = y; + public Paddle(){ + positionX = 0; + positionY = 0; } /** diff --git a/Game_Code/src/model/Player.java b/Game_Code/src/model/Player.java index 7c79bc8..5c37713 100644 --- a/Game_Code/src/model/Player.java +++ b/Game_Code/src/model/Player.java @@ -12,8 +12,6 @@ public class Player { public Player(){ score = LIFE; - //TODO: POSITION X, POSITION Y - } /** diff --git a/Game_Code/src/startGame/GameController.java b/Game_Code/src/startGame/GameController.java index 44c4eaf..cf9fc4f 100644 --- a/Game_Code/src/startGame/GameController.java +++ b/Game_Code/src/startGame/GameController.java @@ -26,15 +26,20 @@ public class GameController{ private HashSet<String> keys = new HashSet<String>(); private JFrame gameFrame; - private int frameWidth, frameHeight; + private int frameWidth=700, frameHeight=500; private PongGameDisplay gameDisplay; private int velX=1, velY=1; private int padWidth = 80, padHeight = 10; private int bottomPadX, bottomPadY, topPadX, topPadY; - private int ballX, ballY, ballSize=20; + private Ball b; + private Paddle paddle_player, paddle_ai; + private int ballX, ballY, ballSize; private int scoreTop, scoreBottom; private int inset; + + private Player player; + private Player ai; private Timer t; @@ -42,7 +47,36 @@ public class GameController{ this.v = v; this.m = m; - /* + /** + * Setups for ball in the Model + */ + b = this.m.getBall(); + ballSize = b.getSize(); + ballX = frameWidth / 2 - ballSize / 2; // setups for the ball positions - in the middle of the screen + ballY = frameHeight / 2 - ballSize / 2; + b.setPositionX(ballX); + b.setPositionY(ballY); + + /** + * Setups for the paddles in the Model + */ + bottomPadX = frameWidth / 2 - padWidth / 2; // setups for the paddles positions - in the middle of the screen + topPadX = bottomPadX; + paddle_player = this.m.getPlayerPaddle(); + paddle_player.setPositionX(bottomPadX); + paddle_player.setPositionY(bottomPadY); + + paddle_ai = this.m.getComputerPaddle(); + + /** + * Setups for the players in the Model + */ + player = this.m.getPlayer(); + ai = this.m.getComputer(); + scoreBottom = player.getScore(); + scoreTop = ai.getScore(); + + /** * Setups for the View */ w = this.v.getWelcome(); @@ -62,18 +96,13 @@ 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{ @@ -130,7 +159,7 @@ public class GameController{ } - /* + /** * Actionlistener for the Single-Mode page */ class ModeListener implements ActionListener{ @@ -150,14 +179,13 @@ public class GameController{ } } - /* + /** * Actionlistener for the tutorial page */ class TutorialListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { - // TODO Auto-generated method stub Object source = e.getSource(); if(source == tut.getBack()){ @@ -169,27 +197,22 @@ public class GameController{ } + /** + * ActionListener for the game + */ class GameListener implements ActionListener, KeyListener{ GameListener(){ - - - ballX = gameDisplay.getBallX(); - ballY = gameDisplay.getBallY(); - t = new Timer(5,this); t.setInitialDelay(1000); // sets initial delay for the movement of the ball - - bottomPadX = gameDisplay.getBottomX(); - bottomPadY = gameDisplay.getBottomY(); - - //TODO: MODEL FOR THE PADDLE - } @Override public void actionPerformed(ActionEvent e) { + /** + * Update the velocity/direction of the Ball + */ // X-direction if(ballX< 0 || ballX > frameWidth-1.5*ballSize){ /* @@ -205,19 +228,35 @@ public class GameController{ * 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 */ velY = -velY; - ++scoreBottom; - gameDisplay.setBottomScore(scoreBottom); + --scoreTop; + checkGameOver(); + + /* + * Update model and view + */ + gameDisplay.setTopScore(scoreTop); + player.decrementLife(); + } 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 + * - check game over or not */ velY = -velY; - ++scoreTop; - gameDisplay.setTopScore(scoreTop); + --scoreBottom; + checkGameOver(); + + /* + * Update model and view + */ + gameDisplay.setBottomScore(scoreBottom); + ai.decrementLife(); + } 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 @@ -232,15 +271,20 @@ public class GameController{ velY = -velY; } - /* + /** * Update the ball position by velocity */ ballX += velX; ballY += velY; + /* + * Update the view and model + */ gameDisplay.setBall(ballX,ballY); + b.setPositionX(ballX); + b.setPositionY(ballY); - /* + /** * Detect the key pressed by the user on the keyboard */ if (keys.size() == 1) { @@ -253,7 +297,12 @@ public class GameController{ if(bottomPadX>0) { //TODO: SPEED bottomPadX-=3; + + /* + * Update the view and model + */ gameDisplay.setBottom(bottomPadX); + paddle_player.setPositionX(bottomPadX); } } else if (keys.contains("RIGHT")) { @@ -265,12 +314,17 @@ public class GameController{ */ //TODO: SPEED bottomPadX+=3; + + /* + * Update the view and model + */ gameDisplay.setBottom(bottomPadX); + paddle_player.setPositionX(bottomPadX); } } } - /* + /** * Create actions for the AI paddles */ double delta = ballX - topPadX; @@ -283,7 +337,12 @@ public class GameController{ * - display the movement on the screen */ topPadX +=1; + + /* + * Update the view and the model + */ gameDisplay.setTop(topPadX); + paddle_ai.setPositionX(topPadX); } } else if (delta < 0) { @@ -295,7 +354,12 @@ public class GameController{ * - display the movement on the screen */ topPadX -=1; + + /* + * Update the view and the model + */ gameDisplay.setTop(topPadX); + paddle_ai.setPositionX(topPadX); } } @@ -343,4 +407,15 @@ public class GameController{ v.display(); } + public void checkGameOver(){ + if(scoreBottom==0){ + v.gameOver(0); + } else if(scoreTop==0){ + v.gameOver(1); + } + + //TODO: SAVE RECORD + + } + } diff --git a/Game_Code/src/startGame/PongGame.java b/Game_Code/src/startGame/PongGame.java index 84f064f..55d881f 100644 --- a/Game_Code/src/startGame/PongGame.java +++ b/Game_Code/src/startGame/PongGame.java @@ -14,7 +14,7 @@ public class PongGame { * Initialize the model, view, and controller for the game */ GameView view = new GameView(); - GameModel model = new GameModel(1,1,2,2,2,2); + GameModel model = new GameModel(); GameController controller = new GameController(view, model); /* diff --git a/Game_Code/src/view/GameView.java b/Game_Code/src/view/GameView.java index dc84e01..64c9ebd 100644 --- a/Game_Code/src/view/GameView.java +++ b/Game_Code/src/view/GameView.java @@ -11,6 +11,7 @@ public class GameView{ private final int FRAMEWIDTH = 700; private final int FRAMEHEIGHT = 500; + private final int LIFE=3; private JFrame gameFrame; @@ -22,7 +23,7 @@ public class GameView{ */ welcome = new Welcome(); mode = new Mode(); - ponggame = new PongGameDisplay(); + ponggame = new PongGameDisplay(LIFE); createGame(); } @@ -75,6 +76,21 @@ public class GameView{ JOptionPane.showMessageDialog(errorFrame, "The record is either damaged or not available, please start a new game!"); } + public void gameOver(int whichplayer){ + + if(whichplayer==0){ + JFrame overFrame = new JFrame("Game Over"); + JOptionPane.showMessageDialog(overFrame, "The game is over! The computer wins!"); + } + else{ + JFrame overFrame = new JFrame("Game Over"); + JOptionPane.showMessageDialog(overFrame, "The game is over! The player wins!"); + } + gameFrame.setVisible(false); + welcome.setVisible(true); + + } + public void tutorialPage(ImageIcon img){ tutorial = new Tutorial(img); } diff --git a/Game_Code/src/view/PongGameDisplay.java b/Game_Code/src/view/PongGameDisplay.java index 0711c49..ce66c52 100644 --- a/Game_Code/src/view/PongGameDisplay.java +++ b/Game_Code/src/view/PongGameDisplay.java @@ -30,18 +30,15 @@ public class PongGameDisplay extends JPanel{ - public PongGameDisplay(){ + public PongGameDisplay(int life){ first = true; ballSize = 20; padW = 80; padH = 10; inset = 10; - scoreTop=0; - scoreBottom=0; - - - + scoreTop=life; + scoreBottom=life; } @Override -- GitLab