📄 plcanvas.java
字号:
/*若不可以下降 则判断是否有可以消除的方块
*
*
*/
if(fallCount%3==0){
int temp=0;
for(int i=0;i<2;i++){
//fallCellY[i]+=1;
//System.out.println(fallCellY[i]<(BGH-1));
//System.out.println(cellOccupied[(fallCellY[i]+1)*BGW+fallCellX[i]]);
if(fallCellY[i]<(BGH-1)&&cellOccupied[(fallCellY[i]+1)*BGW+fallCellX[i]]==0){
fallFlag[i]=0;
//temp+=0;
}else{
fallFlag[i]=1;
//temp+=1;
}
}
}
//changeIndex%2==0
if(changeIndex%2==0){
if(fallFlag[0]+fallFlag[1]!=0){
for(int i=0;i<2;i++){
cellOccupied[fallCellY[i]*BGW+fallCellX[i]]=1;
cellColor[fallCellY[i]*BGW+fallCellX[i]]=fallColor[i]+1;
}
//isPlay=false;
//reset
fallCellX=new int[]{3,3};
fallCellY=new int[]{0,1};
fallStartX=new int[]{3,3};
fallStartY=new int[]{0,1};
fallFlag=new int[]{0,0};
fallCount=0;
System.arraycopy(nextColor,0,fallColor,0,nextColor.length);
haveNext=false;
changeIndex=0;
//checkSame();
checkable=true;
moveRL=false;
}else{
fallCount++;
if(fallCount%3==1){
for(int i=0;i<2;i++){
fallCellY[i]+=1;
}
}
}
}else{
//System.out.println(fallFlag[0]+" "+fallFlag[1]);
//if(fallFlag[0]+fallFlag[1])
//if(fallFlag[0])
fallCount++;
for(int i=0;i<2;i++){
if(fallCount%3==1){
if(fallFlag[i]==0){
fallCellY[i]+=1;
}else{
cellOccupied[fallCellY[i]*BGW+fallCellX[i]]=1;
cellColor[fallCellY[i]*BGW+fallCellX[i]]=fallColor[i]+1;
}
}
}
if(fallFlag[0]*fallFlag[1]==1){
//reset
fallCellX=new int[]{3,3};
fallCellY=new int[]{0,1};
fallStartX=new int[]{3,3};
fallStartY=new int[]{0,1};
fallFlag=new int[]{0,0};
fallCount=0;
System.arraycopy(nextColor,0,fallColor,0,nextColor.length);
haveNext=false;
changeIndex=0;
//checkSame();
checkable=true;
moveRL=false;
}
}
}
private void checkSame(){
checkArr=new int[BGH*BGW];
clearable=false;
downable=true;
int snum=0;
for(int j=0;j<BGH;j++){
for(int i=0;i<BGW;i++){
//最多同时消掉11个相同方格 同时保存x,y信息
holdSame=new int[24];
sameNum=0;
if(cellColor[j*BGW+i]>0){
check(i,j);
if(sameNum>3){
updateScore(sameNum,snum);
snum++;
//System.out.println("have a line");
for(int k=0;k<sameNum;k++){
//System.out.println(holdSame[k*2+1]+" "+holdSame[k*2]);
cellDown[holdSame[k*2+1]*BGW+holdSame[k*2]]=1;
//fallDown=false;
}
clearable=true;
downable=false;
}
//System.out.println("****************");
}
}
}
//System.out.println("&&&&&&&&&&&&&&&&&&&&&&");
}
private void check(int x,int y){
holdSame[sameNum*2]=x;
holdSame[sameNum*2+1]=y;
sameNum+=1;
//StringBuffer str=new StringBuffer();
int dummy=cellColor[y*BGW+x];
checkArr[y*BGW+x]=1;
//str.append(x+" "+y);
for(int i=0;i<4;i++){
int tempx=x+JUDGEX[i];
int tempy=y+JUDGEY[i];
if(tempx>=0&&tempx<BGW&&tempy>=0&&tempy<BGH&&
dummy==cellColor[tempy*BGW+tempx]&&checkArr[tempy*BGW+tempx]==0){
check(tempx,tempy);
}
}
//System.out.println(str);
}
protected void moveRight(){
if(!moveRL)return;
int temp=0;
for(int i=0;i<2;i++){
if(fallFlag[i]==0){
if(fallCellX[i]<(BGW-1)&&cellOccupied[(fallCellY[i]+0)*BGW+(fallCellX[i]+1)]==0){
temp+=0;
}else{
temp+=1;
}
}else{
temp+=0;
}
}
if(temp==0){
moveRight=true;
}
}
protected void moveLeft(){
if(!moveRL)return;
int temp=0;
for(int i=0;i<2;i++){
if(fallFlag[i]==0){
if(fallCellX[i]>0&&cellOccupied[(fallCellY[i]+0)*BGW+(fallCellX[i]-1)]==0){
temp+=0;
}else{
temp+=1;
}
}else{
temp+=0;
}
}
if(temp==0){
moveLeft=true;
}
}
protected void change(){
if(fallFlag[0]+fallFlag[1]!=0)return;
int cellx=fallCellX[1];
int celly=fallCellY[1];
int judgex=JUDGEX[changeIndex];
int judgey=JUDGEY[changeIndex];
if((cellx+judgex)>=0
&&(cellx+judgex)<BGW
&&(celly+judgey)>=0
&&(celly+judgey)<BGH
&&cellOccupied[(celly+0+judgey)*BGW+(cellx+judgex)]==0){
changeable=true;
}
}
/*
public int getKeyStates()
{
int states = this.keyStates;
//this.keyStates &= ~this.pressedKeys;
this.keyStates = this.pressedKeys;
this.pressedKeys = 0;
return states;
}
*/
public int getPressKey(){
int states=pressKeyStates;
pressKeyStates=pressedKeys;
pressedKeys=0;
return states;
}
public int getReleaseKey(){
int states=releaseKeyStates;
releaseKeyStates=releasedKeys;
releasedKeys=0;
return states;
}
protected void keyPressed(int keyCode) {
//右软键
if(keyCode==-7&&!paused&&!overed){
gamePause();
}else if(keyCode==-7&&paused&&!overed){
resume();
}else if(keyCode==-7&&overed){
restart();
}
if(keyCode==-6){
exit();
this.midlet.showMenu();
}
if(paused)return;
int gameAction = getGameAction(keyCode);
//System.out.println(keyCode+" "+gameAction);
this.pressedKeys = gameAction;
}
protected void keyReleased(int keyCode) {
if(paused)return;
int gameAction = getGameAction(keyCode);
this.releasedKeys=gameAction;
}
protected void hideNotify(){
highScore.updateScores(scoreNum);
}
protected void input(){
int keyPress=getPressKey();
int keyRelease=getReleaseKey();
if(keyPress==Canvas.RIGHT){
moveRight();
}else if(keyPress==Canvas.LEFT){
moveLeft();
}else if(keyPress==Canvas.UP){
change();
}else if(keyPress==Canvas.DOWN){
delay=30;
}
if(keyRelease==Canvas.DOWN){
delay=delayValue;
}
}
/*protected void keyPressed(int keyCode){
int key=getGameAction(keyCode);
switch(key){
case Canvas.RIGHT:
moveRight();
break;
case Canvas.LEFT:
moveLeft();
break;
case Canvas.UP:
change();
}
}*/
protected void drawScreen(){
drawBg();
if(first){
drawNext();
System.arraycopy(nextColor,0,fallColor,0,nextColor.length);
first=false;
//isPlay=false;
}else{
if(clearable){
//System.out.println("clear");
drawClearCell();
}else if(downable){
gameOver();
levelUp();
if(checkable){
checkable=false;
checkSame();
}
if(downable)drawFallCell();
}else if(fallDown){
cellFallDown();
}
}
}
protected void paint(Graphics g) {
g.drawImage(buffer,0,0,Graphics.TOP|Graphics.LEFT);
}
public int[] getImageSequenceInfo(Image source,int frameWidth,int frameHeight){
//不能切成大小一样的图片序列
if((source.getWidth()%frameWidth)!=0||(source.getHeight()%frameHeight)!=0){
throw new IllegalArgumentException();
}
int framesW=source.getWidth()/frameWidth;
int framesH=source.getHeight()/frameHeight;
int[] frames=new int[framesW*framesH*2];
int framesCount=0;
for(int j=0;j<framesH;j++){
for(int i=0;i<framesW;i++){
//info of x
frames[framesCount++]=i*frameWidth;
//info of y
frames[framesCount++]=j*frameHeight;
}
}
return frames;
}
private int getRandom(int min,int max){
if(max<=min){
System.out.println("ERROR: max must more than min");
throw new IllegalArgumentException();
}
int temp=Math.abs(RANDOMIZER.nextInt());
return temp%(max-min+1)+min;
}
private void gamePause(){
paused=true;
isPlay=false;
ImageTool.drawFrame(bufferGraphics,prImage,145,192,28,15,prInfo,1);
ImageTool.drawFrame(bufferGraphics,popImage,24,60,POPW,POPH,popInfo,1);
repaint();
}
private void gameOver(){
//fallStartX=new int[]{3,3};
//fallStartY=new int[]{0,1};
boolean over=false;
for(int i=0;i<2;i++){
if(cellColor[fallStartY[i]*BGW+fallStartX[i]]>0){
over=true;
break;
}
}
if(over){
overed=true;
isPlay=false;
ImageTool.drawFrame(bufferGraphics,prImage,145,192,28,15,prInfo,1);
ImageTool.drawFrame(bufferGraphics,popImage,24,50,POPW,POPH,popInfo,0);
//
//highScore.updateScores(scoreNum);
}
}
private void restart(){
init();
isPlay=true;
t=null;
t=new Thread(this);
t.start();
}
private void levelUp(){
if(stageNum>oldStageNum){
oldStageNum=stageNum;
paused=true;
isPlay=false;
ImageTool.drawFrame(bufferGraphics,prImage,145,192,28,15,prInfo,1);
ImageTool.drawFrame(bufferGraphics,popImage,24,60,POPW,POPH,popInfo,2);
//repaint();
}
}
private void resume(){
paused=false;
isPlay=true;
t=null;
t=new Thread(this);
t.start();
}
public void start(){
isPlay=true;
t=new Thread(this);
t.start();
}
public void run() {
while(isPlay){
//delay=100;
input();
drawScreen();
repaint();
try{
Thread.sleep(delay);
}catch(Exception e){
System.out.println("Error~~~~~");
}
}
}
public void exit(){
//System.out.println("exitGame");
t=null;
/*bg=null;
blueImage=null;
greenImage=null;
pinkImage=null;
purpleImage=null;
redImage=null;
yellowImage=null;
blueClear=null;
greenClear=null;
pinkClear=null;
purpleClear=null;
redClear=null;
yellowClear=null;
scoreImage=null;
stageImage=null;
popImage=null;
prImage=null;
imageArr=null;
clearArr=null;
framesInfo=null;
clearInfo=null;
stageInfo=null;
scoreInfo=null;
popInfo=null;
prInfo=null;*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -