作业的要求和相关介绍太多了,直接贴上我写的源代码。不过,还有些问题没有完全搞明白。这个只能算一个初级版本吧。 程序源代码: /* * File: Breakout.java * ------------------- * Name: * Section Leader: * * 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; /* Method: run() */ /** Runs the Breakout program. */ public void run() { /* You fill this in, along with any subsidiary methods */ gameInit(); gamePlay(); } /* * ****************************************** * 函数名: gameInit * 功 能: 初始化设置游戏窗口大小、 * 绘制砖块以及挡板 * ****************************************** */ private void gameInit() { // 界面初始化 /* 设置窗口大小 */ setSize(WIDTH + 8, HEIGHT + 54); //显示器显示存在问题,宽度需加8,高度加54 //add(new GLabel("宽度: " + getWidth() + " 高度:" + getHeight(),10,10)); /* 绘制砖块 */ brickInit(); /* 绘制挡板 */ paddleInit(); /* 绘制球 */ ballInit(); /* 添加鼠标监听事件 */ addMouseListeners(); } /* * ****************************************** * 函数名: brickInit * 功 能: 绘制初始状态时的砖块 * ****************************************** */ private void brickInit() { /* 砖块的起始X坐标 */ double xPos = (WIDTH - BRICK_WIDTH * NBRICKS_PER_ROW - BRICK_SEP * (NBRICKS_PER_ROW - 1))/2; //Color color[] = {Color.RED,Color.ORANGE,Color.YELLOW,Color.GREEN,Color.CYAN}; for(int i=0; i < NBRICK_ROWS;i++){ /* 砖块的Y坐标 */ double y = BRICK_Y_OFFSET + i * (BRICK_SEP + BRICK_HEIGHT); for(int j=0;j < NBRICKS_PER_ROW;j++){ /* 砖块的X坐标 */ double x = xPos + j * (BRICK_SEP + BRICK_WIDTH); GRect brick = new GRect(x,y,BRICK_WIDTH,BRICK_HEIGHT); brick.setFilled(true); /* 砖块的填充颜色 */ //brick.setColor(color[i/2]); if((i / 2) == 0){ brick.setColor(Color.RED); } else if((i / 2) == 1){ brick.setColor(Color.ORANGE); } else if((i / 2) == 2){ brick.setColor(Color.YELLOW); } else if((i / 2) == 3){ brick.setColor(Color.GREEN); } else if((i / 2) == 4){ brick.setColor(Color.CYAN); } add(brick); } } } /* * ****************************************** * 函数名: addleInit * 功 能: 绘制初始状态的挡板 * ****************************************** */ private void paddleInit() { double xPos = (WIDTH - PADDLE_WIDTH)/2, //X坐标 yPos = HEIGHT - PADDLE_HEIGHT - PADDLE_Y_OFFSET; //Y坐标 paddle = new GRect(xPos,yPos,PADDLE_WIDTH,PADDLE_HEIGHT); paddle.setFilled(true); add(paddle); //add(new GLabel("X:" + paddle.getX() + " Y:" + paddle.getY(),10,10)); } /* * ****************************************** * 函数名: ballInit * 功 能: 绘制初始状态的球,并使球 * 位于窗口正中心 * ****************************************** */ public void ballInit(){ double xPos = WIDTH/2 - BALL_RADIUS, //初始X坐标 yPos = HEIGHT/2 - BALL_RADIUS; //初始Y坐标 ball = new GOval(xPos,yPos,2 * BALL_RADIUS,2 * BALL_RADIUS); ball.setColor(Color.BLACK); ball.setFilled(true); add(ball); } /* * ****************************************** * 函数名: mouseMoved * 功 能: 使挡板跟随鼠标的移动而移动 * 参 数: e —— 鼠标事件 * ****************************************** */ public void mouseMoved(MouseEvent e){ paddle.move(e.getX() - paddle.getX(),0); if(paddle.getX() > (WIDTH - PADDLE_WIDTH)){ //若右边出界,则将挡板位置置为靠右边界 paddle.setLocation(WIDTH-PADDLE_WIDTH, paddle.getY()); } } /* * ****************************************** * 函数名: gamePlay * 功 能: 启动游戏进程,并对游戏过程 * 中遇到的问题进行处理 * ****************************************** */ private void gamePlay() { /* 砖块的个数 */ int brickNum = NBRICKS_PER_ROW * NBRICK_ROWS; vx = rgen.nextDouble(-3.0, 3.0); while(!isGameOver()){ ball.move(vx, vy); pause(10); GObject collider = getCollidingObject(); //球碰触到的对象 if(collider == paddle){ vy = -vy; } else if(collider != null){ //bounceClip.play(); vy = -vy; remove(collider); brickNum--; if(brickNum == 0) break; } /* 球碰到两边的边界的情况 */ if((ball.getX() <= 0) || (ball.getX() + (2 * BALL_RADIUS) >= WIDTH)){ vx = -vx; } if(ball.getY() <= 0){ vy = -vy; } } if(brickNum != 0){ /* game over后的操作 */ gameOver(); } else{ /* 砖块已经全部消除*/ afterSuccess(); } } /* * ****************************************** * 函数名: getCollidingObject * 功 能: 获取球碰到的对象 * 返回值: gobj —— 碰到的物体对象 * ****************************************** */ private GObject getCollidingObject(){ GObject gobj = null; gobj = getElementAt(ball.getX(), ball.getY()); if(gobj == null){ gobj = getElementAt(ball.getX() + 2 * BALL_RADIUS, ball.getY() + 2 * BALL_RADIUS); } if(gobj == null){ gobj = getElementAt(ball.getX(), ball.getY() + 2 * BALL_RADIUS); } if(gobj == null){ gobj = getElementAt(ball.getX() + 2 * BALL_RADIUS, ball.getY()); } return gobj; } /* * ****************************************** * 函数名: isGameOver * 功 能: 判断游戏是否因失败而结束 * 备 注: 球接触到窗口的最下端 * ****************************************** */ private boolean isGameOver() { return ((ball.getY() + 2 * BALL_RADIUS) >= HEIGHT); } /* * ****************************************** * 函数名: gameOver * 功 能: 游戏失败后的操作 * ****************************************** */ private void gameOver(){ removeAll(); GLabel over = new GLabel("Game Over !"); over.setFont("Serif-36"); over.setColor(Color.RED); add(over,(WIDTH-over.getWidth())/2,(HEIGHT-over.getAscent())/2); } /* * ****************************************** * 函数名: afterSuccess * 功 能: 砖块全部清除后的庆祝界面 * ****************************************** */ private void afterSuccess(){ removeAll(); GLabel label = new GLabel("SUCCESS ! ! !"); label.setFont("Serif-36"); add(label,(WIDTH-label.getWidth())/2,(HEIGHT-label.getAscent())/2); GRect su = new GRect(0,0,WIDTH,HEIGHT); su.setFilled(true); for(int i=0;i < 5;i++){ su.setFillColor(rgen.nextColor()); add(su); label.sendForward(); pause(400); } } /* 私有实例变量 */ private GRect paddle; //挡板 private GOval ball; //球 private RandomGenerator rgen = RandomGenerator.getInstance(); /* 球的移动坐标 */ private double vx,vy = 3.0; }