⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 breakout.java

📁 不但实现了所有的游戏功能
💻 JAVA
字号:
import acm.graphics.*;import acm.program.*;import acm.util.*;import java.applet.*;import java.awt.*;import java.awt.event.*;import javax.swing.JOptionPane;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;/** 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;/** Offset of the top brick row from the top */	private static final int BRICK_Y_OFFSET = 70;/** Number of turns */	private static int NTURNS = 3;		private static final int DELAY = 3;		private static final int ALLBrickNumber = 100;	/** Number of bricksU*/	private static int BrickNumber = ALLBrickNumber;	/** After this Number of killercount is counted, the speed of ball will change*/		private static final int killerNumber = 7;		private static int score = 0;/* Method: run() *//** Runs the Breakout program. */		public void run(){		while(true){			if(NTURNS == 0) break;			if(BrickNumber == 0) break;			this.removeAll();			rockOn();		}		if(NTURNS == 0)		JOptionPane.showMessageDialog(null, " you don't have any chance left, try harder next time!", "Unfortunately...you lose this time.", JOptionPane.WARNING_MESSAGE);	}		private void rockOn() {		setup();		while(true){				moveBall();			ballCollideWallListener();			ballCollideObjectListener();			if(BrickNumber == 0) break;			else if(isBallCollideWithLowerWall()) break;		}					if(BrickNumber == 0){			JOptionPane.showMessageDialog(null, " you have conquered this level!", "Congratulations!", JOptionPane.INFORMATION_MESSAGE);		}		else{			JOptionPane.showMessageDialog(null, " you have " + (NTURNS-1) + " lives left...", "Unfortunately...you lose this time.", JOptionPane.WARNING_MESSAGE);			NTURNS--;			BrickNumber = ALLBrickNumber;			score = 0;		}			}		private void ballCollideObjectListener(){		GObject object = getCollidingObject();		if(object != null){			if(object != paddle){				addScore(object);				this.remove(object);				ball.setVy(- ball.getVy());				bounceClip.play();				BrickNumber--;				killerSpeed(BrickNumber);				repaintScoreBoard();							}			else{				ball.setVy(- ball.getVy());				ball.setRandomVx();			}		}	}		private void addScore(GObject object) {		if(object.getColor() == Color.RED)			score += 25;		if(object.getColor() == Color.ORANGE)			score += 20;		if(object.getColor() == Color.YELLOW)			score += 15;		if(object.getColor() == Color.GREEN)			score += 10;		if(object.getColor()== Color.CYAN)			score += 5;	}		private void repaintScoreBoard(){		centerScoreBoard();		scoreBoard.setLabel("" + score);			}		private void centerScoreBoard(){		scoreBoard.setLocation((paddle.getX()+ (paddle.getWidth()-scoreBoard.getWidth()) / 2), paddle.getY()+3 * paddle.getHeight());	}	private void killerSpeed(int BrickNumber){		killerCount ++;		if(killerCount == killerNumber){			killerCount = 0;			ball.setVy(1.1 * ball.getVy());		}	}		private void ballCollideWallListener(){		if(isBallCollideWithLeftWall() || isBallCollideWithRightWall()){			ball.setVx(- ball.getVx());		}		if (isBallCollideWithUpperWall()){			ball.setVy(- ball.getVy());	    }	}		private boolean isBallCollideWithLeftWall(){		return ( ball.getX() <= 0.5 );// 0.5 is just the adjustment	}		private boolean isBallCollideWithRightWall(){		return ( ball.getX() >= (getWidth() - ball.getWidth()) - 1.5 );//- 1.5 is just the adjustement	}		private boolean isBallCollideWithUpperWall(){		return ( ball.getY() <= 0 );	}		private boolean isBallCollideWithLowerWall(){		return ( ball.getY() >= (getHeight() - ball.getWidth()) );	}	private void moveBall() {		ball.move(ball.getVx(), ball.getVy());		pause(DELAY);	}	private void setup() {		bounceClip = MediaTools.loadAudioClip("bounce.au");		createBrickMetrix();		createPaddle();		createBall();		addScoreBoard();		addMouseListeners();				}	private void addScoreBoard() {		scoreBoard = new GLabel(""+score, paddle.getX(), paddle.getY()+2 * paddle.getHeight());		this.add(scoreBoard);		centerScoreBoard();	}	private GObject getCollidingObject(){		if(getElementAt(ball.getLocation()) != null){			return getElementAt(ball.getLocation());		}		else if (getElementAt(ball.getLeftLowerPoint()) != null){			return getElementAt(ball.getLeftLowerPoint());		}		else if (getElementAt(ball.getRightLowerPoint()) != null){			return getElementAt(ball.getRightLowerPoint());		}		else if (getElementAt(ball.getRightUpPoint()) != null){			return getElementAt(ball.getRightUpPoint());		}		else return null;		}	private void createBrickMetrix() {		createOneTypeBrickMetrix(Color.RED, 0);		createOneTypeBrickMetrix(Color.ORANGE, 1);		createOneTypeBrickMetrix(Color.YELLOW, 2);		createOneTypeBrickMetrix(Color.GREEN, 3);		createOneTypeBrickMetrix(Color.CYAN, 4);			}			private void createOneTypeBrickMetrix(Color color, int groupIdentity){		for(int i = 0; i < (NBRICK_ROWS / 5); i++){			for(int j = 0; j < NBRICKS_PER_ROW; j++){				add(new Brick(BRICK_WIDTH, BRICK_HEIGHT, color), BRICK_SEP + j * (BRICK_WIDTH + BRICK_SEP), BRICK_Y_OFFSET + (i + 2 * groupIdentity) * (BRICK_HEIGHT + BRICK_SEP));			}		}	}	/*	private void createRandoomBrickMetrix(int groupIdentity){		for(int i = 0; i < (NBRICK_ROWS / 5); i++){			for(int j = 0; j < NBRICKS_PER_ROW; j++){				add(new Brick(BRICK_WIDTH, BRICK_HEIGHT), BRICK_SEP + j * (BRICK_WIDTH + BRICK_SEP), BRICK_Y_OFFSET + (i + 2 * groupIdentity) * (BRICK_HEIGHT + BRICK_SEP));			}		}	}*/		private void createPaddle(){		paddle = new Paddle(getWidth(), getHeight());		add(paddle);	}		private void createBall(){				ball = new Ball();		add(ball, getWidth()/2, paddle.getOriginalY() - ball.getHeight());	}			public void mouseMoved(MouseEvent e){		repaintScoreBoard();			int paddle_location = e.getX()- (int)(paddle.getWidth() / 2); //the x cordinate of the paddle				if(paddle_location > getWidth() - paddle.getWidth())			paddle.setLocation(paddle.getExtremeRightX(),paddle.getOriginalY());		else if (paddle_location < paddle.getWidth()/2)			paddle.setLocation(0, paddle.getOriginalY());		else			paddle.setLocation(paddle_location, paddle.getOriginalY());	}	private Paddle paddle; 	private Ball ball;	private AudioClip bounceClip;	private int killerCount;	private GLabel scoreBoard;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -