当前位置: 代码迷 >> Java相关 >> 贪吃蛇不能动??
  详细解决方案

贪吃蛇不能动??

热度:382   发布时间:2006-10-24 23:20:07.0
贪吃蛇不能动??
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Sneak extends JFrame implements ActionListener,KeyListener,Runnable{

final int width=30;
final int height=20;
int x=0,y=0;
int a[][]=new int[20][30];
int direction=4;
final int up=1;
final int down=3;
final int left=2;
final int right=4;
final int length=8;//初始化长度
final int initx=8;
final int inity=5;
Node node;
Thread thread;
Food food=new Food();
LinkedList linklist;
JPanel mainPanel,operatePanel;
JButton startBt,quitBt;
JLabel label,score;
boolean pause=false;
public Sneak(){

this.setSize(400,320);
this.addKeyListener(this);
this.setLayout(new BorderLayout());
this.setBackground(Color.white);
startBt=new JButton("Start");
quitBt=new JButton("quit");
//restart=new JButton("Restart")
label=new JLabel("your score:");
score=new JLabel("");
mainPanel=new JPanel();
//mainPanel.setFocusable(true);
mainPanel.setSize(310,210);
// mainPanel.addKeyListener(mainPanel);
mainPanel.setBackground(Color.white);
//mainPanel.addKeyListener(this);
this.getContentPane().add(mainPanel,"Center");
//this.addKeyListener(this);
// this.getcontentp
operatePanel=new JPanel(new GridLayout(4,1,6,6));
operatePanel.add(label);
operatePanel.add(score);
operatePanel.add(startBt);
operatePanel.add(quitBt);
this.getContentPane().add(operatePanel,"East");
startBt.addActionListener(this);
quitBt.addActionListener(this);
// restart.addActionListener(this);

}

public void init()
{
arrayIni();
direction=4;
linklist=new LinkedList();

for(int i=10;i>=inity;i--)
{
node=new Node(initx,i);
a[initx][i]=1;
linklist.add(node);

}

this.creatFood();
}
public void arrayIni()
{
for(int i=0;i<20;i++)
{
for(int j=0;j<30;j++)
{
a[i][j]=0;
}
}
}
public void creatFood()
{
do
food.creatFood();
while(a[food.randomx][food.randomy]==1);
a[food.randomx][food.randomy]=2;
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==startBt)
{
//init();
pause=true;
// this.setFocusable(true);
// repaint();
/* try
{
thread=new Thread(this);
thread.start();
}catch(Exception err){System.out.println(err.toString());}


}*/
}
if(e.getSource()==quitBt)
{
System.exit(0);
}

}


public void changeDirection(int i)
{
if((i%2)!=(direction%2))
{
direction=i;
}
}

public boolean go()
{
Node node1=(Node)linklist.getFirst();
//Node node2=(Node)linklist.getLast();
//System.out.println(node1.x+" "+node1.y);
//System.out.println(food.randomx+" "+food.randomy);
x=node1.x;
y=node1.y;
// System.out.println(x+" yes "+y+" "+a[x][y]);
switch(direction)
{

case up:
//x=node1.x;
x--;break;
case down:
//x=node1.x;
x++;break;
case right:
//y=node1.y;
y++;break;
case left:
//y=node1.y;
y--;break;

}

if((x>=20)||(y>=30)||(x<0)||(y<0)||(a[x][y]==1))
{

//System.out.println(x+" "+y);
return false;
}

else
{

if(a[x][y]==2)
{
linklist.addFirst(new Node(x,y));
a[x][y]=1;
creatFood();


}

else if(a[x][y]==0)
{
// System.out.println("asdf");
linklist.addFirst(new Node(x,y));
a[x][y]=1;
Node node=(Node)linklist.removeLast();
a[node.x][node.y]=0;
//System.out.println(node.x+"ok"+node.y+" "+a[node.x][node.y]);

}

repaint();
return true;
}

}

public void keyPressed(KeyEvent e)
{
System.out.println("change");
if(e.getKeyCode()==KeyEvent.VK_UP)
{

changeDirection(up);
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
changeDirection(down);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
changeDirection(left);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
changeDirection(right);
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void run()
{
init();

while(true)
{
try
{
if(pause)
{
Thread.sleep(500);

go();
//System.out.println("is go");
}

}catch(Exception e){System.out.println(e.getMessage());}

}
}

public void paint(Graphics g)
{
// g.setColor(Color.white);
// g.fillRect(0,0,150,100);
// System.out.println(direction);
super.paint(g);
for(int i=0;i<20;i++)
{
for(int j=0;j<30;j++)
{
if(a[i][j]==1)
{
g.setColor(Color.BLACK);
g.fillRect(10*j,10*i,9,9);
}
if(a[i][j]==2)
{
g.setColor(Color.red);
g.fillRect(10*j,10*i,9,9);
}
}
}
}

public static void main(String[] arg){
Sneak sm=new Sneak();

sm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sm.setVisible(true);

try
{
Thread thread =new Thread(sm);
thread.start();
}catch(Exception e){}



}




}先在不能动 我把继承改到了 JPanel 就可以动的 为什么啊

[此贴子已经被作者于2006-10-24 23:23:02编辑过]

搜索更多相关的解决方案: int  final  import  贪吃  java  

----------------解决方案--------------------------------------------------------
http://bbs.bc-cn.net/viewthread.php?tid=74716&extra=&page=100#
这是我以前写的贪吃蛇,你可以参考一下
----------------解决方案--------------------------------------------------------
我也写过一个 置顶有
----------------解决方案--------------------------------------------------------
  相关解决方案