通行证
- 积分
- 706
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2011-8-10
- 最后登录
- 1970-1-1
|
- 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;
- /* Method: run() */
- /** Runs the Breakout program. */
- public void run() {
- setup();
- while(!gameOver){
- moveBall();
- }
- }
- private void setup(){
- createBricks();
- createPaddle();
- createBall();
- addMouseListeners();
- }
- private void createBricks() {
- int x0=(WIDTH-BRICK_WIDTH*NBRICKS_PER_ROW-BRICK_SEP*(NBRICKS_PER_ROW-1))/2; //the starting x coordinate for the first brick in a row
- int y0= BRICK_Y_OFFSET;
-
- for(int i=0;i<NBRICKS_PER_ROW;i++) {
- for(int j=0;j<NBRICK_ROWS;j++) {
- int x1 = (BRICK_WIDTH+BRICK_SEP)*i+x0;
- int y1 = (BRICK_HEIGHT+BRICK_SEP)*j+y0;
- GRect rect = new GRect(x1,y1,BRICK_WIDTH,BRICK_HEIGHT);
- add(rect);
- rect.setFilled(true);
- switch(j){
- case 0:
- case 1:
- rect.setFillColor(Color.RED);
- break;
- case 2:
- case 3:
- rect.setFillColor(Color.ORANGE);
- break;
- case 4:
- case 5:
- rect.setFillColor(Color.YELLOW);
- break;
- case 6:
- case 7:
- rect.setFillColor(Color.GREEN);
- break;
- case 8:
- case 9:
- rect.setFillColor(Color.CYAN);
- break;
- }
- }
- }
- }
- private void createPaddle(){
- int x = (WIDTH-PADDLE_WIDTH)/2;
- int y = HEIGHT-PADDLE_Y_OFFSET-PADDLE_HEIGHT;
- paddle = new GRect(x,y,PADDLE_WIDTH,PADDLE_HEIGHT);
- paddle.setFilled(true);
- add(paddle);
- }
- private void createBall(){
- int x0=WIDTH/2-BALL_RADIUS;
- int y0=HEIGHT/2-BALL_RADIUS;
- ball = new GOval(x0,y0,BALL_RADIUS*2,BALL_RADIUS*2);
- ball.setFilled(true);
- add(ball);
- vy = 3.0;
- vx = rgen.nextDouble(1.0, 3.0);
- if(rgen.nextBoolean(0.5)){vx = -vx;}
- }
-
- public void mouseMoved(MouseEvent e){
- int xLocation = (e.getX() - (PADDLE_WIDTH / 2));
-
- if((xLocation) > 0 && xLocation < WIDTH - PADDLE_WIDTH){
- paddle.setLocation(xLocation, (HEIGHT - (PADDLE_Y_OFFSET + PADDLE_HEIGHT)));
- }//why it does not need to consider the situation that xLocation is larger than WIDTH-PADDLE_WIDTH
- }
- private void checkGameState(){
- if(lives == 0){
- gameOver = true;
- GLabel label = new GLabel("Game Over. Score:"+score, 0, 300);
- label.setFont("Serif-36");
- label.setColor(Color.RED);
- add(label);
- }
- if(brickAmount == 0){
- gameOver = true;
- GLabel label = new GLabel("Congratulations.", 200, 200);
- label.setFont("Serif");
- add(label);
- }
- }
-
- private void moveBall(){
- ball.move(vx, vy);
- x = ball.getX();
- y = ball.getY();
- checkForCollisions(x,y);
- checkGameState();
- if(ball.getX() < 0 || ball.getX() > (WIDTH - BALL_RADIUS*2)){vx = -vx;}
- if(ball.getY() < 0 ){vy = -vy;}
- if(ball.getY() > (HEIGHT - (BALL_RADIUS*2))){
- newLife();
- }
- pause(10);
- }
- private void newLife(){
- remove(ball);
- lives--;
- GLabel label = new GLabel(lives+" Lives Remained", 200, 200);
- label.setFont("Serif");
- add(label);
- pause(1);
- createBall();
- moveBall();
- }
- private void checkForCollisions(double x, double y){
- if(getElementAt(x, y) != null){
- collider = getElementAt(x, y);
- if(collider == paddle) {
- newLife();
- } else {
- vy=-vy;
- remove(collider);
- brickAmount--;
- score+=10;
- playAudio();
- }
- }
- else if(getElementAt(x + BALL_RADIUS*2, y) != null){
- collider = getElementAt(x + BALL_RADIUS*2, y);
- if(collider == paddle) {
- newLife();
- } else {
- vy=-vy;
- remove(collider);
- brickAmount--;
- score+=10;
- playAudio();
- }
-
- }
- else if(getElementAt(x, y + BALL_RADIUS*2) != null){
- collider = getElementAt(x, y + BALL_RADIUS*2);
- removeBrick(collider);
- }
- else if(getElementAt(x + BALL_RADIUS*2, y + BALL_RADIUS*2) != null){
- collider = getElementAt(x + BALL_RADIUS*2, y + BALL_RADIUS*2);
- removeBrick(collider);
- }
- }
-
- /** Removes the appropriate brick */
- private void removeBrick(GObject collider){
- vy = -vy;
- if(collider != paddle){
- remove(collider);
- brickAmount--;
- score+=10;
- playAudio();
- }
- }
- private void playAudio(){
- AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au");
- bounceClip.play();
- }
- private GRect paddle;
- private GOval ball;
- private GObject collider;
- private double vx;
- private double vy;
- private double x, y;
- private RandomGenerator rgen = RandomGenerator.getInstance();
- private int brickAmount = NBRICKS_PER_ROW * NBRICK_ROWS;
- private int lives = NTURNS;
- private boolean gameOver = false;
- private int score=0;
- }
复制代码
|
|