中级农民
- 积分
- 206
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2011-12-12
- 最后登录
- 1970-1-1
|
还在进行吗?花了一个星期的时间,在实验之余写了个带声音带分数的。(不知道直接粘贴上来行不行,见笑了)
/*
* 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
* Should not be used directly (use getWidth()/getHeight() instead).
* * */
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 = 12;
/** 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 = 5; // number of turns
private int started = 0; // A flag to determine whether playGame should respond to mouse click
private int ready = 0; // A flay to determine whether it is ready to clean canvas
private int stopped = 0; // A flay to determin whether the game is paused
private GOval ball;
private GRect paddle;
private double vx, vy; // the velocity of the ball
private RandomGenerator rgen = RandomGenerator.getInstance(); // generate the horizontal velocity randomly
private int bricknum = NBRICK_ROWS*NBRICKS_PER_ROW;
private int turnnum = NTURNS; // count how many bricks left
private AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au");
private GLabel score;
private final int velocityY=3;
/* Method: run() */
/** Runs the Breakout program. */
public void run() {
/* You fill this in, along with any subsidiary methods */
addMouseListeners(); // must add listener before initiate game
initGame();
}
// Init the game
private void initGame(){
bricknum = NBRICK_ROWS*NBRICKS_PER_ROW;
started = 0;
createWall(); // draw bricks
createPaddle(); // draw paddle
createBall(); // draw ball
createScore();
while(started ==0); // wait for click to start
playGame();
}
// Remove all the remaining GObjects in the canvas
private void clearCanvas(){
for (int i =0; i<getWidth();i++){
for (int j=0; j<getHeight();j++){
GObject canvasPoint = getElementAt(i,j);
if (canvasPoint!=null){
remove(canvasPoint);
}
}
}
}
// Create the wall at the top of the screen
private void createWall(){
// wx and wy are start point of the wall
double wx = 0;
double wy = BRICK_Y_OFFSET;
for (int i = 0; i<NBRICK_ROWS; i++){
for (int j=0; j<NBRICKS_PER_ROW; j++){
GRect brick = new GRect(wx+j*(BRICK_WIDTH+BRICK_SEP),wy+i*(BRICK_HEIGHT+BRICK_SEP),
BRICK_WIDTH,BRICK_HEIGHT);
brick.setFilled(true);
// select color for different rows
switch(i){
case 0:
case 1: brick.setFillColor(Color.RED); break;
case 2:
case 3: brick.setFillColor(Color.ORANGE); break;
case 4:
case 5: brick.setFillColor(Color.YELLOW); break;
case 6:
case 7: brick.setFillColor(Color.GREEN); break;
case 8:
case 9: brick.setFillColor(Color.CYAN); break;
}
add(brick);
}
}
return;
}
// Create the paddle
private void createPaddle(){
double px = getWidth()/2;
double py = getHeight()- PADDLE_Y_OFFSET - PADDLE_HEIGHT;
paddle = new GRect(px-PADDLE_WIDTH/2,py,PADDLE_WIDTH,PADDLE_HEIGHT);
paddle.setFilled(true);
paddle.setFillColor(Color.BLACK);
add(paddle);
return;
}
// Create the ball
private void createBall(){
double bx = getWidth()/2 - BALL_RADIUS;
double by = getHeight() - PADDLE_Y_OFFSET - PADDLE_HEIGHT - 2*BALL_RADIUS;
ball = new GOval(bx, by, 2*BALL_RADIUS, 2*BALL_RADIUS);
ball.setFilled(true);
ball.setFillColor(Color.BLACK);
add(ball);
return;
}
private void createScore(){
score = new GLabel("SCORE: "
+ (NBRICK_ROWS*NBRICKS_PER_ROW-bricknum)
+"/"
+ (NBRICK_ROWS*NBRICKS_PER_ROW),0, getHeight());
add(score);
}
/////////////////////////////////////////////////////////////////////////////////////////
// Once clicked the game, the game starts
public void mousePressed(MouseEvent e){
// started determines whether the game should start upon mouse click.
// for a initiated game, started = 0.
// Once it responds the mouse click, it will never respond to mouse click any more,
// until the game ends.
if (stopped == 1){ // when the game is paused, clicking mouse means you are ready to clean the canvas
ready = 1;
}
if (started == 0){ // when the game has not been started yet, clicking mouse means to start the game.
started = 1;
stopped = 0;
}
//println(ready);
}
// paddle tracks the mouse position
public void mouseDragged(MouseEvent e){
double mx = e.getX();
double my = getHeight()- PADDLE_Y_OFFSET - PADDLE_HEIGHT;
if ((mx>=0)&&(mx<= getWidth()-PADDLE_WIDTH))
paddle.setLocation(mx,my); // use set location to change the position of the paddle
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
// start the game, the ball begins to move
private void playGame(){
vx = rgen.nextDouble(1.0,3.0); // generate the velocity randomly
if (rgen.nextBoolean(0.5)) vx = -vx; // generate the velocity direction randomly.
vy = -velocityY;
while (isOver()==0){
ball.move(vx, vy);
add(ball);
pause(20);
checkBouncing(); // check whether the ball reaches the left, right and top boundary
checkCollision(); // check whether the ball collides with the bricks and paddle
updateScore();
}
if (isOver()==1){ // if lose game in one round
turnnum--; // remaining turns reduced by one
if (isGameOver()){ // if there is no chances left
GLabel label = new GLabel("You lose the game",0,0);
double ldx = getWidth()/2-label.getWidth()/2;
double ldy = getHeight()/2+label.getAscent()/2;
label.move(ldx, ldy);
add(label);
}
else{ // if you still have chances
GLabel label = new GLabel("You have "+turnnum+ " chances",0,0);
double ldx = getWidth()/2-label.getWidth()/2;
double ldy = getHeight()/2+label.getAscent()/2;
label.move(ldx, ldy);
add(label);
ready = 0;
stopped = 1;
while (ready == 0);
clearCanvas(); // remove all remaining GObjects in the canvas
initGame(); // restart the game
}
}
if (isOver()==2){ // if win the game
GLabel label = new GLabel("You win the game",0,0);
double ldx = getWidth()/2-label.getWidth()/2;
double ldy = getHeight()/2+label.getAscent()/2;
label.move(ldx, ldy);
add(label);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Check whether the ball reaches the left or right or top boundary
private void checkBouncing(){
if ((ball.getX()<=0)||(ball.getX()+2*BALL_RADIUS> getWidth())){
vx = - vx;
}
if (ball.getY()<=0){
vy = -vy;
}
}
// Check whether win or lose the game in one round
private int isOver(){
if (bricknum == 0)
return 2; // win the game
if (isFailure()){
return 1; // lose the game
}
return 0; // the game continues
}
// Check whether you missed the ball
private boolean isFailure(){
if (ball.getY()>=getHeight()-PADDLE_Y_OFFSET+PADDLE_HEIGHT){
return true;
}
return false;
}
// Check whether the game is over
private boolean isGameOver(){
if (turnnum == 0)
return true;
return false;
}
// check whether the ball collides with other GObject and deal with the GObject
private void checkCollision(){
GObject collider = getCollidingObject();
if (collider == null) // does not collide
return;
bounceClip.play(); // collides, play music
if ((collider != paddle)&&(collider != score)){ // collide with the bricks
remove(collider); // remove the brick
bricknum--; // remaining bricks reduced by one
}
}
// Check four corner of the ball to decide whether it collides.
// change the velocity direction if it collides
private GObject getCollidingObject(){
// upper
GObject colObject1 = getElementAt(ball.getX()+BALL_RADIUS,ball.getY()-1);
// right
GObject colObject2 = getElementAt(ball.getX()+2*BALL_RADIUS+1,ball.getY()+BALL_RADIUS);
// left
GObject colObject3 = getElementAt(ball.getX()-1,ball.getY()+BALL_RADIUS);
// bottom
GObject colObject4 = getElementAt(ball.getX()+BALL_RADIUS,ball.getY()+2*BALL_RADIUS+1);
if ((colObject1!=null)&&(colObject1!=score)){
vy = velocityY;
return colObject1;
}
if ((colObject2!=null)&&(colObject2!=score)){
vx = -vx;
return colObject2;
}
if ((colObject3!=null)&&(colObject3!=score)){
vx = -vx;
return colObject3;
}
if ((colObject4!=null)&&(colObject4!=score)){
vy = -velocityY;
return colObject4;
}
return null;
}
private void updateScore(){
score.setLabel("SCORE: "
+ (NBRICK_ROWS*NBRICKS_PER_ROW-bricknum)
+"/"
+ (NBRICK_ROWS*NBRICKS_PER_ROW));
}
}
|
|