1
0
Fork 0

Aufgabe 2.1 + reindent project

This commit is contained in:
rxbn 2019-12-01 18:31:50 +01:00
parent 1f5c6edd03
commit c9f87ace17
13 changed files with 399 additions and 201 deletions

View File

@ -6,7 +6,6 @@ import java.awt.Color;
* A class that contains all constant values to configure the game
*
* @author dmlux, modified by I. Schumacher
*
*/
public class Constants {

View File

@ -6,15 +6,14 @@ import break_out.view.View;
/**
* The entry point of the program. The game get started here and all components
* are initialized here.
*
*
* @author dmlux, modified by I. Schumacher
*
*/
public class Main {
/**
* The main method
*
*
* @param args The arguments that were passed by the command line.
*/
public static void main(String[] args) {

View File

@ -13,9 +13,8 @@ import break_out.view.View;
/**
* The controller takes care of the input events and reacts on those events by
* manipulating the view and updates the model.
*
* @author dmlux, modified by I. Schumacher and I. Traupe
*
*
* @author dmlux, modified by I. Schumacher and I. Traupe, modified by Gruppe 175
*/
public class Controller implements ActionListener, KeyListener {
@ -31,9 +30,8 @@ public class Controller implements ActionListener, KeyListener {
/**
* The constructor expects a view to construct itself.
*
* @param view
* The view that is connected to this controller
*
* @param view The view that is connected to this controller
*/
public Controller(View view) {
this.view = view;
@ -101,6 +99,7 @@ public class Controller implements ActionListener, KeyListener {
/**
* This method will be called, after a key was typed. This means, that the key
* was pressed and released, before this method get called.
*
* @param e The key event
*/
@Override
@ -110,20 +109,22 @@ public class Controller implements ActionListener, KeyListener {
/**
* This method will be called, after a key was pressed down.
*
* @param e The key event
*/
@Override
public void keyPressed(KeyEvent e) {
}
/**
* This method will be called, after a key was released.
*
* @param e The key event
*/
@Override
public void keyReleased(KeyEvent e) {
}
/**

View File

@ -4,91 +4,92 @@ import break_out.Constants;
/**
* This class contains the information about the balls characteristics and behavior
*
*
* @author iSchumacher
* @author modified by Gruppe 175: Moritz Henseleit, Ruben Meyer
*
*/
public class Ball implements IBall{
public class Ball implements IBall {
/**
* The balls position on the playground
*/
private Position position;
/**
* The balls direction
*/
private Vector2D direction;
/**
* The constructor of a ball
* The balls position and direction are initialized here.
*/
public Ball() {
this.position = new Position(0, 0);
this.direction = new Vector2D(Constants.BALL_SPEED,Constants.BALL_SPEED);
this.direction = new Vector2D(Constants.BALL_SPEED, Constants.BALL_SPEED);
this.direction.rescale();
// start at bottom-center
this.position.setX((Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER)/2);
this.position.setY(Constants.SCREEN_HEIGHT-Constants.BALL_DIAMETER);
this.position.setX((Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER) / 2.0);
this.position.setY(Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER - Constants.PADDLE_HEIGHT);
}
/**
* The getter for the balls position
*
* @return position The balls current position
*/
public Position getPosition() {
return this.position;
}
/**
* The getter for the balls direction
*
* @return direction The balls current direction
*/
public Vector2D getDirection() {
return this.direction;
}
/**
* updates ball position
*/
public void updatePosition() {
// sets X position
this.position.setX(this.position.getX()+this.direction.getDx());
this.position.setX(this.position.getX() + this.direction.getDx());
// sets Y position
this.position.setY(this.position.getY()+this.direction.getDy());
// sets Y position
this.position.setY(this.position.getY() + this.direction.getDy());
}
/**
* Ball reacts to contact with the borders
*/
public void reactOnBorder() {
// reacts on left border
if(this.position.getX() <= 0) {
this.position.setX(0);
this.direction.setDx(-(this.direction.getDx()));
// reacts on left border
if (this.position.getX() <= 0) {
this.position.setX(0);
this.direction.setDx(-(this.direction.getDx()));
}
// reacts on right border (-Diameter because of hitbox)
if(this.position.getX() >= Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER) {
// reacts on right border (-Diameter because of hitbox)
if (this.position.getX() >= Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER) {
this.position.setX(Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER);
this.direction.setDx(-(this.direction.getDx()));
}
// reacts on top border
if(this.position.getY() <= 0) {
this.position.setY(0);
this.direction.setDy(-(this.direction.getDy()));
// reacts on top border
if (this.position.getY() <= 0) {
this.position.setY(0);
this.direction.setDy(-(this.direction.getDy()));
}
// reacts on bottom border (+Diameter because of hitbox)
if(this.position.getY() >= Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER) {
this.position.setY(Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER);
this.direction.setDy(-(this.direction.getDy()));
// reacts on bottom border (+Diameter because of hitbox)
if (this.position.getY() >= Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER) {
this.position.setY(Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER);
this.direction.setDy(-(this.direction.getDy()));
}
}
}

View File

@ -8,9 +8,8 @@ import break_out.view.View;
/**
* This class contains information about the game (the model in MVC)
*
* @author dmlux, modified by I. Schumacher
*
*
* @author dmlux, modified by I. Schumacher, modified by Gruppe 175
*/
public class Game {
@ -46,9 +45,8 @@ public class Game {
/**
* The constructor creates a new game instance with the given Controller
*
* @param controller
* The controller to manage this instance (MVC-patter)
*
* @param controller The controller to manage this instance (MVC-patter)
*/
public Game(Controller controller) {
this.controller = controller;
@ -72,7 +70,7 @@ public class Game {
/**
* Getter for the Controller
*
*
* @return controller The controller of this game
*/
public Controller getController() {
@ -81,7 +79,7 @@ public class Game {
/**
* Getter for the current Level
*
*
* @return level The current level of the game
*/
public Level getLevel() {
@ -90,7 +88,7 @@ public class Game {
/**
* Getter for the total score
*
*
* @return score The current score of the game
*/
public int getScore() {
@ -101,11 +99,9 @@ public class Game {
* Creates the first or the next level, if the level number is less or equal
* maxLevel. If the current level is higher than maxLevel the view will be
* switched to the startScreen.
*
* @param levelnr
* The number for the next level
* @param score
* The current players score after finishing the previous level.
*
* @param levelnr The number for the next level
* @param score The current players score after finishing the previous level.
*/
public void createLevel(int levelnr, int score) {
this.score = score;

View File

@ -1,125 +1,162 @@
package break_out.model;
import break_out.Constants;
/**
* This class contains information about the running game
*
*
* @author dmlux
* @author I. Schumacher
* @author modified by Gruppe 175: Moritz Henseleit, Ruben Meyer
*/
public class Level extends Thread {
public class Level extends Thread implements ILevel {
/**
* The game to which the level belongs
*/
private Game game;
/**
* The number of the level
*/
private int levelnr;
/**
/**
* The game to which the level belongs
*/
private Game game;
/**
* The number of the level
*/
private int levelnr;
/**
* The score of the level
*/
private int score;
/**
* The ball of the level
*/
private Ball ball;
/**
* Flag that shows if the ball was started
*/
private boolean ballWasStarted = true;
/**
* The constructor creates a new level object and needs the current game object,
* the number of the level to be created and the current score
* @param game The game object
* @param levelnr The number of the new level object
* @param score The score
*/
public Level(Game game, int levelnr, int score) {
this.game = game;
this.levelnr = levelnr;
this.score = score;
this.ball = new Ball();
loadLevelData(levelnr);
}
private int score;
/**
* The getter for the ball object
* @return ball The ball of the level
*/
public Ball getBall() {
return this.ball;
}
/**
* Sets ballWasStarted to true, the ball is moving
*/
public void startBall() {
ballWasStarted = true;
}
/**
* The ball of the level
*/
private Ball ball;
/**
* Sets ballWasStarted to false, the ball is stopped
*/
public void stopBall() {
ballWasStarted = false;
}
/**
* Returns if the ball is moving or stopped
* @return ballWasStarted True: the ball is moving; false: the ball is stopped
*/
public boolean ballWasStarted() {
return ballWasStarted;
}
/**
* Flag that shows if the ball was started
*/
private boolean ballWasStarted = true;
/**
* The paddles of the level
*/
private Paddle paddleTop, paddleBottom;
/**
* The constructor creates a new level object and needs the current game object,
* the number of the level to be created and the current score
*
* @param game The game object
* @param levelnr The number of the new level object
* @param score The score
*/
public Level(Game game, int levelnr, int score) {
this.game = game;
this.levelnr = levelnr;
this.score = score;
this.ball = new Ball();
// calc paddle positions
Position posPaddleTop = new Position((Constants.SCREEN_WIDTH - Constants.PADDLE_WIDTH) / 2.0, 0);
Position posPaddleBottom = new Position((Constants.SCREEN_WIDTH - Constants.PADDLE_WIDTH) / 2.0, Constants.SCREEN_HEIGHT - Constants.PADDLE_HEIGHT);
// set paddles
this.paddleTop = new Paddle(posPaddleTop);
this.paddleBottom = new Paddle(posPaddleBottom);
loadLevelData(levelnr);
}
/**
* The getter for the ball object
*
* @return ball The ball of the level
*/
public Ball getBall() {
return this.ball;
}
/**
* Sets ballWasStarted to true, the ball is moving
*/
public void startBall() {
ballWasStarted = true;
}
/**
* Sets ballWasStarted to false, the ball is stopped
*/
public void stopBall() {
ballWasStarted = false;
}
/**
* Returns if the ball is moving or stopped
*
* @return ballWasStarted True: the ball is moving; false: the ball is stopped
*/
public boolean ballWasStarted() {
return ballWasStarted;
}
/**
* The method of the level thread
*/
public void run() {
game.notifyObservers();
// endless loop
while (true) {
// if ballWasStarted is true, the ball is moving
if (ballWasStarted) {
// Call here the balls method for updating his position on the playground
this.ball.updatePosition();
// Call here the balls method for reacting on the borders of the playground
this.ball.reactOnBorder();
// Tells the observer to repaint the components on the playground
game.notifyObservers();
}
// The thread pauses for a short time
try {
Thread.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* Loads the information for the level from a json-file located in the folder /res of the project
*
* @param levelnr The number X for the LevelX.json file
*/
private void loadLevelData(int levelnr) {
}
/**
* The getter for the top paddle object
*
* @return paddleTop The top paddle of the level
*/
public Paddle getPaddleTop() {
return paddleTop;
}
/**
* The getter for the bottom paddle object
*
* @return paddleBottom The bottom paddle of the level
*/
public Paddle getPaddleBottom() {
return paddleBottom;
}
/**
* The method of the level thread
*/
public void run() {
game.notifyObservers();
// endless loop
while (true) {
// if ballWasStarted is true, the ball is moving
if (ballWasStarted) {
// Call here the balls method for updating his position on the playground
this.ball.updatePosition();
// Call here the balls method for reacting on the borders of the playground
this.ball.reactOnBorder();
// Tells the observer to repaint the components on the playground
game.notifyObservers();
}
// The thread pauses for a short time
try {
Thread.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* Loads the information for the level from a json-file located in the folder /res of the project
* @param levelnr The number X for the LevelX.json file
*/
private void loadLevelData(int levelnr) {
}
}

View File

@ -0,0 +1,115 @@
package break_out.model;
import break_out.Constants;
import java.awt.*;
/**
* This class contains the information about the paddles characteristics and behavior
*
* @author Gruppe 175: Moritz Henseleit, Ruben Meyer
*/
public class Paddle implements IPaddle {
/**
* The paddles position on the playground
*/
private Position position;
/**
* The paddles sizing
*/
private double width;
private double height;
/**
* The paddles color
*/
private Color color;
/**
* The constructor of a paddle
*/
public Paddle(Position position) {
this.position = position;
// set sizing
width = Constants.PADDLE_WIDTH;
height = Constants.PADDLE_HEIGHT;
// set color
color = Color.CYAN;
}
/**
* The getter for the paddles position
*
* @return position The paddles current position
*/
public Position getPosition() {
return position;
}
/**
* The setter for the paddles position
*
* @param position The paddles new position
*/
public void setPosition(Position position) {
this.position = position;
}
/**
* The getter for the paddles color
*
* @return color The paddles current color
*/
public Color getColor() {
return color;
}
/**
* The setter for the paddles color
*
* @param color The paddles new color
*/
public void setColor(Color color) {
this.color = color;
}
/**
* The getter for the paddles width
*
* @return width The paddles current width
*/
public double getWidth() {
return width;
}
/**
* The setter for the paddles width
*
* @param width The paddles new width
*/
public void setWidth(double width) {
this.width = width;
}
/**
* The getter for the paddles height
*
* @return height The paddles current height
*/
public double getHeight() {
return height;
}
/**
* The setter for the paddles height
*
* @param height The paddles new height
*/
public void setHeight(double height) {
this.height = height;
}
}

View File

@ -3,9 +3,8 @@ package break_out.model;
/**
* This class represents a position within the board in pixel coordinates
*
*
* @author dmlux
*
*/
public class Position {
@ -18,10 +17,10 @@ public class Position {
* Y coordinate
*/
private double y;
/**
* The constructor needs a x and y coordinate to be called
*
*
* @param x The x position of the object on the board
* @param y The y position of the object on the board
*/
@ -32,7 +31,7 @@ public class Position {
/**
* Getter for the x-coordinate
*
*
* @return x The x value of this position
*/
public double getX() {
@ -41,6 +40,7 @@ public class Position {
/**
* Setter for the x-coordinate
*
* @param x The new x-coordinate
*/
public void setX(double x) {
@ -49,7 +49,7 @@ public class Position {
/**
* Getter for y-coordinate
*
*
* @return y The y value of the position
*/
public double getY() {
@ -58,10 +58,11 @@ public class Position {
/**
* Setter for the y-coordinate
*
* @param y The new y-coordinate
*/
public void setY(double y) {
this.y = y;
}
}

View File

@ -5,7 +5,7 @@ import break_out.model.Position;
/**
* This class represent a two dimensional vector.
*
*
* @author I. Schumacher
* @author modified by Gruppe 175: Moritz Henseleit, Ruben Meyer
*/
@ -23,7 +23,7 @@ public class Vector2D implements IVector2D {
/**
* This constructor creates a new vector with the given x and y parts.
*
*
* @param dx the delta x part for the new vector
* @param dy the delty y part for the new vector
*/
@ -34,7 +34,7 @@ public class Vector2D implements IVector2D {
/**
* Getter for the dx-part
*
*
* @return dx The dx part of this vector
*/
public double getDx() {
@ -43,7 +43,7 @@ public class Vector2D implements IVector2D {
/**
* Setter for the dx-part
*
*
* @param dx The new dx part of this vector
*/
public void setDx(double dx) {
@ -52,7 +52,7 @@ public class Vector2D implements IVector2D {
/**
* Getter for the dy-part
*
*
* @return dy The dy part of this vector
*/
public double getDy() {
@ -61,7 +61,7 @@ public class Vector2D implements IVector2D {
/**
* Setter for the dy-part
*
*
* @param dy The new dy part of this vector
*/
public void setDy(double dy) {
@ -74,7 +74,7 @@ public class Vector2D implements IVector2D {
public void rescale() {
// calc unit vector and set it
double vectorLength = Math.sqrt(Math.pow(getDx(), 2) + Math.pow(getDy(), 2)); //using the square root of x and y
setDx((1/vectorLength) * getDx() * Constants.BALL_SPEED);
setDy((1/vectorLength) * getDy() * Constants.BALL_SPEED);
setDx((1 / vectorLength) * getDx() * Constants.BALL_SPEED);
setDy((1 / vectorLength) * getDy() * Constants.BALL_SPEED);
}
}

View File

@ -13,7 +13,7 @@ import net.miginfocom.swing.MigLayout;
/**
* The field represents the board of the game. All components are on the board
*
*
* @author dmlux, modified by iSchumacher
* @author modified by Gruppe 175: Moritz Henseleit, Ruben Meyer
*/
@ -36,7 +36,7 @@ public class Field extends JPanel {
/**
* The constructor needs a view
*
*
* @param view The view of this board
*/
public Field(View view) {
@ -61,15 +61,17 @@ public class Field extends JPanel {
/**
* Change the background color
*
* @param color The new color
*/
public void changeBackground(Color color) {
background = color;
repaint();
}
/**
* This method is called when painting/repainting the playground
*
* @param g the graphics object
*/
@Override
@ -91,20 +93,27 @@ public class Field extends JPanel {
// Setting the background color
g2.setColor(background);
g2.fillRect(0, 0, getWidth(), getHeight());
// Setting the color for the following components
g2.setColor(new Color(200, 200, 200));
// Calls the method for drawing the ball
drawBall(g2);
// Calls the method for drawing the grid
drawGrid(g2);
// Calls the method for drawing the bottom paddle
drawPaddleBottom(g2);
// Calls the method for drawing the top paddle
drawPaddleTop(g2);
}
/**
* Draws the ball
*
* @param g2 The graphics object
*/
private void drawBall(Graphics2D g2) {
@ -116,6 +125,7 @@ public class Field extends JPanel {
/**
* Draws the grid
*
* @param g2 The graphics object
*/
private void drawGrid(Graphics2D g2) {
@ -125,14 +135,44 @@ public class Field extends JPanel {
int blockHeight = Constants.SCREEN_HEIGHT / Constants.SQUARES_Y;
// draw vertical lines
for(int i = 1 ; i < Constants.SQUARES_X; i++) {
g2.drawLine(i*blockWidth, 0, i*blockWidth, Constants.SCREEN_HEIGHT);
for (int i = 1; i < Constants.SQUARES_X; i++) {
g2.drawLine(i * blockWidth, 0, i * blockWidth, Constants.SCREEN_HEIGHT);
}
// draw horizontal lines
for(int i = 1 ; i < Constants.SQUARES_Y; i++) {
g2.drawLine(0, i*blockHeight, Constants.SCREEN_WIDTH, i*blockHeight);
for (int i = 1; i < Constants.SQUARES_Y; i++) {
g2.drawLine(0, i * blockHeight, Constants.SCREEN_WIDTH, i * blockHeight);
}
}
/**
* Draws the bottom paddle
*
* @param g2 The graphics object
*/
private void drawPaddleBottom(Graphics2D g2) {
// fillRoundRect(x, y, width, height, arcWidth, arcHeight)
g2.fillRoundRect((int) view.getGame().getLevel().getPaddleBottom().getPosition().getX(),
(int) view.getGame().getLevel().getPaddleBottom().getPosition().getY(),
(int) view.getGame().getLevel().getPaddleBottom().getWidth(),
(int) view.getGame().getLevel().getPaddleBottom().getHeight(),
10,
10);
}
/**
* Draws the top paddle
*
* @param g2 The graphics object
*/
private void drawPaddleTop(Graphics2D g2) {
// fillRoundRect(x, y, width, height, arcWidth, arcHeight)
g2.fillRoundRect((int) view.getGame().getLevel().getPaddleTop().getPosition().getX(),
(int) view.getGame().getLevel().getPaddleTop().getPosition().getY(),
(int) view.getGame().getLevel().getPaddleTop().getWidth(),
(int) view.getGame().getLevel().getPaddleTop().getHeight(),
10,
10);
}
}

View File

@ -14,7 +14,6 @@ import javax.swing.JPanel;
* This panel represents the background for special divisions in this application
*
* @author dmlux
*
*/
public class SectionPanel extends JPanel {

View File

@ -16,9 +16,8 @@ import net.miginfocom.swing.MigLayout;
/**
* This screen serves the configuration of the game.
*
*
* @author dmlux, modified by I. Schumacher
*
*/
public class StartScreen extends JPanel {
@ -52,10 +51,10 @@ public class StartScreen extends JPanel {
*/
private JLabel error;
/**
* The constructor needs a view
*
*
* @param view The view of this board
*/
public StartScreen(View view) {
@ -71,7 +70,7 @@ public class StartScreen extends JPanel {
initialize();
}
/**
* Initializes the settings for this screen
*/
@ -136,12 +135,13 @@ public class StartScreen extends JPanel {
headline.setFont(new Font("Sans-serif", Font.PLAIN, 16));
headline.setHorizontalAlignment(SwingConstants.CENTER);
scoreMenu.add(headline, "cell 0 0, gaptop 5");
add(scoreMenu, "cell 1 0, gapleft 5");
}
/**
* Adds an action listener to the start button
*
* @param l The actionListener
*/
public void addActionListenerToStartButton(ActionListener l) {
@ -150,6 +150,7 @@ public class StartScreen extends JPanel {
/**
* Returns the start button
*
* @return startGame The button for starting the game
*/
public JButton getStartButton() {
@ -158,6 +159,7 @@ public class StartScreen extends JPanel {
/**
* Adds an action listener to the quit button
*
* @param l The actionListener
*/
public void addActionListenerToQuitButton(ActionListener l) {
@ -166,6 +168,7 @@ public class StartScreen extends JPanel {
/**
* Returns the quit button
*
* @return quitGame The button for ending the game
*/
public JButton getQuitButton() {
@ -174,6 +177,7 @@ public class StartScreen extends JPanel {
/**
* Returns the players name
*
* @return The name of the player in the JTextField playersName
*/
public String getPlayersName() {
@ -182,6 +186,7 @@ public class StartScreen extends JPanel {
/**
* Shows an error in the menu
*
* @param message The String to be shown
*/
public void showError(String message) {

View File

@ -10,12 +10,11 @@ import break_out.model.Game;
/**
* The view class manages the depiction of the components inside the JFrames. It
* gets the components from the game which is connected to this class
*
*
* @author dmlux
* @author modified by Gruppe 175: Moritz Henseleit, Ruben Meyer
*
*/
public class View extends JFrame {
public class View extends JFrame {
/**
* Automatic generated serial version UID
@ -42,7 +41,7 @@ public class View extends JFrame {
*/
private Field field;
/**
* The constructor of the view
*/
@ -78,6 +77,7 @@ public class View extends JFrame {
/**
* Getter for the start screen
*
* @return startScreen
*/
public StartScreen getStartScreen() {
@ -86,6 +86,7 @@ public class View extends JFrame {
/**
* Getter for the playground
*
* @return field
*/
public Field getField() {
@ -94,14 +95,16 @@ public class View extends JFrame {
/**
* Getter for the game
*
* @return game
*/
public Game getGame() {
return game;
}
/**
* Setter for the game
*
* @param game The current game
*/
public void setGame(Game game) {
@ -109,9 +112,10 @@ public class View extends JFrame {
this.game = game;
game.addObserver(this);
}
/**
* Shows a given screen if the card layout contains this screen
*
* @param screenName The screen to be shown
*/
public void showScreen(String screenName) {
@ -121,6 +125,7 @@ public class View extends JFrame {
/**
* Called by game.notifyObservers() in the run()-method of Level class
* to repaint the playground
*
* @param game The game to observe
*/
public void modelChanged(Game game) {