你可以用java写一个贪吃蛇小游戏吗,要求有主菜单界面
时间: 2024-01-02 20:03:50 浏览: 24
当然可以!以下是一个简单的Java贪吃蛇游戏示例,包含主菜单界面:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JPanel implements ActionListener {
private static final int SCREEN_WIDTH = 600;
private static final int SCREEN_HEIGHT = 600;
private static final int UNIT_SIZE = 25;
private static final int UNIT_COUNT = (SCREEN_WIDTH * SCREEN_HEIGHT) / (UNIT_SIZE * UNIT_SIZE);
private static final int DELAY = 75;
private final int x[] = new int[UNIT_COUNT];
private final int y[] = new int[UNIT_COUNT];
private int bodyParts = 6;
private int appleX;
private int appleY;
private char direction = 'R';
private boolean running = false;
private Timer timer;
private JFrame frame;
private JButton startButton;
private JButton exitButton;
public SnakeGame() {
frame = new JFrame("Snake Game");
frame.getContentPane().add(this);
frame.getContentPane().setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
startButton = new JButton("Start Game");
startButton.addActionListener(this);
exitButton = new JButton("Exit Game");
exitButton.addActionListener(this);
JPanel menuPanel = new JPanel(new GridLayout(2, 1, 10, 10));
menuPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
menuPanel.add(startButton);
menuPanel.add(exitButton);
menuPanel.setBackground(Color.BLACK);
frame.getContentPane().add(menuPanel, BorderLayout.CENTER);
frame.setVisible(true);
initGame();
}
public void initGame() {
newApple();
running = true;
timer = new Timer(DELAY, this);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
if (running) {
g.setColor(Color.RED);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(Color.GREEN);
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
} else {
g.setColor(new Color(45, 180, 0));
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
}
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 18));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Score: " + (bodyParts - 6), (SCREEN_WIDTH - metrics.stringWidth("Score: " + (bodyParts - 6))) / 2, g.getFont().getSize());
} else {
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 36));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Game Over", (SCREEN_WIDTH - metrics.stringWidth("Game Over")) / 2, SCREEN_HEIGHT / 2);
}
}
public void move() {
for (int i = bodyParts; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
switch (direction) {
case 'U':
y[0] -= UNIT_SIZE;
break;
case 'D':
y[0] += UNIT_SIZE;
break;
case 'L':
x[0] -= UNIT_SIZE;
break;
case 'R':
x[0] += UNIT_SIZE;
break;
}
}
public void checkApple() {
if ((x[0] == appleX) && (y[0] == appleY)) {
bodyParts++;
newApple();
}
}
public void checkCollisions() {
for (int i = bodyParts; i > 0; i--) {
if ((x[0] == x[i]) && (y[0] == y[i])) {
running = false;
}
}
if (x[0] < 0) {
running = false;
}
if (x[0] > SCREEN_WIDTH) {
running = false;
}
if (y[0] < 0) {
running = false;
}
if (y[0] > SCREEN_HEIGHT) {
running = false;
}
if (!running) {
timer.stop();
}
}
public void newApple() {
appleX = (int) (Math.random() * ((SCREEN_WIDTH / UNIT_SIZE))) * UNIT_SIZE;
appleY = (int) (Math.random() * ((SCREEN_HEIGHT / UNIT_SIZE))) * UNIT_SIZE;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
frame.getContentPane().remove(startButton);
frame.getContentPane().remove(exitButton);
frame.repaint();
frame.getContentPane().add(this, BorderLayout.CENTER);
frame.pack();
initGame();
} else if (e.getSource() == exitButton) {
System.exit(0);
} else {
if (running) {
move();
checkApple();
checkCollisions();
}
repaint();
}
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if (direction != 'R') {
direction = 'L';
}
break;
case KeyEvent.VK_RIGHT:
if (direction != 'L') {
direction = 'R';
}
break;
case KeyEvent.VK_UP:
if (direction != 'D') {
direction = 'U';
}
break;
case KeyEvent.VK_DOWN:
if (direction != 'U') {
direction = 'D';
}
break;
}
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
new SnakeGame();
}
}
```
这个示例使用了Java的Swing库来创建游戏窗口和菜单界面,使用了定时器来控制游戏循环,同时使用了键盘事件来监听玩家的操作。你可以根据自己的需要进行修改和扩展。