当前位置: 代码迷 >> Java相关 >> [原创][开源]贪吃蛇
  详细解决方案

[原创][开源]贪吃蛇

热度:425   发布时间:2006-06-02 11:07:00.0

兄弟,你蛮厉害的嘛


----------------解决方案--------------------------------------------------------
楼主.如果你看到到的贴子的话请加我QQ:4354623.我有问题想请教.另外说点额外的,如果你的出生日期是真的话,咱俩是同年同月同日生,太有缘了
----------------解决方案--------------------------------------------------------

把蛇提了出来
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
class NewSnakeMain
{
public static void main(String [] args)
{
NewSnakeFrame f=new NewSnakeFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);
}
}
class NewSnakeFrame extends JFrame
{
public NewSnakeFrame()
{
NewSnakePanel p=new NewSnakePanel();
Container con=getContentPane();
con.add(p);
JMenuBar menubar=new JMenuBar();
JMenu game=new JMenu("game");
menubar.add(game);
game.add(p.getStart());
game.addSeparator();
game.add(p.getHang());
game.addSeparator();
game.add(p.getRestart());
setTitle("贪吃蛇");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
setJMenuBar(menubar);
}
public static final int DEFAULT_WIDTH=400;
public static final int DEFAULT_HEIGHT=300;
}
class NewSnakePanel extends JPanel implements ActionListener,Runnable
{
public NewSnakePanel()
{
start=new JMenuItem("开始");
hang=new JMenuItem("暂停");
restart=new JMenuItem("重新开始");
start.addActionListener(this);
hang.addActionListener(this);
restart.addActionListener(this);
snake=new Snake(this);
th=new Thread(this);
}
public JMenuItem getStart()
{
return start;
}
public JMenuItem getHang()
{
return hang;
}
public JMenuItem getRestart()
{
return restart;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==start)
{
try
{
if(!snake.getMove()&&startOrNot==false)
{
th.start();
snake.setMove(true);
startOrNot=true;
}
else
{
resumeThread();
snake.setMove(true);
snake.setDead(false);
}

}catch(Exception e1){}
}
else if(e.getSource()==hang)
{
snake.setMove(false);
}
else if(e.getSource()==restart)
{
snake.reStart();
snake.setDead(false);
resumeThread();
snake.setMove(true);
}
}
public synchronized void hangThread() throws InterruptedException
{
wait();
}
public synchronized void resumeThread()
{
notifyAll();
}
public void run()
{
while(th!=null)
{
while(!snake.getMove())
{
try
{
hangThread();
}catch(InterruptedException e){}
}

try
{
Thread.sleep(5);
}catch(InterruptedException e){}
snake.move();
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
snake.paint(g);
}
private JMenuItem start;
private JMenuItem hang;
private JMenuItem restart;
private boolean startOrNot;
private Thread th;
private Snake snake;
}
class Snake
{
public Snake(NewSnakePanel p)
{
rects=new LinkedList<Rectangle2D>();
leftX=100;
topY=100;
itleft=20;
ittop=20;
canUp=false;
canDown=true;
canLeft=true;
canRight=true;
howlong=15;
move=false;
where=RIGHT;
creatOne();
KeyHandler listener=new KeyHandler();
p.addKeyListener(listener);
p.setFocusable(true);
}
public void creatOne()
{
itleft=(int)(Math.random()*350);
ittop=(int)(Math.random()*235);
}
public void move()
{
deadOrNot();
eatOrNot();
if(where==UP)
{
canLeft=true;
canRight=true;
topY-=1;
}
else if(where==DOWN)
{
canLeft=true;
canRight=true;
topY+=1;
}
else if(where==LEFT)
{
canUp=true;
canDown=true;
leftX-=1;
}
else if(where==RIGHT)
{
canUp=true;
canDown=true;
leftX+=1;
}
}
public void eatOrNot()
{
Point2D p1=new Point2D.Double(itleft,ittop);
Point2D p2=new Point2D.Double(itleft+WIDTH,ittop+HEIGHT);
if(rect.contains(p1)||rect.contains(p2))
{
setLong();
alreadyEat=true;
}
}
public void deadOrNot()
{
if(topY<0||topY>235||leftX<0||leftX>385)
{
dead=true;
}
for(int j=rects.size()-8;j>0;j--)
{
Rectangle2D r=rects.get(j);
if(r.getX()==leftX && r.getY()==topY)
{
dead=true;
}
}
}
public void setLong()
{
howlong+=10;
}
public void reStart()
{
for(int i=0;i<rects.size();i++)
{
rects.remove(i);
}
score=0;
leftX=100;
topY=100;
howlong=15;
where=RIGHT;
}
public void paint(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
String message="Game Over!";
String counter="";
g2.drawString("Score:"+counter.valueOf(score),10,10);
Font f=new Font("Serif",Font.BOLD,36);
g2.setFont(f);
g2.setPaint(Color.black);
rect=new Rectangle2D.Double(leftX,topY,WIDTH,HEIGHT);
rectP=new Rectangle2D.Double(itleft,ittop,WIDTH,HEIGHT);
rects.add(rect);
rects.add(0,rectP);
if(alreadyEat)
{
++score;
rects.remove(0);
creatOne();
alreadyEat=false;
}
if(dead)
{
g2.drawString(message,130,180);
move=false;
}
for(int i=0;i<rects.size();i++)
{
g2.draw((Rectangle2D)rects.get(i));
if(i>howlong)
{
rects.remove(i-howlong);
rects.remove(0);
}

}
}
public boolean getDead()
{
return dead;
}
public void setDead(boolean b)
{
dead=b;
}
public boolean getMove()
{
return move;
}
public void setMove(boolean b)
{
move=b;
}
private double leftX;
private double topY;
private int itleft;
private int ittop;
private boolean canUp;
private boolean canDown;
private boolean canLeft;
private boolean canRight;
private boolean dead;
public static final double WIDTH=6;
public static final double HEIGHT=6;
private Rectangle2D rect,rectP;
private LinkedList<Rectangle2D> rects;
private int howlong;
private boolean move;
private boolean alreadyEat;
private int score;
private int where;
public static final int UP=1;
public static final int DOWN=2;
public static final int LEFT=3;
public static final int RIGHT=4;
private class KeyHandler implements KeyListener
{
public void keyPressed(KeyEvent e)
{
int keyCode=e.getKeyCode();
if(keyCode==KeyEvent.VK_LEFT&&canLeft)
{
where=LEFT;
canRight=false;
}
else if(keyCode==KeyEvent.VK_RIGHT&&canRight)
{
where=RIGHT;
canLeft=false;
}
else if(keyCode==KeyEvent.VK_UP&&canUp)
{
where=UP;
canDown=false;
}
else if(keyCode==KeyEvent.VK_DOWN&&canDown)
{
where=DOWN;
canUp=false;
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}

}
}
----------------解决方案--------------------------------------------------------
private LinkedList<Rectangle2D> rects;
这句话是什么意思呢?特别是:在LinkedList后面加上<Rectangle2D>,我编译的时候总是报错。
----------------解决方案--------------------------------------------------------
因为你用的是老码本的编译器
这是泛型,要1.5以上的JAVA才支持
----------------解决方案--------------------------------------------------------
什么是范型,要怎么样才能让编译器通过呢?
----------------解决方案--------------------------------------------------------
升级到1.5就行了啊!

----------------解决方案--------------------------------------------------------
泛型简单地说是可以让你放到Vector等数据结构里面的对象取出来的时候不用Cast成某种类的对象

比如说 Vector
你在1.42的时候是这么声明的
Vector v = new Vector();

而你JDK是1.5的时候 这么声明的话 下面会报个安全警告 但不影响运行程序
注意: C:\JAVA\SnakeMain.java 使用了未经检查或不安全的操作。
注意: 要了解详细信息,请使用 -Xlint:unchecked 重新编译。

所以要写成
Vector<Object> v = new Vector<Object>();



[此贴子已经被作者于2006-6-24 15:18:45编辑过]


----------------解决方案--------------------------------------------------------
楼上斑竹加我QQ85127658
----------------解决方案--------------------------------------------------------
我QQ不加别人的 不要建议啊 我QQ只是为了联系身边的人用的  有问题可以发帖子啊 真不好意思啊
----------------解决方案--------------------------------------------------------
  相关解决方案