📄 breakout.java
字号:
/**************************************************************
Paddle Game Demo
This code shows how to display blocks, make a ball bounce, and
to control a paddle my moving the mouse.
@author zxy
@author Eric Roberts (provided original code)
@version October 25, 2007
**************************************************************/
import acm.graphics.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Breakout implements ActionListener, Runnable, MouseMotionListener{
/** GUI button to serve each ball */
private JButton serveButton;
/** JFrame holds the game */
private JFrame myGUI;
private int WIDTH = 400;
private int HEIGHT = 500;
private int ballWidth =20;
private int brickWidth = 38;
private int brickHeight =20;
private int paddleWidth;
private int paddleHeight = 10;
private GCanvas canvas;
private GOval ball;
private GRect brick;
private GRect paddle;
private double dx,dy;
private int key, hits,ballAmounts=0,pauseTime=8;
/** menu items */
JMenuBar menus;
JMenu fileMenu;
JMenuItem quitItem;
JMenuItem restartItem;
JTextField paddleWidthS, ballAmountsS,ballLeftover;
String[] levels={"level1","level2","level3"};
JComboBox myChoice;
/****************************************************
Sets up the game like usual.
****************************************************/
public Breakout() {
myGUI = new JFrame();
myGUI.setTitle("My Simple Breakout");
myGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myGUI.setLocation(100,100);
myGUI.setSize(WIDTH, HEIGHT);
myGUI.setLayout(new BorderLayout());
paddleWidthS = new JTextField(2);
ballAmountsS = new JTextField(2);
ballLeftover = new JTextField(2);
myChoice= new JComboBox(levels);
JPanel panel = new JPanel();
panel.add(new JLabel("PaddleWidth"));
panel.add(paddleWidthS);
panel.add(new JLabel("Balls"));
panel.add(ballAmountsS);
panel.add(new JLabel("BallsLeft"));
panel.add(ballLeftover);
panel.add(new JLabel("Levels"));
panel.add(myChoice);
myChoice.addActionListener(this);
myGUI.add(BorderLayout.NORTH, panel);
paddleWidthS.setText("70");
ballAmountsS.setText("3");
ballLeftover.setText("3");
paddleWidth=Integer.parseInt(paddleWidthS.getText());
// create a GCanvas
canvas = new GCanvas();
canvas.setBackground(Color.cyan);
myGUI.add(BorderLayout.CENTER,canvas);
canvas.addMouseMotionListener(this);
// set up the serve button
serveButton = new JButton("Serve");
serveButton.setBackground(Color.gray);
serveButton.addActionListener(this);
myGUI.add(BorderLayout.SOUTH, serveButton);
// set up File menu
fileMenu = new JMenu("File");
quitItem = new JMenuItem("Quit");
restartItem = new JMenuItem("Restart");
quitItem.addActionListener(this);
restartItem.addActionListener(this);
fileMenu.add(restartItem);
fileMenu.add(quitItem);
menus = new JMenuBar();
myGUI.setJMenuBar(menus);
menus.add(fileMenu);
myGUI.setVisible(true);
myGUI.validate();
gameLayout();
}
/****************************************************
Creating and return a new ball.
****************************************************/
private GOval Ball() {
double x = (WIDTH - ballWidth)/2;
double y = (HEIGHT- ballWidth)/2;
GOval ball = new GOval(x, y,ballWidth, ballWidth);
ball.setFilled(true);
ball.setColor(Color.magenta);
if (Math.random() < 0.5)
dx = -dx;
return ball;
}
/****************************************************
Creating the initial game layout.
****************************************************/
private void gameLayout() {
paddle = new GRect(WIDTH, HEIGHT-paddleHeight, paddleWidth, paddleHeight);
paddle.setFilled(true);
paddle.setColor(Color.blue);
canvas.add(paddle);
int x, y = 30;
int n = WIDTH/brickWidth;
for (int i=1; i<=2; i++){
x = 0;
for (int j = 0; j < n; j++) {
GRect brick = new GRect (x, y, brickWidth, brickHeight);
brick.setFilled(true);
brick.setColor(Color.RED);
canvas.add(brick);
x += brickWidth+2;
}
y +=brickHeight+2;
}
for (int i=1; i<=2; i++){
x = 0;
for (int j = 0; j < n; j++) {
GRect brick = new GRect (x, y, brickWidth, brickHeight);
brick.setFilled(true);
brick.setColor(Color.GREEN);
canvas.add(brick);
x += brickWidth+2;
}
y += brickHeight+2;
}
for (int i=1; i<=2; i++){
x = 0;
for (int j = 0; j < n; j++) {
GRect brick = new GRect (x, y, brickWidth, brickHeight);
brick.setFilled(true);
brick.setColor(Color.YELLOW);
canvas.add(brick);
x += brickWidth+2;
}
y += brickHeight+2;
}
}
/****************************************************
Checking the ball position against paddle position.
****************************************************/
private void checkPaddle(){
double x = ball.getX();
double y = ball.getY();
Object aj = canvas.getElementAt(x,y+ballWidth);
if (aj == null)
aj = canvas.getElementAt(x+ballWidth,y+ballWidth);
if(aj == paddle){
dy = -dy;
hits++;
double pedPoint = paddle.getX();
if (dx > 0 && x < pedPoint + 0.5*paddleWidth)
dx = -dx;
else if(dx < 0 && x > pedPoint + 0.5*paddleWidth)
dx = -dx;
}
}
/****************************************************
Checking the ball position against wall position.
****************************************************/
private void checkWall() {
double x = ball.getX();
double y = ball.getY();
if (x <= 0 || x >= WIDTH -ballWidth) {
dx = -dx;
}
if (y <= 0){
dy = -dy;
}
}
/****************************************************
Checking the ball position against all of the bricks.
****************************************************/
private void checkBrick() {
double x = ball.getX();
double y = ball.getY();
GObject aj = canvas.getElementAt(x,y);
if(aj == null){
aj = canvas.getElementAt(x, y+ballWidth);
}
if(aj == null){
aj = canvas.getElementAt(x+ballWidth, y+ballWidth);
}
if(aj == null){
aj = canvas.getElementAt(x+ballWidth, y);
}
if(aj != null && aj != paddle){
dy = -dy;
canvas.remove(aj);
}
}
/****************************************************
Returning a boolean true if the ball is still in play;
return false if the ball moves past the paddle.
****************************************************/
private boolean isBallPlay() {
boolean ballPlay = true;
if (ball.getY() > HEIGHT || canvas.getElementCount() == 2)
ballPlay = false;
return ballPlay;
}
/****************************************************
Set the ball in play until the player misses or the
game is over.
****************************************************/
private void playRound() {
dx = 2.0;
dy = 3.0;
int hits = 0;
ball = Ball();
canvas.add(ball);
ballAmounts++;
int leftover=Integer.parseInt(ballAmountsS.getText())-ballAmounts;
String leftoverS =""+leftover;
ballLeftover.setText(leftoverS);
while (isBallPlay()){
checkWall();
checkBrick();
checkPaddle();
ball.move(dx, dy);
if(hits == 10){
hits = 0;
pauseTime--;
}
pause(pauseTime);
}
canvas.remove(ball);
if (canvas.getElementCount() == 1){
serveButton.setEnabled(false);
JOptionPane.showMessageDialog(null, "you win!.", "Game Over", JOptionPane.INFORMATION_MESSAGE);
}else if (ballAmounts== Integer.parseInt(ballAmountsS.getText())){
serveButton.setEnabled(false);
JOptionPane.showMessageDialog(null, "you lose.", "Game Over", JOptionPane.INFORMATION_MESSAGE);
}
}
/****************************************************
Move the Paddle as the Mouse moves
****************************************************/
public void mouseMoved(MouseEvent e){
int aa=Integer.parseInt(paddleWidthS.getText());
if(aa !=paddleWidth){
paddleWidth=aa;
canvas.remove(paddle);
paddle = new GRect(WIDTH, HEIGHT-paddleHeight, paddleWidth, paddleHeight);
paddle.setFilled(true);
paddle.setColor(Color.BLUE);
canvas.add(paddle);
}
double x = e.getX();
if (x > WIDTH - paddleWidth)
x = WIDTH - paddleWidth;
paddle.setLocation(x, canvas.getHeight()-paddleHeight);
}
public void mouseDragged(MouseEvent e){
// no need to complete this method
}
/****************************************************
Respond to a button click
Do not perform any actions directly in
this method. Instead, invoke a new thread so this
method will return immediately.
****************************************************/
public void actionPerformed(ActionEvent e){
JComponent itemPressed = (JComponent) e.getSource();
// create a new thread and indirectly call the run() method
if (itemPressed == quitItem)
System.exit(1);
if (itemPressed == restartItem){
ballAmounts=0;
ballLeftover.setText(""+Integer.parseInt(ballAmountsS.getText()));
canvas.removeAll();
gameLayout();
serveButton.setEnabled(true);
}
if (itemPressed == serveButton){
Thread theThread = new Thread(this);
theThread.start();
}
switch(myChoice.getSelectedIndex()){
case 0: pauseTime=8; break;
case 1: pauseTime=5; break;
default: pauseTime=3;
}
}
/****************************************************
A new round is started and runs in a separate thread.
No need to change this method.
****************************************************/
public void run(){
playRound();
}
/****************************************************
This will pause the current thread the provided
number of milliseconds.
No need to change this method.
****************************************************/
private void pause(int msecs){
try{
Thread.sleep(msecs);
}catch (InterruptedException e){
// ignore the exception
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -