📄 block.java
字号:
import java.awt.*;
public class Block
{
final public int D_NONE=0;
final public int D_UP=1;
final public int D_DOWN=2;
final public int D_LEFT=3;
final public int D_RIGHT=4;
final public int M_CLOCKW=1;
final public int M_ACLOCKW=2;
final public int M_OTHER=3;
final public int M_NONE=4;
final public int IMGSIZE=20;
public int xpos;
public int ypos;
public boolean animates;
public int animArray[];
public int currentImg;
public boolean moves;
public int ID;
public int xdir;
public int ydir;
public int currentDir;
public int moveType;
public boolean pickupAble;
public boolean moveAble;
public boolean blowupAble;
public boolean killer;
public boolean needsToBeDrawn;
private Graphics gr;
private Image im;
// Note, graphics page and image source is passed in g,i
// for each block, so we always know where to draw and from what
public Block(Graphics g, Image i)
{
this.xpos=0;
this.ypos=0;
this.animates=false;
this.animArray=new int[10];
this.animArray[0]=0;
this.currentImg=0;
this.moves=false;
this.ID=0;
this.xdir=0;
this.ydir=0;
this.currentDir=D_LEFT;
// Note movetype=0 means it is never drawn generally
this.moveType=0;
this.pickupAble=false;
this.moveAble=false;
this.blowupAble=false;
this.killer=false;
this.needsToBeDrawn=true;
this.gr=g;
this.im=i;
}
public void setAnimArray(int x0, int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9)
// Set up animation frames
{
this.animArray[0]=x0;
this.animArray[1]=x1;
this.animArray[2]=x2;
this.animArray[3]=x3;
this.animArray[4]=x4;
this.animArray[5]=x5;
this.animArray[6]=x6;
this.animArray[7]=x7;
this.animArray[8]=x8;
this.animArray[9]=x9;
}
public void animate()
// Animate this block depending on current direction
{
switch (currentDir)
{
case D_LEFT:
if (currentImg==0)
currentImg=1;
else
currentImg=0;
this.needsToBeDrawn=true;
break;
case D_RIGHT:
if (currentImg==2)
currentImg=3;
else
currentImg=2;
this.needsToBeDrawn=true;
break;
case D_UP:
if (currentImg==4)
currentImg=5;
else
currentImg=4;
this.needsToBeDrawn=true;
break;
case D_DOWN:
if (currentImg==6)
currentImg=7;
else
currentImg=6;
this.needsToBeDrawn=true;
break;
case D_NONE:
currentImg=0;
this.needsToBeDrawn=true;
break;
default:;
}
}
public void changeDir()
// Change direction of our ent, maybe we hit something?
// at the moment we go in a circle
{
switch (moveType)
{
case M_CLOCKW:
switch (currentDir)
{
case D_LEFT:
currentDir=D_UP;
break;
case D_RIGHT:
currentDir=D_DOWN;
break;
case D_UP:
currentDir=D_RIGHT;
break;
case D_DOWN:
currentDir=D_LEFT;
break;
case D_NONE:
break;
default:;
}
break;
case M_ACLOCKW:
switch (currentDir)
{
case D_LEFT:
currentDir=D_DOWN;
break;
case D_RIGHT:
currentDir=D_UP;
break;
case D_UP:
currentDir=D_LEFT;
break;
case D_DOWN:
currentDir=D_RIGHT;
break;
case D_NONE:
break;
default:;
}
break;
case M_OTHER:
switch (currentDir)
{
case D_LEFT:
currentDir=D_RIGHT;
break;
case D_RIGHT:
currentDir=D_LEFT;
break;
case D_UP:
currentDir=D_DOWN;
break;
case D_DOWN:
currentDir=D_UP;
break;
case D_NONE:
break;
default:;
}
break;
case M_NONE:
this.moves=false;
break;
}
}
public void jumpBlock(int w)
// Jump over a block
{
for (int q=0; q<9; q++)
{
move(w);
}
}
public void move(int w)
// Move this block depending on current direction
// w is pixel size of world to stop us going too far!
{
switch (currentDir)
{
case D_LEFT:
if (this.xpos>0)
{
xpos-=4;
this.needsToBeDrawn=true;
}
break;
case D_RIGHT:
if (this.xpos<w-20)
{
xpos+=4;
this.needsToBeDrawn=true;
}
break;
case D_UP:
if (this.ypos>0)
{
ypos-=4;
this.needsToBeDrawn=true;
}
break;
case D_DOWN:
if (this.ypos<w-20)
{
ypos+=4;
this.needsToBeDrawn=true;
}
break;
case D_NONE:
break;
default:;
}
}
public void die()
// 'Kill' this block
{
this.ID=0;
this.animates=false;
this.currentImg=0;
this.moves=false;
this.xdir=0;
this.ydir=0;
this.xpos=0;
this.ypos=0;
this.currentDir=D_NONE;
this.pickupAble=false;
this.moveAble=true;
this.blowupAble=true;
this.killer=false;
this.needsToBeDrawn=false;
}
public void draw()
// Draw this block, assuming image size is fixed at 20!
{
int xoff=animArray[currentImg]*IMGSIZE;
gr.drawImage(im, xpos,ypos,
xpos+IMGSIZE,ypos+IMGSIZE,
xoff,0,
xoff+IMGSIZE,IMGSIZE,
null);
this.needsToBeDrawn=false;
}
public void delete()
// Delete this block, assuming image size is fixed at 20!
{
gr.setColor(Color.black);
gr.fillRect(xpos,ypos,IMGSIZE,IMGSIZE);
this.needsToBeDrawn=true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -