📄 iceblox.java
字号:
// Iceblox
// By Karl H鰎nell, April 8 1996
import java.awt.*;
import java.awt.image.*;
import java.net.*;
public final class iceblox extends java.applet.Applet implements Runnable
{
int i,j,k;
final int playX=390,playY=330,mainX=390,mainY=348,smalls=48,blockX=13,blockY=11;
final int animP[]={7,8,9,8,10,11,12,11,4,5,6,5,1,2,3,2};
final int animF[]={32,33,34,35,36,35,34,33};
final int levFlame[]={2,3,4,2,3,4},levRock[]={5,6,7,8,9,10};
final int levSpeed[]={3,3,3,5,5,5},levIce[]={35,33,31,29,27,25};
final int effMax=5;
int playArea[];
int gameState,counter,dir,inFront,inFront2,level,coins;
int effLevel,lives=3;
int x[],y[],dx[],dy[],motion[],look[],creature[],ccount[],actors,flames;
int sideIX[]={0,-1,1,-15,15},coorDx[]={0,-30,30,0,0},coorDy[]={0,0,0,-30,30};
Image collection,offImage,playField,small[],title;
Graphics offGraphics,playGraphics,tempG;
MediaTracker tracker;
ImageFilter filter;
ImageProducer collectionProducer;
long snooze=100,score;
Thread game;
Math m;
public void init()
{
setBackground(Color.black);
offImage=createImage(mainX,mainY);
offGraphics=offImage.getGraphics();
offGraphics.setColor(Color.black);
offGraphics.fillRect(0,0,mainX,mainY);
playField=createImage(playX,playY);
playGraphics=playField.getGraphics();
playGraphics.setColor(Color.black);
tracker=new MediaTracker(this);
collection = getImage(getCodeBase(),"iceblox.gif");
tracker.addImage(collection,0);
try
{
tracker.waitForID(0);
}
catch(InterruptedException e) {}
collectionProducer=collection.getSource();
small=new Image[smalls];
k=0;i=0;j=0;
while (k<smalls)
{
filter=new CropImageFilter(j*30,i*30,30,30);
small[k]=createImage(new FilteredImageSource(
collectionProducer,filter));
tracker.addImage(small[k],1);
/* small[k]=createImage(30,30);
tempG=small[k].getGraphics();
tempG.drawImage(collection,-j*30,-i*30,this);*/
k++;
j++;
if (j==8)
{
j=0;
i++;
}
}
filter=new CropImageFilter(0,180,224,64);
title=createImage(new FilteredImageSource(
collectionProducer,filter));
tracker.addImage(title,1);
playArea=new int[(blockX+2)*(blockY+3)];
x=new int[20];
y=new int[20];
dx=new int[20];
dy=new int[20];
look=new int[20];
motion=new int[20];
creature=new int[20];
ccount=new int[20];
gameState=7;
try
{
tracker.waitForID(1);
}
catch(InterruptedException e) {}
resize(mainX,mainY);
}
public void run()
{
while (game !=null)
{
try
{
game.sleep(snooze);
} catch (InterruptedException e) {}
counter=(counter+1)&255;
switch (gameState)
{
case 0:
prepareField();
break;
case 1:
showField();
break;
case 2:
gameLoop();
break;
case 3:
happyPenguin();
break;
case 4:
clearField();
break;
case 5:
fixDeath();
break;
case 6:
gameOver();
break;
case 7:
drawIntro1();
break;
case 8:
waitIntro1();
break;
case 9:
drawIntro2();
break;
case 10:
waitIntro2();
break;
case 11:
drawIntro3();
break;
case 12:
waitIntro3();
break;
default:
break;
}
repaint();
}
}
public void start()
{
if (game==null)
{
game=new Thread(this);
game.start();
}
}
public void stop()
{
if ((game!=null)&&(game.isAlive()))
{
game.stop();
}
game=null;
}
public boolean keyDown(java.awt.Event e,int key)
{
if (gameState==2)
{
switch (key)
{
case 97:
dir=1; // A:Left
break;
case 100:
dir=2; // D:Right
break;
case 107:
dir=3; // K:Up
break;
case 109:
dir=4; // M:Down
break;
default:
break;
}
}
else if ((gameState>6)&&(key==32))
gameState=0;
return false;
}
public boolean keyUp(java.awt.Event e,int key)
{
dir=0;
return false;
}
public void prepareField()
{
int i,j,p,q;
if (level>effMax)
effLevel=effMax;
else
effLevel=level;
offGraphics.setColor(Color.black);
offGraphics.fillRect(0,0,mainX,mainY);
playGraphics.setColor(Color.black);
playGraphics.fillRect(0,0,playX,playY);
offGraphics.setColor(Color.lightGray);
offGraphics.fill3DRect(0,mainY-playY-4,mainX,4,true);
offGraphics.setColor(Color.white);
offGraphics.drawString("SCORE:",2,12);
updateScore(0);
offGraphics.drawString("LEVEL: "+(level+1),125,12);
offGraphics.drawString("SPARE LIVES:",220,12);
for (i=0;i<lives;i++)
offGraphics.drawImage(small[13],300+i*15,-16,this);
for (i=0;i<(blockX+2)*(blockY+3);i++)
playArea[i]=1;
for (i=1;i<=blockY;i++)
for (j=1;j<=blockX;j++)
playArea[i*(blockX+2)+j]=0;
playArea[blockX+3]=1; // Make room for start square
i=0;
while (i<levIce[effLevel]) // Ice cubes
{
p=1+(int)(m.random()*blockX);
q=1+(int)(m.random()*blockY);
if (playArea[q*(blockX+2)+p]==0)
{
playArea[q*(blockX+2)+p]=2;
playGraphics.drawImage(small[16],(p-1)*30,(q-1)*30,this);
i++;
}
}
i=0;
while (i<levRock[effLevel]) // Rock
{
p=1+(int)(m.random()*blockX);
q=1+(int)(m.random()*blockY);
if (playArea[q*(blockX+2)+p]==0)
{
playArea[q*(blockX+2)+p]=1;
playGraphics.drawImage(small[14],(p-1)*30,(q-1)*30,this);
i++;
}
}
i=0;
while (i<5) // Coins
{
p=1+(int)(m.random()*blockX);
q=1+(int)(m.random()*blockY);
if (playArea[q*(blockX+2)+p]==0)
{
playArea[q*(blockX+2)+p]=10;
playGraphics.drawImage(small[24],(p-1)*30,(q-1)*30,this);
i++;
}
}
playArea[blockX+3]=0; // Clear start square
gameState=1;
snooze=100;
counter=0;
motion[0]=0;
actors=1;
coins=0;
flames=0;
look[0]=0;
x[0]=30;
y[0]=30;
dx[0]=6;
dy[0]=6;
look[0]=2;
creature[0]=1;
for (i=0;i<20;i++)
ccount[i]=0;
}
public void showField()
{
Graphics saveGraphics;
saveGraphics=offGraphics.create();
offGraphics.clipRect(playX/2-(counter*playX/2/30),mainY-playY+
playY/2-(counter*playY/2/30),counter*playX/30,counter*playY/30);
offGraphics.drawImage(playField,0,mainY-playY,this);
if (counter==30)
{
gameState=2;
snooze=100;
}
offGraphics=saveGraphics;
}
public void gameLoop()
{
if (flames<levFlame[effLevel])
{
if (x[0]<(playX/2))
x[actors]=playX+30;
else
x[actors]=0;
y[actors]=30*(1+(int)(m.random()*blockY));
j=(y[actors]/30)*(blockX+2)+x[actors]/30;
motion[actors]=0;
dx[actors]=levSpeed[effLevel];
dy[actors]=levSpeed[effLevel];
creature[actors]=4;
if ((playArea[j+1]==0)||(playArea[j-1]==0))
{
actors++;
flames++;
}
}
for (i=0;i<actors;i++)
{
ccount[i]++;
switch(motion[i])
{
case 1:
x[i]-=dx[i];
break;
case 2:
x[i]+=dx[i];
break;
case 3:
y[i]-=dy[i];
break;
case 4:
y[i]+=dy[i];
break;
default:
break;
}
j=(y[i]/30)*(blockX+2)+x[i]/30;
switch(creature[i])
{
case 1: // Penguin
if ((x[i]%30 == 0)&&(y[i]%30 == 0))
motion[i]=0;
if (motion[i]==0)
{
inFront=playArea[j+sideIX[dir]];
if ((j+2*sideIX[dir])<0)
inFront2=1;
else
inFront2=playArea[j+2*sideIX[dir]];
if (inFront==0)
motion[i]=dir;
else
{
if ((inFront2==0)&&((inFront==2)||(inFront==10))) // Push ice block?
{
if (inFront==2)
{
creature[actors]=2;
look[actors]=16;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -