java贪吃蛇游戏源码分享.docx

上传人:b****4 文档编号:3999769 上传时间:2022-11-27 格式:DOCX 页数:9 大小:16.51KB
下载 相关 举报
java贪吃蛇游戏源码分享.docx_第1页
第1页 / 共9页
java贪吃蛇游戏源码分享.docx_第2页
第2页 / 共9页
java贪吃蛇游戏源码分享.docx_第3页
第3页 / 共9页
java贪吃蛇游戏源码分享.docx_第4页
第4页 / 共9页
java贪吃蛇游戏源码分享.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

java贪吃蛇游戏源码分享.docx

《java贪吃蛇游戏源码分享.docx》由会员分享,可在线阅读,更多相关《java贪吃蛇游戏源码分享.docx(9页珍藏版)》请在冰豆网上搜索。

java贪吃蛇游戏源码分享.docx

java贪吃蛇游戏源码分享

1蛇的运动实现方法

定义一个集合用于存放蛇头运动时的坐标点

2蛇头碰撞身体的实现方法

检查蛇头的坐标是否等于现有集合内坐标点的坐标

3下面是完整游戏代码

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

游戏有四个类

packageTcs;

importjava.awt.Color;

importjava.awt.Container;

importjava.awt.Font;

importjava.awt.Graphics;

importjava.awt.event.KeyEvent;

importjava.awt.event.KeyListener;

importjava.awt.event.MouseAdapter;

importjava.awt.event.MouseEvent;

importjava.awt.event.MouseListener;

importjava.util.ArrayList;

importjava.util.Arrays;

importjava.util.Collection;

importjava.util.Random;

importjava.util.Timer;

importjava.util.TimerTask;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JPanel;

