栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

贪吃蛇游戏java

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

贪吃蛇游戏java

贪吃蛇游戏

图片可自行提供

public class startGame {

    public static void main(String[] args) {
        Jframe frame = new Jframe();
        frame.setBounds(10,10,900,720);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        frame.add(new GamePanel());

        frame.setVisible(true);
    }

事件监听和键盘监听都在这里

public class GamePanel extends JPanel implements KeyListener, ActionListener {

    //定义蛇的数据结构
    int length=0;//蛇长度
    int[] snakeX = new int[200];//蛇的x坐标
    int[] snakeY = new int[200];//y坐标
    String fx = "R";
    boolean isStart = false;
    boolean isFail = false;
    int foodx ;
    int foody ;
    int score;
    Random random = new Random();
    int n=100;
    int m=1;
    Timer timer = new Timer(n,this);
    Timer timer1 = new Timer(70,this);
    Timer timer2 = new Timer(40,this);

    public GamePanel() {
        n=100;
        init();
       timer.start();//游戏开始,定时器启动
        //获得焦点和键盘事件
        this.setFocusable(true);//获得焦点,即为监听这个面板
        this.addKeyListener(this);//键盘事件
        //因为这个类实现了键盘监听接口,否者还要new GamePanel()

    }

    //初始化
    public  void init(){
        length = 3;
        n=100;
        snakeX[0] =100;snakeY[0] =100;
        snakeX[1] =75;snakeY[1] =100;
        snakeX[2] =50;snakeY[2] =100;
        foodx = 25+25*random.nextInt(30);
        foody = 75+25*random.nextInt(20);
//        timer1.restart();
//        timer2.restart();

    }



    //绘制面板,所有东西游戏的都在这里画
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏防止闪烁
        this.setBackground(Color.orange);

        g.fillRect(15, 75, 850, 550);//默认的游戏界面

        //画积分
        g.setColor(Color.white);
        g.setFont(new Font("微软雅黑", Font.BOLD, 18));
        g.drawString("长度"+length, 750, 35);
        g.drawString("分数"+score, 750, 65);
       // g.drawString("速度"+n, 750, 85);
        //画食物
        Data.food.paintIcon(this,g,foodx,foody);

        if (fx.equals("R")) {
            Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if (fx.equals("L")) {
            Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if (fx.equals("U")) {
            Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if (fx.equals("D")) {
            Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
        }


        for (int i = 1; i < length; i++) {
            Data.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, 300);
        }

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

    //键盘监听,接口实现
        @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){
                    fx = "U";
                }else if(keyCode == KeyEvent.VK_DOWN){
                fx = "D";
            }else if(keyCode == KeyEvent.VK_LEFT){
                    fx = "L";
                }else if(keyCode == KeyEvent.VK_RIGHT){
                    fx = "R";
                }
        }

        //事件监听,通过接口来实现 ,
    // 固定刷新,用Time来实现1秒十次
        @Override
    public void actionPerformed(ActionEvent e) {

        if(isStart&&isFail==false){

            if(snakeX[0]==foodx && snakeY[0]==foody){
                length++;

                score+=10;
                n=n-5;
                foodx = 25+25*random.nextInt(30);
                foody = 75+25*random.nextInt(18);
                if(n==85){
                    timer1.start();
                }
                if(n==65){
                    timer2.start();
                }

            }
            //移动,用数组坐标非常智能,只要写头的代码就OK了
            for (int i = length-1; i >0; i--) {
                snakeX[i] = snakeX[i-1];
                snakeY[i] = snakeY[i-1];
            }

            if(fx.equals("R")){
                snakeX[0] += 25;
                if(snakeX[0]>825) { snakeX[0] = 25;}//边界判断
            }else if(fx.equals("L")){
                snakeX[0] -= 25;
                if(snakeX[0]<25) { snakeX[0] = 825;}//边界判断
            }else if(fx.equals("U")){
                snakeY[0] -= 25;
                if(snakeY[0]<75) { snakeY[0] = 600;}//边界判断
            }else if(fx.equals("D")){
                snakeY[0] += 25;
                if(snakeY[0]>600) { snakeY[0] = 75;}//边界判断
            }

            //失败判定
            for(int i=1;i 

读取图片

public class Data {



    public static URL upURL = Data.class.getResource("statics/up.png");
    public static ImageIcon up = new ImageIcon(upURL);


    public static URL rightURL = Data.class.getResource("statics/rigth.png");
    public static ImageIcon right = new ImageIcon(rightURL);

    public static URL leftURL = Data.class.getResource("statics/left.png");
    public static ImageIcon left = new ImageIcon(leftURL);

    public static URL downURL = Data.class.getResource("statics/down.png");
    public static ImageIcon down = new ImageIcon(downURL);

    public static URL foodURL = Data.class.getResource("statics/food.png");
    public static ImageIcon food = new ImageIcon(foodURL);

    public static URL bodyURL = Data.class.getResource("statics/body.png");
    public static ImageIcon body = new ImageIcon(bodyURL);




}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/439612.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号