import java.awt.*;
import java.awt.event.*;
public class GreedSnake //主类
{
public static void main(String[] args) {
new MyWindow();
}
}
class MyPanel extends Panel implements KeyListener,Runnable//自定义面板类,继承了键盘和线程接口
{MyPanel my1;
Button snake[]; //定义蛇按钮
int shu=0; //蛇的节数
int food[]; //食物数组
boolean result=true; //判定结果是输 还是赢
Thread thread; //定义线程
static int weix,weiy; //食物位置
boolean t=true; //判定游戏是否结束
int fangxiang=0; //蛇移动方向
int x=0,y=0; //蛇头位置
boolean paust=true;
MyPanel()
{
setLayout(null);
snake=new Button[70];
food=new int [70];
thread=new Thread(this);
for(int j=0;j<70;j++)
{
food[j]=(int)(Math.random()*99);//定义70个随机食物
}
weix=(int)(food[0]*0.1)*60; //十位*60为横坐标
weiy=(int)(food[0]%10)*40; //个位*40为纵坐标
for(int i=0;i<70;i++)
{
snake[i]=new Button();
}
add(snake[0]);
snake[0].setBackground(Color.black);
snake[0].addKeyListener(this); //为蛇头添加键盘监视器
snake[0].setBounds(0,0,10,10);
setBackground(Color.blue);
}
public void run() //接收线程
{
while(t)
{
//try{thread.sleep(400);} catch(Exception e){}
if(paust){
if(fangxiang==0)//向右
{
try
{
x+=10;
//snake[0].setBounds(x+10,y,x+20,y+10);
snake[0].setLocation(x, y);//设置蛇头位置
if(x==weix&&y==weiy) //吃到食物
{
shu++; int q=shu;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
//try{thread.sleep(400);} catch(Exception e){};
repaint(); //重绘下一个食物
//try{thread.sleep(400);} catch(Exception e){};
if(shu%2==0)
snake[shu].setBackground(Color.yellow);
else
snake[shu].setBackground(Color.green);
add(snake[shu]); //增加蛇节数和位置
//try{thread.sleep(400);} catch(Exception e){};
snake[shu].setBounds(snake[shu-1].getBounds());
}
if(shu<=15)
thread.sleep(100);
if(shu>15&&shu<=35)
thread.sleep(80);
if(shu>35&&shu<=70)
thread.sleep(50);
if(shu>=70)
thread.sleep(10); //睡眠100ms
}
catch(Exception e){}
}
else if(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();
if(shu%2==0)
snake[shu].setBackground(Color.yellow);
else
snake[shu].setBackground(Color.green);
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
if(shu<15)
thread.sleep(100);
if(shu>=15&&shu<35)
thread.sleep(80);
if(shu>=35&&shu<70)
thread.sleep(50);
if(shu>=70)
thread.sleep(10);
}
catch(Exception e){}
}
else if(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();
if(shu%4==0)
snake[shu].setBackground(Color.yellow);
else if(shu%4==1)
snake[shu].setBackground(Color.green);
if(shu%4==3)
snake[shu].setBackground(Color.red);
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
if(shu<15)
thread.sleep(100);
if(shu>=15&&shu<35)
thread.sleep(80);
if(shu>=35&&shu<70)
thread.sleep(50);
if(shu>=70)
thread.sleep(10);
}
catch(Exception e){}
}
else if(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();
if(shu%3==0)
snake[shu].setBackground(Color.yellow);
else if(shu%3==1)
snake[shu].setBackground(Color.green);
else
snake[shu].setBackground(Color.red);
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
if(shu<15)
thread.sleep(100);
if(shu>=15&&shu<35)
thread.sleep(80);
if(shu>=35&&shu<70)
thread.sleep(50);
if(shu==70)
thread.sleep(10);
}
catch(Exception e){}
}
int num1=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();
}
int num=shu;
while(num>0) //设置蛇节位置
{
snake[num].setBounds(snake[num-1].getBounds());
//try{thread.sleep(400);} catch(Exception e){};
num--;
}
}
if(shu==70) //如果蛇节数等于15则胜利
{
t=false;
result=true;
repaint();
}
}
}
public void keyPressed(KeyEvent e) //按下键盘方向键
{
if(e.getKeyCode()==KeyEvent.VK_RIGHT)//右键
{
if(fangxiang!=1)//如果先前方向不为左
fangxiang=0;
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{ if(fangxiang!=0)
fangxiang=1;
}
else if(e.getKeyCode()==KeyEvent.VK_UP)
{ if(fangxiang!=3)
fangxiang=2;
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{ if(fangxiang!=2)
fangxiang=3;
}
else if (e.getKeyCode()==KeyEvent.VK_ENTER)
new MyWindow();
else if(e.getKeyCode()==KeyEvent.VK_SPACE)
paust=!paust;
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void paint(Graphics g) //在面板上绘图
{
int x1=this.getWidth()-1;
int y1=this.getHeight()-1;
if(shu%2==0)
g.setColor(Color.red);
if(shu%2!=0)
g.setColor(Color.orange);
g.fillOval(weix, weiy, 10, 10);//食物
g.drawRect(0, 0, x1, y1); //墙
g.drawString("小心!!毒蛇!!!",250,450);
if(shu<=15)
{ int q=shu;
g.drawString("分数:"+q,250,300);
g.drawString("第一关:",250,350); }
if(shu>15&&shu<=35)
{int q=15+2*(shu-15);
g.drawString("第二关:",250,350);
g.drawString("分数:"+q,250,300);
}
if(shu>35&&shu<=70)
{int q=55+3*(shu-35);
g.drawString("分数:"+q,250,300);
g.drawString("第三关:",250,350); }
if(t==false&&result==false)
g.drawString("GAME OVER!", 250, 200);//输出游戏失败
else if(t==false&&result==true)
g.drawString("YOU WIN!", 250, 200);//输出游戏成功
}
}
class MyWindow extends Frame //自定义窗口类
{
MyPanel my;
MyWindow begin;
Panel panel;
MyWindow()
{
super("GreedSnake");
my=new MyPanel();
panel=new Panel();
panel.add(new Label("按上下左右键控制蛇行动 空格暂停"));
add(panel,BorderLayout.NORTH);
add(my,BorderLayout.CENTER);
setBounds(100,100,610,500);
setVisible(true);
try
{
my.thread.start(); //开始线程
my.validate();
}
catch(Exception ee){}
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
//继承:继承就是两个类之间的关系,A类继承B类的成员变量和成员函数
//类:类是具有某些共同特征的一些对象的集合。
//类是对对象的抽象 ,对象是对类的具体化。
//接口:接口就是定义了若干 的抽象方法和常量,形成某一个属性集合 ,该//属//性集合通常都对应某一组功能。,其主要作用是实现类似于类的多重继承。
//包:包是java提供的文件组织形式。一个包对应一个文件夹,一个文件夹种包含很多类文件。