贪吃蛇练习

贪吃蛇练习

/**
 * 保存数据的类
 */
public class MyData {

    public static URL headerUrl = MyData.class.getResource("statics/header.png");
    public static URL bodyUrl = MyData.class.getResource("statics/body.png");
    public static URL foodUrl = MyData.class.getResource("statics/food.png");

    public static URL upUrl = MyData.class.getResource("statics/up.png");
    public static URL downUrl = MyData.class.getResource("statics/down.png");
    public static URL leftUrl = MyData.class.getResource("statics/left.png");
    public static URL rightUrl = MyData.class.getResource("statics/right.png");


    public static ImageIcon header = new ImageIcon(headerUrl);
    public static ImageIcon body = new ImageIcon(bodyUrl);
    public static ImageIcon food = new ImageIcon(foodUrl);

    public static ImageIcon up = new ImageIcon(upUrl);
    public static ImageIcon down = new ImageIcon(downUrl);
    public static ImageIcon left = new ImageIcon(leftUrl);
    public static ImageIcon right = new ImageIcon(rightUrl);

}
/**
 * 游戏面板
 */
public class GamePanel extends JPanel implements KeyListener, ActionListener {

    //定义蛇的数据结构
    int length; //蛇的长度
    int[] snakeX = new int[600]; //蛇的x坐标
    int[] snakeY = new int[600]; //蛇的y坐标
    String direction; //蛇头方向
    boolean isStart; //游戏状态(开始true、停止false)
    //食物的坐标
    int foodX;
    int foodY;
    Random random = new Random();
    //判断失败状态
    boolean isFail;
    //积分
    int score;

    //定时器
    Timer timer = new Timer(100,this); //100毫秒执行一次actionPerformed()方法

    //构造器
    public GamePanel(){
        init();
        this.setFocusable(true);//获得焦点事件
        this.addKeyListener(this);//获得键盘监听事件
    }

    //初始化方法
    public void init(){
        length = 3;
        direction = "R"; //默认蛇头方向向右
        isStart = false; //默认不开始
        snakeX[0] = 100;snakeY[0] = 75; //头部坐标
        snakeX[1] = 75;snakeY[1] = 75; //第一个身体的坐标
        snakeX[2] = 50;snakeY[2] = 75; //第二个身体的坐标
        timer.start(); //定时器开启
        isFail = false; //初始状态为正常
        score = 0;

        //把食物随机分布在游戏面板上
        foodX = 25+25*random.nextInt(34); //25的初始值+随机25个宽,最多只能是34个25宽
        foodY = 25+25*random.nextInt(30); //25的初始值+随机25个高,最多只能是30个25宽
    }

    //绘制面板 游戏所有的绘制都是用这个画笔绘制的
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(Color.white);

        //绘制头部广告栏
        //MyData.header.paintIcon(this,g,20,11);

        //绘制游戏背景面板
        g.fillRect(25,50,850,750);

        //绘制食物
        MyData.food.paintIcon(this,g,foodX,foodY);

        //绘制积分面板
        g.setColor(Color.RED);
        g.setFont(new Font("微软雅黑",Font.BOLD,25));
        g.drawString("长度:"+length,100,33);
        g.drawString("成绩:"+score,670,33);

        //画小蛇
        if (direction.equals("R")){
            MyData.right.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else if (direction.equals("L")){
            MyData.left.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else if (direction.equals("U")){
            MyData.up.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else {
            MyData.down.paintIcon(this,g,snakeX[0],snakeY[0]);
        }

        for (int i = 1; i < length; i++) {
            MyData.body.paintIcon(this,g,snakeX[i],snakeY[i]);
        }

        if (isStart==false){
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始游戏!",300,380);
        }

        if (isFail==true){
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("失败!按下空格重新开始游戏!",200,380);
        }
    }

    //键盘监听事件
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode(); //获得键盘按键是哪一个
        if (keyCode == KeyEvent.VK_SPACE){ //空格键
            if (isFail){
                isFail=false;
                init();
            }else {
                isStart = !isStart; //取反
            }
            repaint(); //重新绘制
        }

        if (keyCode == KeyEvent.VK_UP){
            direction = "U";
        }else if (keyCode == KeyEvent.VK_DOWN){
            direction = "D";
        }else if (keyCode == KeyEvent.VK_LEFT){
            direction = "L";
        }else if (keyCode == KeyEvent.VK_RIGHT){
            direction = "R";
        }
    }

    //事件监听--需要固定的时间来刷新
    @Override
    public void actionPerformed(ActionEvent e) {
        if (isStart && isFail==false){

            //吃食物
            if (snakeX[0] == foodX && snakeY[0] == foodY){
                length++;
                score = score+10; //吃一个分数加10
                foodX = 25+25*random.nextInt(34); //25的初始值+随机25个宽,最多只能是34个25宽
                foodY = 25+25*random.nextInt(30); //25的初始值+随机25个高,最多只能是30个25宽
            }

            //身体移动
            for (int i = length-1; i > 0; i--) { //后一节变到前一节的位置
                snakeX[i] = snakeX[i-1];
                snakeY[i] = snakeY[i-1];
            }

            //蛇头移动
            if (direction.equals("R")){
                snakeX[0] = snakeX[0]+25;
            }else if (direction.equals("L")){
                snakeX[0] = snakeX[0]-25;
            }else if (direction.equals("U")){
                snakeY[0] = snakeY[0]-25;
            }else if (direction.equals("D")){
                snakeY[0] = snakeY[0]+25;
            }

            //边界判断
            if (snakeX[0] > 850){
                isFail = true;
            }else if (snakeX[0] < 25){
                isFail = true;
            }
            if (snakeY[0] > 775){
                isFail = true;
            }else if (snakeY[0] < 50){
                isFail = true;
            }

            //失败判断
            for (int i = 1; i < length; i++) {
                if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]){
                    isFail = true;
                }
            }

            repaint();
        }
        timer.start(); //定时器开启
    }

    @Override
    public void keyTyped(KeyEvent e) {}
    @Override
    public void keyReleased(KeyEvent e) {}

}
/**
 * 游戏入口
 */
public class StartGame {

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        GamePanel gamePanel = new GamePanel();

        jFrame.setTitle("小小贪吃蛇");
        jFrame.add(gamePanel);
        jFrame.setResizable(false);
        jFrame.setBounds(0,0,900,850);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值