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

📄 sliding block.txt

📁 5个用java编写的小游戏源码
💻 TXT
📖 第 1 页 / 共 3 页
字号:
					(offyy==originalY)) {
				redoLimit = originalRedoLimit;
				if (historyCount<redoLimit)
					slide.redoButton.enable();
			} else {
				slide.redoButton.disable();
			}
			pointHistory[historyCount-1].x = offxx;
			pointHistory[historyCount-1].y = offyy;
			pieceHistory[historyCount-1] = currentPiece;
		} 
		else if(offx!=0 || offy!=0) {
			System.out.println("Move " + String.valueOf(step+1) + ": " + 
					currentPiece.cid+
					", "+String.valueOf(offx)+","+
					String.valueOf(offy));

			originalRedoLimit = 0;
			if (historyCount<redoLimit)
				if (pieceHistory[historyCount].cid==currentPiece.cid) {
					// save information about now-current move 
					// for future reinstatement of forward history
					originalRedoLimit = redoLimit;
					originalX = pointHistory[historyCount].x;
					originalY = pointHistory[historyCount].y;
				}
			pieceHistory[historyCount] = currentPiece;
			pointHistory[historyCount++] = new Point(offx,offy);
			redoLimit = historyCount;

			slide.backButton.enable();
			slide.redoButton.disable();
			reduceHistory();
			step++;
			slide.showcounter();
		}

		if (isSolution()) {
			System.out.println("SOLUTION - "+String.valueOf(step));
			if (step>minStep && minStep>0)
				slide.showMsg("Done!");
			if (minStep==0)
				slide.showMsg("Done - please report results!");
			if (step==minStep)
				slide.showMsg("Complete with minimum moves!!");
			if (step<minStep)
				slide.showMsg("Minimum IMPROVED!!!");
		}

		borderpaint(slide.getGraphics());

		currentPiece = null;
		if(dPoint.x!=offx*unit || dPoint.y!=offy*unit)
			slide.sound.play();
		painting = false;
	}

	//----------------------------------------------------------------
	public void backHistory(){
		if(dragFlag) return;
		if(historyCount <= 0) return;
		Point p = pointHistory[--historyCount];
		pieceHistory[historyCount].move(-p.x,-p.y);
		slide.paint(slide.getGraphics());
		slide.sound.play();
		step--;
		slide.showcounter();
		System.out.println("Back " + String.valueOf(step+1) + ": "+
				pieceHistory[historyCount].cid+", "+
				String.valueOf(-p.x)+", "+String.valueOf(-p.y));
		slide.redoButton.enable();
		if (historyCount==0) slide.backButton.disable();

		originalRedoLimit = 0;
	}

	//----------------------------------------------------------------
	public void forwardHistory(){
		if(dragFlag) return;
		if(historyCount == redoLimit) return;
		Point p = pointHistory[historyCount];
		System.out.println("Forward " + String.valueOf(step+1) + ": " + 
				pieceHistory[historyCount].cid+", "+
				String.valueOf(p.x)+", "+String.valueOf(p.y));
		pieceHistory[historyCount++].move(p.x,p.y);
		slide.paint(slide.getGraphics());
		slide.sound.play();
		step++;
		slide.showcounter();
		slide.backButton.enable();
		if (historyCount == redoLimit) slide.redoButton.disable();

		originalRedoLimit = 0;
	}

	//----------------------------------------------------------------
	private void reduceHistory(){
		if(historyCount >= maxHistory){
			System.out.println("Reduce history");
			int i=0;
			for(int j=maxHistory/2; j<maxHistory; i++,j++){
				pieceHistory[i] = pieceHistory[j];
				pointHistory[i] = pointHistory[j];
			}
			historyCount = i;
			redoLimit = i;
		}
	}

	//----------------------------------------------------------------
	private boolean isSolution(){
		char c;
		for (int j=0;j<ay;++j) for (int i=0;i<ax;++i) {
			c = targetBlocks[i][j].charAt(0);
			if (c=='.' || c=='#') continue;
			if (blocks[i][j] == null) return false;
			if (targetBlocks[i][j].indexOf(blocks[i][j].piece.cid) >= 0)
				continue;
			return false;
		}
		return true;
	}

	//----------------------------------------------------------------
	public void setBlockShape(){
		for(int j=ay;--j>=0;) for(int i=ax;--i>=0;) {
			Block bk = null;
			char c = '.';
			try{
				bk = blocks[i][j];
				c = bk.piece.cid;
			} catch( Exception e ){ continue; }
			try{
				if(blocks[i-1][j].piece.cid==c) bk.linkW=true;
			} catch( Exception e ){}
			try{
				if(blocks[i+1][j].piece.cid==c) bk.linkE=true;
			} catch( Exception e ){}
			try{
				if(blocks[i][j-1].piece.cid==c) bk.linkN=true;
			} catch( Exception e ){}
			try{
				if(blocks[i][j+1].piece.cid==c) bk.linkS=true;
			} catch( Exception e ){}
		}
	}

	//----------------------------------------------------------------
	public boolean isinside(int xx, int yy) {
		if( xx < 0 || xx >= ax )
			return false;
		if( yy < 0 || yy >= ay )
			return false;
		Block blk = blocks[xx][yy];
		if( blk != null && blk.outside)
			return false;
		return true;
	}

	//----------------------------------------------------------------
	// returns true if is a blocking block
	public boolean isblocking(int xx, int yy) {
		if( xx < 0 || xx >= ax )
			return false;
		if( yy < 0 || yy >= ay )
			return false;
		Block blk = blocks[xx][yy];
		if( blk != null && blk.outside)
			return true;
		return false;
	}

	private void borderpaint(Graphics g){
		int thick = 4;
		int bWidth;
		int nwDelta[],swDelta[],neDelta[],seDelta[];
		Color borderColor;

		if (isSolution()) {
			borderColor = slide.msgcolor;
			bWidth = thick;
		} else {
			borderColor = slide.shadowDark;
			bWidth = 1;
		}
		nwDelta = new int[thick+1];
		swDelta = new int[thick+1];
		neDelta = new int[thick+1];
		seDelta = new int[thick+1];
		
		for(int j=ay;--j>=0;){
			int y0 = y + unit*j;
			for(int i=ax;--i>=0;){
				int x0 = x + unit*i;
				if( ! isinside(i,j) ) {
					continue;
				}
				for (int b=0;b<=thick;b++) 
					nwDelta[b]=swDelta[b]=neDelta[b]=seDelta[b] = b+1;
				if (isinside(i-1,j-1)) 
					for (int b=0;b<=thick;b++) nwDelta[b] = 1-nwDelta[b];
				if (isinside(i-1,j+1)) 
					for (int b=0;b<=thick;b++) swDelta[b] = 1-swDelta[b];
				if (isinside(i+1,j-1)) 
					for (int b=0;b<=thick;b++) neDelta[b] = 1-neDelta[b];
				if (isinside(i+1,j+1)) 
					for (int b=0;b<=thick;b++) seDelta[b] = 1-seDelta[b];

				if( ! isinside(i-1,j) ) { 
					for (int b=0;b<=thick;b++) {
						if (b==0)
							g.setColor(slide.shadowLight);
						else if (b>bWidth)
							g.setColor(slide.backcolor);
						else
							g.setColor(borderColor);
						g.drawLine(x0-b-1,y0-nwDelta[b],
								x0-b-1,y0+unit-1+swDelta[b]);
					}
				}
				if( ! isinside(i+1,j) ) { 
					for (int b=0;b<=thick;b++) {
						if (b==0)
							g.setColor(slide.shadowLight);
						else if (b>bWidth)
							g.setColor(slide.backcolor);
						else
							g.setColor(borderColor);
						g.drawLine(x0+unit+b,y0-neDelta[b],
								x0+unit+b,y0+unit-1+seDelta[b]);
					}
				}
				if( ! isinside(i,j-1) ) { 
					for (int b=0;b<=thick;b++) {
						if (b==0)
							g.setColor(slide.shadowLight);
						else if (b>bWidth)
							g.setColor(slide.backcolor);
						else
							g.setColor(borderColor);
						g.drawLine(x0-nwDelta[b],y0-1-b,
								x0+unit-1+neDelta[b],y0-1-b);
					}
				}
				if( ! isinside(i,j+1) ) { 
					for (int b=0;b<=thick;b++) {
						if (b==0)
							g.setColor(slide.shadowLight);
						else if (b>bWidth)
							g.setColor(slide.backcolor);
						else
							g.setColor(borderColor);
						g.drawLine(x0-swDelta[b],y0+unit+b,
								x0+unit-1+seDelta[b],y0+unit+b);
					}
				}
			}
		}
		for(int j=ay;--j>=0;){
			int y0 = y + unit*j;

			for(int i=ax;--i>=0;){
				int x0 = x + unit*i;
				if( isblocking(i,j) ) {
					Block blk = blocks[i][j];
					if( blk.image != null) {
						boolean flg = g.drawImage(blk.image, 
							x0, y0, blk.width*unit*blk.wFactor, 
							blk.height*unit*blk.hFactor, 
							slide.shadowLight, null);
					}
				}
			}
		}
	}

	//----------------------------------------------------------------
	public void paint(Graphics g){

		borderpaint( g);

//		g.setColor( slide.backcolor );
//		g.fillRect(x,y,width,height);

//		g.setColor( slide.shadowLight );
//		g.drawLine(x,y+height,x+width,y+height);
//		g.drawLine(x+width,y,x+width,y+height);
//		g.drawLine(x-1,y+height+1,x+width+1,y+height+1);
//		g.drawLine(x+width+1,y-1,x+width+1,y+height+1);
//		g.setColor( slide.shadowDark );
//		g.drawLine(x-1,y-1,x-1,y+height-1);
//		g.drawLine(x-1,y-1,x+width-1,y-1);
//		g.drawLine(x-2,y-2,x-2,y+height);
//		g.drawLine(x-2,y-2,x+width,y-2);

		for(int j=ay;--j>=0;){
			for(int i=ax;--i>=0;){
				Block blk = blocks[i][j];
				if( blk == null ) {
					g.setColor( slide.backcolor );
					g.fillRect(x+i*unit, y+j*unit, unit, unit);
				} else if( ! blk.outside ) {
					blk.paint(g);
				}
			}
		}
	}

	//----------------------------------------------------------------
	public void paintHint(Graphics g){
		char c;
		int hWidth = 4;

		if (hintBlocks==null) return;
		g.setColor( slide.hintcolor );
		for(int v=ax; --v>=0;){
			c = hintBlocks[v][0];
			if(c!='.' && c!='#'){
				g.fillRect(x+v*unit,y-10,unit,hWidth);
			}else{
				g.clearRect(x+v*unit,y-10,unit,hWidth);
			}
			c = hintBlocks[v][ay-1];
			if(c!='.' && c!='#'){
				g.fillRect(x+v*unit,y+height+(10-hWidth),unit,hWidth);
			}else{
				g.clearRect(x+v*unit,y+height+(10-hWidth),unit,hWidth);
			}
		}
		for(int w=ay; --w>=0;){
			c = hintBlocks[0][w];
			if(c!='.' && c!='#'){
				g.fillRect(x-10,y+w*unit,hWidth,unit);
			}else{
				g.clearRect(x-10,y+w*unit,hWidth,unit);
			}
			c = hintBlocks[ax-1][w];
			if(c!='.' && c!='#'){
				g.fillRect(x+width+(10-hWidth),y+w*unit,hWidth,unit);
			}else{
				g.clearRect(x+width+(10-hWidth),y+w*unit,hWidth,unit);
			}
		}
	}


	//----------------------------------------------------------------
	private	void loadData(String filename){
		URL		url;
		DataInputStream file;
		try {
			url = new URL( slide.getDocumentBase(), filename );
			System.out.println("Load: "+url);
			file = new DataInputStream( url.openStream() );
		} catch( MalformedURLException e ) {
			slide.showStatus( "file name error : " + filename );
			return;
		} catch( IOException e ) {
			slide.showStatus( "file open error : " + filename );
			return;
		}

		try {
			slide.nextfile = null;
			String line; 
			StringBuffer str = new StringBuffer("");
			int	y = 0;
			MediaTracker mTracker;
			mTracker = new MediaTracker(slide);

		    nextline:
			for(;;) {
				line=file.readLine();
				if( line == null )
					break;
				if( ';' == line.charAt(0) )
					continue;
//				System.out.println( "line:"+line );

				StringTokenizer st = new StringTokenizer(line);
				try {
					String tk = st.nextToken();
					if( tk.equals("size") ) {
						tk = st.nextToken();
						ax = (Integer.valueOf(tk)).
								intValue();
						tk = st.nextToken();
						ay = (Integer.valueOf(tk)).
								intValue();
//						System.out.println("\tboard size=" + String.valueOf(ax) + "," + String.valueOf(ay));
						blocks = new Block[ax][ay];
						targetBlocks = new String[ax][ay];
						continue nextline;
					}
					if (tk.equals("image") ) {
						tk = st.nextToken();
						int row = (Integer.valueOf(tk)).intValue();

						tk = st.nextToken();
						int col = (Integer.valueOf(tk)).intValue();
						
						tk = st.nextToken();
						blocks[col][row].image = 
							slide.getImage(slide.getCodeBase(),tk);

						tk = st.nextToken();
						blocks[col][row].hFactor = 
								(Integer.valueOf(tk)).intValue();

						tk = st.nextToken();
						blocks[col][row].wFactor = 
							(Integer.valueOf(tk)).intValue();
						
						mTracker.addImage(blocks[col][row].image,0,
							blocks[col][row].wFactor*unit,
							blocks[col][row].hFactor*unit);

						continue nextline;
					}
					if (tk.equals("label") ) {
						tk = st.nextToken();
						char x = tk.charAt(0);
						int k;
						for (k=piecen;--k>=0;) {
							if (pieces[k].cid == x) break;
						}
						tk = st.nextToken();
						if (k>=0)
							pieces[k].setLabel(tk);
						continue nextline;
					}
					if (tk.equals("labeloffset") ) {
						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 offsetX = (Integer.valueOf(tk)).intValue();
						tk = st.nextToken();
						int offsetY = (Integer.valueOf(tk)).intValue();
						if (k>=0)
							pieces[k].setLabelOffset(offsetX,offsetY);
						continue nextline;
					}
					if (tk.equals("color") ) {

⌨️ 快捷键说明

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