From 29457b25577c129dca134f81e542a0727fa228b1 Mon Sep 17 00:00:00 2001 From: Samuel Fu Date: Wed, 28 Feb 2018 19:32:46 -0800 Subject: [PATCH] fixed #42 + #72 --- .F17_lab03.md.swp | Bin 12288 -> 0 bytes .../ucsb/cs56/projects/games/pong/Game.java | 17 ++- .../games/pong/gameplay/.Ball.java.swp | Bin 16384 -> 20480 bytes .../projects/games/pong/gameplay/Ball.java | 118 +++++++++++----- .../games/pong/gameplay/DifficultyLevel.java | 130 +++++++++++------- .../projects/games/pong/gameplay/Paddle.java | 32 +++-- .../projects/games/pong/gameplay/Screen.java | 28 ++-- .../games/pong/gameplay/gameObject.java | 8 +- 8 files changed, 221 insertions(+), 112 deletions(-) delete mode 100644 .F17_lab03.md.swp rename .W16_lab05.md.swp => src/edu/ucsb/cs56/projects/games/pong/gameplay/.Ball.java.swp (54%) diff --git a/.F17_lab03.md.swp b/.F17_lab03.md.swp deleted file mode 100644 index 8f2fdf900ef3a861ae9534779ad9cd871a4ff8ca..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeHN&yN&E6fQKzpF|@E4<=HI0ZnFRciDi%+k${Y6i8My;z6sstEY>p?rN&4cXmAJ z*_+-?{73u)JbCq|2NQ34FnSO#ey^%~nE?_)@Iq3Peauc*)%)?i_r6zTC)@mR`aVB1 z*<^UVz}P=Oxa;2^|Lf%+k1%#a$+Dy8_j%<`>GAf-N$w_H?y`w2?tSWupl~C1r%s1| z#xqe%H*QTc+qkgx)~8xzo7APc!y4JFc2scLhFX zo#(!i{bOGVA9f$C)6v>Eua`VGbuAmxVLCtIw^cW0SaZ+?4NffiqFSyfCN*p4xv^Z8 zv;$jBrwi`9(i*?pITsBale}~IZjxTLB)B%7o06NhHP&~H@=C&};c~yFQxqKx?MW+U zlFL2W@NCI%ba-l|s#`571l7!fZ|fe>*|^e!AheaDSOTnrNO-BVbQ5;$7=Po^n~VVj zm@6b#j+a7Nz_gGE!`PW<)aSAY-t$}w=iq21_9SoU8&S~MTX9mCW4NzHBfPOoUIH3Q z3Bk@=VS)9u;i4!ItZ7EHHssVGl|ZA9!|_6Tq4W@5*)=&G345f-D_%=q8Td5yaq!vv2pF(?%DCV-BYK>SFO1L@7!HqhEvV~zKt{6>?%^L z5mlZ`jk=|bJ+#)7?1I_{5ZYMx(8{(#=V1Y6K z*HXPB(omqKsi6eU)&gBSj@S+w-#JU9{(n9U2qPGtR0VT0UPcnhGOks%HODTM3siy}H9k`bo`&?%& zXc);t9Dp~dg?F$(-FUMD=}~BeKb98)g1LYlNd9dE2HpzshZ;SPaCTsn2*e&RfL0(4 zsi#(z(RO{c$f5KXUeZ&sx)v6%wU3FlHwq8nyjhKq*mZ5@;=Tk=xkIT)CY=p zrbDI5vZU4zAcUFsrBtqLN6A@AQS_ zOz9WY61HpH2~Y$(?TG9P=?BbU$Ki~M;#nSkY?e+uz}z2^+c~4=WQ3OZNzsy zssyQSK*ym!QFd})VOoAfGlvG1yK~FOc-W(Wb&J9)OO9a2SioH&6=Z5X}; zS+EhNC{+x0fJ3}Bn#wI=7EXKH-$>?Cwpa?-me$miGD58t80%12$C#rs8jKh@)W4Mh z%aB1jIl^|-a`;RRj6#T^jj;oFnGP7E)NXdD#lg#%k#l$S0vRwZkQugtwamHzJP?rH8nr3 z6=D4M#Z_W-(9FSAj8xoJ4HZagy%6XwQ6VjT4P??UWP~57$K8ga&@!eiR0(J~RtK%X vNGxJaJ&TL4DiP*NT?WG2f#0}`?lgr(8(cMdKH?v$9QQ~i_%7y1VH)-yrXp=& 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 207a97327f3b946a2466a774abb4e881da771837..56192b797d538354d6528f89109a1f724d6263ef 100644 GIT binary patch literal 20480 zcmeI2e~cVu8OKK}&_$^tpcqQ}Ac5|ld%L%L6p`!Iw%1>Jh1OnsZQ&1@+j;kPdb2a@ z%xT6+o7bNkTh=jG)Z2W2 zm8%xH-tcI(REtXOS~ODk;rx5Q44Xec+k%EbA}eDewTe1DpgN zH~>BYM#074byWH+_#5~kxDDJ2ra=`{zy`1u{QI4j^)z@A+z;*mH-ev^Ygvn67q|dC zpF_LAm%tan7eEM#;JLM^4?G6$0pAB-2A>5dz;Vz7b6_L*`#I}Z9#EBE!%NCL2jLDK^Wq22F-~jawBSmB8r65 zogep2G7B{_bi&F-B0f411fk>lb}aIFnyQ6D_@%~h{;DCa>H2naMudf-9d5nu*4kdY zv{%dvuQV&-J;I%B#Q6dhs7zHVjLAZ|$RquVTo~?C#$nfq8|fl)+_0!6(Eg!u8m^34 zIk%p7quqh)&mQ0k>B=j0g3UBi&~?T^i%+5Cbt8_;+pYGD=hkRGa2e89FNZBce>!<&sq*85{FEY|7G6+u&mMIBfF+3?$_VnsX#+ zWP!%VxdS|?@=>S#=|`yEt~H<&NG+=&HSk?Mn+XC>*gi~P3f*f7C$AEv$5uBrNm^It z1nb~VBTpiaR4Ih2YjZG8HBNHSdvX|YustQ6(`pKx>=`ko-+aGZmY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ai#;ygwVbC_JB61ts*=yIX%>@bC z|KG*_{D&ag|6_~IpFd$wAA=O=jWgKGgDMb>`vHv&pMXF>ARrJB2nYlO0s;YnfIvVX zAP^AvZxFbTQz2~e{XvH2+k{3reEs1;^I0XEodB(nSU~oR~<*!2OMpKiZ%TFHXpDPs!40idPM%@maYis42R5yIoP>FRewv0y?%o`N;>T28@c|5 za=0z*@AR;j$1&`Q5fgnHHppTSAia0Th_UiqGvj|EC~2!(KJ zNap$5jTAQ?e8-7-{(=L7VvP$5W9f?W{_M)cUssSg#}_7qP=Z4uJ}sh?xQ-7S`b!gk zp#fgDYjN_`*ceyBQ*idT12g#<4yB<1E+v*=Adbq{zeERK3B1>kCOF3+iNkS<8F| 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() { }