用java写一个贪吃蛇代码
时间: 2024-05-19 14:13:11 浏览: 8
以下是一个简单的贪吃蛇代码示例,仅供参考:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Snake extends JPanel implements ActionListener, KeyListener {
private Timer timer;
private int delay = 100;
private int x = 0;
private int y = 0;
private int size = 20;
private int foodX;
private int foodY;
private int[] snakeX = new int[100];
private int[] snakeY = new int[100];
private int length = 1;
private boolean left = false;
private boolean right = true;
private boolean up = false;
private boolean down = false;
public Snake() {
setPreferredSize(new Dimension(400, 400));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
startGame();
}
private void startGame() {
snakeX[0] = x;
snakeY[0] = y;
generateFood();
timer = new Timer(delay, this);
timer.start();
}
private void generateFood() {
foodX = (int)(Math.random() * 20) * size;
foodY = (int)(Math.random() * 20) * size;
}
private void move() {
for (int i = length; i > 0; i--) {
snakeX[i] = snakeX[i-1];
snakeY[i] = snakeY[i-1];
}
if (left) {
snakeX[0] -= size;
}
if (right) {
snakeX[0] += size;
}
if (up) {
snakeY[0] -= size;
}
if (down) {
snakeY[0] += size;
}
}
private void checkCollision() {
if (snakeX[0] == foodX && snakeY[0] == foodY) {
length++;
generateFood();
}
for (int i = 1; i < length; i++) {
if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
timer.stop();
JOptionPane.showMessageDialog(this, "Game Over");
}
}
if (snakeX[0] < 0 || snakeX[0] >= getWidth() ||
snakeY[0] < 0 || snakeY[0] >= getHeight()) {
timer.stop();
JOptionPane.showMessageDialog(this, "Game Over");
}
}
private void draw(Graphics g) {
g.setColor(Color.RED);
g.fillRect(foodX, foodY, size, size);
for (int i = 0; i < length; i++) {
g.setColor(Color.GREEN);
g.fillRect(snakeX[i], snakeY[i], size, size);
}
}
public void actionPerformed(ActionEvent e) {
move();
checkCollision();
repaint();
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_LEFT && !right) {
left = true;
up = false;
down = false;
}
if (code == KeyEvent.VK_RIGHT && !left) {
right = true;
up = false;
down = false;
}
if (code == KeyEvent.VK_UP && !down) {
up = true;
left = false;
right = false;
}
if (code == KeyEvent.VK_DOWN && !up) {
down = true;
left = false;
right = false;
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new Snake(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
}
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)