java贪吃蛇程序v1

本程序是参照传智播客学习视频而成,非全部是原创,转载请注明
贪吃蛇程序,实现了:
1.点击键盘的上下左右键可以移动蛇
2.蛇吃掉食物后可以增加长度
打包好的jar包程序,双击可以运行(需要电脑安装jdk)

package snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.Random;

/**
 * Created by Administrator on 2017/4/10.
 */
public class SnakeView extends JPanel{
    public static final int HEIGHT = 30;//地图的高(行数)
    public static final int WIDTH = 30;//地图的宽(列数)
    public static final int CELLHEIGHT = 20;//地图的宽
    public static final int CELLWIDTH = 20;//地图的宽

    private boolean[][] background= new boolean[HEIGHT][WIDTH];//地图

    //蛇当前的方向
    int currentDirection = -2;//默认蛇的方向是向右的

    boolean flag = true;

    //使用四个常量表示四个方向
    public static final int UP_DIRECTION = 1;
    public static final int DOWN_DIRECTION = -1;
    public static final int LEFT_DIRECTION = 2;
    public static final int RIGHT_DIRECTION = -2;

    //食物
    Point food;
    //生成食物
    public void createFood(){
        //创建一个随机数对象
        Random random = new Random();
        while (true) {
            int x = random.nextInt(WIDTH);//random(10)产生0-9的随机数,不包括10
            int y = random.nextInt(HEIGHT);
            if(!background[y][x]){
                food = new Point(x,y);
                break;
            }
        }
    }

    //蛇吃食物
    public boolean eatFood(){
        Point head = snake.getFirst();
        if(head.equals(food)){
            return true;
        }
        return false;
    }
    //记录游戏是否结束
    static boolean isGameOver = false;
    //游戏结束的方法
    public void isGameOver(){
        //撞墙死亡
        Point head = snake.getFirst();
        if(background[head.y][head.x]){
            isGameOver = true;
        }

        //咬到自己蛇身
        for(int i = 1; i < snake.size(); i++){
            Point body = snake.get(i);
            if(head.equals(body)){
                isGameOver = true;
            }
        }
    }
    //蛇移动的方法
    //蛇的移动就是添加一个新的蛇头,删除一个蛇尾
    public void move(){
        Point head = snake.getFirst();
        switch (currentDirection){
            case UP_DIRECTION:
                snake.addFirst(new Point(head.x,head.y-1));
                break;
            case DOWN_DIRECTION:
                snake.addFirst(new Point(head.x,head.y+1));
                break;
            case LEFT_DIRECTION:
                snake.addFirst(new Point(head.x - 1,head.y));
                break;
            case RIGHT_DIRECTION:
                snake.addFirst(new Point(head.x + 1,head.y));
                break;
            default:
                break;

        }
        if(eatFood()){
            createFood();
        }else {
            //删除蛇尾
            snake.removeLast();
        }

    }
    //改变蛇的方向
    public void changeDirection(int newDirection){
        //判断新方向与旧方向是否相反,相反则不能改变
        if((currentDirection + newDirection) != 0){
            this.currentDirection = newDirection;
        }
    }

    //用集合存储蛇的信息
    private LinkedList<Point> snake = new LinkedList<Point>();

    //初始化地图
    public void initMap(){
        for(int i = 0;i<background.length;i++){
            for(int j = 0;j<background[i].length;j++){
                if(i == 0 || i == background.length-1 || j == 0 || j == background[i].length-1){
                    background[i][j] = true;
                }
            }
        }
    }
    //初始化蛇
    public void initSnake(){
        int x = WIDTH/2;
        int y = HEIGHT/2;
        //开始给list中添加元素
        snake.addFirst(new Point(x-1,y));
        snake.addFirst(new Point(x,y));
        snake.addFirst(new Point(x+1,y));
    }

    @Override
    public void paint(Graphics g) {
        //画地图
        drawMap(g);

        //画蛇
        drawSnake(g);

        //画食物
        drawFood(g);
    }
    private void drawFood(Graphics g) {
        g.setColor(Color.YELLOW);
        g.fill3DRect(food.x * CELLWIDTH,food.y * CELLHEIGHT,CELLWIDTH,CELLHEIGHT,true);
    }

    private void drawSnake(Graphics g) {

        g.setColor(Color.green);
        for(int i = 1;i<snake.size();i++){
            Point point = snake.get(i);
            g.fill3DRect(point.x * CELLWIDTH,point.y * CELLHEIGHT,CELLWIDTH,CELLHEIGHT,true);
        }
        g.setColor(Color.RED);
        Point head = snake.getFirst();
        g.fill3DRect(head.x * CELLWIDTH,head.y * CELLHEIGHT,CELLWIDTH,CELLHEIGHT,true);
    }

    private void drawMap(Graphics g) {
        for(int i = 0 ; i < background.length; i++){
            for(int j = 0; j < background[i].length ;j++){
                if(background[i][j] == true){
                    //说明是石头
                    g.setColor(Color.GRAY);
                }else {
                    g.setColor(Color.WHITE);
                }
                g.fill3DRect(CELLWIDTH * j,CELLHEIGHT * i,CELLWIDTH,CELLHEIGHT,true);
            }
        }
    }
    public static void initFrame(JFrame frame,int width , int height){
        Toolkit toolkit = Toolkit.getDefaultToolkit(); //获取一个与系统相关工具类对象
        //获取屏幕的分辨率
        Dimension d = toolkit.getScreenSize();

        int x = (int) d.getWidth();
        int y = (int) d.getHeight();

        frame.setBounds((x-width)/2, (y-height)/2, width, height);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        JFrame frame = new JFrame("贪吃蛇");
        SnakeView s = new SnakeView();
        s.initMap();
        s.initSnake();
        s.createFood();
        frame.add(s);
        initFrame(frame,CELLWIDTH * WIDTH + 7,CELLHEIGHT * HEIGHT + 28);

        frame.setResizable(false);
        frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int code = e.getKeyCode();
                switch (code){
                    case KeyEvent.VK_UP:
                        s.changeDirection(UP_DIRECTION);
                        break;
                    case KeyEvent.VK_DOWN:
                        s.changeDirection(DOWN_DIRECTION);
                        break;
                    case KeyEvent.VK_LEFT:
                        s.changeDirection(LEFT_DIRECTION);
                        break;
                    case KeyEvent.VK_RIGHT:
                        s.changeDirection(RIGHT_DIRECTION);
                        break;
                    default:
                        break;
                }
                s.move();
                s.isGameOver();
                s.repaint();//实际上就是调用了paint方法
                if(isGameOver){
                    JOptionPane.showMessageDialog(frame, "游戏结束", "提示",JOptionPane.PLAIN_MESSAGE);
                    System.exit(0);
                }
            }
        });
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值