⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sliding block.txt

📁 5个用java编写的小游戏源码
💻 TXT
📖 第 1 页 / 共 3 页
字号:
						tk = st.nextToken();
						char x = tk.charAt(0);
						int k;
						for (k=piecen;--k>=0;) {
							if (pieces[k].cid == x) break;
						}
						tk = st.nextToken();
						int r = (Integer.valueOf(tk)).intValue();
						tk = st.nextToken();
						int g = (Integer.valueOf(tk)).intValue();
						tk = st.nextToken();
						int b = (Integer.valueOf(tk)).intValue();
						if (k>=0) 
							pieces[k].setColor(r,g,b);
						continue nextline;
					}
					if (tk.equals("equiv") ) {
						tk = st.nextToken();
						for (int j=0;j<ay;++j) for (int i=0;i<ax;++i) {
							if ( tk.indexOf( targetBlocks[i][j].charAt(0) ) >=0 ) {
								targetBlocks[i][j] = tk;
							}
}					
						continue nextline;
					}
					if( tk.equals("next")){
						tk = st.nextToken();
						slide.nextfile = tk;
//						System.out.println("\tnext:" + tk);
						boardType="none";
					}
					if( tk.equals("step")){
						tk = st.nextToken();
						minStep = (Integer.valueOf(tk))
								.intValue();
						boardType="none";
					}
					if( tk.equals("initial") ) {
//						System.out.println("\tload initial");
						boardType = tk;
						str = new StringBuffer("");
						y = 0;
						continue nextline;
					}
					if( tk.equals("hint") ) {
//						System.out.println("\tload hint");
						boardType = tk;
						hintBlocks = new char[ax][ay];
						str = new StringBuffer("");
						y = 0;
						continue nextline;
					}
					if( tk.equals("target") ) {
//						System.out.println("\tload target");
						boardType = tk;
						str = new StringBuffer("");
						y = 0;
						continue nextline;
					}
					if( tk.equals("end") ) {
						break nextline;
					}

					if( ! boardType.equals("none") ) {
						++y;
						str.append( line );
						if( y >= ay ) {
						    setupData(str.toString());
						    boardType="none";
						}
					}
				} catch( NoSuchElementException e ) {
				    System.out.println(
					"No such element(s).(file error)");
				}
			}

			try {
				mTracker.waitForID(0); 
			} catch(InterruptedException e) {
				System.out.println("MediaTracker error");
			}

		} catch( IOException e ) {}
		System.out.println( "-- End of LoadData--" );
	}

	//----------------------------------------------------------------
	private	void setupData(String str) {
		int	idx = 0;
		if(boardType.equals("initial")) {
			for(int j=0;j<ay;++j) {
			    for(int i=0;i<ax;++i ) {
				char c = str.charAt(idx++);
				if(c=='.') continue;
				Block bk = new Block(i,j,this);
				blocks[i][j] = bk;
				if(c=='#') {
					blocks[i][j].outside = true;
					continue;
				}
				int k;
				for(k=piecen;--k>=0;){
					if(pieces[k].cid==c) break;
				}
				if(k<0){
					k = piecen;
					pieces[piecen++] = new Piece(this,c);
				}
				pieces[k].add(bk);
			    }
			}
		}
		if(boardType.equals("hint")) {
			for(int j=0;j<ay;++j) for(int i=0;i<ax;++i ) {
				hintBlocks[i][j] = str.charAt(idx++);
			}
		}
		if(boardType.equals("target")) {
			for(int j=0;j<ay;++j) for(int i=0;i<ax;++i ) {
				targetBlocks[i][j] = String.valueOf(str.charAt(idx++));
			}
		}
	}

}




//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	Block
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
class Block extends Rectangle {

	public	Board	board;
		Piece	piece;
		boolean	outside;
		boolean	linkN;
		boolean	linkS;
		boolean	linkW;
		boolean	linkE;
		Image		image;
		String	label;
		int		labelX;
		int		labelY;
		Color		color;
		int		hFactor;
		int		wFactor;

	//----------------------------------------------------------------
	Block(int cx,int cy,Board bd){
		board = bd;
		piece = null;
		x = cx; y = cy; width = height = 1;
		linkN = linkS = linkW = linkE = outside = false;
		image = null;
		label = null;
		color = bd.slide.piececolor;
		hFactor = wFactor = 1;
		// labelX and labelY are offsets from the center of the block
		labelX = -5;
		labelY = 5;
	}

	//----------------------------------------------------------------
	public void paint(Graphics g){
		paint(g,0,0);
	}
	public void paint(Graphics g, int offx, int offy){
		int unit = board.unit;
		int x1 = board.x+x*unit+offx;
		int y1 = board.y+y*unit+offy;
		int x2 = x1+width*unit-1;
		int y2 = y1+height*unit-1;
		Slide slide = board.slide;

		if (image != null) {
			boolean flg = g.drawImage(image, 
				x1, y1, width*unit*wFactor, height*unit*hFactor, 
				slide.shadowLight, board.slide);
			return;
		}

		if (piece.getHead().image == null) {
			g.setColor( piece.getHead().color );
			g.fillRect(x1,y1,width*unit,height*unit);

			if (this == piece.getHead()) {
				g.setColor(slide.shadowDark);
				Font pf = new java.awt.Font("Helvetica",0,20);
				g.setFont(pf);
				if (label == null) {
					char x[];
					x = new char[1];
					x[0] = piece.cid;
					g.drawChars(x, 0, 1, 
						x1+(width*board.unit*wFactor)/2 + labelX, 
						y1+(height*board.unit*hFactor)/2 + labelY);
				} else {
					if (label.charAt(0) != '*')
					g.drawString(label, 
						x1+(width*board.unit*wFactor)/2 + labelX, 
						y1+(height*board.unit*hFactor)/2 + labelY);
				}
			}

			g.setColor( slide.shadowDark );
			if(!linkE) {
				g.drawLine(x2-1,y1,x2-1,y2);
				g.drawLine(x2,y1,x2,y2);
			}
			if(!linkS) {
				g.drawLine(x1,y2-1,x2,y2-1);
				g.drawLine(x1,y2,x2,y2);
			}
			g.setColor( slide.shadowLight );
			if(!linkW) {
				g.drawLine(x1,y1,x1,y2);
				g.drawLine(x1+1,y1,x1+1,y2);
			}
			if(!linkN) {
				g.drawLine(x1,y1,x2,y1);
				g.drawLine(x1,y1+1,x2,y1+1);				
			}
		}
	}

	//----------------------------------------------------------------
	public void clear(Graphics g, int offx, int offy, int dx, int dy){
		int unit = board.unit;
		Slide slide = board.slide;
		g.setColor( slide.backcolor );

		int x1 = board.x+unit*x+offx;
		int w1 = unit;
		int y1 = board.y+unit*y+offy;
		int h1 = unit;
		int x2,w2,y2,h2,x3,w3,y3,h3;
		boolean bx,by;

		if(dx>=0){
			x2 = x1;	w2 = (dx<unit)? dx : unit;
			x3 = x1+w2;	w3 = unit-w2;
			bx = linkW;
		}else{
			x2 = x1+w1+dx;	w2 = (-dx<unit)? -dx : unit;
			x3 = x1;	w3 = unit-w2;
			bx = linkE;
		}
		if(dy>=0){
			y2 = y1;	h2 = (dy<unit)? dy : unit;
			by = linkN;
		}else{
			y2 = y1+h1+dy;	h2 = (-dy<unit)? -dy : unit;
			by = linkS;
		}
		if(bx) g.fillRect(x2,y2,w2,h2);
		else g.fillRect(x2,y1,w2,h1);
		if(!by) g.fillRect(x3,y2,w3,h2);
	}
}





//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//	Piece
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

class Piece{
	final	int	maxBlocks = 36;
	public
		Board	board;
		char	cid;

	private	Block	blocks[];
		int	blockn;
		Point	delta;

	//----------------------------------------------------------------
	Piece( Board b, char c ) {
		board = b;
		blockn = 0;
		blocks = new Block[maxBlocks];
		cid = c;
		delta = new Point(0,0);
	}

	//----------------------------------------------------------------
	// getHead() returns the first block in the piece.

	public Block getHead() {
		if (blockn == 0)
			return null;
		return blocks[0];
	}

	//----------------------------------------------------------------
	// setLabel() sets the optional label of the first block.

	public void setLabel(String lab) {
		blocks[0].label = lab;
		blocks[0].labelX = blocks[0].labelX - 5*(lab.length()-1);
	}

	//----------------------------------------------------------------
	// setLabelOffset() sets the display offset for a label.

	public void setLabelOffset(int x, int y) {
		blocks[0].labelX = x;
		blocks[0].labelY = y;
	}

	//----------------------------------------------------------------
	// setColor() sets the optional RGB color of the first block.

	public void setColor(int r, int g, int b) {
		blocks[0].color = new Color(r,g,b);
	}

	//----------------------------------------------------------------
	public void add(Block bk){
		if(bk.piece!=null){
			System.out.println("Double definition");
			return;
		}
		blocks[blockn++] = bk;
		bk.piece = this;
	}

	//----------------------------------------------------------------
	public int moveX(int x,int y,int dx){
		if(dx == 0) return 0;
		int unit = board.unit;
		int offx = (x+unit*1024)/unit-1024;
		int offy = (y+unit*1024)/unit-1024;
		int adj = x-offx*unit;
		if(adj != 0){
			if(dx < -adj) dx = -adj;
			if(dx > unit-adj) dx = unit-adj;
			return dx;
		}else{
			offx = (dx>0)? offx+1 : offx-1;
			for(int i=blockn;--i>=0;){
				Block bk = blocks[i];
				Block bk2 = null;
				try{
				    bk2 = board.blocks[bk.x+offx][bk.y+offy];
				    if(bk2 != null) {
					if( bk2.outside || bk2.piece.cid != cid) {
								return 0;
					}
				    }
				    if(y%unit != 0){
					bk2 = board.blocks[bk.x+offx][bk.y+offy+1];
					if(bk2 != null && bk2.piece.cid != cid) return 0;
				    }
				} catch( Exception e ){return 0;}
			}
			if(dx > unit) dx = unit;
			if(dx <-unit) dx = -unit;
			return dx;
		}
	}


	//----------------------------------------------------------------
	public int moveY(int x,int y,int dy){
		if(dy == 0) return 0;
		int unit = board.unit;
		int offx = (x+unit*1024)/unit-1024;
		int offy = (y+unit*1024)/unit-1024;
		int adj = y-offy*unit;
		if(adj != 0){
			if(dy < -adj) dy = -adj;
			if(dy > unit-adj) dy = unit-adj;
			return dy;
		}else{
			offy = (dy>0)? offy+1 : offy-1;
			for(int i=blockn;--i>=0;){
				Block bk = blocks[i];
				Block bk2 = null;
				try{
				    bk2 = board.blocks[bk.x+offx][bk.y+offy];
				    if(bk2 != null) {
					if( bk2.outside || bk2.piece.cid != cid) {
								return 0;
					}
				    }
				    if(x%unit !=0){
					bk2 = board.blocks[bk.x+offx+1][bk.y+offy];
					if(bk2 != null && bk2.piece.cid != cid) return 0;
				    }
				} catch( Exception e ){return 0;}
			}
			if(dy > unit) dy = unit;
			if(dy <-unit) dy = -unit;
			return dy;
		}
	}

	//------------------------------------------------------------
	public void move( int dx, int dy ) {
		int unit = board.unit;
		int i;
		for(i=blockn;--i>=0;){
			Block bk = blocks[i];
			board.blocks[bk.x][bk.y] = null;
		}
		for(i=blockn;--i>=0;){
			Block bk = blocks[i];
			blocks[i].move(bk.x+dx,bk.y+dy);
			board.blocks[bk.x][bk.y] = blocks[i];

		}
	}


	//----------------------------------------------------------------
	public void resetAdjust(){
		delta.x = delta.y = 0;
	}

	//----------------------------------------------------------------
	public void update(Graphics g,int dx,int dy){
		if(delta.x == dx && delta.y == dy) return;
		for(int i=blockn;--i>=0;){
		    blocks[i].clear(g,delta.x,delta.y,dx-delta.x,dy-delta.y);
		}
		delta.x = dx;
		delta.y = dy;



		for(int i=blockn;--i>=0;){
			blocks[i].paint(g,dx,dy);
		}
	}

}

//================================================================
//			End of File
//================================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -