📄 likrect.java
字号:
//Lik俄罗斯方块 1.0beta1import java.awt.geom.Rectangle2D;import java.util.ArrayList;public class LikRecT { public LikRecT(){ this.moverts=new ArrayList<RecT>(); this.rts=new ArrayList<RecT>(); this.above=new ArrayList<RecT>(); this.clear(); } public int getn(){ return this.n; } private void clear(){ this.moverts.clear(); this.rts.clear(); this.above.clear(); this.n=0; this.moverts_n=0; this.above_n=0; } public void start(){ this.clear(); //this.above_n=(int)Math.random()*7+1; this.above_n=CC; this.createabove(); } private boolean clearrt(){ int j=0; ArrayList<Integer> del=new ArrayList<Integer>(); for(int i=MAX_Y;i>1;i--){ for(RecT rt:this.rts){ if(rt.getY()==i){ del.add(j); } j++; } if(del.size()==MAX_X){ int n=0; for(int x:del) this.rts.remove(x-(n++)); for(RecT rt:this.rts) if(rt.getY()<i) rt.setmovexy(0,1); i++; n++; this.n++; } if(j==0) return true; j=0; del.clear(); } del.clear(); return false; } private void setnNext_m(){ RecT r=this.moverts.get(0); int x=(int)r.getX(); int y=(int)r.getY(); for(int i=1;i<this.moverts.size();i++){ r=this.moverts.get(i); int mx=(int)r.getX()-x; int my=(int)r.getY()-y; r.setxy(x+my,y-mx); } if(this.isBob()||this.isOut()||this.isBobDown()) for(int i=1;i<this.moverts.size();i++){ r=this.moverts.get(i); int mx=(int)r.getX()-x; int my=(int)r.getY()-y; r.setxy(x-my,y+mx); } } public void setRecT_m(){ switch(this.moverts_n){ case LD: this.setnNext_m(); break; case RD: this.setnNext_m(); break; case FK: break; case LRD: this.setnNext_m(); break; case CC: this.setnNext_m(); break; case LC: this.setnNext_m(); break; case RC: this.setnNext_m(); break; default: break; } } // 是否触碰到非移动方块 private boolean isBob(){ for(RecT mrt:this.moverts) for(RecT rt:this.rts){ if(mrt.getRec2D_out().getX()==rt.getRec2D_out().getX() &&mrt.getRec2D_out().getY()==rt.getRec2D_out().getY()) return true; } return false; } private boolean isBobDown(){ for(RecT mrt:this.moverts) if(mrt.getRec2D_out().getY()>MAX_Y*H-1) return true; return false; }// 是否越界 private boolean isOut(){ for(RecT rt:this.moverts){ if(rt.getRec2D_out().getX()<0 ||rt.getRec2D_out().getY()<3 ||rt.getRec2D_out().getX()>MAX_X*W-1) return true; } return false; } public ArrayList<RecT> getmoverts(){ return this.moverts; } public ArrayList<RecT> getrts(){ return this.rts; } public ArrayList<RecT> getabove(){ return this.above; } // 以相对坐标来移动moverts public boolean movert(int mx,int my){ if(moverts.isEmpty()==true) return false; for(RecT rt:moverts){ rt.setmovexy(mx,my); } if(this.isBob()){ if(my>2) return false; this.movert(-mx,-my); if(mx==0){ return this.createmove(); } return true; } if(this.isBobDown()){ this.movert(-mx,-my); this.createmove(); return true; } if(this.isOut()){ this.movert(-mx,-my); } return true; } // 添加一个相对于above[0]坐标为(mx,my)的RecT到above public boolean add(int mx,int my){ if(above.isEmpty()) return false; RecT rt=new RecT((int)(above.get(0).getX()+mx),(int)(above.get(0).getY()+my)); above.add(rt); return true; } // 添加一个随机移动方块 public boolean createmove(){ if(!this.add()) return false; int x=(int)(Math.random()*7)+1; this.above_n=x; return this.createabove(); } // 添加类型为n的移动方块 private boolean createabove(){ RecT r=new RecT(MAX_X/2,2); above.add(r); switch(this.above_n){ case LD: this.add(-1,-1); this.add(-1,0); this.add(0,1); break; case RD: this.add(1,0); this.add(1,-1); this.add(0,1); break; case FK: this.add(1,0); this.add(0,1); this.add(1,1); break; case LRD: this.add(-1,0); this.add(1,0); this.add(0,1); break; case CC: this.add(1,0); this.add(2,0); this.add(-1,0); this.add(-2,0); break; case LC: this.add(-1,-1); this.add(0,1); this.add(0,-1); break; case RC: this.add(0,-1); this.add(0,1); this.add(1,-1); break; default: break; } return true; } // 把移动的方块添加到底层非移动方块中 @SuppressWarnings("unchecked") private boolean add(){ for(RecT r:moverts) rts.add(r); this.clearrt(); moverts.clear(); moverts=(ArrayList<RecT>) above.clone(); above.clear(); this.moverts_n=this.above_n; this.above_n=-1; return this.movert(0,3); } // 单独的方块 public static class RecT{ public RecT(int x,int y){ out=new Rectangle2D.Float(); in=new Rectangle2D.Float(); this.setxy(x,y); } public RecT clone(){ RecT r=new RecT((int)this.getRec2D_out().getX()/W+1,(int)this.getRec2D_out().getY()/H+1); return r; } public void setxy(int x,int y){ x=(x-1)*W+W/2; y=(y-1)*H+H/2; out.setFrame(x-W/2,y-H/2,W,H); in.setFrame(out.getX()+S/2,out.getY()+S/2,W-S,H-S); } public void setmovexy(int mx,int my){ setxy((int)out.getX()/W+1+mx,(int)out.getY()/H+my+1); } public Rectangle2D getRec2D_out(){ return out; } public Rectangle2D getRec2D_in(){ return in; } public int getX(){ return (int)this.out.getX()/W+1; } public int getY(){ return (int)this.out.getY()/H+1; } private Rectangle2D out; private Rectangle2D in; } private ArrayList<RecT> rts; private ArrayList<RecT> moverts; private ArrayList<RecT> above; private int moverts_n; private int above_n; private int n=0; public static final int LD=1; public static final int RD=2; public static final int FK=3; public static final int LRD=4; public static final int CC=5; public static final int LC=6; public static final int RC=7; public static final int W=16; public static final int H=16; public static final int S=6; public static final int MAX_X=20; public static final int MAX_Y=25;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -