后端贪吃蛇小游戏

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package cn.itcast;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class SnakeGame extends JFrame {
    public Container container;
    public static final int IMAGE_SIZE = 15;
    public static final int COLUMN_COUNT = 50;
    public static final int ROW_COUNT = 25;
    public LinkedList<Point> snake = new LinkedList();
    public LinkedList<JLabel> snakeImageList = new LinkedList();
    public static final int DIRECTION_RIGHT = 1;
    public static final int DIRECTION_LEFT = -1;
    public static final int DIRECTION_UP = 2;
    public static final int DIRECTION_DOWN = -2;
    public static int currentDirection = 1;
    public Point food;
    public JLabel foodImage;
    public static int eatFoodCount = 0;
    public static int numberOfSnakeMoves = 0;
    public int periodTime = 300;

    public SnakeGame() {
        this.addKeyListener();
        this.initFrame();
        this.initBackground();
        this.initSnake();
        this.createFood();
        this.autoMove();
    }

    public void addKeyListener() {
        this.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                switch(keyCode) {
                case 37:
                case 65:
                    SnakeGame.this.move(-1);
                    break;
                case 38:
                case 87:
                    SnakeGame.this.move(2);
                    break;
                case 39:
                case 68:
                    SnakeGame.this.move(1);
                    break;
                case 40:
                case 83:
                    SnakeGame.this.move(-2);
                }

            }
        });
    }

    private void initBackground() {
        for(int row = 0; row < 25; ++row) {
            for(int column = 0; column < 50; ++column) {
                if (column == 0 || column == 49 || row == 0 || row == 24) {
                    JLabel body = new JLabel(new ImageIcon("D:/work/workspace/class_193/SnakeGame/resources/wall.png"));
                    body.setBounds(column * 15, row * 15, 15, 15);
                    this.container.add(body);
                }
            }
        }

        this.container.repaint();
    }

    private void initFrame() {
        this.setTitle("贪吃蛇");
        this.setSize(765, 410);
        this.setDefaultCloseOperation(3);
        this.setResizable(false);
        this.setLocationRelativeTo((Component)null);
        this.setVisible(true);
        this.container = this.getContentPane();
        this.container.setLayout((LayoutManager)null);
        this.container.setBackground(Color.LIGHT_GRAY);
    }

    private void initSnake() {
        int centerX = 25;
        int centerY = 12;
        Point head = new Point(centerX + 1, centerY);
        Point body1 = new Point(centerX, centerY);
        Point body2 = new Point(centerX - 1, centerY);
        this.snake.add(head);
        this.snake.add(body1);
        this.snake.add(body2);
        this.flushSnake();
    }

    public void flushSnake() {
        JLabel headImage;
        for(int i = 0; i < this.snakeImageList.size(); ++i) {
            headImage = (JLabel)this.snakeImageList.get(i);
            this.container.remove(headImage);
        }

        Point head = (Point)this.snake.getFirst();
        headImage = new JLabel(new ImageIcon("D:/work/workspace/class_193/SnakeGame/resources/head_up.png"));
        headImage.setBounds(head.x * 15, head.y * 15, 15, 15);
        this.container.add(headImage);
        this.snakeImageList.add(headImage);

        for(int i = 1; i < this.snake.size(); ++i) {
            Point body = (Point)this.snake.get(i);
            JLabel bodyImage = new JLabel(new ImageIcon("D:/work/workspace/class_193/SnakeGame/resources/body.png"));
            bodyImage.setBounds(body.x * 15, body.y * 15, 15, 15);
            this.container.add(bodyImage);
            this.snakeImageList.add(bodyImage);
        }

        this.container.repaint();
    }

    public void autoMove() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                SnakeGame.this.move(SnakeGame.currentDirection);
            }
        }, 3000L, (long)this.periodTime);
    }

    public void move(int newDirection) {
        if (currentDirection + newDirection == 0) {
            System.out.println("不能反反向移动!");
        } else {
            ++numberOfSnakeMoves;
            currentDirection = newDirection;
            Point head = (Point)this.snake.getFirst();
            switch(currentDirection) {
            case -2:
                this.snake.addFirst(new Point(head.x, head.y + 1));
                break;
            case -1:
                this.snake.addFirst(new Point(head.x - 1, head.y));
            case 0:
            default:
                break;
            case 1:
                this.snake.addFirst(new Point(head.x + 1, head.y));
                break;
            case 2:
                this.snake.addFirst(new Point(head.x, head.y - 1));
            }

            if (this.isEatFood()) {
                this.createFood();
                ++eatFoodCount;
            } else {
                this.snake.removeLast();
            }

            if (this.isGameOver()) {
                System.out.println("游戏结束!");
                System.exit(0);
            }

            this.flushSnake();
        }
    }

    public boolean isGameOver() {
        Point head = (Point)this.snake.getFirst();
        if (head.x != 0 && head.x != 49 && head.y != 0 && head.y != 24) {
            for(int i = 1; i < this.snake.size(); ++i) {
                Point body = (Point)this.snake.get(i);
                if (head.equals(body)) {
                    System.out.println("蛇头在身体上, 结束游戏");
                    return true;
                }
            }

            return false;
        } else {
            System.out.println("蛇头在墙壁上, 结束游戏");
            return true;
        }
    }

    public boolean isEatFood() {
        Point head = (Point)this.snake.getFirst();
        return head.equals(this.food);
    }

    private void createFood() {
        Random r = new Random();

        while(true) {
            int x = r.nextInt(48) + 1;
            int y = r.nextInt(7) + 1;
            this.food = new Point(x, y);
            boolean flag = false;

            for(int i = 0; i < this.snake.size(); ++i) {
                Point snakePoint = (Point)this.snake.get(i);
                if (snakePoint.x == this.food.x && snakePoint.y == this.food.y) {
                    flag = true;
                    break;
                }
            }

            if (!flag) {
                if (this.foodImage != null) {
                    this.container.remove(this.foodImage);
                }

                this.foodImage = new JLabel(new ImageIcon("D:/work/workspace/class_193/SnakeGame/resources/food.png"));
                this.foodImage.setBounds(this.food.x * 15, this.food.y * 15, 15, 15);
                this.container.add(this.foodImage);
                this.container.repaint();
                return;
            }

            System.out.println("运气很好, 食物在蛇身上, 重新生成");
        }
    }
}

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值