呵呵 不过你还是挺幸运 给你吧
连连看的代码(基本算法)加了部分注释
import javaawt;
import javaawtevent;
public class lianliankan implements ActionListener
{
static String s="no"; //用来纪录点击按钮的信息
int x0=0,y0=0,x=0,y=0,n1=0,n2=0; //用来纪录按钮的位置信息
Frame f,f1;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10; //用比较笨的方法添加了
Button b11,b12,b13,b14,b15,b16,b17,b18; //30个按钮来实现游戏界面
Button b19,b20,b21,b22,b23,b24,b25; //可以用数组实现,这是本人
Button b26,b27,b28,b29,b30,bc; //学java时,入门的联系,所以
Button b,ba,br,bt1,bt2; //有些东西很业余!!嘻嘻
Panel p1,p2,p3;
TextField t; //用来显示一些随机信息,方法是下面的guli()
Label l;
int d[][]={ //用来和界面的按钮建立映射关系
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}
};
public static void main(String[] args)
{
lianliankan t=new lianliankan();
tsuiji();
tgo();
}
public void actionPerformed(ActionEvent e) //再来一次按钮的响应事件。
{
int d[][]={
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}
};
thisd=d;
suiji();
fsetVisible(false);
f1setVisible(false);
s="no";
go();
}
public void go()//初始化界面
{
l=new Label("亲爱的玩家,");
f=new Frame("连连看");
t=new TextField();
p2=new Panel();
p1=new Panel();
p3=new Panel();
bc=new Button("退出");
br=new Button("重列");
b=new Button();
b1=new Button(StringvalueOf(d[1][1]));
b2=new Button(StringvalueOf(d[1][2]));
b3=new Button(StringvalueOf(d[1][3]));
b4=new Button(StringvalueOf(d[1][4]));
b5=new Button(StringvalueOf(d[1][5]));
b6=new Button(StringvalueOf(d[2][1]));
b7=new Button(StringvalueOf(d[2][2]));
b8=new Button(StringvalueOf(d[2][3]));
b9=new Button(StringvalueOf(d[2][4]));
b10=new Button(StringvalueOf(d[2][5]));
b11=new Button(StringvalueOf(d[3][1]));
b12=new Button(StringvalueOf(d[3][2]));
b13=new Button(StringvalueOf(d[3][3]));
b14=new Button(StringvalueOf(d[3][4]));
b15=new Button(StringvalueOf(d[3][5]));
b16=new Button(StringvalueOf(d[4][1]));
b17=new Button(StringvalueOf(d[4][2]));
b18=new Button(StringvalueOf(d[4][3]));
b19=new Button(StringvalueOf(d[4][4]));
b20=new Button(StringvalueOf(d[4][5]));
b21=new Button(StringvalueOf(d[5][1]));
b22=new Button(StringvalueOf(d[5][2]));
b23=new Button(StringvalueOf(d[5][3]));
b24=new Button(StringvalueOf(d[5][4]));
b25=new Button(StringvalueOf(d[5][5]));
b26=new Button(StringvalueOf(d[6][1]));
b27=new Button(StringvalueOf(d[6][2]));
b28=new Button(StringvalueOf(d[6][3]));
b29=new Button(StringvalueOf(d[6][4]));
b30=new Button(StringvalueOf(d[6][5]));
p3setLayout(null);
p1setSize(250,300);
p2setSize(100,40);
p3setSize(300,30);
tsetSize(60,30);
lsetSize(70,30);
p1setLayout(new GridLayout(6,5));
p1setBackground(Colorpink);
p1setLocation(100,100);
p2setLocation(0,400);
p3setLocation(50,50);
tsetLocation(230,2);
lsetLocation(150,2);
bcsetLocation(0,40);
brsetLocation(0,100);
fadd(p1);
fadd(p2);
fadd(p3);
p3add(l);
p3add(t);
p2add(bc);
p2add(br);
p1add(b1);
p1add(b2);
p1add(b3);
p1add(b4);
p1add(b5);
p1add(b6);
p1add(b7);
p1add(b8);
p1add(b9);
p1add(b10);
p1add(b11);
p1add(b12);
p1add(b13);
p1add(b14);
p1add(b15);
p1add(b16);
p1add(b17);
p1add(b18);
p1add(b19);
p1add(b20);
p1add(b21);
p1add(b22);
p1add(b23);
p1add(b24);
p1add(b25);
p1add(b26);
p1add(b27);
p1add(b28);
p1add(b29);
p1add(b30);
fpack();
fsetBounds(280,100,500,450);
fsetResizable(false);
fsetVisible(true);
bcaddMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
ex();
}
});
braddMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
chonglie();
}
});
b1addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(1,1,b1);
}
});
b2addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(1,2,b2);
}
});
b3addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(1,3,b3);
}
});
b4addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(1,4,b4);
}
});
b5addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(1,5,b5);
}
});
b6addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(2,1,b6);
}
});
b7addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(2,2,b7);
}
});
b8addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(2,3,b8);
}
});
b9addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(2,4,b9);
}
});
b10addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(2,5,b10);
}
});
b11addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(3,1,b11);
}
});
b12addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(3,2,b12);
}
});
b13addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(3,3,b13);
}
});
b14addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(3,4,b14);
}
});
b15addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(3,5,b15);
}
});
b16addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(4,1,b16);
}
});
b17addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(4,2,b17);
}
});
b18addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(4,3,b18);
}
});
b19addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(4,4,b19);
}
});
b20addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(4,5,b20);
}
});
b21addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(5,1,b21);
}
});
b22addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(5,2,b22);
}
});
b23addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(5,3,b23);
}
});
b24addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(5,4,b24);
}
});
b25addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(5,5,b25);
}
});
b26addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(6,1,b26);
}
});
b27addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(6,2,b27);
}
});
b28addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(6,3,b28);
}
});
b29addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(6,4,b29);
}
});
b30addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
wei(6,5,b30);
}
});
}
public void ex() //退出界面,可用diolog来实现有模式的类型,更加符合
{
f1=new Frame("游戏作业");
f1setLayout(new GridLayout(1,1));
bt1=new Button("确定退出");
bt2=new Button("再来一局");
f1add(bt1);
f1add(bt2);
f1pack();
f1setBounds(400,250,90,60);
f1setResizable(false);
f1show();
f1setVisible(true);
bt1addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
Systemexit(0);
}
});
bt2addActionListener(this);
}
public void suiji() //产生随机数,来填充游戏界面对应的数组的各个位置
{
int m,n,k=0,k1,k2,k3;
for(m=1;m<=15;m++)
{
k1=(int)(Mathrandom()25+1);
for(n=1;n<=2;n++)
{
k2=(int)(Mathrandom()6+1);
k3=(int)(Mathrandom()5+1);
while(d[k2][k3]!=0 && k!=30)
{
k2=(int)(Mathrandom()6+1);
k3=(int)(Mathrandom()5+1);
}
thisd[k2][k3]=k1;
k++;
}
}
}
public void guli() //随机信息
{
int l=0;
tsetText("");
l=(int)(Mathrandom()10);
Systemoutprintln(l);
switch(l)
{
case 1:
tsetText("好!加油!");
break;
case 3:
tsetText("你真棒!");
break;
case 5:
tsetText("加快速度!");
break;
case 6:
tsetText("不错啊!");
break;
case 8:
tsetText("加油吧!");
break;
case 9:
tsetText("够聪明!");
break;
default:
break;
}
}
public void chonglie() //重列方法
{
int save[],i,j,n=0,k2,k3,k;
int d[][]={
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}
};
save=new int[30];
for(n=0;n<30;n++)
save[n]=0; //定义一个数组来保存当前的每个按钮位置上的信息
n=0;
for(i=0;i<=6;i++)
for(j=0;j<=5;j++)
{
if(thisd[i][j]!=0)
{
save[n]=thisd[i][j];
n++;
}
}
n=n-1;
thisd=d;
while(n>=0) //产生随机位置,放置按钮
{
k2=(int)(Mathrandom()6+1);
k3=(int)(Mathrandom()5+1);
while(d[k2][k3]!=0)
{
k2=(int)(Mathrandom()6+1);
k3=(int)(Mathrandom()5+1);
}
thisd[k2][k3]=save[n];
n--;
}
fsetVisible(false);
s="no"; //这里一定要将按钮点击信息归为初始
go();
ling();
}
public void ling() //将数组中为零的成员对应的按钮消去
{ //用按钮类型的数组实现会简化得多,
if(d[1][1]==0)
b1setVisible(false);
if(d[1][2]==0)
b2setVisible(false);
if(d[1][3]==0)
b3setVisible(false);
if(d[1][4]==0)
b4setVisible(false);
if(d[1][5]==0)
b5setVisible(false);
if(d[2][1]==0)
b6setVisible(false);
if(d[2][2]==0)
b7setVisible(false);
if(d[2][3]==0)
b8setVisible(false);
if(d[2][4]==0)
b9setVisible(false);
if(d[2][5]==0)
b10setVisible(false);
if(d[3][1]==0)
b11setVisible(false);
if(d[3][2]==0)
b12setVisible(false);
if(d[3][3]==0)
b13setVisible(false);
if(d[3][4]==0)
b14setVisible(false);
if(d[3][5]==0)
b15setVisible(false);
if(d[4][1]==0)
b16setVisible(false);
if(d[4][2]==0)
b17setVisible(false);
if(d[4][3]==0)
b18setVisible(false);
if(d[4][4]==0)
b19setVisible(false);
if(d[4][5]==0)
b20setVisible(false);
if(d[5][1]==0)
b21setVisible(false);
if(d[5][2]==0)
b22setVisible(false);
if(d[5][3]==0)
b23setVisible(false);
if(d[5][4]==0)
b24setVisible(false);
if(d[5][5]==0)
b25setVisible(false);
if(d[6][1]==0)
b26setVisible(false);
if(d[6][2]==0)
b27setVisible(false);
if(d[6][3]==0)
b28setVisible(false);
if(d[6][4]==0)
b29setVisible(false);
if(d[6][5]==0)
b30setVisible(false);
}
public void wei(int w1,int w2,Button bz) //判断并纪录每次点击按钮的信息
{ //当两次的按钮相同才能消去
if((strim())equals("no"))
{
s=b1getLabel();
x0=w1;
y0=w2;
n1=d[x0][y0];
b=bz;
x=w1;
y=w2;
n2=d[x][y];
ba=bz;
}
else
{
x0=x;
y0=y;
n1=d[x0][y0];
b=ba;
x=w1;
y=w2;
n2=d[x][y];
ba=bz;
if(n1==n2 && ba!=b)
{
xiao();
}
}
}
public void xiao() //这里是整个游戏最重要的部分,就是判断两个按钮在信息
{ //相同的情况下能不能消去。仔细分析,不一条条注释
int i=0, j=0,n=0,k=0;
if((x0==x &&(y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)&&(y0==y))) //相邻的情况
{
basetVisible(false);
bsetVisible(false);
guli();
s="no";
d[x0][y0]=0;
d[x][y]=0;
}
else
{
for (j=0;j<7;j++ ) //两个按钮按行分析,看能否消去
{
if (d[x0][j]==0)
{
if (y>j)
{
for (i=y-1;i>=j;i-- )
{
if (d[x][i]!=0)
{
k=0;
break;
}
else
{
k=1;
}
}
if (k==1)
{
if (y0>j)
{
for (i=y0-1;i>=j ;i-- )
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (y0<j)
{
for (i=y0+1;i<=j ;i++)
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (y<j)
{
for (i=y+1;i<=j ;i++ )
{
if (d[x][i]!=0)
{
k=0;
break;
}
else
{
k=1;
}
}
if (k==1)
{
if (y0>j)
{
for (i=y0-1;i>=j ;i-- )
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (y0<j)
{
for (i=y0+1;i<=j ;i++)
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (y==j )
{
if (y0>j)
{
for (i=y0-1;i>=j ;i-- )
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (y0<j)
{
for (i=y0+1;i<=j ;i++)
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (k==2)
{ if (x0==x)
{
bsetVisible(false);
basetVisible(false);
guli();
s="no";
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
if (x0<x)
{
for (n=x0;n<=x-1;n++ )
{
if (d[n][j]!=0)
{
k=0;
break;
}
if(d[n][j]==0 && n==x-1)
{
bsetVisible(false);
basetVisible(false);
guli();
s="no";
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
}
}
if (x0>x)
{
for (n=x0;n>=x+1 ;n-- )
{
if (d[n][j]!=0)
{
k=0;
break;
}
if(d[n][j]==0 && n==x+1)
{
bsetVisible(false);
basetVisible(false);
guli();
s="no";
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
}
}
}
}
for (i=0;i<8;i++ ) //按列分析,看能不能消去
{
if (d[i][y0]==0)
{
if (x>i)
{
for (j=x-1;j>=i ;j-- )
{
if (d[j][y]!=0)
{
k=0;
break;
}
else
{
k=1;
}
}
if (k==1)
{
if (x0>i)
{
for (j=x0-1;j>=i ;j-- )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (x0<i)
{
for (j=x0+1;j<=i;j++ )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (x<i)
{
for (j=x+1;j<=i;j++ )
{
if (d[j][y]!=0)
{
k=0;
break;
}
else
{
k=1;
}
}
if (k==1)
{
if (x0>i)
{
for (j=x0-1;j>=i ;j-- )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (x0<i)
{
for (j=x0+1;j<=i ;j++ )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (x==i)
{
if (x0>i)
{
for (j=x0-1;j>=i ;j-- )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (x0<i)
{
for (j=x0+1;j<=i ;j++ )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (k==2)
{
if (y0==y)
{
bsetVisible(false);
basetVisible(false);
guli();
s="no";
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
if (y0<y)
{
for (n=y0;n<=y-1 ;n++ )
{
if (d[i][n]!=0)
{
k=0;
break;
}
if(d[i][n]==0 && n==y-1)
{
bsetVisible(false);
basetVisible(false);
guli();
s="no";
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
}
}
if (y0>y)
{
for (n=y0;n>=y+1 ;n--)
{
if (d[i][n]!=0)
{
k=0;
break;
}
if(d[i][n]==0 && n==y+1)
{
bsetVisible(false);
basetVisible(false);
guli();
s="no";
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
}
}
}
}
}
}
}太简单了,我都写在自己手机上了。
都不看书的,全部自己思考。
他这代码也太烂了。
// 蛇是用一个二维的int数组表示
//snake[i][0] 表示横轴 snake[i][1]表示纵轴
public int[][] snake = new int[91][2];
public int SnakeNum; //蛇长度
public final int SnakeSize; //大概是蛇的最长值
public int foodX; //食物横轴
public int foodY; //食物纵轴
public Random random = new Random(); //随机丢食物
private void init()
{
SnakeNum = 5; //初始蛇长
exp = 0;
for (int i = 0; i < SnakeNum; i++)
{
snake[i][0] = SnakeSize 3;
snake[i][1] = SnakeSize 3;
}
DIR = DIR_DOWN;
foodX = SnakeSize 5; 初始食物
foodY = SnakeSize 5;
}
private void move(int dir)
{
//把头以外的部分向前一格 去掉了尾那一格
for (int i = SnakeNum; i > 0; i--)
{
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}
//根据 dir 方向做蛇头
switch (dir)
{
case DIR_UP:
snake[0][1] = snake[0][1] - SnakeSize;
break;
case DIR_DOWN:
snake[0][1] = snake[0][1] + SnakeSize;
break;
case DIR_LEFT:
snake[0][0] = snake[0][0] - SnakeSize;
break;
case DIR_RIGHT:
snake[0][0] = snake[0][0] + SnakeSize;
break;
}
}
private void eatFood()
{
//如果蛇头与食物相等 所以吃掉
if (snake[0][0] == foodX && snake[0][1] == foodY)
{
SnakeNum++; //蛇长加一格
exp++; //加分 还是什么
one++; //加分 还是什么
levelup(); //加速度吧
filename = 1; //文件名 ??
isPlaySound = true; //声音吧
PlaySound(filename); //拨声音
makeFood(); // 抛食物
}
}
private void makeFood()
{
while (true)
{
//食物是随机的。但是不能在蛇身上和屏幕外
//并且坐标要为 蛇每一格长度的整数倍
foodX = Mathabs(randomnextInt() % (SCREEN_WIDTH - SnakeSize + 1)) / SnakeSize SnakeSize;
foodY = Mathabs(randomnextInt() % (SCREEN_HEIGHT - B_BLACK - SnakeSize + 1)) / SnakeSize SnakeSize;
boolean b = true;
//不能在蛇身上
for (int i = 0; i < SnakeNum; i++)
{
if (foodX == snake[i][0] && snake[i][1] == foodY)
{
b = false;
break;
}
}
if (b)
{
break;
}
}
}
//判断游戏结束
private boolean isGameOver()
{
// 如果蛇头走出了屏幕 结束
if (snake[0][0] < 0 || snake[0][0] > (SCREEN_WIDTH - SnakeSize) ||
snake[0][1] < 0 || snake[0][1] > (SCREEN_HEIGHT - B_BLACK - SnakeSize))
{
return true;
}
for (int i = 4; i < SnakeNum; i++)
{
if (snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1])
{
return true;
}
}
return false;
} //后面的写不下了 QQ 312627459package snake;
import javaxswingUIManager;
import javaawt;
/
<p>Title: 贪食蛇游戏</p>
<p>Description: </p>
<p>Copyright: Copyright (c) 2005</p>
<p>Company: </p>
@author zsb
@version 10
/
public class Snake {
private boolean packFrame = false;
//Construct the application
public Snake() {
MainFrame frame = new MainFrame();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, eg from their layout
if (packFrame) {
framepack();
}
else {
framevalidate();
}
//Center the window
Dimension screenSize = ToolkitgetDefaultToolkit()getScreenSize();
Dimension frameSize = framegetSize();
if (frameSizeheight > screenSizeheight) {
frameSizeheight = screenSizeheight;
}
if (frameSizewidth > screenSizewidth) {
frameSizewidth = screenSizewidth;
}
framesetLocation((screenSizewidth - frameSizewidth) / 2, (screenSizeheight - frameSizeheight) / 2);
framesetVisible(true);
}
//Main method
public static void main(String[] args) {
/ try {
UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName());
}
catch(Exception e) {
eprintStackTrace();
}/
new Snake();
}
}
package snake;
import javaawt;
import javaxswing;
/
<p>Title: 贪食蛇游戏</p>
<p>Description: </p>
<p>Copyright: Copyright (c) 2005</p>
<p>Company: </p>
@author zsb
@version 10
/
public class Help_Dialog extends JDialog {
JPanel panel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JTextArea jTextArea1 = new JTextArea();
private String help = "游戏说明:\n1 :方向键控制蛇移动的方向"+
"\n2 :按开始键开始游戏"+
"\n3 :按暂停键可以暂停游戏,再按暂停键能继续玩游戏"+
"\n4 :**为普通食物,吃一个得100分"+
"\n5 :青色为穿身宝物,吃一个得100分,该宝物允许玩家穿过一次蛇身"+
"\n6 :红色为穿墙宝物,吃一个得200分,该宝物允许玩家穿过一次墙壁"+
"\n7 :当分数到达一定时,会自动升级当到达最高级别时就没有升级了";
GridBagLayout gridBagLayout1 = new GridBagLayout();
public Help_Dialog(Frame frame, String title, boolean modal) {
super(frame, title, modal);
try {
jbInit();
pack();
}
catch(Exception ex) {
exprintStackTrace();
}
}
public Help_Dialog() {
this(null, "", false);
}
private void jbInit() throws Exception {
panel1setLayout(borderLayout1);
jTextArea1setBackground(SystemColorcontrol);
jTextArea1setEditable(false);
jTextArea1setText("游戏说明:\n1 :方向键控制蛇移动的方向\n2 :按开始键开始游戏\n3 :按暂停键可以暂停游戏,再按暂停键能继续玩游戏\n4 :**为普通食物,吃一个得100分\n" +
"5 :青色为穿身宝物,吃一个得100分,该宝物允许玩家穿过一次蛇身\n6 :红色为穿墙宝物,吃一个得100分,该宝物允许玩家穿过一次墙壁\n" +
"7 :当分数到达一定时,会自动升级当到达最高级别时就没有升级了");
thissetResizable(false);
thissetTitle("帮助");
thisgetContentPane()setLayout(gridBagLayout1);
getContentPane()add(panel1, new GridBagConstraints(0, 0, 1, 1, 10, 10
,GridBagConstraintsCENTER, GridBagConstraintsBOTH, new Insets(0, 0, 0, 0), 45, 83));
panel1add(jTextArea1, BorderLayoutCENTER);
}
}
package snake;
import javaawt;
import javaawtevent;
import javaxswing;
import comborlandjbcllayout;
/
<p>Title: 贪食蛇游戏</p>
<p>Description: </p>
<p>Copyright: Copyright (c) 2005</p>
<p>Company: </p>
@author zsb
@version 10
/
public class MainFrame extends JFrame {
private JPanel contentPane;//窗体内容网格
private JToolBar jToolBar1 = new JToolBar();//工具栏
private JButton jButton1 = new JButton();//游戏开始按钮
private JButton jButton2 = new JButton();//游戏暂停按钮
private JButton jButton3 = new JButton();//游戏退出按钮
private JButton jButton4 = new JButton();//帮助按钮
private JPanel jPanel1 = new JPanel();//游戏主体面板容器
private JPanel jPanel_PlayArea = new JPanel();//游戏区面板容器
private XYLayout xYLayout1 = new XYLayout();//容器布局管理器
private XYLayout xYLayout2 = new XYLayout();
private XYLayout xYLayout3 = new XYLayout();
private static final int UP = 1, LEFT = 2, DOWN = 3, RIGHT = 4;//贪食蛇运动方向
private static final int BEGINNER = 1, MIDDLE = 2, EXPERT = 3;//游戏级别常量
private static final int ROWS = 30;//游戏区行数
private static final int COLS = 50;//游戏区列数
private boolean isPause = false;//游戏暂停标志
private boolean isEnd;//游戏结束标志
private SnakeBody snake ;//贪食蛇
private int score = 0;//当前得分
private int level = BEGINNER;//当前游戏级别
SnakeThread thread = new SnakeThread();//游戏主线程
private GridLayout gridLayout1 = new GridLayout(ROWS, COLS, 0, 0);//游戏区布局
private JButton[][] playBlocks;//游戏区的所有方块
JPanel jPanel2 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
ButtonGroup buttonGroup1 = new ButtonGroup();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
JRadioButton jRadioButton3 = new JRadioButton();
//Construct the frame
public MainFrame() {
enableEvents(AWTEventWINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
eprintStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(ToolkitgetDefaultToolkit()createImage(MainFrameclassgetResource("[Your Icon]")));
contentPane = (JPanel)thisgetContentPane();
contentPanesetLayout(xYLayout2);
thissetResizable(false);
thissetSize(new Dimension(512, 414));
thissetTitle("贪食蛇");
thisaddKeyListener(new MainFrame_this_keyAdapter(this));
jButton1setFont(new javaawtFont("DialogInput", 0, 12));
jButton1setMaximumSize(new Dimension(79, 39));
jButton1setMinimumSize(new Dimension(79, 39));
jButton1setPreferredSize(new Dimension(79, 39));
jButton1setFocusPainted(false);
jButton1setText("开始");
jButton1addActionListener(new MainFrame_jButton1_actionAdapter(this));
jButton1addKeyListener(new MainFrame_this_keyAdapter(this));
jButton2setText("暂停");
jButton2addActionListener(new MainFrame_jButton2_actionAdapter(this));
jButton2setPreferredSize(new Dimension(79, 39));
jButton2setFocusPainted(false);
jButton2setMinimumSize(new Dimension(79, 39));
jButton2setMaximumSize(new Dimension(79, 39));
jButton2setFont(new javaawtFont("DialogInput", 0, 12));
jButton2addKeyListener(new MainFrame_this_keyAdapter(this));
jButton3setText("退出");
jButton3addActionListener(new MainFrame_jButton3_actionAdapter(this));
jButton3setPreferredSize(new Dimension(79, 39));
jButton3setFocusPainted(false);
jButton3setMinimumSize(new Dimension(79, 39));
jButton3setMaximumSize(new Dimension(79, 39));
jButton3setFont(new javaawtFont("DialogInput", 0, 12));
jButton3addKeyListener(new MainFrame_this_keyAdapter(this));
jButton4setText("帮助");
jButton4addActionListener(new MainFrame_jButton4_actionAdapter(this));
jButton4setPreferredSize(new Dimension(79, 39));
jButton4setFocusPainted(false);
jButton4setMinimumSize(new Dimension(79, 39));
jButton4setMaximumSize(new Dimension(79, 39));
jButton4setFont(new javaawtFont("DialogInput", 0, 12));
jButton4addKeyListener(new MainFrame_this_keyAdapter(this));
jPanel1setLayout(xYLayout1);
jPanel1addKeyListener(new MainFrame_this_keyAdapter(this));
jPanel_PlayAreasetLayout(gridLayout1);
jPanel_PlayAreasetBorder(BorderFactorycreateEtchedBorder());
jPanel_PlayAreaaddKeyListener(new MainFrame_this_keyAdapter(this));
jLabel1setFont(new javaawtFont("DialogInput", 0, 12));
jLabel1setText("得分:");
jPanel2setLayout(xYLayout3);
jLabel2setFont(new javaawtFont("DialogInput", 0, 12));
jLabel2setToolTipText("");
jLabel2setText("0");
jLabel3setText("穿身:");
jLabel3setFont(new javaawtFont("DialogInput", 0, 12));
jLabel4setFont(new javaawtFont("DialogInput", 0, 12));
jLabel4setText("0");
jLabel5setText("穿墙:");
jLabel5setFont(new javaawtFont("DialogInput", 0, 12));
jLabel5setToolTipText("");
jLabel6setText("0");
jLabel6setFont(new javaawtFont("DialogInput", 0, 12));
jLabel6setToolTipText("");
jRadioButton1setFont(new javaawtFont("DialogInput", 0, 12));
jRadioButton1setText("初级");
jRadioButton1addActionListener(new MainFrame_jRadioButton1_actionAdapter(this));
jRadioButton1addKeyListener(new MainFrame_this_keyAdapter(this));
jRadioButton2setText("中级");
jRadioButton2addActionListener(new MainFrame_jRadioButton2_actionAdapter(this));
jRadioButton2addKeyListener(new MainFrame_this_keyAdapter(this));
jRadioButton2setFont(new javaawtFont("DialogInput", 0, 12));
jRadioButton3setText("高级");
jRadioButton3addActionListener(new MainFrame_jRadioButton3_actionAdapter(this));
jRadioButton3setFont(new javaawtFont("DialogInput", 0, 12));
jRadioButton3addKeyListener(new MainFrame_this_keyAdapter(this));
buttonGroup1add(jRadioButton1);
buttonGroup1add(jRadioButton2);
buttonGroup1add(jRadioButton3);
jRadioButton1setSelected(true);
jLabel7setFont(new javaawtFont("DialogInput", 0, 12));
jLabel7setText("等级:");
contentPaneadd(jToolBar1, new XYConstraints(0, 0, 512, -1));
jToolBar1add(jButton1, null);
jToolBar1add(jButton2, null);
jToolBar1add(jButton3, null);
jToolBar1add(jButton4, null);
jToolBar1addKeyListener(new MainFrame_this_keyAdapter(this));
contentPaneadd(jPanel1, new XYConstraints(0, 43, 512, 371));
jPanel1add(jPanel_PlayArea, new XYConstraints(0, 33, 524, 314));
jPanel1add(jPanel2, new XYConstraints(0, 0, 498, 30));
jPanel2add(jRadioButton1, new XYConstraints(331, 1, -1, -1));
jPanel2add(jRadioButton3, new XYConstraints(443, 1, -1, -1));
jPanel2add(jRadioButton2, new XYConstraints(390, 1, -1, -1));
jPanel2add(jLabel6, new XYConstraints(245, 6, 30, -1));
jPanel2add(jLabel7, new XYConstraints(287, 5, 47, -1));
jPanel2add(jLabel5, new XYConstraints(197, 6, 47, -1));
jPanel2add(jLabel4, new XYConstraints(166, 7, 26, -1));
jPanel2add(jLabel3, new XYConstraints(120, 6, 38, -1));
jPanel2add(jLabel1, new XYConstraints(23, 6, 39, -1));
jPanel2add(jLabel2, new XYConstraints(62, 6, 54, -1));
//创建并初始化游戏区方块
playBlocks = new JButton[ROWS][COLS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
playBlocks[i][j] = new JButton();
playBlocks[i][j]setBackground(ColorlightGray);
playBlocks[i][j]setVisible(false);
jPanel_PlayAreaadd(playBlocks[i][j]);
}
}
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
superprocessWindowEvent(e);
if (egetID() == WindowEventWINDOW_CLOSING) {
Systemexit(0);
}
}
public void start() {
//初始化游戏参数
score = 0;
isPause = false;
isEnd = false; //
jButton2setText("暂停");
//初始化游戏区
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
playBlocks[i][j]setBackground(ColorlightGray);
playBlocks[i][j]setVisible(false);
}
}
//创建蛇身
snake = new SnakeBody();
//在游戏区内随机放置食物
int x = (int) (Mathrandom() ROWS);
int y = (int) (Mathrandom() COLS);
while (playBlocks[x][y]isVisible()) {
x = (int) (Mathrandom() ROWS);
y = (int) (Mathrandom() COLS);
}
playBlocks[x][y]setBackground(Coloryellow);
playBlocks[x][y]setVisible(true);
//初始化状态提示区
jLabel2setText(IntegertoString(score));
jLabel4setText(IntegertoString(snakethroughbody));
jLabel2setText(IntegertoString(snakethroughwall));
try {
//启动游戏
threadstart();
}
catch (IllegalThreadStateException illegalThreadStateException) {}
}
//完整的>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)