diff --git a/.F17_lab03.md.swp b/.F17_lab03.md.swp deleted file mode 100644 index 8f2fdf9..0000000 Binary files a/.F17_lab03.md.swp and /dev/null differ diff --git a/src/edu/ucsb/cs56/projects/games/pong/Game.java b/src/edu/ucsb/cs56/projects/games/pong/Game.java index b3bb6a7..a8f9bb8 100644 --- a/src/edu/ucsb/cs56/projects/games/pong/Game.java +++ b/src/edu/ucsb/cs56/projects/games/pong/Game.java @@ -37,7 +37,8 @@ public Game(String title, Dimension dimension) { * @param args not used */ public static void main(String[] args) { - window = new Game("Main Menu",DIMENSION); + Game windowNew = new Game("Main Menu",getDIMENSION()); + setWindow(windowNew); MainMenuUI game = new MainMenuUI(); window.add(game); //Opens the window with a static function that can be called from anywhere @@ -47,4 +48,18 @@ public static void main(String[] args) { public static void setWindowVisibility(boolean visibility){ window.setVisible(visibility); } + + /** setters and getters + */ + public static Dimension getDIMENSION() { + return DIMENSION; + } + + public static Game getWindow() { + return window; + } + + public static void setWindow(Game window) { + Game.window = window; + } } diff --git a/.W16_lab05.md.swp b/src/edu/ucsb/cs56/projects/games/pong/gameplay/.Ball.java.swp similarity index 54% rename from .W16_lab05.md.swp rename to src/edu/ucsb/cs56/projects/games/pong/gameplay/.Ball.java.swp index 207a973..56192b7 100644 Binary files a/.W16_lab05.md.swp and b/src/edu/ucsb/cs56/projects/games/pong/gameplay/.Ball.java.swp differ diff --git a/src/edu/ucsb/cs56/projects/games/pong/gameplay/Ball.java b/src/edu/ucsb/cs56/projects/games/pong/gameplay/Ball.java index 9d32b12..58b443d 100644 --- a/src/edu/ucsb/cs56/projects/games/pong/gameplay/Ball.java +++ b/src/edu/ucsb/cs56/projects/games/pong/gameplay/Ball.java @@ -18,15 +18,15 @@ public class Ball extends gameObject{ /**Number of balls lost */ - public int ballsLost; + protected int ballsLost; /**X Speed of the ball */ - public int speedX = 1; + protected int speedX = 1; /**Speed of the ball */ - public int speed = 1; + protected int speed = 1; /**Holds if the ball is attached to a paddle */ - public boolean attached = false; + protected boolean attached = false; /**Paddle being held to, false=p1, true=p2*/ private boolean paddle = false; @@ -53,10 +53,11 @@ public class Ball extends gameObject{ public Ball( int x, int y, int w, int h, boolean isGoingRight ) { super( x, y, w, h ); - speed = DifficultyLevel.getSpeed(); + setSpeed( DifficultyLevel.getSpeed()); if(!isGoingRight){ - speed *= -1; + //speed *= -1; + setSpeedX(getSpeed()*-1); } startBall(); @@ -78,8 +79,8 @@ public void draw( Graphics g ) /** startBall(): When the ball is stopped, this will start the ball in the direction of the way it was going*/ public void startBall() { //reset speeds to original - setXVelocity( speed ); - setYVelocity( speed ); + setXVelocity( getSpeed() ); + setYVelocity( getSpeed() ); } /** isStopped() checks if the ball is stopped @@ -87,7 +88,7 @@ public void startBall() { */ public boolean isStopped() { - if( ( getXVelocity() == 0 ) && ( getYVelocity() == 0 ) && attached == false ) + if( ( getXVelocity() == 0 ) && ( getYVelocity() == 0 ) && isAttached() == false ) return true; else return false; @@ -106,8 +107,8 @@ public void stopBall() public void resetBall(int ballNumber) { stopBall(); - if(isGoingRight && speed < 0) - speed *= -1; + if(isGoingRight && getSpeed() < 0) + setSpeed(getSpeed()* (-1)); setXCoordinate(( Screen.w-DifficultyLevel.getWidth() ) / 2 ); setYCoordinate(( Screen.h-(4*ballNumber)*DifficultyLevel.getHeight() ) / 2 ); } @@ -118,18 +119,30 @@ public void resetBall(int ballNumber) */ public void holdBallToPaddle(KeyEvent evt, double distance) { - attached = true; - speedX = getXVelocity(); + //attached = true; + setAttached(true); + setSpeedX(getXVelocity()); - if(evt.getKeyCode() == KeyEvent.VK_A && ( (int)distance < DifficultyLevel.getPaddleHeight() ) ) { + if(evt.getKeyCode() == KeyEvent.VK_A && ( (int)(distance+50) < (DifficultyLevel.getPaddleHeight()) ) ) { stopBall(); paddle = false; } - - if(evt.getKeyCode() == KeyEvent.VK_LEFT && ( (int)distance < DifficultyLevel.getPaddleHeight() ) ) { + + if(evt.getKeyCode() == KeyEvent.VK_LEFT && ( (int)(distance+40) <(DifficultyLevel.getPaddleHeight()) )) { stopBall(); paddle = true; } + /* + if(evt.getKeyCode() == KeyEvent.VK_A && (distance <(double)(DifficultyLevel.getPaddleHeight()/3 ))) { + stopBall(); + paddle = false; + } + + if(evt.getKeyCode() == KeyEvent.VK_LEFT && ( distance <(double)(DifficultyLevel.getPaddleHeight()/3 ) )) { + stopBall(); + paddle = true; + } + */ } /** Takes in keyEvent and distance and releases ball from paddle if correct key is released @@ -138,21 +151,22 @@ public void holdBallToPaddle(KeyEvent evt, double distance) */ public void releaseBallFromPaddle(KeyEvent evt, double distance) { - attached = false; - if(evt.getKeyCode() == KeyEvent.VK_A && ( (int)distance < DifficultyLevel.getPaddleHeight() ) ) { + //attached = false; + setAttached(false); + if(evt.getKeyCode() == KeyEvent.VK_A && ( (int)(distance+50) < DifficultyLevel.getPaddleHeight() ) ) { setYVelocity(p1.getYVelocity()); - if(speedX < 0) - setXVelocity(speedX * -1); + if(getSpeedX() < 0) + setXVelocity(getSpeedX() * -1); else - setXVelocity(speedX); + setXVelocity(getSpeedX()); } - if(evt.getKeyCode() == KeyEvent.VK_LEFT && ( (int)distance < DifficultyLevel.getPaddleHeight() ) ) { + if(evt.getKeyCode() == KeyEvent.VK_LEFT && ( (int)(distance+40) < DifficultyLevel.getPaddleHeight() ) ) { setYVelocity(p2.getYVelocity()); - if(speedX > 0) - setXVelocity(speedX * -1); + if(getSpeedX() > 0) + setXVelocity(getSpeedX() * -1); else - setXVelocity(speedX); + setXVelocity(getSpeedX()); } } @@ -161,10 +175,10 @@ public void releaseBallFromPaddle(KeyEvent evt, double distance) * @param distance ArrayList of distances from balls */ public void keyPressed(KeyEvent evt, ArrayList distance){ - if ( (evt.getKeyCode() == KeyEvent.VK_A) && ( distance.get(0) < DifficultyLevel.getPaddleHeight()) && (attached == false)) { + if ( (evt.getKeyCode() == KeyEvent.VK_A) && ( distance.get(0) < DifficultyLevel.getPaddleHeight()) && (isAttached() == false)) { holdBallToPaddle(evt, distance.get(0)); } - if(paddle == false && attached == true){ + if(paddle == false && isAttached() == true){ if(p1.isPaddleMoving() == true){ if( evt.getKeyCode() == KeyEvent.VK_W && distance.get(4) > 5 ){ setYVelocity(p1.getYVelocity()); @@ -172,15 +186,13 @@ public void keyPressed(KeyEvent evt, ArrayList distance){ if( evt.getKeyCode() == KeyEvent.VK_S && distance.get(2) > 5){ setYVelocity(p1.getYVelocity()); } - } else { - setYVelocity(0); - } + } } - if (evt.getKeyCode() == KeyEvent.VK_LEFT && distance.get(1)< DifficultyLevel.getPaddleHeight() && attached == false) { + if (evt.getKeyCode() == KeyEvent.VK_LEFT && distance.get(1)< DifficultyLevel.getPaddleHeight() && isAttached() == false) { holdBallToPaddle(evt, distance.get(1)); } - if(paddle == true && attached == true){ + if(paddle == true && isAttached() == true){ if(p2.isPaddleMoving() == true) { if( evt.getKeyCode() == KeyEvent.VK_UP && distance.get(5) > 5 ){ setYVelocity(p2.getYVelocity()); @@ -188,15 +200,13 @@ public void keyPressed(KeyEvent evt, ArrayList distance){ if( evt.getKeyCode() == KeyEvent.VK_DOWN && distance.get(3) > 5 ){ setYVelocity(p2.getYVelocity()); } - } else { - setYVelocity(0); - } + } } } /**Updates the velocity if the ball is attached*/ public void ballAttachUpdate() { - if(attached == true) { + if(isAttached() == true) { if(paddle == false) { if(p1.isPaddleMoving() == true) { setYVelocity(p1.getYVelocity()); @@ -228,7 +238,7 @@ public int getYVelocity() { * @param distance Arraylist of distances from balls */ public void keyReleased(KeyEvent evt, ArrayList distance){ - if(attached == true) { + if(isAttached() == true) { if ( evt.getKeyCode() == KeyEvent.VK_A) { releaseBallFromPaddle(evt, distance.get(0)); } @@ -248,4 +258,38 @@ public void keyReleased(KeyEvent evt, ArrayList distance){ } } } + + /** getters and setters + */ + public int getBallsLost() { + return ballsLost; + } + + public void setBallsLost(int ballsLost) { + this.ballsLost = ballsLost; + } + + public int getSpeedX() { + return speedX; + } + + public void setSpeedX(int speedX) { + this.speedX = speedX; + } + + public int getSpeed() { + return speed; + } + + public void setSpeed(int speed) { + this.speed = speed; + } + + public boolean isAttached() { + return attached; + } + + public void setAttached(boolean attached) { + this.attached = attached; + } } diff --git a/src/edu/ucsb/cs56/projects/games/pong/gameplay/DifficultyLevel.java b/src/edu/ucsb/cs56/projects/games/pong/gameplay/DifficultyLevel.java index 6361969..64eba20 100644 --- a/src/edu/ucsb/cs56/projects/games/pong/gameplay/DifficultyLevel.java +++ b/src/edu/ucsb/cs56/projects/games/pong/gameplay/DifficultyLevel.java @@ -8,28 +8,28 @@ public class DifficultyLevel { /** The game difficulty level */ - public static int diflevel; + protected static int diflevel; /** The screen multiplier factor */ - public static int screenfactor; + protected static int screenfactor; /** New screen width */ - public static int width; + protected static int width; /** New screen height */ - public static int height; + protected static int height; /** The original size of the ball */ - public static int origballsize=5; + protected static int origballsize=5; /** New paddle height */ - public static int paddleHeight; + protected static int paddleHeight; /** New ball speed */ - public static int speed; + protected static int speed; /** How many balls there are in game */ - public static int ballNum; + protected static int ballNum; /** edu.ucsb.cs56.projects.games.pong.gameplay.DifficultyLevel constructor to initialize difficulty of game onto the screen * @param difficultylevel set game difficulty @@ -39,55 +39,55 @@ public class DifficultyLevel { // inputs are game difficulty [80,100,120,130,140,170] public DifficultyLevel(int difficultylevel) { - diflevel=difficultylevel; - ballNum = 1; - if(diflevel==80)//supereasy + setDiflevel(difficultylevel); + setBallNum(1); + if(getDifficulty()==80)//supereasy { - screenfactor=10; - width=origballsize*screenfactor; - height=origballsize*screenfactor; - paddleHeight = 110; - speed = 1; + setScreenfactor(10); + setWidth(getOrigballsize()*getScreenFactor()); + setHeight(getOrigballsize()*getScreenFactor()); + setPaddleHeight(110); + setSpeed(1); } - else if(diflevel==100)//easy + else if(getDifficulty()==100)//easy { - screenfactor=8; - width=origballsize*screenfactor; - height=origballsize*screenfactor; - paddleHeight = 100; - speed = 2; + setScreenfactor(8); + setWidth(getOrigballsize()*getScreenFactor()); + setHeight(getOrigballsize()*getScreenFactor()); + setPaddleHeight(100); + setSpeed(2); } - else if(diflevel==120)//medium + else if(getDifficulty()==120)//medium { - screenfactor=6; - width=origballsize*screenfactor; - height=origballsize*screenfactor; - paddleHeight = 90; - speed = 3; + setScreenfactor(6); + setWidth(getOrigballsize()*getScreenFactor()); + setHeight(getOrigballsize()*getScreenFactor()); + setPaddleHeight(90); + setSpeed(3); } - else if(diflevel==130)//hard + else if(getDifficulty()==130)//hard { - screenfactor=4; - width=origballsize*screenfactor; - height=origballsize*screenfactor; - paddleHeight = 80; - speed = 3; + setScreenfactor(4); + setWidth(getOrigballsize()*getScreenFactor()); + setHeight(getOrigballsize()*getScreenFactor()); + setPaddleHeight(80); + setSpeed(3); } - else if(diflevel==140)//extreme + else if(getDifficulty()==140)//extreme { - screenfactor=2; - width=origballsize*screenfactor; - height=origballsize*screenfactor; - paddleHeight = 70; - speed = 4; + setScreenfactor(2); + setWidth(getOrigballsize()*getScreenFactor()); + setHeight(getOrigballsize()*getScreenFactor()); + setPaddleHeight(70); + setSpeed(4); } - else if(diflevel==170)//chaos + else if(getDifficulty()==170)//chaos { - screenfactor=1; - width=origballsize*screenfactor; - height=origballsize*screenfactor; - paddleHeight = 60; - speed = 5; + setScreenfactor(1); + setWidth(getOrigballsize()*getScreenFactor()); + setHeight(getOrigballsize()*getScreenFactor()); + setPaddleHeight(60); + setSpeed(5); } } @@ -124,9 +124,43 @@ else if(diflevel==170)//chaos * @return int number of balls */ public static int getBallNum() {return ballNum; } - - public static void setBallNum(int ballNumber) { + + public static int getOrigballsize() { + return origballsize; + } + + /** setters + */ + public static void setBallNum(int ballNumber) { if(ballNumber >= 1 && ballNumber <= 5){ ballNum = ballNumber;} } + + public static void setDiflevel(int diflevel) { + DifficultyLevel.diflevel = diflevel; + } + + public static void setScreenfactor(int screenfactor) { + DifficultyLevel.screenfactor = screenfactor; + } + + public static void setWidth(int width) { + DifficultyLevel.width = width; + } + + public static void setHeight(int height) { + DifficultyLevel.height = height; + } + + public static void setOrigballsize(int origballsize) { + DifficultyLevel.origballsize = origballsize; + } + + public static void setPaddleHeight(int paddleHeight) { + DifficultyLevel.paddleHeight = paddleHeight; + } + + public static void setSpeed(int speed) { + DifficultyLevel.speed = speed; + } } diff --git a/src/edu/ucsb/cs56/projects/games/pong/gameplay/Paddle.java b/src/edu/ucsb/cs56/projects/games/pong/gameplay/Paddle.java index 96ed8b6..cf7f91c 100644 --- a/src/edu/ucsb/cs56/projects/games/pong/gameplay/Paddle.java +++ b/src/edu/ucsb/cs56/projects/games/pong/gameplay/Paddle.java @@ -16,13 +16,13 @@ public class Paddle extends gameObject{ /**Number of points a player has*/ - public int points; + protected int points; /**Holds the number of ballsLeft*/ - public int ballCount; + protected int ballCount; /**If this is the right side paddle*/ - public boolean right; + protected boolean right; /**True if paddle is moving*/ private boolean paddleMoving; @@ -48,9 +48,9 @@ public Paddle(int x, int y, int rectH, int ballNumber){ public Paddle(int x, int y, int rectH, int ballNumber, boolean sideRight){ super( x, y, rectH ); - ballCount = 3*ballNumber; - points = 0; - right = sideRight; + setBallCount(3*ballNumber); + setPoints(0); + setRight(sideRight); } @@ -59,7 +59,7 @@ public Paddle(int x, int y, int rectH, int ballNumber, boolean sideRight){ */ public void keyPressed(KeyEvent evt){ - if ( this.right == false ) { + if ( isRight() == false ) { if( evt.getKeyCode() == KeyEvent.VK_Q ){ System.exit( 0 ); } @@ -85,7 +85,7 @@ public void keyPressed(KeyEvent evt){ */ public void keyReleased(KeyEvent evt){ - if ( this.right == false ) { + if ( isRight() == false ) { if( evt.getKeyCode() == KeyEvent.VK_W ){ setYVelocity( 0 ); } @@ -176,6 +176,22 @@ public void movePaddle(){ setYCoordinate( getYCoordinate() + getYVelocity() ); } + public boolean isRight() { + return right; + } + + public void setPoints(int points) { + this.points = points; + } + + public void setBallCount(int ballCount) { + this.ballCount = ballCount; + } + + public void setRight(boolean right) { + this.right = right; + } + /**isPaddleMoving() return is the paddle is moving or not * @return boolean if paddle is moving */ diff --git a/src/edu/ucsb/cs56/projects/games/pong/gameplay/Screen.java b/src/edu/ucsb/cs56/projects/games/pong/gameplay/Screen.java index da579c6..e863114 100644 --- a/src/edu/ucsb/cs56/projects/games/pong/gameplay/Screen.java +++ b/src/edu/ucsb/cs56/projects/games/pong/gameplay/Screen.java @@ -108,11 +108,11 @@ public void draw(Graphics g){ g.setColor(Color.WHITE); g.drawString("Hits: " + game.getHits(), Screen.w/2 - 60, 40); g.drawString( "player 1 ", 30, 40 ); - g.drawString( "" + game.p1.getPoints(), 30, 70 ); + g.drawString( "" + game.getPlayer1().getPoints(), 30, 70 ); g.drawString( "player 2 ", Screen.w - 180, 40 ); - g.drawString( "" + game.p2.getPoints(), Screen.w - 70, 70 ); - g.drawString( "Lives " + ( game.p1.ballCount ), 30, Screen.h - 47 ); - g.drawString( "Lives " + ( game.p2.ballCount ), Screen.w - 140 , Screen.h - 47 ); + g.drawString( "" + game.getPlayer2().getPoints(), Screen.w - 70, 70 ); + g.drawString( "Lives " + ( game.getPlayer1().ballCount ), 30, Screen.h - 47 ); + g.drawString( "Lives " + ( game.getPlayer2().ballCount ), Screen.w - 140 , Screen.h - 47 ); //draws all the balls for(int i = 0; i < ballNum; i++){ @@ -124,8 +124,8 @@ public void draw(Graphics g){ g.drawString( "Game Paused", Screen.w/2 - 100, Screen.h/2 - 100 ); g.drawString( "Press M to return to Main Menu", Screen.w/2 - 220, Screen.h/2 + 100 ); } - game.p1.draw(g); - game.p2.draw(g); + game.getPlayer1().draw(g); + game.getPlayer2().draw(g); jf.repaint(); } @@ -146,8 +146,8 @@ public class myKeyAdapter extends KeyAdapter { * @param evt the KeyEvent */ public void keyPressed(KeyEvent evt) { - game.p1.keyPressed(evt); - game.p2.keyPressed(evt); + game.getPlayer1().keyPressed(evt); + game.getPlayer2().keyPressed(evt); for(int i = 0; i < ballNum; i++){ ArrayList distance = distanceCalc(i); game.b[i].keyPressed(evt, distance); @@ -187,8 +187,8 @@ public void keyPressed(KeyEvent evt) { public void keyReleased(KeyEvent evt){ for(int i = 0; i < ballNum; i++){ ArrayList distance = distanceCalc(i); - game.p1.keyReleased(evt); - game.p2.keyReleased(evt); + game.getPlayer1().keyReleased(evt); + game.getPlayer2().keyReleased(evt); game.b[i].keyReleased(evt, distance); } } @@ -201,10 +201,10 @@ public void keyReleased(KeyEvent evt){ public ArrayList distanceCalc(int i) { ArrayList distance = new ArrayList(); - int p1x=game.p1.getXCoordinate(); - int p1y=game.p1.getYCoordinate(); - int p2x=game.p2.getXCoordinate(); - int p2y=game.p2.getYCoordinate(); + int p1x=game.getPlayer1().getXCoordinate(); + int p1y=game.getPlayer1().getYCoordinate(); + int p2x=game.getPlayer2().getXCoordinate(); + int p2y=game.getPlayer2().getYCoordinate(); int bx =game.b[i].getXCoordinate(); int by =game.b[i].getYCoordinate(); diff --git a/src/edu/ucsb/cs56/projects/games/pong/gameplay/gameObject.java b/src/edu/ucsb/cs56/projects/games/pong/gameplay/gameObject.java index 0143ca0..5e829f9 100644 --- a/src/edu/ucsb/cs56/projects/games/pong/gameplay/gameObject.java +++ b/src/edu/ucsb/cs56/projects/games/pong/gameplay/gameObject.java @@ -10,16 +10,16 @@ class gameObject{ /** Gameobject horizontal speed (x)*/ - public int xVelocity; + protected int xVelocity; /** Gameobject vertival speed (y)*/ - public int yVelocity; + protected int yVelocity; /**Holds x and y coordinates and width and height */ - public Rectangle rectangle; + protected Rectangle rectangle; /** Holds the color of the gameObject*/ - public Color color; + protected Color color; /** Default constructor, not used */ gameObject() { }