publicclassSnackextendsJPanelimplementsKeyListener{

    publicJButtonbt=newJButton("重新开始");

    publicArrayListbw=newArrayList();

    publicbody[]b=newbody[5];

    publicStringstate="";

    publicArrayListp=newArrayList();

    publicstaticintscore;

    publicSnack(){

        this.addKeyListener(this);

        shengc();

    }

    publicvoidshengc(){

        for(inti=0;i

            b[i]=newbody();

            b[i].x=10-i*10;

            b[i].y=150;

        }

    }

    publicintx=0,y=0;

    publicvoidpaint(Graphicsg){

        super.paint(g);

        g.setColor(Color.green);

        g.setFont(newFont(Font.SANS_SERIF,Font.BOLD,20));

        for(inti=0;i

            bodyz1=b[i];

            g.drawString("O",b[i].x,b[i].y);

        }

        g.setColor(Color.BLUE);

        g.setFont(newFont(Font.SANS_SERIF,Font.BOLD,20));

        g.drawString("SCORE:

"+score,30,30);

        paintjs(g);

        paintbw(g);

    }

    publicvoidpaintjs(Graphicsg){

        g.setColor(Color.BLACK);

        if(state.length()>1){

            g.drawString(state,140,200);

        }

    }

    publicvoidpaintbw(Graphicsg){

        g.setFont(newFont(Font.SANS_SERIF,Font.BOLD,25));

        g.setColor(Color.RED);

        for(inti=0;i

            g.drawString("o",bw.get(i).x,bw.get(i).y);

        }

    }

    publicbooleanyj(){

        if((b[0].x<400&&b[0].x>0)&&(b[0].y<400&&b[0].y>0)){

            returnfalse;

        }else{

            state="GAMEOVER";

            returntrue;

        }

    }

    publicvoidstmove(){

        if(pzjc()==false&&(yj()==false)){

            b[0].speed=10;

            b[0].move();

            p.add(newpoint(b[0].x,b[0].y,b[0].fx));

            if(p.size()>b.length){

                p.remove(p.get(0));

                //System.out.println(p.size());

            }

        }

    }

    publicintjl(bodya,Treasureb){

        intjl=0;

        jl=(int)Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)

                *(a.y-b.y));

        returnjl;

    }//暂时无用

    publicvoidssmove(){

        if(p.size()>=b.length){

            for(inti=0;i

                b[i+1].fx=p.get(i).fx;

                b[i+1].x=p.get(i).x;

                b[i+1].y=p.get(i).y;

            }

        }

    }

    Randomr=newRandom();

    publicvoidbzbw(){

        if(bw.size()<1){

            Treasures=newTreasure();

            s.x=r.nextInt(300)+50;

            s.y=r.nextInt(300)+50;

            bw.add(s);

        }

    }

    publicvoidbwxs(){

        Timert=newTimer();

        t.schedule(newTimerTask(){

            publicvoidrun(){

            }

        },0,8000);

    }

    publicbooleanpzjc(){

        for(inti=1;i

            if(p.get(0).equals(p.get(i))){

                state="GAMEOVER";

                returntrue;

            }

        }

        returnfalse;

    }

    publicvoidcrush(){

        if(bw.size()>0){

            if(jl(b[0],bw.get(0))<8){

                bw.remove(0);

                b=Arrays.copyOf(b,b.length+1);

                b[b.length-1]=newbody();

                score+=10;

            }

        }

    }

    publicvoidgameover(){

        MouseListenerk=newMouseAdapter(){

            publicvoidmouseClicked(MouseEvente){

                super.mouseClicked(e);

                state="";

                b=Arrays.copyOf(b,5);

                p.clear();

                shengc();

                score=0;

                bt.setVisible(false);

                 

            }

        };

        if(state.length()>1){

            this.add(bt);

            bt.setVisible(true);

             

            bt.setBounds(150,150,100,30);

            bt.addMouseListener(k);

        }

        if(bt.isVisible()==false){this.remove(bt);} 

        this.requestFocus();

    

    }

    publicvoidzmAction(){

        Timertimer=newTimer();

        timer.schedule(newTimerTask(){

            publicvoidrun(){

                bzbw();//生成宝物

                stmove();//蛇头运动

                ssmove();//蛇身运动

                crush();//碰撞检测

                gameover();

                repaint();

            }

        },10,83);

    }

    publicstaticvoidmain(String[]args){

        JFramejf=newJFrame("贪吃蛇");

        jf.setBounds(0,0,400,400);

        jf.setVisible(true);

        jf.setLayout(null);

        Containerc=newContainer();

        c=jf.getContentPane();

        c.setBackground(Color.WHITE);

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Snacks=newSnack();

        s.setVisible(true);

        s.setBounds(0,0,600,600);

        s.setLocation(0,0);

        s.setBackground(Color.ORANGE);

        jf.add(s);

        s.zmAction();

        s.requestFocus();

    }

    publicvoidkeyTyped(KeyEvente){

        //TODOAuto-generatedmethodstub

    }

    @Override

    publicvoidkeyPressed(KeyEvente){

        intk=e.getKeyCode();

        switch(k){

        caseKeyEvent.VK_UP:

            if(b[0].fx!

="sz"&&b[0].fx!

="xz"){

                b[0].fx="sz";

            }

            break;

        caseKeyEvent.VK_DOWN:

            if(b[0].fx!

="sz"&&b[0].fx!

="xz"){

                b[0].fx="xz";

            }

            break;

        caseKeyEvent.VK_LEFT:

            if(b[0].fx!

="zz"&&b[0].fx!

="yz"){

                b[0].fx="zz";

            }

            break;

        caseKeyEvent.VK_RIGHT:

            if(b[0].fx!

="zz"&&b[0].fx!

="yz"){

                b[0].fx="yz";

            }

            break;

        }

        repaint();

    }

    publicvoidkeyReleased(KeyEvente){

    }

}

2、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

packageTcs;

publicclassbody{

publicintx=0;

publicinty=0;

publicintspeed;

privateStringstr;

publicStringfx;

publicbody(){

    fx="yz";

}

publicintgetX(){

    returnx;

}

publicvoidsetX(intx){

    this.x=x;

}

publicintgetY(){

    returny;

}

publicvoidsetY(inty){

    this.y=y;

}

publicStringgetStr(){

    returnstr;

}

publicvoidsetStr(Stringstr){

    this.str=str;

}

publicvoidsz(){

     

    this.y+=-speed;

}

publicvoidxz(){

    this.y+=speed;

}

publicvoidzz(){

    this.x+=-speed;

}

publicvoidyz(){

    this.x+=speed;

}

publicvoidmove(){

    if(fx=="xz"){

        xz();

    }

    if(fx=="sz"){

        sz();

    }

    if(fx=="zz"){

        zz();

    }

    if(fx=="yz"){

        yz();

    }

}

}

3、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

packageTcs;

publicclasspoint{

publicintx;

publicinty;

publicStringfx;

publicpoint(intx,inty,Stringfx){

    

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 成人教育 > 自考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1