贪吃蛇java程序代码
时间: 2023-11-12 13:25:10 浏览: 212
以下是一个简单的贪吃蛇Java程序代码示例:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SnakeGame extends JPanel implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
public static final int ROWS = 30;
public static final int COLS = 30;
public static final int BLOCK_SIZE = 20;
private Thread thread;
private boolean isRunning;
private LinkedList<Point> snake;
private Point food;
private int direction;
public SnakeGame() {
setPreferredSize(new Dimension(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
init();
}
private void init() {
snake = new LinkedList<Point>();
snake.add(new Point(COLS / 2, ROWS / 2));
snake.add(new Point(COLS / 2, ROWS / 2 + 1));
snake.add(new Point(COLS / 2, ROWS / 2 + 2));
direction = KeyEvent.VK_UP;
createFood();
isRunning = true;
thread = new Thread(this);
thread.start();
}
private void createFood() {
int x = (int) (Math.random() * COLS);
int y = (int) (Math.random() * ROWS);
food = new Point(x, y);
}
private void move() {
Point head = snake.getFirst();
int x = head.x;
int y = head.y;
switch (direction) {
case KeyEvent.VK_LEFT:
x--;
break;
case KeyEvent.VK_RIGHT:
x++;
break;
case KeyEvent.VK_UP:
y--;
break;
case KeyEvent.VK_DOWN:
y++;
break;
}
if (x < 0 || x >= COLS || y < 0 || y >= ROWS) {
gameOver();
return;
}
Point newHead = new Point(x, y);
if (snake.contains(newHead)) {
gameOver();
return;
}
snake.addFirst(newHead);
if (newHead.equals(food)) {
createFood();
} else {
snake.removeLast();
}
}
private void gameOver() {
isRunning = false;
System.out.println("Game Over!");
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(food.x * BLOCK_SIZE, food.y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
g.setColor(Color.WHITE);
for (Point p : snake) {
g.fillRect(p.x * BLOCK_SIZE, p.y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
@Override
public void run() {
while (isRunning) {
move();
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT && direction != KeyEvent.VK_RIGHT) {
direction = keyCode;
} else if (keyCode == KeyEvent.VK_RIGHT && direction != KeyEvent.VK_LEFT) {
direction = keyCode;
} else if (keyCode == KeyEvent.VK_UP && direction != KeyEvent.VK_DOWN) {
direction = keyCode;
} else if (keyCode == KeyEvent.VK_DOWN && direction != KeyEvent.VK_UP) {
direction = keyCode;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new SnakeGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
该程序使用Java Swing库创建了一个简单的贪吃蛇游戏。玩家可以使用上下左右箭头键控制蛇的移动方向,吃掉食物后蛇的长度会增加。如果蛇撞到边界或自己的身体,则游戏结束。
阅读全文