贪吃蛇
importjava.awt甲切缩高滑风完金快务.*;
importjava.awt.event火虽.*;
publicclassGreedSnake//主类
{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
newMyWindow();
}
}
classMyP经在构anelextendsPanelimplementsKeyListener,Runnable//自定义面板类,继承了键盘和线程接口
{
Buttonsnake[];//定义蛇按钮
intsh走配u=0;//蛇的节数
intfood[];//食物数组
booleanresult=true;//判定结果是输还是赢
Threadthread;//定义线程
staticintweix,weiy;//奏促续即食物位置
boolea久应本半伟味践氧nt=true;//判定片限校矛扩度英使述游戏是否结束
intfangxiang=0;//蛇移动方向
intx=0,y=0;//蛇头位置
MyPanel()
{
setLayout(null);
snake=newButton[20];
food=newint[20];
thread=n反既ewThread(this);
for(intj=0;j<20;j++)
{
fo又被od[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].setBac读具象表赶府张kground(***.black);
snake[0].addKey顺型Listener(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(***.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,***.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){}
}
}
}
希望对你能有所帮助。
标签:Java,贪吃蛇,代码