Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
iamSamuelFu committed Mar 1, 2018
1 parent 508791e commit 29457b2
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 112 deletions.
Binary file removed .F17_lab03.md.swp
Binary file not shown.
17 changes: 16 additions & 1 deletion src/edu/ucsb/cs56/projects/games/pong/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
Binary file not shown.
118 changes: 81 additions & 37 deletions src/edu/ucsb/cs56/projects/games/pong/gameplay/Ball.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -78,16 +79,16 @@ 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
* @return boolean if ball is stopped
*/
public boolean isStopped()
{
if( ( getXVelocity() == 0 ) && ( getYVelocity() == 0 ) && attached == false )
if( ( getXVelocity() == 0 ) && ( getYVelocity() == 0 ) && isAttached() == false )
return true;
else
return false;
Expand All @@ -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 );
}
Expand All @@ -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
Expand All @@ -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());
}
}

Expand All @@ -161,42 +175,38 @@ public void releaseBallFromPaddle(KeyEvent evt, double distance)
* @param distance ArrayList of distances from balls
*/
public void keyPressed(KeyEvent evt, ArrayList<Double> 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());
}
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());
}
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());
Expand Down Expand Up @@ -228,7 +238,7 @@ public int getYVelocity() {
* @param distance Arraylist of distances from balls
*/
public void keyReleased(KeyEvent evt, ArrayList<Double> distance){
if(attached == true) {
if(isAttached() == true) {
if ( evt.getKeyCode() == KeyEvent.VK_A) {
releaseBallFromPaddle(evt, distance.get(0));
}
Expand All @@ -248,4 +258,38 @@ public void keyReleased(KeyEvent evt, ArrayList<Double> 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;
}
}
Loading

0 comments on commit 29457b2

Please sign in to comment.