java写贪吃蛇代码

java
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Random; public class SnakeGame extends JFrame { private static final long serialVersionUID = 1L; private static final int GRID_SIZE = 20; private static final int GRID_COUNT = 20; private static final int CELL_SIZE = 20; private enum Direction { UP, DOWN, LEFT, RIGHT } private Point snakeHead; private Point snakeTail; private Direction direction; private Point food; public SnakeGame() { setTitle("Snake Game"); setSize(GRID_SIZE * CELL_SIZE, GRID_SIZE * CELL_SIZE); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); snakeHead = new Point(GRID_COUNT / 2, GRID_COUNT / 2); direction = Direction.RIGHT; addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { handleKeyPress(e.getKeyCode()); } }); generateFood(); Timer timer = new Timer(100, new ActionListener() { public void actionPerformed(ActionEvent arg0) { move(); checkCollision(); repaint(); } }); timer.start(); } private void handleKeyPress(int keyCode) { switch (keyCode) { case KeyEvent.VK_UP: if (direction != Direction.DOWN) { direction = Direction.UP; } break; case KeyEvent.VK_DOWN: if (direction != Direction.UP) { direction = Direction.DOWN; } break; case KeyEvent.VK_LEFT: if (direction != Direction.RIGHT) { direction = Direction.LEFT; } break; case KeyEvent.VK_RIGHT: if (direction != Direction.LEFT) { direction = Direction.RIGHT; } break; } } private void move() { switch (direction) { case UP: snakeHead.y--; break; case DOWN: snakeHead.y++; break; case LEFT: snakeHead.x--; break; case RIGHT: snakeHead.x++; break; } // Check if the snake eats the food if (snakeHead.equals(food)) { generateFood(); } else { // Move the tail snakeTail = new Point(snakeHead.x, snakeHead.y); } } private void checkCollision() { // Check if the snake hits the walls if (snakeHead.x < 0 || snakeHead.x >= GRID_COUNT || snakeHead.y < 0 || snakeHead.y >= GRID_COUNT) { gameOver(); } // Check if the snake hits itself if (snakeTail != null && snakeHead.equals(snakeTail)) { gameOver(); } } private void generateFood() { Random random = new Random(); int x, y; do { x = random.nextInt(GRID_COUNT); y = random.nextInt(GRID_COUNT); } while (snakeHead.equals(new Point(x, y))); food = new Point(x, y); } private void gameOver() { JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } @Override public void paint(Graphics g) { super.paint(g); g.clearRect(0, 0, getWidth(), getHeight()); // Draw the snake g.setColor(Color.GREEN); g.fillRect(snakeHead.x * CELL_SIZE, snakeHead.y * CELL_SIZE, CELL_SIZE, CELL_SIZE); if (snakeTail != null) { g.fillRect(snakeTail.x * CELL_SIZE, snakeTail.y * CELL_SIZE, CELL_SIZE, CELL_SIZE); } // Draw the food g.setColor(Color.RED); g.fillRect(food.x * CELL_SIZE, food.y * CELL_SIZE, CELL_SIZE, CELL_SIZE); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new SnakeGame().setVisible(true); } }); } }

这是一个简单的控制台贪吃蛇游戏,使用了Swing库来创建一个简单的GUI。你可以根据需要进行更多的改进和添加功能。希望这对你有帮助!

java
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.LinkedList; import java.util.Random; public class SnakeGame extends JFrame implements ActionListener, KeyListener { private static final long serialVersionUID = 1L; private static final int GRID_SIZE = 20; private static final int CELL_SIZE = 20; private enum Direction { UP, DOWN, LEFT, RIGHT } private LinkedList<Point> snake; private Point food; private Direction direction; public SnakeGame() { setTitle("Snake Game"); setSize(GRID_SIZE * CELL_SIZE, GRID_SIZE * CELL_SIZE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setResizable(false); snake = new LinkedList<>(); snake.add(new Point(GRID_SIZE / 2, GRID_SIZE / 2)); direction = Direction.RIGHT; spawnFood(); Timer timer = new Timer(100, this); timer.start(); addKeyListener(this); setFocusable(true); } private void spawnFood() { Random random = new Random(); int x, y; do { x = random.nextInt(GRID_SIZE); y = random.nextInt(GRID_SIZE); } while (snake.contains(new Point(x, y))); food = new Point(x, y); } private void move() { Point head = snake.getFirst(); Point newHead; switch (direction) { case UP: newHead = new Point(head.x, (head.y - 1 + GRID_SIZE) % GRID_SIZE); break; case DOWN: newHead = new Point(head.x, (head.y + 1) % GRID_SIZE); break; case LEFT: newHead = new Point((head.x - 1 + GRID_SIZE) % GRID_SIZE, head.y); break; case RIGHT: newHead = new Point((head.x + 1) % GRID_SIZE, head.y); break; default: return; } if (newHead.equals(food)) { snake.addFirst(newHead); spawnFood(); } else { snake.addFirst(newHead); snake.removeLast(); } } private boolean checkCollision() { Point head = snake.getFirst(); for (int i = 1; i < snake.size(); i++) { if (head.equals(snake.get(i))) { return true; // Snake collided with itself } } return false; } private void checkGameOver() { if (checkCollision()) { JOptionPane.showMessageDialog(this, "Game Over!"); System.exit(0); } } private void paintGrid(Graphics g) { g.setColor(Color.BLACK); for (int i = 0; i < GRID_SIZE; i++) { g.drawLine(i * CELL_SIZE, 0, i * CELL_SIZE, getHeight()); g.drawLine(0, i * CELL_SIZE, getWidth(), i * CELL_SIZE); } } private void paintSnake(Graphics g) { g.setColor(Color.GREEN); for (Point point : snake) { g.fillRect(point.x * CELL_SIZE, point.y * CELL_SIZE, CELL_SIZE, CELL_SIZE); } } private void paintFood(Graphics g) { g.setColor(Color.RED); g.fillRect(food.x * CELL_SIZE, food.y * CELL_SIZE, CELL_SIZE, CELL_SIZE); } @Override public void actionPerformed(ActionEvent e) { move(); checkGameOver(); repaint(); } @Override public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); switch (key) { case KeyEvent.VK_UP: if (direction != Direction.DOWN) { direction = Direction.UP; } break; case KeyEvent.VK_DOWN: if (direction != Direction.UP) { direction = Direction.DOWN; } break; case KeyEvent.VK_LEFT: if (direction != Direction.RIGHT) { direction = Direction.LEFT; } break; case KeyEvent.VK_RIGHT: if (direction != Direction.LEFT) { direction = Direction.RIGHT; } break; } } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } @Override public void paint(Graphics g) { super.paint(g); paintGrid(g); paintSnake(g); paintFood(g); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { SnakeGame snakeGame = new SnakeGame(); snakeGame.setVisible(true); }); } }

这个简单的贪吃蛇游戏使用了Java的Swing库,通过继承JFrame类创建了一个窗口。游戏中的蛇和食物都是通过Point对象表示的坐标,而方向则是通过枚举类型Direction表示的。键盘事件用于改变蛇的移动方向。游戏中使用了Timer来定时触发蛇的移动。

你可以根据需要进行修改和扩展,添加更多功能和美化界面。希望这个示例能对你有所帮助!