CS106A assignment4 --gameBreakout

4683 단어
    /*
     * File: Breakout.java
     * -------------------
     * Name:ZhendongYi
     * Email:[email protected]
     * Time:2016/07/02
     * 
     * This file will eventually implement the game of Breakout.
     */
    
    import acm.graphics.*;
    import acm.program.*;
    import acm.util.*;
    
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Breakout extends GraphicsProgram {
    
        /** Width and height of application window in pixels */
        public static final int APPLICATION_WIDTH = 400;
        public static final int APPLICATION_HEIGHT = 600;
    
        /** Dimensions of game board (usually the same) */
        private static final int WIDTH = APPLICATION_WIDTH;
        private static final int HEIGHT = APPLICATION_HEIGHT;
    
        /** Dimensions of the paddle */
        private static final int PADDLE_WIDTH = 60;
        private static final int PADDLE_HEIGHT = 10;
    
        /** Offset of the paddle up from the bottom */
        private static final int PADDLE_Y_OFFSET = 30;
    
        /** Number of bricks per row */
        private static final int NBRICKS_PER_ROW = 10;
    
        /** Number of rows of bricks */
        private static final int NBRICK_ROWS = 10;
    
        /** Separation between bricks */
        private static final int BRICK_SEP = 4;
    
        /** Width of a brick */
        private static final int BRICK_WIDTH =
          (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;
    
        /** Height of a brick */
        private static final int BRICK_HEIGHT = 8;
    
        /** Radius of the ball in pixels */
        private static final int BALL_RADIUS = 10;
    
        /** Offset of the top brick row from the top */
        private static final int BRICK_Y_OFFSET = 70;
    
        /** Number of turns */
        private static final int NTURNS = 3;
        
        /** Color of bricks */
        private static final Color[] colors={Color.red,Color.red,Color.orange,Color.orange,
                Color.yellow,Color.yellow,Color.green,Color.green,Color.blue,Color.blue};
    
        public void run() {
            createPaddle();
            createBricks();
            createBallAndBounce();
            
    
        }
    
        private GRect paddle;
        
        /** coordinates of the paddle */
        private static int paddleX = (WIDTH - PADDLE_WIDTH)/2;
        private static int paddleY = (HEIGHT - PADDLE_HEIGHT-PADDLE_Y_OFFSET);
        
        /** create a paddle */
        private void createPaddle() {
            
            paddle = new GRect(paddleX,paddleY,PADDLE_WIDTH,PADDLE_HEIGHT);
            paddle.setFilled(true);
            add(paddle);
            
            addMouseListeners();
        }
        
        /** when mouse moves the paddle moves horizontally , too*/
        public void mouseMoved(MouseEvent e){
            paddleX = e.getX();
            if(paddleX > WIDTH - PADDLE_WIDTH) paddleX = WIDTH - PADDLE_WIDTH;
            paddle.setLocation(paddleX,paddleY);
        }
        
        /** create several rows bricks */
        public void createBricks(){
            for(int i = 0;i=HEIGHT;
        }
    
        private boolean ballAboveCeiling(GOval ball) {
            return ball.getY()<=0;
        }
    
        private boolean ballLeftWall(GOval ball) {
            return ball.getX()<=0;
        }
    
        private boolean ballRightWall(GOval ball) {
            return ball.getX()+BALL_RADIUS*2>=WIDTH;
        }
    }

좋은 웹페이지 즐겨찾기