用java写一个挡板游戏,要求使用“多线程”方式
时间: 2024-02-17 12:03:41 浏览: 95
基于java多线程的一款小游戏.zip
好的,以下是使用多线程方式实现的 Java 挡板游戏,你可以根据需要对它进行修改:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PaddleGame extends JFrame implements ActionListener, KeyListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 600;
private static final int HEIGHT = 500;
private static final int PADDLE_WIDTH = 100;
private static final int PADDLE_HEIGHT = 10;
private static final int BALL_SIZE = 20;
private static final int BALL_SPEED = 5;
private static final int PADDLE_SPEED = 10;
private JPanel gamePanel;
private Timer timer;
private int ballX, ballY, ballDX, ballDY;
private int paddleX, paddleY, paddleDX;
private boolean leftPressed, rightPressed;
private boolean running;
public PaddleGame() {
setTitle("Paddle Game");
setSize(WIDTH, HEIGHT);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
gamePanel = new JPanel();
gamePanel.setBackground(Color.BLACK);
getContentPane().add(gamePanel);
ballX = WIDTH / 2 - BALL_SIZE / 2;
ballY = HEIGHT / 2 - BALL_SIZE / 2;
ballDX = BALL_SPEED;
ballDY = BALL_SPEED;
paddleX = WIDTH / 2 - PADDLE_WIDTH / 2;
paddleY = HEIGHT - PADDLE_HEIGHT - 20;
timer = new Timer(10, this);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
public void start() {
running = true;
timer.start();
new Thread(new GameLoop()).start();
}
public void stop() {
running = false;
timer.stop();
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.WHITE);
g.fillOval(ballX, ballY, BALL_SIZE, BALL_SIZE);
g.fillRect(paddleX, paddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
}
public void actionPerformed(ActionEvent e) {
if (leftPressed) {
paddleDX = -PADDLE_SPEED;
} else if (rightPressed) {
paddleDX = PADDLE_SPEED;
} else {
paddleDX = 0;
}
paddleX += paddleDX;
if (paddleX < 0) {
paddleX = 0;
} else if (paddleX + PADDLE_WIDTH > WIDTH) {
paddleX = WIDTH - PADDLE_WIDTH;
}
ballX += ballDX;
ballY += ballDY;
if (ballX < 0 || ballX + BALL_SIZE > WIDTH) {
ballDX = -ballDX;
}
if (ballY < 0) {
ballDY = -ballDY;
} else if (ballY + BALL_SIZE > paddleY && ballX + BALL_SIZE > paddleX && ballX < paddleX + PADDLE_WIDTH) {
ballDY = -ballDY;
} else if (ballY + BALL_SIZE > HEIGHT) {
stop();
JOptionPane.showMessageDialog(this, "Game Over", "Message", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
repaint();
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
leftPressed = true;
} else if (key == KeyEvent.VK_RIGHT) {
rightPressed = true;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
leftPressed = false;
} else if (key == KeyEvent.VK_RIGHT) {
rightPressed = false;
}
}
public void keyTyped(KeyEvent e) {}
private class GameLoop implements Runnable {
public void run() {
while (running) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
actionPerformed(null);
}
}
}
public static void main(String[] args) {
PaddleGame game = new PaddleGame();
game.setVisible(true);
game.start();
}
}
```
在这个例子中,我们使用了一个额外的线程来执行游戏循环,而不是使用定时器。在游戏循环中,我们不断调用 `actionPerformed` 方法来更新游戏状态,然后休眠一段时间。
这种方式可以更好地控制游戏循环的速率,避免因为计算量过大或者其他原因导致游戏卡顿的情况。同时,它也可以更好地控制游戏的暂停和恢复,以及游戏结束后的处理。
你可以根据需要对这个例子进行修改和扩展,来实现更加丰富的挡板游戏。
阅读全文