Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This object contains information about the running game.
- *
- * Author: Denis M. Lux; Version: 1.0 Date: 2014-08-22
- * modified to project frame: Ingrid Schumacher; Version: 0.5 Date: 2014-11-20
- * @author D. M. Lux, modified by I. Schumacher
- * @version: 1.5
- *
- * modified by :
- * matriculation number:
- * university course :
- */
- package break_out.model;
- import java.awt.event.ActionEvent;
- import break_out.Constants;
- import break_out.controller.JSONReader;
- import break_out.controller.Controller;
- import break_out.view.StartScreen;
- import break_out.view.View;
- public class Game extends Thread {
- /**
- * The constants of the field and the ball.
- */
- double screenWidth = Constants.SCREEN_WIDTH;
- double screenHeight = Constants.SCREEN_HEIGHT;
- double d = Constants.BALL_DIAMETER;
- double paddleWidth = Constants.PADDLE_WIDTH;
- //Hier die Variablen dx und dy fuer die Ballrichtung ergaenzen
- Vector2D vector = new Vector2D(5 ,5);
- public static boolean pause = true;
- public static boolean end = false;
- public static boolean moveLeft = false;
- public static boolean moveRight = false;
- /**
- * The model of this game.
- */
- private Model model;
- /**
- * The ball position
- */
- private Position ballPos;
- private View view;
- private Controller controller;
- private int[][] stones;
- private boolean stoneCollision = false;
- public int lifecount;
- /**
- * The paddle position
- */
- private Position paddlePos;
- /**
- * Flag that shows if the game was started
- */
- private boolean wasStarted = false;
- /**
- * The constructor expects the current position of the ball and and model
- *
- * @param ballPos
- * The position of the ball
- *
- * @param model
- * The connected model
- */
- public Game(Position ballPos, Position paddlePos, Model model, JSONReader json, View view, Controller controller) {
- this.ballPos = ballPos;
- this.model = model;
- this.paddlePos = paddlePos;
- this.stones = json.getStones2DArray();
- this.lifecount = json.getLifeCounter();
- this.view = view;
- this.controller = controller;
- }
- /**
- * Getter for the ball position
- * @return ballPos
- * The position of the ball
- */
- public Position getBallPosition() {
- return ballPos;
- }
- /**
- * Getter for the paddle position
- * @return paddlePos
- * The position of the paddle
- */
- public Position getPaddlePosition() {
- return paddlePos;
- }
- public int[][] getStones(){
- return stones;
- }
- /**
- * Starts a game
- */
- public void startGame() {
- wasStarted = true;
- }
- /**
- * Shows if the game was stared
- * @return wasStarted
- * If the game was started
- */
- public boolean wasStarted() {
- return wasStarted;
- }
- /**
- * Normalizer for the speed of the ball
- */
- public void normalize(){
- vector.normalize(Constants.BALL_SPEED);
- }
- /**
- * This method is the thread logic.
- */
- public void run() {
- while (true) {
- // if game is startet
- if (wasStarted && !pause) {
- // System.out.println(ballPos.getX()+" || "+ballPos.getY() );
- // Contact with the left border
- if(ballPos.getX()<=0)
- {
- vector.setDX(vector.getDX()* -1);
- }
- // Contact with the right border
- if(ballPos.getX()+Constants.BALL_DIAMETER>=screenWidth)
- {
- vector.setDX(vector.getDX()* -1);
- }
- // Contact with the top border
- if(ballPos.getY()<=0)
- {
- vector.setDY(vector.getDY()* -1);
- }
- if (end == true) {
- this.view.showScreen(StartScreen.class.getName());
- end = false;
- pause = true;
- break;
- }
- // Contact with the bottom border
- if(ballPos.getY()+Constants.BALL_DIAMETER>=screenHeight)
- {
- ballPos.setPos(screenWidth/2 - Constants.BALL_DIAMETER/2, screenHeight - Constants.BALL_DIAMETER - Constants.PADDLE_HEIGHT);
- paddlePos.setPos(screenWidth/2 - paddleWidth/2, screenHeight - Constants.PADDLE_HEIGHT);
- pause = true;
- lifecount--;
- if (lifecount == 0) {
- // view.cardLayout.show(view.getContentPane(), StartScreen.class.getName());
- this.view.showScreen(StartScreen.class.getName());
- break;
- //controller.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_FIRST+1, ""));
- }
- System.out.println(end);
- System.out.println(lifecount);
- }
- //System.out.println(ballPos.getX()+" || "+ballPos.getY() );
- // Contact with the paddletop
- if (ballPos.getX()+Constants.BALL_DIAMETER >= paddlePos.getX() + 10
- && ballPos.getX() <= paddlePos.getX() + paddleWidth - 10
- && ballPos.getY()+Constants.BALL_DIAMETER >= screenHeight - Constants.PADDLE_HEIGHT
- && vector.getDY() > 0)
- {
- vector.setDY(vector.getDY()* -1);
- }
- // Contact with the left side of paddle
- if (ballPos.getX()+Constants.BALL_DIAMETER >= paddlePos.getX()
- && ballPos.getX()+Constants.BALL_DIAMETER <= paddlePos.getX() + 10
- && ballPos.getY()+Constants.BALL_DIAMETER >= screenHeight - Constants.PADDLE_HEIGHT
- && vector.getDY() > 0)
- {
- vector.setDX(vector.getDX()* -1);
- vector.setDY(vector.getDY()* -1);
- }
- // Contact with the right side if paddle
- if (ballPos.getX() >= paddlePos.getX() + paddleWidth - 10
- && ballPos.getX() <= paddlePos.getX() + paddleWidth
- && ballPos.getY()+ Constants.BALL_DIAMETER >= screenHeight - Constants.PADDLE_HEIGHT
- && vector.getDY() > 0)
- {
- vector.setDX(vector.getDX()* -1);
- vector.setDY(vector.getDY()* -1);
- }
- // The paddle movement to the right
- if (moveRight == true) {
- paddlePos.setX(paddlePos.getX() + Constants.DX_MOVEMENT);
- }
- // System.out.println(paddlePos.getX()+" || "+paddlePos.getY() );
- // The paddle movement to the left
- if (moveLeft == true) {
- paddlePos.setX(paddlePos.getX() - Constants.DX_MOVEMENT);
- }
- // Stopping of the paddle on the left
- if (paddlePos.getX() <= 0) {
- paddlePos.setX(0);
- }
- // Stopping of the paddle on the right
- if (paddlePos.getX() >= screenWidth - paddleWidth) {
- paddlePos.setX(screenWidth - paddleWidth);
- }
- for ( int i = 0; i < stones.length; i++) {
- for ( int j = 0; j < stones[i].length; j++){
- if (stones[i][j] == 1) {
- this.stoneCollision (i, j);
- }
- }
- }
- // if (ballPos.getX()+Constants.BALL_DIAMETER)
- this.normalize();
- ballPos.setPos(ballPos.getX()+vector.getDX(), ballPos.getY()+vector.getDY());
- // update view
- model.notifyObservers();
- }
- // pause thread by a few millis
- try {
- Thread.sleep(4);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- private boolean stoneCollision (int i, int j){
- //if (stones[i][j] == 1){
- double stoneWidth = screenWidth/Constants.SQUARES_X;
- double stoneHeight = screenHeight/Constants.SQUARES_Y;
- // Contact with the stonetop
- if (ballPos.getX()+Constants.BALL_DIAMETER >= stones[i][j]
- && ballPos.getX() <= stones[i][j] + stoneWidth
- && ballPos.getY()+Constants.BALL_DIAMETER >= stones[i][j]
- && ballPos.getY()+Constants.BALL_DIAMETER <= stones[i][j]
- && vector.getDY() > 0)
- {
- vector.setDY(vector.getDY()* -1);
- }
- // Contact with the left side of stone
- if (ballPos.getX()+Constants.BALL_DIAMETER >= stones[i][j]
- && ballPos.getX()+Constants.BALL_DIAMETER <= stones[i][j]
- && ballPos.getY()+Constants.BALL_DIAMETER >= stones[i][j]
- && ballPos.getY()+Constants.BALL_DIAMETER <= stones[i][j] + stoneHeight
- && vector.getDX() > 0)
- {
- vector.setDX(vector.getDX()* -1);
- vector.setDY(vector.getDY()* -1);
- }
- // Contact with the right side if stone
- if (ballPos.getX() >= stones[i][j] + stoneWidth
- && ballPos.getX() <= stones[i][j] + stoneWidth
- && ballPos.getY()+ Constants.BALL_DIAMETER >= stones[i][j]
- && ballPos.getY()+ Constants.BALL_DIAMETER >= stones[i][j] + stoneHeight
- && vector.getDX() < 0)
- {
- vector.setDX(vector.getDX()* -1);
- vector.setDY(vector.getDY()* -1);
- }
- // Contact with the bottom of the stone
- if (ballPos.getX()+Constants.BALL_DIAMETER >= stones[i][j]
- && ballPos.getX() <= stones[i][j] + stoneWidth
- && ballPos.getY()+Constants.BALL_DIAMETER >= stones[i][j] + stoneHeight
- && ballPos.getY()+Constants.BALL_DIAMETER <= stones[i][j] + stoneHeight
- && vector.getDY() < 0)
- {
- vector.setDY(vector.getDY()* -1);
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement