📄 snake.java~2~
字号:
package snake;
import java.awt.*;
import java.applet.*;
public class Snake extends Applet implements Runnable
{
Image dot[]=new Image[400];
Image back;
Image offI;
Graphics offG;
int x[]=new int[ 400];
int y[]=new int[400];
int rtemp=1;
int game=1;
int coutn=0;
int score=0;
int add=1;
int level,z,n;
Button level1=new Button("初级");
Button level2=new Button("中级");
Button level3=new Button("高级");
Button level4=new Button("专家级");
String stemp;
String s,t;
boolean go[]=new boolean[400];
boolean left=false;
boolean right=false ;
boolean up=false;
boolean down=false ;
boolean started=false;
Thread setTime;
public void init()
{
//添加按钮组件
add(level1);
add(level2);
add(level3);
add(level4);
//设置背景颜色
setBackground(Color.black);
back=getImage(getCodeBase(),"screan.gif");
for(z=0;z<400;z++)
{
dot[z]=getImage(getCodeBase(),"screan.gif");
}
}
public void update(Graphics g)
{
Dimension d=this.size();
if(offI==null)
{
offI=createImage(d.width,d.height);
offG=offI.getGraphics();
}
offG.clearRect(0,0,d.width,d.height);
paint(offG);
g.drawImage(offI,0,0,null);
}
public void paint(Graphics g)
{
g.drawImage(back,0,0,this);
g.setColor(Color.white);
if(started)
{
g.setFont(new Font("TimesRoman",1,12));
t="分数"+score+"";
g.drawString(t,75,220);
}
if(game==1)
{
g.setFont(new Font("TimesRoman",1,13));
s="游戏模式选择";
g.drawString(s,65,30);
//移动按钮的位置
level1.move(75,50);
level2.move(75,90);
level3.move(75,130);
level4.move(75,170);
}
if((game==2)||(game==3))
{
if(!started)
{
g.setFont(new Font ("TimeRoman",1,13));
t="用方向键移动!";
g.drawString(t,5,215);
}
for(z=0;z<=n;z++)
{
g.drawImage(dot[z],x[z],y[z],this);
}
}
if(game==3)
{
g.setFont(new Font("TimesRoman",1,13));
s="游戏结束!";
g.drawString(s,65,60);
}
}
public void run()
{
for(z=4;z<400;z++)
{
go[z]=false;
}
for(z=0;z<4;z++)
{
go[z]=true;
x[z]=91;
y[z]=91;
}
n=3;
game=2;
score=0;
level1.move(70,-100);
level2.move(70,-100);
level3.move(70,-100);
level4.move(70,-100);
left=false;
right=false;
up=false;
down=false;
locateRandom(4);
while(true)
{
if(game==2)
{
if((x[0]==x[n])&&(y[0]==y[n]))
{
go[n]=true;
locateRandom((n+1));
score+=add;
}
for(z=399;z>0;z--)
{
if(go[z])
{
x[z]=x[(z-1)];
y[z]=y[(z-1)];
if((z>4)&&(x[0]==x[z])&&(y[0]==y[z]))
{
game=3;
}
}
}
if(left) x[0]-=10;
if(right) x[0]+=10;
if(up) y[0]-=10;
if(down) y[0]+=10;
}
}
if(y[0]>191) { y[0]=191; game=3;}
if(y[0]<1) { y[0]=1; game=3;}
if(x[0]>191){ x[0]=191; game=3;}
if(x[0]<1){ x[0]=1;game=3;}
if(game==3)
{
if(count<(1500/level))
{
count++;
}
else
{
count=0;
game=1;
repaint();
setTime.stop();
}
}
repaint();
try {
setTime.sleep(level);
} catch (InterruptedException ex) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -