当前位置: 代码迷 >> Java相关 >> 我自己写的贪吃蛇代码
  详细解决方案

我自己写的贪吃蛇代码

热度:461   发布时间:2006-06-29 17:16:10.0
我自己写的贪吃蛇代码

我自己写的贪吃蛇代码,在此感谢千里冰封的指导.可以运行在1.4的版本上.请大家多多指教!!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Snake extends JFrame
{
private int sleep=300;
public int loc;
private MySnake ms;
public Rectangle food;
private boolean bool=false;
public boolean br=false;
public Snake()
{
Container c=getContentPane();

ms=new MySnake();
makeFood();

setSize(800,600);
setLocation(400,300);
show();

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

new Thread(new Runnable()
{
public void run()
{
while(true)
{
try
{
Thread.sleep(sleep);
}
catch (Exception e)
{
return;
}
if(br==false)
repaint();
}
}
}).start();
}

public void paint(Graphics g)
{
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
loc=e.getKeyCode();
}
});
super.paint(g);
g.setColor(Color.RED);
g.fillRect(food.x,food.y,food.width,food.height);
ms.move(loc);
ms.drawMe(g);
ms.Food(food);
}

public static void main(String args[])
{
new Snake();
}

public void makeFood()
{
while(true)
{
int x=(int)(Math.random()*200);
int y=(int)(Math.random()*200);
if(x%22==0 && y%22==0 && x<778 && y<578 && x>22 && y>22)
{
food=new Rectangle(x,y,20,20);
break;
}
}

}

class MySnake
{
private ArrayList list,array;
private Rectangle rec,rect;
private int block=20;
private int bolckSpace=2;
private int j=0;
boolean bool=false;
private int total=0;
boolean b=false;

public MySnake()
{
list=new ArrayList();
array=new ArrayList();
list.add(new Rectangle(22+44,22,20,20));
list.add(new Rectangle(22+22,22,20,20));
list.add(new Rectangle(22,22,20,20));
}

public void Food(Rectangle food)
{
try
{
array.add(0,food);
rect=(Rectangle)array.get(0);
}
catch (Exception e)
{
return;
}

}

public void move(int x)
{
if(x==40)
{
bool=true;
rec=(Rectangle)list.get(0);
Rectangle rec1=new Rectangle(rec.x,rec.y+(block+bolckSpace),rec.width,rec.height);if(rec.y>556)b=true;
list.add(j,rec1);
}
else if(x==38)
{
bool=true;
rec=(Rectangle)list.get(0);
Rectangle rec1=new Rectangle(rec.x,rec.y-(block+bolckSpace),rec.width,rec.height);if(rec.y<44)b=true;
list.add(j,rec1);
}
else if(x==37)
{
bool=true;
rec=(Rectangle)list.get(0);
Rectangle rec1=new Rectangle(rec.x-(block+bolckSpace),rec.y,rec.width,rec.height);if(rec.x<44)b=true;
list.add(j,rec1);
}
else if(x==39)
{
bool=true;
rec=(Rectangle)list.get(0);
Rectangle rec1=new Rectangle(rec.x+(block+bolckSpace),rec.y,rec.width,rec.height);if(rec.x>756)b=true;
list.add(j,rec1);
}
if(b==true)
{
try
{
JOptionPane.showMessageDialog(Snake.this,"You Are Lose","Error Message",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
catch (Exception e)
{
return;
}
}

}

public void drawMe(Graphics g)
{
if(bool==true)list.remove(list.size()-1);
g.setColor(Color.blue);
for(int i=0;i<list.size();i++)
{
rec=(Rectangle)list.get(i);
try
{
if(rect.x==rec.x && rect.y==rec.y){br=true;total=total+1;array.remove(0);makeFood();}
}
catch (Exception e)
{
return;
}
g.fillRect(rec.x,rec.y,rec.width,rec.height);
}
if(br==true && total%2==1){list.add(rect);}br=false;
}
}
};

搜索更多相关的解决方案: 贪吃  代码  

----------------解决方案--------------------------------------------------------
很好 继续努力

我没运行啊 我刚卸了JDK
----------------解决方案--------------------------------------------------------
谢谢楼主!我运行了,挺有意思的.
----------------解决方案--------------------------------------------------------
你不应该直接把蛇画在JFrame上,这样看上去会闪
最好的办法是画在JPanel上,然后把JPanel放到JFrame上
----------------解决方案--------------------------------------------------------
千里老兄,受教了!!!
----------------解决方案--------------------------------------------------------
很厉害!!!!!
----------------解决方案--------------------------------------------------------
发现红点只会在窗体的左边靠上的地方出现……而且没有GAME OVER……
写得相当好了~继续努力~
----------------解决方案--------------------------------------------------------
总体 不错哦 继续努力 可是 有两个bug 贪吃蛇在前进中是不能直接到着走的 还有撞到自己是死不掉的 至于豆豆 总在 左面出来是因为你的界面是600 800 但产生的随即数是 200 200 所以就这样了 还有就是 论坛最近怎么掀起了 贪吃蛇热潮 嘿嘿

[此贴子已经被作者于2006-6-30 8:59:55编辑过]


----------------解决方案--------------------------------------------------------
不错,不错~~~
----------------解决方案--------------------------------------------------------

偶明天放假了,回家闭关修炼2个月.谢谢hellboy的提醒啊!


----------------解决方案--------------------------------------------------------
  相关解决方案