java打砖块游戏代码
时间: 2024-03-04 15:47:16 浏览: 163
Java打砖块游戏是一个经典的小游戏,下面是一个简单的Java打砖块游戏的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BrickBreaker extends JFrame implements ActionListener {
private Timer timer;
private boolean play = false;
private int score = 0;
private int paddleX = 310;
private int ballPosX = 120;
private int ballPosY = 350;
private int ballXDir = -1;
private int ballYDir = -2;
private int[][] bricks;
private int brickRows = 3;
private int brickCols = 7;
public BrickBreaker() {
bricks = new int[brickRows][brickCols];
for (int i = 0; i < brickRows; i++) {
for (int j = 0; j < brickCols; j++) {
bricks[i][j] = 1;
}
}
setTitle("Brick Breaker");
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
if (paddleX >= 600) {
paddleX = 600;
} else {
moveRight();
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if (paddleX <= 10) {
paddleX = 10;
} else {
moveLeft();
}
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (!play) {
play = true;
ballPosX = 120;
ballPosY = 350;
ballXDir = -1;
ballYDir = -2;
paddleX = 310;
score = 0;
bricks = new int[brickRows][brickCols];
for (int i = 0; i < brickRows; i++) {
for (int j = 0; j < brickCols; j++) {
bricks[i][j] = 1;
}
}
repaint();
}
}
}
});
timer = new Timer(8, this);
timer.start();
setVisible(true);
}
public void paint(Graphics g) {
// 绘制背景
g.setColor(Color.black);
g.fillRect(1, 1, 792, 592);
// 绘制砖块
for (int i = 0; i < brickRows; i++) {
for (int j = 0; j < brickCols; j++) {
if (bricks[i][j] == 1) {
g.setColor(Color.white);
g.fillRect(j * 110 + 30, i * 50 + 50, 100, 40);
}
}
}
// 绘制球拍
g.setColor(Color.green);
g.fillRect(paddleX, 550, 100, 8);
// 绘制球
g.setColor(Color.yellow);
g.fillOval(ballPosX, ballPosY, 20, 20);
// 绘制得分
g.setColor(Color.white);
g.setFont(new Font("serif", Font.BOLD, 25));
g.drawString("" + score, 700, 30);
if (ballPosY > 570) {
play = false;
ballXDir = 0;
ballYDir = 0;
g.setColor(Color.red);
g.setFont(new Font("serif", Font.BOLD, 30));
g.drawString("Game Over, Score: " + score, 190, 300);
g.setFont(new Font("serif", Font.BOLD, 20));
g.drawString("Press Enter to Restart", 290, 350);
}
g.dispose();
}
public void moveRight() {
play = true;
paddleX += 20;
}
public void moveLeft() {
play = true;
paddleX -= 20;
}
public void actionPerformed(ActionEvent e) {
timer.start();
if (play) {
if (new Rectangle(ballPosX, ballPosY, 20, 20).intersects(new Rectangle(paddleX, 550, 100, 8))) {
ballYDir = -ballYDir;
}
A: for (int i = 0; i < brickRows; i++) {
for (int j = 0; j < brickCols; j++) {
if (bricks[i][j] == 1) {
int brickX = j * 110 + 30;
int brickY = i * 50 + 50;
int brickWidth = 100;
int brickHeight = 40;
Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight);
Rectangle ballRect = new Rectangle(ballPosX, ballPosY, 20, 20);
Rectangle brickRect = rect;
if (ballRect.intersects(brickRect)) {
bricks[i][j] = 0;
score += 5;
if (ballPosX + 19 <= brickRect.x || ballPosX + 1 >= brickRect.x + brickRect.width) {
ballXDir = -ballXDir;
} else {
ballYDir = -ballYDir;
}
break A;
}
}
}
}
ballPosX += ballXDir;
ballPosY += ballYDir;
if (ballPosX < 0) {
ballXDir = -ballXDir;
}
if (ballPosY < 0) {
ballYDir = -ballYDir;
}
if (ballPosX > 770) {
ballXDir = -ballXDir;
}
}
repaint();
}
public static void main(String[] args) {
new BrickBreaker();
}
}
```
这段代码实现了一个简单的打砖块游戏,包括砖块、球拍、球和得分的绘制,以及键盘事件的处理。你可以在Java环境中运行该代码,通过方向键控制球拍的移动,按下回车键开始游戏。当球碰到砖块时,砖块消失并增加得分。当球触底时,游戏结束并显示最终得分。
阅读全文