用java编写一个贪吃蛇游戏
时间: 2023-06-02 08:04:05 浏览: 91
好的,这是一个简单的问题,我可以回答。你可以使用Java创建一个贪吃蛇游戏,你需要使用Java Swing库来创建游戏的图形用户界面,使用Java 2D API来处理游戏中的图形,使用Java的基本语法和算法来实现游戏逻辑。具体实现的过程需要根据具体情况进行编写,但是这是一个很好的练手项目,适合Java初学者练手。
相关问题
用Java编写一个贪吃蛇游戏
好的,下面是一个简单的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.ArrayList;
import java.util.List;
import java.util.Random;
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 WIDTH = 500;
public static final int HEIGHT = 500;
public static final int DOT_SIZE = 10;
private static final int INITIAL_SNAKE_SIZE = 10;
private List<Point> snake = new ArrayList<Point>();
private int direction = KeyEvent.VK_RIGHT;
private Point food = new Point(0, 0);
private Random random = new Random();
private boolean running = true;
public SnakeGame() {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.add(this);
frame.addKeyListener(this);
frame.setVisible(true);
for (int i = 0; i < INITIAL_SNAKE_SIZE; i++) {
snake.add(new Point(50 - i * DOT_SIZE, 50));
}
spawnFood();
}
private void spawnFood() {
int x = random.nextInt(WIDTH / DOT_SIZE) * DOT_SIZE;
int y = random.nextInt(HEIGHT / DOT_SIZE) * DOT_SIZE;
food.setLocation(x, y);
}
private void move() {
Point head = snake.get(0);
Point newHead = new Point(head);
switch (direction) {
case KeyEvent.VK_LEFT:
newHead.x -= DOT_SIZE;
break;
case KeyEvent.VK_RIGHT:
newHead.x += DOT_SIZE;
break;
case KeyEvent.VK_UP:
newHead.y -= DOT_SIZE;
break;
case KeyEvent.VK_DOWN:
newHead.y += DOT_SIZE;
break;
}
if (newHead.equals(food)) {
snake.add(0, newHead);
spawnFood();
} else {
snake.remove(snake.size() - 1);
snake.add(0, newHead);
}
if (snakeCollidesWithItself()) {
running = false;
}
if (snakeHitsWall()) {
running = false;
}
}
private boolean snakeCollidesWithItself() {
Point head = snake.get(0);
for (int i = 1; i < snake.size(); i++) {
Point part = snake.get(i);
if (head.equals(part)) {
return true;
}
}
return false;
}
private boolean snakeHitsWall() {
Point head = snake.get(0);
if (head.x < 0 || head.x >= WIDTH || head.y < 0 || head.y >= HEIGHT) {
return true;
}
return false;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
for (Point part : snake) {
g.fillRect(part.x, part.y, DOT_SIZE, DOT_SIZE);
}
g.setColor(Color.RED);
g.fillRect(food.x, food.y, DOT_SIZE, DOT_SIZE);
}
public static void main(String[] args) {
new SnakeGame().start();
}
public void start() {
Thread t = new Thread(this);
t.start();
}
@Override
使用java 编写一个贪吃蛇的游戏
好的,下面是使用 Java 编写的贪吃蛇游戏代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JFrame implements KeyListener {
private JPanel gamePanel;
private JLabel scoreLabel;
private int score;
private int direction;
private Timer timer;
private boolean gameOver;
private int width = 20;
private int height = 20;
private int blockSize = 20;
private int[][] field = new int[width][height];
private int[] snakeX = new int[width * height];
private int[] snakeY = new int[width * height];
private int snakeLength;
public SnakeGame() {
setTitle("贪吃蛇游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(width * blockSize + 20, height * blockSize + 70);
setLocationRelativeTo(null);
gamePanel = new JPanel();
gamePanel.setBackground(Color.WHITE);
gamePanel.setFocusable(true);
gamePanel.addKeyListener(this);
getContentPane().add(gamePanel, BorderLayout.CENTER);
scoreLabel = new JLabel("得分: 0");
getContentPane().add(scoreLabel, BorderLayout.SOUTH);
initGame();
setVisible(true);
}
private void initGame() {
gameOver = false;
score = 0;
direction = KeyEvent.VK_RIGHT;
snakeLength = 3;
snakeX[0] = width / 2;
snakeY[0] = height / 2;
snakeX[1] = snakeX[0] - 1;
snakeY[1] = snakeY[0];
snakeX[2] = snakeX[1] - 1;
snakeY[2] = snakeY[1];
generateFood();
timer = new Timer(200, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveSnake();
repaint();
if (gameOver) {
timer.stop();
JOptionPane.showMessageDialog(SnakeGame.this, "游戏结束,得分: " + score);
}
}
});
timer.start();
}
private void generateFood() {
int x, y;
do {
x = (int)(Math.random() * width);
y = (int)(Math.random() * height);
} while (field[x][y] != 0);
field[x][y] = 2;
}
private void moveSnake() {
int dx = 0, dy = 0;
switch (direction) {
case KeyEvent.VK_UP: dy = -1; break;
case KeyEvent.VK_DOWN: dy = 1; break;
case KeyEvent.VK_LEFT: dx = -1; break;
case KeyEvent.VK_RIGHT: dx = 1; break;
}
int newHeadX = snakeX[0] + dx;
int newHeadY = snakeY[0] + dy;
if (newHeadX < 0 || newHeadX >= width || newHeadY < 0 || newHeadY >= height) {
gameOver = true;
return;
}
if (field[newHeadX][newHeadY] == 1) {
gameOver = true;
return;
}
if (field[newHeadX][newHeadY] == 2) {
score++;
scoreLabel.setText("得分: " + score);
generateFood();
snakeLength++;
}
for (int i = snakeLength - 1; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
snakeX[0] += dx;
snakeY[0] += dy;
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
field[i][j] = 0;
for (int i = 0; i < snakeLength; i++)
field[snakeX[i]][snakeY[i]] = 1;
}
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++) {
if (field[i][j] == 2) {
g.setColor(Color.RED);
g.fillRect(i * blockSize, j * blockSize, blockSize, blockSize);
}
if (field[i][j] == 1) {
g.setColor(Color.BLACK);
g.fillRect(i * blockSize, j * blockSize, blockSize, blockSize);
}
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP && direction != KeyEvent.VK_DOWN)
direction = KeyEvent.VK_UP;
if (e.getKeyCode() == KeyEvent.VK_DOWN && direction != KeyEvent.VK_UP)
direction = KeyEvent.VK_DOWN;
if (e.getKeyCode() == KeyEvent.VK_LEFT && direction != KeyEvent.VK_RIGHT)
direction = KeyEvent.VK_LEFT;
if (e.getKeyCode() == KeyEvent.VK_RIGHT && direction != KeyEvent.VK_LEFT)
direction = KeyEvent.VK_RIGHT;
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
new SnakeGame();
}
}
```
运行上述代码,即可开始游戏。
阅读全文