java实现不同大小交互界面的贪吃蛇
时间: 2024-01-27 15:11:52 浏览: 23
以下是一个简单的Java实现不同大小交互界面的贪吃蛇游戏:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Snake extends JFrame implements ActionListener, KeyListener {
private final int WIDTH = 600;
private final int HEIGHT = 600;
private final int UNIT_SIZE = 20;
private final int GAME_UNITS = (WIDTH * HEIGHT) / UNIT_SIZE;
private final int DELAY = 80;
private int[] x = new int[GAME_UNITS];
private int[] y = new int[GAME_UNITS];
private int bodyParts = 3;
private int applesEaten = 0;
private int appleX;
private int appleY;
private char direction = 'R';
private boolean running = false;
private Timer timer;
private Random random;
public Snake() {
setTitle("Snake Game");
setSize(WIDTH, HEIGHT);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
addKeyListener(this);
random = new Random();
initGame();
setVisible(true);
}
private void initGame() {
running = true;
newApple();
timer = new Timer(DELAY, this);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
move();
checkApple();
checkCollisions();
}
repaint();
}
private 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;
}
}
private void checkApple() {
if (x[0] == appleX && y[0] == appleY) {
bodyParts++;
applesEaten++;
newApple();
}
}
private void checkCollisions() {
// check if head collides with body
for (int i = bodyParts; i > 0; i--) {
if (x[0] == x[i] && y[0] == y[i]) {
running = false;
}
}
// check if head touches left border
if (x[0] < 0) {
running = false;
}
// check if head touches right border
if (x[0] > WIDTH) {
running = false;
}
// check if head touches top border
if (y[0] < 0) {
running = false;
}
// check if head touches bottom border
if (y[0] > HEIGHT) {
running = false;
}
if (!running) {
timer.stop();
}
}
private void newApple() {
appleX = random.nextInt((int) (WIDTH / UNIT_SIZE)) * UNIT_SIZE;
appleY = random.nextInt((int) (HEIGHT / UNIT_SIZE)) * UNIT_SIZE;
}
@Override
public void paint(Graphics g) {
// draw grid
for (int i = 0; i < WIDTH / UNIT_SIZE; i++) {
g.drawLine(i * UNIT_SIZE, 0, i * UNIT_SIZE, HEIGHT);
g.drawLine(0, i * UNIT_SIZE, WIDTH, i * UNIT_SIZE);
}
// draw apple
g.setColor(Color.red);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
// draw snake
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(Color.green);
} else {
g.setColor(new Color(45, 180, 0));
}
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
// game over message
if (!running) {
g.setColor(Color.red);
g.setFont(new Font("Ink Free", Font.BOLD, 40));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Game Over", (WIDTH - metrics.stringWidth("Game Over")) / 2, HEIGHT / 2);
}
// score message
g.setColor(Color.white);
g.setFont(new Font("Ink Free", Font.BOLD, 20));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Score: " + applesEaten, (WIDTH - metrics.stringWidth("Score: " + applesEaten)) / 2, g.getFont().getSize());
}
@Override
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;
}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
new Snake();
}
}
```
这个游戏使用了Java的Swing库来创建交互界面,利用键盘监听器来控制蛇的运动方向。游戏会在窗口中绘制蛇、苹果和网格,并在游戏结束时显示得分和游戏结束信息。