java贪吃蛇游戏源码分享文档格式.docx
- 文档编号:16906685
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:9
- 大小:16.51KB
java贪吃蛇游戏源码分享文档格式.docx
《java贪吃蛇游戏源码分享文档格式.docx》由会员分享,可在线阅读,更多相关《java贪吃蛇游戏源码分享文档格式.docx(9页珍藏版)》请在冰豆网上搜索。
importjavax.swing.JFrame;
importjavax.swing.JPanel;
publicclassSnackextendsJPanelimplementsKeyListener{
publicJButtonbt=newJButton("
重新开始"
);
publicArrayList<
Treasure>
bw=newArrayList<
();
publicbody[]b=newbody[5];
publicStringstate="
"
;
point>
p=newArrayList<
publicstaticintscore;
publicSnack(){
this.addKeyListener(this);
shengc();
}
publicvoidshengc(){
for(inti=0;
i<
b.length;
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));
bodyz1=b[i];
g.drawString("
O"
b[i].x,b[i].y);
g.setColor(Color.BLUE);
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);
bw.size();
o"
bw.get(i).x,bw.get(i).y);
publicbooleanyj(){
if((b[0].x<
400&
&
b[0].x>
0)&
(b[0].y<
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(){
=b.length){
b.length-1;
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()<
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;
p.size();
if(p.get(0).equals(p.get(i))){
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);
b=Arrays.copyOf(b,5);
p.clear();
score=0;
bt.setVisible(false);
};
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(){
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="
break;
caseKeyEvent.VK_DOWN:
caseKeyEvent.VK_LEFT:
zz"
yz"
caseKeyEvent.VK_RIGHT:
publicvoidkeyReleased(KeyEvente){
}
2、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
publicclassbody{
publicintx=0;
publicinty=0;
publicintspeed;
privateStringstr;
publicStringfx;
publicbody(){
fx="
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();
sz();
zz();
yz();
3、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
publicclasspoint{
publicintx;
publicinty;
publicpoint(intx,inty,Stringfx){
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 贪吃 游戏 源码 分享