标题:贪吃蛇不能动??
只看楼主
hellboy
Rank: 1
等 级:新手上路
威 望:1
帖 子:245
专家分:0
注 册:2006-6-24
 问题点数:0 回复次数:2 
贪吃蛇不能动??
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 
2006-10-24 23:20
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
得分:0 
http://bbs.bc-cn.net/viewthread.php?tid=74716&extra=&page=100#
这是我以前写的贪吃蛇,你可以参考一下

可惜不是你,陪我到最后
2006-10-25 09:09
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
得分:0 
我也写过一个 置顶有

win32汇编
病毒 加密
目前兴趣所在
2006-10-25 10:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-98498-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.189990 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved