//
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class snake extends Applet
implements Runnable,KeyListener
{
private int x[];
private int y[],n,d,m;
private int board[][];
private int x0,t,s;
private int y0,score,level;
private boolean begin,end,stop;
private int x1,y1;
Button b1;
Label l1,l2,l3,l4;
Font f1,f2;
Thread a;
Color color[];
AudioClip a1,a2;
public void init()
{
a1=getAudioClip(getDocumentBase(),"1.au");
a2=getAudioClip(getDocumentBase(),"2.au");
f1=new Font("Helvetica",1,16);
f2=new Font("Helvetica",1,11);
color=new Color[10];
board=new int[40][27];
color[0]=new Color(0,0,0);
color[2]=new Color(180,0,0);
color[1]=new Color(160,80,240);
color[3]=new Color(80,200,160);
color[4]=new Color(240,80,160);
color[5]=new Color(0,180,0);
color[6]=new Color(180,0,180);
color[9]=new Color(180,180,0);
color[8]=new Color(140,140,140);
color[7]=new Color(200,200,200);
setLayout(null);
b1=new Button("Restart");
add(b1);
b1.setFont(f1);
b1.setBackground(color[6]);
b1.setForeground(color[5]);
b1.setBounds(320,410,100,30);
l1=new Label();
add(l1);
l1.setText("xiangdong wen");
l1.setFont(f2);
l1.setBackground(color[7]);
l1.setForeground(color[5]);
l1.setBounds(450,410,100,15);
l2=new Label();
l3=new Label();
l4=new Label();
add(l2);
l2.setText("wen@math.temple.edu");
l2.setFont(f2);
l2.setBackground(color[7]);
l2.setForeground(color[5]);
l2.setBounds(450,425,140,15);
add(l3);
l3.setText("LEVEL:"+Integer.toString(level));
l3.setFont(f1);
l3.setBackground(color[6]);
l3.setForeground(color[5]);
l3.setBounds(170,410,120,30);
add(l4);
l4.setText("SCORE:"+Integer.toString(score));
l4.setBackground(color[6]);
l4.setForeground(color[5]);
l4.setFont(f1);
l4.setBounds(20,410,120,30);
a=null;
randnumber();
begin();
addKeyListener(this);
}
public void randnumber()
{ x1=(int)(Math.random()*38D)+1;
y1=(int)(Math.random()*25D)+1;
if(x1==39)x1=1;
if(y1==27)y1=1;
}
public boolean action(Event event,Object obj)
{
if(event.target==b1)
{
begin();
}
return true;
}
private void begin()
{
begin=false;
end=false;
stop=false;
score=0;
level=1;
m=100;
n=1;
t=250;
d=3;
s=45;
y=new int[m];
x=new int[m];
x[0]=2;y[0]=14;
if(a==null){a=new Thread(this);
a.start();}
repaint();
requestFocus();
}
public void keyTyped(KeyEvent keyevent) {
}
public void keyReleased(KeyEvent keyevent) {
}
public void run()
{
while(!end)
{
if(!stop)Go();
try{Thread.sleep(t);}
catch(InterruptedException _EX){}
}
}
public void snake()
{}
public void update(Graphics g)
{paint(g);
}
public void paint(Graphics g)
{
int i,j;
if(!begin)
{g.setColor(color[0]);
g.fillRect(15,15,570,375);
g.setColor(color[7]);
g.fillRect(0,405,600,450);
g.setColor(color[1]);
for( j=0;j<27;j++)
{
g.fill3DRect(0*15,j*15,15,15,true);
g.fill3DRect(39*15,j*15,15,15,true);
}
for( j=0;j<40;j++)
{
g.fill3DRect(j*15,0*15,15,15,true);
g.fill3DRect(j*15,26*15,15,15,true);
}
begin=true;
}
g.setColor(color[0]);
g.fill3DRect((x[n])*15,(y[n])*15,15,15,true);
g.setColor(color[3]);
for( i=0;i0;i--)
{
x[i]=x[i-1];y[i]=y[i-1];
}
x[0]+=x0;y[0]+=y0;
if(x[0]==x1&&y[0]==y1)
{
a1.play();
n=n+1;
score+=100;
l4.setText("SCORE:"+Integer.toString(score));
level=(int)(score/300)+1;
l3.setText("LEVEL:"+Integer.toString(level));
t=250-level*20;
randnumber();
}
if(x[0]==0||x[0]==39||y[0]==0||y[0]==26)
{ stop1();
}
for(int i=1;i