求Java贪吃蛇代码

2022-04-06 教育 162阅读
贪吃蛇
importjava.awt.*;
importjava.awt.event.*;
publicclassGreedSnake//主类
{


publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
newMyWindow();

}

}
classMyPanelextendsPanelimplementsKeyListener,Runnable//自定义面板类,继承了键盘和线程接口
{

Buttonsnake[];//定义蛇按钮
intshu=0;//蛇的节数
intfood[];//食物数组
booleanresult=true;//判定结果是输还是赢
Threadthread;//定义线程
staticintweix,weiy;//食物位置
booleant=true;//判定游戏是否结束
intfangxiang=0;//蛇移动方向
intx=0,y=0;//蛇头位置
MyPanel()
{

setLayout(null);
snake=newButton[20];
food=newint[20];
thread=newThread(this);

for(intj=0;j<20;j++)
{
food[j]=(int)(Math.random()*99);//定义20个随机食物
}

weix=(int)(food[0]*0.1)*60;//十位*60为横坐标
weiy=(int)(food[0]%10)*40;//个位*40为纵坐标
for(inti=0;i<20;i++)
{
snake[i]=newButton();
}

add(snake[0]);
snake[0].setBackground(Color.black);
snake[0].addKeyListener(this);//为蛇头添加键盘监视器
snake[0].setBounds(0,0,10,10);
setBackground(Color.cyan);
}

publicvoidrun()//接收线程
{

while(t)
{

if(fangxiang==0)//向右
{
try
{
x+=10;
snake[0].setLocation(x,y);//设置蛇头位置

if(x==weix&&y==weiy)//吃到食物
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();//重绘下一个食物
add(snake[shu]);//增加蛇节数和位置
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);//睡眠100ms
}
catch(Exceptione){}
}
elseif(fangxiang==1)//向左
{
try
{
x-=10;
snake[0].setLocation(x,y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}

thread.sleep(100);
}
catch(Exceptione){}
}
elseif(fangxiang==2)//向上
{
try
{
y-=10;
snake[0].setLocation(x,y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exceptione){}
}
elseif(fangxiang==3)//向下
{
try
{
y+=10;
snake[0].setLocation(x,y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exceptione){}
}
intnum1=shu;
while(num1>1)//判断是否咬自己的尾巴
{
if(snake[num1].getBounds().x==snake[0].getBounds().x&&snake[num1].getBounds().y==snake[0].getBounds().y)
{
t=false;
result=false;
repaint();
}
num1--;
}
if(x<0||x>=this.getWidth()||y<0||y>=this.getHeight())//判断是否撞墙
{
t=false;
result=false;
repaint();
}
intnum=shu;
while(num>0)//设置蛇节位置
{
snake[num].setBounds(snake[num-1].getBounds());
num--;
}

if(shu==15)//如果蛇节数等于15则胜利
{
t=false;
result=true;
repaint();
}

}

}
publicvoidkeyPressed(KeyEvente)//按下键盘方向键
{
if(e.getKeyCode()==KeyEvent.VK_RIGHT)//右键
{
if(fangxiang!=1)//如果先前方向不为左
fangxiang=0;

}
elseif(e.getKeyCode()==KeyEvent.VK_LEFT)
{if(fangxiang!=0)
fangxiang=1;

}
elseif(e.getKeyCode()==KeyEvent.VK_UP)
{if(fangxiang!=3)
fangxiang=2;

}
elseif(e.getKeyCode()==KeyEvent.VK_DOWN)
{if(fangxiang!=2)
fangxiang=3;

}
}

publicvoidkeyTyped(KeyEvente)
{

}
publicvoidkeyReleased(KeyEvente)
{

}
publicvoidpaint(Graphicsg)//在面板上绘图
{
intx1=this.getWidth()-1;
inty1=this.getHeight()-1;
g.setColor(Color.red);
g.fillOval(weix,weiy,10,10);//食物
g.drawRect(0,0,x1,y1);//墙
if(t==false&&result==false)
g.drawString("GAMEOVER!",250,200);//输出游戏失败
elseif(t==false&&result==true)
g.drawString("YOUWIN!",250,200);//输出游戏成功
}

}
classMyWindowextendsFrameimplementsActionListener//自定义窗口类
{
MyPanelmy;
Buttonbtn;
Panelpanel;
MyWindow()
{
super("GreedSnake");
my=newMyPanel();
btn=newButton("begin");
panel=newPanel();
btn.addActionListener(this);
panel.add(newLabel("begin后请按Tab键选定蛇"));
panel.add(btn);
panel.add(newLabel("按上下左右键控制蛇行动"));
add(panel,BorderLayout.NORTH);
add(my,BorderLayout.CENTER);
setBounds(100,100,610,500);
setVisible(true);
validate();
addWindowListener(newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
});
}
publicvoidactionPerformed(ActionEvente)//按下begin按钮
{

if(e.getSource()==btn)
{
try
{
my.thread.start();//开始线程
my.validate();
}
catch(Exceptionee){}
}

}
}
希望对你能有所帮助。
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com