写一个贪吃蛇的java代码
时间: 2024-01-06 14:06:51 浏览: 182
好的,以下是一个简单的贪吃蛇Java代码示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Snake extends JFrame implements ActionListener {
private Timer timer;
private SnakePanel panel;
private JButton startButton;
private JButton pauseButton;
private JButton resumeButton;
public Snake() {
super("Snake");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
panel = new SnakePanel();
getContentPane().add(panel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout());
startButton = new JButton("Start");
startButton.addActionListener(this);
buttonPanel.add(startButton);
pauseButton = new JButton("Pause");
pauseButton.addActionListener(this);
buttonPanel.add(pauseButton);
pauseButton.setEnabled(false);
resumeButton = new JButton("Resume");
resumeButton.addActionListener(this);
buttonPanel.add(resumeButton);
resumeButton.setEnabled(false);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
panel.start();
startButton.setEnabled(false);
pauseButton.setEnabled(true);
} else if (e.getSource() == pauseButton) {
panel.pause();
pauseButton.setEnabled(false);
resumeButton.setEnabled(true);
} else if (e.getSource() == resumeButton) {
panel.resume();
resumeButton.setEnabled(false);
pauseButton.setEnabled(true);
}
}
public static void main(String[] args) {
new Snake();
}
}
class SnakePanel extends JPanel implements ActionListener {
private static final int ROWS = 20;
private static final int COLS = 20;
private static final int CELL_SIZE = 20;
private int[][] grid;
private int snakeLength;
private int[] snakeRow;
private int[] snakeCol;
private int foodRow;
private int foodCol;
private boolean gameover;
private Timer timer;
public SnakePanel() {
setPreferredSize(new Dimension(COLS * CELL_SIZE, ROWS * CELL_SIZE));
setBackground(Color.WHITE);
setFocusable(true);
requestFocus();
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_LEFT && grid[snakeRow[0]][snakeCol[0] - 1] != 1) {
move(-1, 0);
} else if (code == KeyEvent.VK_RIGHT && grid[snakeRow[0]][snakeCol[0] + 1] != 1) {
move(1, 0);
} else if (code == KeyEvent.VK_UP && grid[snakeRow[0] - 1][snakeCol[0]] != 1) {
move(0, -1);
} else if (code == KeyEvent.VK_DOWN && grid[snakeRow[0] + 1][snakeCol[0]] != 1) {
move(0, 1);
}
}
});
grid = new int[ROWS][COLS];
snakeLength = 3;
snakeRow = new int[ROWS * COLS];
snakeCol = new int[ROWS * COLS];
for (int i = 0; i < snakeLength; i++) {
snakeRow[i] = ROWS / 2;
snakeCol[i] = COLS / 2 - i;
grid[snakeRow[i]][snakeCol[i]] = 1;
}
placeFood();
timer = new Timer(100, this);
}
public void start() {
timer.start();
}
public void pause() {
timer.stop();
}
public void resume() {
timer.start();
}
public void actionPerformed(ActionEvent e) {
move(0, 1);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (grid[row][col] == 1) {
g.setColor(Color.BLACK);
g.fillRect(col * CELL_SIZE, row * CELL_SIZE, CELL_SIZE, CELL_SIZE);
} else if (grid[row][col] == 2) {
g.setColor(Color.RED);
g.fillOval(col * CELL_SIZE, row * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
}
}
if (gameover) {
g.setColor(Color.BLUE);
g.setFont(new Font("Arial", Font.BOLD, 40));
g.drawString("Game Over", COLS * CELL_SIZE / 2 - 100, ROWS * CELL_SIZE / 2);
}
}
private void move(int dx, int dy) {
if (gameover) {
return;
}
int newHeadRow = snakeRow[0] + dy;
int newHeadCol = snakeCol[0] + dx;
if (newHeadRow < 0 || newHeadRow >= ROWS || newHeadCol < 0 || newHeadCol >= COLS) {
gameover = true;
repaint();
return;
}
if (grid[newHeadRow][newHeadCol] == 1) {
gameover = true;
repaint();
return;
}
if (grid[newHeadRow][newHeadCol] == 2) {
snakeLength++;
snakeRow[snakeLength - 1] = snakeRow[snakeLength - 2];
snakeCol[snakeLength - 1] = snakeCol[snakeLength - 2];
grid[newHeadRow][newHeadCol] = 1;
placeFood();
} else {
for (int i = snakeLength - 1; i > 0; i--) {
snakeRow[i] = snakeRow[i - 1];
snakeCol[i] = snakeCol[i - 1];
}
}
snakeRow[0] = newHeadRow;
snakeCol[0] = newHeadCol;
grid[newHeadRow][newHeadCol] = 1;
repaint();
}
private void placeFood() {
int row, col;
do {
row = (int) (Math.random() * ROWS);
col = (int) (Math.random() * COLS);
} while (grid[row][col] != 0);
foodRow = row;
foodCol = col;
grid[foodRow][foodCol] = 2;
}
}
```
这个代码使用Java Swing实现了一个简单的贪吃蛇游戏,并且具有基本的游戏控制(开始、暂停、恢复)。
阅读全文