Skip to content

Commit

Permalink
Merge pull request #1 from rightoneX/home
Browse files Browse the repository at this point in the history
up to date
  • Loading branch information
rightoneX authored Sep 20, 2022
2 parents 594a3b7 + 193e2c5 commit 77391dd
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 70 additions & 7 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 22 additions & 6 deletions src/Ball.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,72 @@ public class Ball extends Sprite {

// Constructor
public Ball() {
// TODO: Set width to Settings.BALL_WIDTH
// TODO: Set width to Settings.BALL_HEIGHT
// TODO: Call resetPosition
// Set width to Settings.BALL_WIDTH
width = Settings.BALL_WIDTH;
// Set width to Settings.BALL_HEIGHT
height = Settings.BALL_HEIGHT;
// Call resetPosition
resetPosition();
}

/**
* Resets the ball to the initial position
* Uses Settings.INITIAL_BALL_X/Y to set the position of the ball
*/
public void resetPosition() {
// Set the balls y by using the INITIAL_BALL_Y
setX(Settings.INITIAL_BALL_X);
// TODO: Set the balls y by using the INITIAL_BALL_Y (see above)
setY(Settings.INITIAL_BALL_Y);
}

public void update() {
x += xVelocity;
// TODO: Increase the y variable by yVelocity (see above)
y += yVelocity;

// Bounce off left side of screen
if(x <= 0) {
// TODO: Set x to 0 so it does not leave the screen
x = 0;
// TODO: Change the x velocity to make the ball go right
xVelocity *= -1;
}

// Bounce off right side of screen
if(x >= Settings.WINDOW_WIDTH - Settings.BALL_WIDTH) {
// TODO: Set x to the right edge of the screen (see the above if condition)
// if(x >= Settings.WINDOW_WIDTH - Settings.BALL_WIDTH) {
//
// }
// TODO: Change the x velocity to make the ball go left
xVelocity *= -1;

}

// Bounce off top of screen
if(y <= 0) {
// TODO: Set y to 0 so it does not leave the screen
y = 0;
// TODO: Change the y velocity to make the ball go downward
yVelocity *= -1;
}

}

public void setXVelocity(int x) {
// TODO: Set the x velocity
xVelocity = 1;
}
public void setYVelocity(int y) {
// TODO: Set the y velocity
yVelocity = -1;
}

public int getXVelocity() {
return 0; // TODO: Return the x velocity
return xVelocity; // TODO: Return the x velocity
}
public int getYVelocity() {
return 0; // TODO: Return the y velocity
return yVelocity; // TODO: Return the y velocity
}

public void paint(Graphics g) {
Expand Down
18 changes: 10 additions & 8 deletions src/Breakout.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import java.awt.Color;
import java.awt.*;

import javax.swing.JFrame;

Expand All @@ -9,14 +9,16 @@ public class Breakout extends JFrame{
private BreakoutPanel panel;

public Breakout() {
// TODO: Set the size of the screen (use Settings.WINDOW_WIDTH/HEIGHT)
// TODO: Set the title
// TODO: Set the background colour to white
// TODO: Set resizable to false
setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setPreferredSize(new Dimension(Settings.WINDOW_WIDTH,Settings.WINDOW_HEIGHT ));
this.setTitle("Breakout");
this.setBackground(Color.LIGHT_GRAY);
this.pack();
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new BreakoutPanel(this);
add(panel);
// TODO: Set visible to true
this.add(panel);
this.setVisible(true);
}

public static void main(String[] args) {
Expand Down
45 changes: 34 additions & 11 deletions src/BreakoutPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class BreakoutPanel extends JPanel implements ActionListener, KeyListener

static final long serialVersionUID = 2L;

private boolean gameRunning = true;
private boolean gameRunning = false;
private int livesLeft = 3;
private String screenMessage = "";
private Ball ball;
Expand All @@ -28,11 +28,15 @@ public BreakoutPanel(Breakout game) {

Timer timer = new Timer(5, this);
timer.start();

// TODO: Create a new ball object and assign it to the appropriate variable
// TODO: Create a new paddle object and assign it to the appropriate variable
// TODO: Create a new bricks array (Use Settings.TOTAL_BRICKS)
// TODO: Call the createBricks() method

// Create a new ball object and assign it to the appropriate variable
ball = new Ball();
// Create a new paddle object and assign it to the appropriate variable
paddle = new Paddle();
// Create a new bricks array (Use Settings.TOTAL_BRICKS)
bricks = new Brick[Settings.TOTAL_BRICKS];
// Call the createBricks() method
createBricks();
}

private void createBricks() {
Expand All @@ -51,24 +55,32 @@ private void createBricks() {
}

private void paintBricks(Graphics g) {
// TODO: Loop through the bricks and call the paint() method
// Loop through the bricks and call the paint() method
for(int i = 0; i < Settings.TOTAL_BRICKS; i++){
if(!bricks[i].isBroken()){
bricks[i].paint(g);
}
}

}

private void update() {
if(gameRunning) {
// TODO: Update the ball and paddle
ball.update();
collisions();
repaint();
}
}

private void gameOver() {
// TODO: Set screen message
// Set screen message
screenMessage = "Game Over";
stopGame();
}

private void gameWon() {
// TODO: Set screen message
// Set screen message
screenMessage = "You Won";
stopGame();
}

Expand Down Expand Up @@ -100,6 +112,7 @@ private void collisions() {
break;
}
}

if(!bricksLeft) {
gameWon();
return;
Expand Down Expand Up @@ -145,7 +158,6 @@ private void collisions() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

ball.paint(g);
paddle.paint(g);
paintBricks(g);
Expand All @@ -164,11 +176,22 @@ public void paintComponent(Graphics g) {
@Override
public void keyPressed(KeyEvent e) {
// TODO: Set the velocity of the paddle depending on whether the player is pressing left or right
if(e.getKeyCode() == KeyEvent.VK_RIGHT && paddle.x < (getWidth() - paddle.width)){
paddle.x +=15;
}
if(e.getKeyCode() == KeyEvent.VK_LEFT && paddle.x > 0){
paddle.x -=15;
}
//start the game
if(e.getKeyCode() == KeyEvent.VK_ENTER){
gameRunning = true;
}
}

@Override
public void keyReleased(KeyEvent e) {
// TODO: Set the velocity of the paddle after the player has released the keys

}

@Override
Expand Down
7 changes: 6 additions & 1 deletion src/Brick.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ public class Brick extends Sprite {

public Brick(int x, int y) {
// TODO: Set x using the parameter
this.x = x;
// TODO: Set y using the parameter
this.y = y;
// TODO: Set the width and height of the brick using Settings.BRICK_WIDTH/HEIGHT
this.height = Settings.BRICK_HEIGHT;
this.width = Settings.BRICK_WIDTH;
}

public boolean isBroken() {
return false; // TODO: Return the correct variable
return broken; // TODO: Return the correct variable
}
public void setBroken(boolean b) {
// TODO: Set the broken variable using the parameter given
broken = b;
}

public void paint(Graphics g) {
Expand Down
18 changes: 12 additions & 6 deletions src/Paddle.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@ public class Paddle extends Sprite {
private int xVelocity;

public Paddle() {
// TODO: Set width to Settings.PADDLE_WIDTH
// TODO: Set width to Settings.PADDLE_HEIGHT
// TODO: Call resetPosition
// Set width to Settings.PADDLE_WIDTH
width = Settings.PADDLE_WIDTH;
// Set width to Settings.PADDLE_HEIGHT
height = Settings.PADDLE_HEIGHT;
// Call resetPosition
resetPosition();

}

public void resetPosition() {
// TODO: Set initial position x and y (use INITIAL_PADDLE_X/Y)
// Note: Check Ball.java for a hint
// Set initial position x and y (use INITIAL_PADDLE_X/Y)
setX(Settings.INITIAL_PADDLE_X);
setY(Settings.INITIAL_PADDLE_Y);
}

public void update() {
x += xVelocity;
// x += xVelocity;

// TODO: Prevent the paddle from moving outside of the screen
// This can be done using two if statements (one for the left side of the screen and one for the right)
Expand All @@ -29,5 +34,6 @@ public void paint(Graphics g) {

public void setXVelocity(int vel) {
// TODO: Set x velocity

}
}
Loading

0 comments on commit 77391dd

Please sign in to comment.