Advertisement
Guest User

BreakOut Game.java

a guest
Jan 30th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.28 KB | None | 0 0
  1. /**
  2.  * This object contains information about the running game.
  3.  *
  4.  * Author:                    Denis M. Lux;      Version: 1.0   Date: 2014-08-22
  5.  * modified to project frame: Ingrid Schumacher; Version: 0.5   Date: 2014-11-20
  6.  * @author D. M. Lux, modified by I. Schumacher
  7.  * @version: 1.5
  8.  *
  9.  * modified by         :
  10.  * matriculation number:
  11.  * university course   :
  12.  */
  13.  
  14.  
  15. package break_out.model;
  16.  
  17. import java.awt.event.ActionEvent;
  18.  
  19. import break_out.Constants;
  20. import break_out.controller.JSONReader;
  21. import break_out.controller.Controller;
  22. import break_out.view.StartScreen;
  23. import break_out.view.View;
  24.  
  25. public class Game extends Thread {
  26.  
  27.     /**
  28.      * The constants of the field and the ball.
  29.      */
  30.     double screenWidth = Constants.SCREEN_WIDTH;
  31.     double screenHeight = Constants.SCREEN_HEIGHT;
  32.     double d = Constants.BALL_DIAMETER;
  33.     double paddleWidth = Constants.PADDLE_WIDTH;
  34.    
  35.     //Hier die Variablen dx und dy fuer die Ballrichtung ergaenzen
  36.    
  37.     Vector2D vector = new Vector2D(5 ,5);
  38.    
  39.    
  40.     public static boolean pause = true;
  41.    
  42.     public static boolean end = false;
  43.    
  44.     public static boolean moveLeft = false;
  45.    
  46.     public static boolean moveRight = false;
  47.    
  48.     /**
  49.      * The model of this game.
  50.      */
  51.     private Model model;
  52.  
  53.     /**
  54.      * The ball position
  55.      */
  56.     private Position ballPos;
  57.    
  58.     private View view;
  59.    
  60.     private Controller controller;
  61.    
  62.     private int[][] stones;
  63.    
  64.     private boolean stoneCollision = false;
  65.    
  66.     public int lifecount;
  67.     /**
  68.      * The paddle position
  69.      */
  70.     private Position paddlePos;
  71.    
  72.     /**
  73.      * Flag that shows if the game was started
  74.      */
  75.     private boolean wasStarted = false;
  76.  
  77.  
  78.    
  79.     /**
  80.      * The constructor expects the current position of the ball and and model
  81.      *
  82.      * @param ballPos
  83.      *            The position of the ball
  84.      *
  85.      * @param model
  86.      *            The connected model
  87.      */
  88.     public Game(Position ballPos, Position paddlePos, Model model, JSONReader json, View view, Controller controller) {
  89.         this.ballPos = ballPos;
  90.         this.model = model;
  91.         this.paddlePos = paddlePos;
  92.         this.stones = json.getStones2DArray();
  93.         this.lifecount = json.getLifeCounter();
  94.         this.view = view;
  95.         this.controller = controller;
  96.     }
  97.  
  98.     /**
  99.      * Getter for the ball position
  100.      * @return ballPos
  101.      *            The position of the ball
  102.      */
  103.     public Position getBallPosition() {
  104.         return ballPos;
  105.     }
  106.    
  107.     /**
  108.      * Getter for the paddle position
  109.      * @return paddlePos
  110.      *              The position of the paddle
  111.      */
  112.     public Position getPaddlePosition() {
  113.         return paddlePos;
  114.     }
  115.    
  116.     public int[][] getStones(){
  117.         return stones;
  118.     }
  119.     /**
  120.      * Starts a game
  121.      */
  122.     public void startGame() {
  123.         wasStarted = true;
  124.     }
  125.  
  126.     /**
  127.      * Shows if the game was stared
  128.      * @return wasStarted
  129.      *            If the game was started
  130.      */
  131.     public boolean wasStarted() {
  132.         return wasStarted;
  133.     }
  134.    
  135.    
  136.     /**
  137.      *  Normalizer for the speed of the ball
  138.      */
  139.     public void normalize(){
  140.         vector.normalize(Constants.BALL_SPEED);
  141.     }
  142.  
  143.     /**
  144.      * This method is the thread logic.
  145.      */
  146.     public void run() {
  147.        
  148.        
  149.         while (true) {
  150.             // if game is startet
  151.             if (wasStarted && !pause) {
  152.                
  153.                      // System.out.println(ballPos.getX()+"   ||   "+ballPos.getY() );
  154.                    
  155.                     // Contact with the left border
  156.                     if(ballPos.getX()<=0)
  157.                     {
  158.                      vector.setDX(vector.getDX()* -1);
  159.                     }
  160.                    
  161.                     // Contact with the right border
  162.                     if(ballPos.getX()+Constants.BALL_DIAMETER>=screenWidth)
  163.                     {
  164.                      vector.setDX(vector.getDX()* -1);
  165.                     }
  166.                    
  167.                     // Contact with the top border
  168.                     if(ballPos.getY()<=0)
  169.                     {
  170.                      vector.setDY(vector.getDY()* -1);
  171.                     }
  172.                    
  173.                     if (end == true) {
  174.                         this.view.showScreen(StartScreen.class.getName());
  175.                         end = false;
  176.                         pause = true;
  177.                         break;
  178.                     }
  179.                    
  180.                     // Contact with the bottom border
  181.                     if(ballPos.getY()+Constants.BALL_DIAMETER>=screenHeight)
  182.                     {
  183.                         ballPos.setPos(screenWidth/2 - Constants.BALL_DIAMETER/2, screenHeight - Constants.BALL_DIAMETER - Constants.PADDLE_HEIGHT);
  184.                         paddlePos.setPos(screenWidth/2 - paddleWidth/2, screenHeight - Constants.PADDLE_HEIGHT);
  185.                         pause = true;
  186.                         lifecount--;
  187.                        
  188.                         if (lifecount == 0) {
  189.                         //  view.cardLayout.show(view.getContentPane(), StartScreen.class.getName());
  190.                             this.view.showScreen(StartScreen.class.getName());
  191.                             break;
  192.                             //controller.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_FIRST+1, ""));
  193.                         }
  194.                         System.out.println(end);
  195.                         System.out.println(lifecount);
  196.  
  197.                     }
  198.                     //System.out.println(ballPos.getX()+"   ||   "+ballPos.getY() );
  199.                    
  200.                     // Contact with the paddletop
  201.                     if (ballPos.getX()+Constants.BALL_DIAMETER >= paddlePos.getX() + 10
  202.                             && ballPos.getX() <= paddlePos.getX() + paddleWidth - 10
  203.                             && ballPos.getY()+Constants.BALL_DIAMETER >= screenHeight - Constants.PADDLE_HEIGHT
  204.                             && vector.getDY() > 0)
  205.                     {
  206.                      vector.setDY(vector.getDY()* -1);
  207.                     }
  208.                     // Contact with the left side of paddle
  209.                     if (ballPos.getX()+Constants.BALL_DIAMETER >= paddlePos.getX()
  210.                             && ballPos.getX()+Constants.BALL_DIAMETER <= paddlePos.getX() + 10
  211.                             && ballPos.getY()+Constants.BALL_DIAMETER >= screenHeight - Constants.PADDLE_HEIGHT
  212.                             && vector.getDY() > 0)
  213.                     {
  214.                      vector.setDX(vector.getDX()* -1);
  215.                      vector.setDY(vector.getDY()* -1);
  216.                     }
  217.                    
  218.                     // Contact with the right side if paddle
  219.                     if (ballPos.getX() >= paddlePos.getX() + paddleWidth - 10
  220.                             && ballPos.getX() <= paddlePos.getX() + paddleWidth
  221.                             && ballPos.getY()+ Constants.BALL_DIAMETER >= screenHeight - Constants.PADDLE_HEIGHT
  222.                             && vector.getDY() > 0)
  223.                     {
  224.                      vector.setDX(vector.getDX()* -1);
  225.                      vector.setDY(vector.getDY()* -1);
  226.                     }
  227.                    
  228.                     // The paddle movement to the right
  229.                     if (moveRight == true) {
  230.                         paddlePos.setX(paddlePos.getX() + Constants.DX_MOVEMENT);
  231.                     }
  232.                     // System.out.println(paddlePos.getX()+"   ||   "+paddlePos.getY() );
  233.                    
  234.                     // The paddle movement to the left
  235.                     if (moveLeft == true) {
  236.                         paddlePos.setX(paddlePos.getX() - Constants.DX_MOVEMENT);
  237.                     }
  238.                    
  239.                     // Stopping of the paddle on the left
  240.                     if (paddlePos.getX() <= 0) {
  241.                         paddlePos.setX(0);
  242.                     }
  243.                    
  244.                     // Stopping of the paddle on the right
  245.                     if (paddlePos.getX() >= screenWidth - paddleWidth) {
  246.                         paddlePos.setX(screenWidth - paddleWidth);
  247.                     }
  248.                    
  249.                     for ( int i = 0; i < stones.length; i++) {
  250.                         for ( int j = 0; j < stones[i].length; j++){
  251.                             if (stones[i][j] == 1) {
  252.                                 this.stoneCollision (i, j);
  253.                            
  254.                             }
  255.                         }
  256.                     }
  257.                        
  258.                    
  259.                   //  if (ballPos.getX()+Constants.BALL_DIAMETER)
  260.                        
  261.                 this.normalize();
  262.                
  263.                  ballPos.setPos(ballPos.getX()+vector.getDX(), ballPos.getY()+vector.getDY());
  264.                  
  265.                
  266.            
  267.                
  268.                 // update view
  269.                 model.notifyObservers();
  270.             }
  271.             // pause thread by a few millis
  272.             try {
  273.                 Thread.sleep(4);
  274.             } catch (InterruptedException e) {
  275.                 e.printStackTrace();
  276.             }
  277.            
  278.         }
  279.        
  280.     }
  281.    
  282.     private boolean stoneCollision (int i, int j){
  283.         //if (stones[i][j] == 1){
  284.        
  285.            double stoneWidth = screenWidth/Constants.SQUARES_X;
  286.            double stoneHeight = screenHeight/Constants.SQUARES_Y;
  287.        
  288.            // Contact with the stonetop
  289.            if (ballPos.getX()+Constants.BALL_DIAMETER >= stones[i][j]
  290.               && ballPos.getX() <= stones[i][j] + stoneWidth
  291.               && ballPos.getY()+Constants.BALL_DIAMETER >= stones[i][j]
  292.               && ballPos.getY()+Constants.BALL_DIAMETER <= stones[i][j]
  293.               && vector.getDY() > 0)
  294.            {
  295.             vector.setDY(vector.getDY()* -1);
  296.            }
  297.            // Contact with the left side of stone
  298.            if (ballPos.getX()+Constants.BALL_DIAMETER >= stones[i][j]
  299.               && ballPos.getX()+Constants.BALL_DIAMETER <= stones[i][j]
  300.               && ballPos.getY()+Constants.BALL_DIAMETER >= stones[i][j]
  301.               && ballPos.getY()+Constants.BALL_DIAMETER <= stones[i][j] + stoneHeight
  302.               && vector.getDX() > 0)
  303.            {
  304.             vector.setDX(vector.getDX()* -1);
  305.             vector.setDY(vector.getDY()* -1);
  306.            }
  307.        
  308.            // Contact with the right side if stone
  309.            if (ballPos.getX() >= stones[i][j] + stoneWidth
  310.               && ballPos.getX() <= stones[i][j] + stoneWidth
  311.               && ballPos.getY()+ Constants.BALL_DIAMETER >= stones[i][j]
  312.               && ballPos.getY()+ Constants.BALL_DIAMETER >= stones[i][j] + stoneHeight
  313.               && vector.getDX() < 0)
  314.            {
  315.             vector.setDX(vector.getDX()* -1);
  316.             vector.setDY(vector.getDY()* -1);
  317.            }
  318.        
  319.            // Contact with the bottom of the stone
  320.            if (ballPos.getX()+Constants.BALL_DIAMETER >= stones[i][j]
  321.               && ballPos.getX() <= stones[i][j] + stoneWidth
  322.               && ballPos.getY()+Constants.BALL_DIAMETER >= stones[i][j] + stoneHeight
  323.               && ballPos.getY()+Constants.BALL_DIAMETER <= stones[i][j] + stoneHeight
  324.               && vector.getDY() < 0)
  325.            {
  326.             vector.setDY(vector.getDY()* -1);
  327.            }
  328.         return true;
  329.     }
  330.  
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement