赞
踩
实现一个简单的贪吃蛇游戏需要使用Java的图形库,通常可以使用Swing或JavaFX。下面是一个使用Swing库来创建一个简单贪吃蛇游戏的示例代码:
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.util.LinkedList;
- import java.util.Random;
-
- public class SnakeGame extends JPanel implements ActionListener {
- private final int CELL_SIZE = 20;
- private final int GRID_WIDTH = 20;
- private final int GRID_HEIGHT = 20;
- private final int GAME_SPEED = 150;
-
- private LinkedList<Point> snake;
- private Point food;
- private int direction;
- private boolean isMoving;
- private boolean isGameOver;
-
- public SnakeGame() {
- setPreferredSize(new Dimension(CELL_SIZE * GRID_WIDTH, CELL_SIZE * GRID_HEIGHT));
- setBackground(Color.BLACK);
- setFocusable(true);
-
- addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
- int key = e.getKeyCode();
- if ((key == KeyEvent.VK_LEFT) && (direction != 1)) direction = 0;
- if ((key == KeyEvent.VK_RIGHT) && (direction != 0)) direction = 1;
- if ((key == KeyEvent.VK_UP) && (direction != 3)) direction = 2;
- if ((key == KeyEvent.VK_DOWN) && (direction != 2)) direction = 3;
- }
- });
-
- snake = new LinkedList<>();
- generateFood();
- isMoving = true;
- isGameOver = false;
-
- Timer timer = new Timer(GAME_SPEED, this);
- timer.start();
- }
-
- @Override
- public void actionPerformed(ActionEvent e) {
- if (isMoving && !isGameOver) {
- move();
- checkCollision();
- repaint();
- }
- }
-
- private void generateFood() {
- Random rand = new Random();
- int x, y;
- do {
- x = rand.nextInt(GRID_WIDTH);
- y = rand.nextInt(GRID_HEIGHT);
- } while (snake.contains(new Point(x, y));
- food = new Point(x, y);
- }
-
- private void move() {
- Point head = snake.peekFirst();
- Point newHead;
- switch (direction) {
- case 0:
- newHead = new Point(head.x - 1, head.y);
- break;
- case 1:
- newHead = new Point(head.x + 1, head.y);
- break;
- case 2:
- newHead = new Point(head.x, head.y - 1);
- break;
- case 3:
- newHead = new Point(head.x, head.y + 1);
- break;
- default:
- newHead = head;
- }
-
- snake.addFirst(newHead);
-
- if (newHead.equals(food)) {
- generateFood();
- } else {
- snake.removeLast();
- }
- }
-
- private void checkCollision() {
- if (snake.size() == GRID_WIDTH * GRID_HEIGHT) {
- isMoving = false;
- isGameOver = true;
- }
-
- Point head = snake.peekFirst();
- if (head.x < 0 || head.x >= GRID_WIDTH || head.y < 0 || head.y >= GRID_HEIGHT) {
- isMoving = false;
- isGameOver = true;
- }
-
- if (snake.size() > 1 && snake.contains(head)) {
- isMoving = false;
- isGameOver = true;
- }
- }
-
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- if (isGameOver) {
- g.setColor(Color.WHITE);
- g.setFont(new Font("SansSerif", Font.BOLD, 30));
- g.drawString("Game Over!", CELL_SIZE * 5, CELL_SIZE * 10);
- } else {
- // 绘制食物
- g.setColor(Color.RED);
- g.fillRect(food.x * CELL_SIZE, food.y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
-
- // 绘制蛇
- g.setColor(Color.GREEN);
- for (Point p : snake) {
- g.fillRect(p.x * CELL_SIZE, p.y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
- }
- }
- }
-
- public static void main(String[] args) {
- JFrame frame = new JFrame("贪吃蛇游戏");
- SnakeGame game = new SnakeGame();
- frame.add(game);
- frame.pack();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- }
这个示例创建了一个简单的贪吃蛇游戏,使用了Swing库来构建图形用户界面。玩家可以使用方向键控制蛇的移动,目标是吃到食物,躲避碰撞和自身。游戏会在一定条件下结束,显示"Game Over"。你可以根据需要对游戏进行扩展和改进。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。