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

📄 mystrategyimplement.java

📁 情话宝典
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

	private boolean isSelecting(){
		if( currentButton.getNumber() == buttons[10].getNumber() ){
			return false;
		}
		else{
			return true;
		}
	}

	private String[] parseBlackString( Graphics g, String s, int width ){
		int tmp = 0;
		int index = 0;
		int c = 0;
		int b = 0;
		String[] ss = new String[10];
		for( int i=0; i<s.length(); i++ ){
			if( s.charAt(i) == ' ' ){
				b = i;
			}
			c = c + g.getFont().charWidth(s.charAt(i));
			if( c > width ){
				i = b+1;
				c = 0;
				ss[index] = s.substring(tmp,i);
				tmp = i;
				index++;
			}
		}
		ss[index] = s.substring(tmp);
		String[] rs = new String[index+1];
		System.arraycopy( ss, 0, rs, 0, index+1 );
		System.gc();
		return rs;
	}
	private String[] parseString( Graphics g, String s, int width ){
		s = s + "|";
		return parseBlackString( g, s, width );
	}


	private void drawBackground(){
		if( imgGirl != null ){
			for( int i=0; i*imgBackground.getWidth() < width; i++ ){
				g.drawImage( imgBackground, i*imgBackground.getWidth(), 0, 16|4 );
			}
//			g.setColor( 255, 255, 255 );
//			g.fillRect( 0, 0, width, height );
//			g.setColor( 0, 0, 0 );
			int x = (width-imgWidth)/2;
			g.drawImage( imgGirl, x, 0, 16|4 );
			int xDir = (imgWidth-imgFace.getWidth())/2-2;
			int yDir = (imgHeight-imgFace.getHeight())/2-4;
			if( level > 9 ){
				g.setClip( x+7, 0+9, 142, 115 );
				g.drawImage( imgFace, x+xDir-7, 0+yDir+14, 16|4 );
//				g.fillRect( 0, 0, width, height );
				g.setClip( 0, 0, width, height );
			}
			else{
				g.drawImage( imgFace, x+xDir, 0+yDir+5, 16|4 );
			}
//			g.drawString( "lev"+(level+1), 0, 30, 16|4 );
//			g.drawString( "sen"+(sentenceCounter+1), 0, 40, 16|4 );
			g.drawImage( imgFontFrame, 0, height-imgFontFrame.getHeight(), 4|16 );

			if( (string.length() == s0.length()-1) && (currentButton.getNumber() == '$' ) ){
				g.setColor( 255, 255, 255 );
				g.fillRect( 40, 80, 110, 28 );
				g.setColor( 0, 255, 0 );
				g.drawString( "按0确定", 50, 80, 4|16 );
				g.drawString( "按#修改", 50, 90, 4|16 );
			}
		}
	}
	private void drawIsUpperCase(){
		g.setColor( 255, 0, 0 );
		if( caseFlag == 0 ){
			g.drawString( "abc", 110, 30, 16|4 );
		}
		else{
			g.drawString( "ABC", 110, 30, 16|4 );
		}
	}
	private void drawTimeBar(){
		int t = 0;
		if( totalTime != 0 ){
			t = time*imgTimeBar.getWidth()/totalTime;
		}
		g.drawImage( imgTime, xTime, yTime, 4|16 );
		g.setClip( xTimeBar, yTimeBar, t, imgTimeBar.getHeight() );
		g.drawImage( imgTimeBar, xTimeBar, yTimeBar, 4|16 );
		g.setClip( 0, 0, width, height );
	}

	private void drawHeart(){
		int tt = level%5;
		int w = imgTimeHeart.getHeight();
		g.setClip( xTimeHeart, yTimeHeart, w, w );
		g.drawImage( imgTimeHeart, xTimeHeart-tt*w, yTimeHeart, 4|16 );
		g.setClip( 0, 0, width, height );
	}

	private void drawLife(){
		g.setClip( xLife, yLife, wLife, hLife );
//		g.drawImage( imgNumber, xLife-(life-1)*wLife, yLife, 4|16 );
		g.setClip( 0, 0, width, height );
	}
	private void drawRemainCount(){
		g.setColor( 255, 0, 0 );
		g.drawString( Integer.toString(remainCount), 110, 5, 16|4 );
	}
	private void drawPause(){
		String s = "Pause";
		int x = (getWidth()-g.getFont().stringWidth(s))/2;
		int y = (getHeight()-g.getFont().getHeight())/2;
		g.setColor( 255, 0, 0 );
		g.drawString( s, x, y, 16|4 );
	}
	private void drawBackgroundWord(){
		if( lineFrom == 0 && lineNumber > line ){
//			g.drawImage( imgDown, 110, 40, 16|4 );
		}
		if( lineFrom > 0 && lineFrom < lineNumber - line ){
//			g.drawImage( imgUpDown, 110, 40, 16|4 );
		}
		else if( lineFrom == lineNumber-line && lineNumber > line ){
//			g.drawImage( imgUp, 110, 40, 16|4 );
		}
		drawBackgroundString( s0 );
	}
	private void drawTypingWord(){
		if( string.length() == 0 ){
			return;
		}
		else if( isBlackString ){
			drawBlackString( string );
		}
		else if( isWrong() ){
			drawRedString( string );
		}
		else{
			drawNoBlackString( string );
		}
	}

	private void drawBackgroundString( String s ){
		drawBackgroundString( g, s, bx, by, actionWidth, line, lineFrom );
	}
	private void drawNoBlackString( String s ){
		drawNoBlackString( g, s+"|", actionX, actionY, actionWidth, line );
	}
	private void drawBlackString( String s ){
		drawColorString( g, s, actionX, actionY, actionWidth, line, COLOR_BLACK );
	}
	private void drawRedString( String s ){
		drawColorString( g, s, actionX, actionY, actionWidth, line, COLOR_RED );
	}

	private void drawBackgroundString( Graphics g, String s, int x, int y, int width, int line, int from ){
		g.setColor( 255, 208, 223 );
//		g.fillRect( x, y, width, g.getFont().getHeight()*line );
		g.setColor( 1, 76, 177 );
		String[] strings = parseBlackString( g, s, width );
		for( int i=0; (i<line)&&((i+from)<strings.length); i++ ){
			g.drawString( strings[from+i], x, y+g.getFont().getHeight()*i, 16|4 );
		}
	}

	private void drawNoBlackString( Graphics g, String s, int x, int y, int width, int line ){
		g.setColor( 208, 240, 255 );
//		g.fillRect( x, y, width, g.getFont().getHeight()*line );
		g.setColor( 1, 76, 177 );
		String[] strings = parseBlackString( g, s, width );
		changeLine( strings.length-1 ); 
		if( strings.length <= line ){		// 从头画起
			for( int i=0; i<strings.length; i++ ){
				g.drawString( strings[i], x, y+g.getFont().getHeight()*i, g.TOP|g.LEFT );
			}
		}
		else{								// 从尾画起
			for( int i=0; i<line; i++ ){
				g.drawString( strings[strings.length-line+i], x, y+g.getFont().getHeight()*i, 16|4 );
			}
		}
	}
	private void drawColorString( Graphics g, String s, int x, int y, int width, int line, int type ){
		g.setColor( 208, 240, 255 );
//		g.fillRect( x, y, width, g.getFont().getHeight()*line );
		g.setColor( 1, 76, 177 );
		String[] strings = parseBlackString( g, s, width );
		changeLine( strings.length-1 );
		if( strings.length <= line ){		// 从头画起
			for( int i=0; i<strings.length; i++ ){
				g.drawString( strings[i], x, y+g.getFont().getHeight()*i, g.TOP|g.LEFT );
			}
			drawColorLastChar( g, strings[strings.length-1],
										x, y+g.getFont().getHeight()*(strings.length-1), type );
		}
		else{								// 从尾画起
			for( int i=0; i<line; i++ ){
				g.drawString( strings[strings.length-line+i], x, y+g.getFont().getHeight()*i, 16|4 );
			}
			drawColorLastChar( g, strings[strings.length-1],
										x, y+g.getFont().getHeight()*(line-1), type );
		}
	}
	// single line !
	private void drawColorLastChar( Graphics g, String s, int baseX, int baseY, int type ){
		if( s.length() == 0 ) return;

		int x = baseX + g.getFont().stringWidth( s.substring(0,s.length()-1) );
		int y = baseY;
		int width = g.getFont().stringWidth( s.substring(s.length()-1) );
		int height = g.getFont().getHeight();
		switch( type ){
			case COLOR_RED:
				g.setColor( 220, 0, 0 );
				g.fillRect( x, y, width, height );
				g.setColor( 255, 255, 255 );
				break;
			case COLOR_BLACK:
				g.setColor( 200, 200, 200 );
				g.fillRect( x, y, width, height );
				g.setColor( 1, 76, 177 );
				break;
			default:
				System.out.println( "drawColorLastChar() error: unknow color type" );
				break;
		}
		g.drawString( s.substring(s.length()-1), x, y, 16|4 );
	}

	private void changeLine( int i ){
		lineFrom = i;
	}

	private int getLineNumber( Graphics g, String s, int width ){
		String[] ss = parseString( g, s, width );
		return ss.length;
	}

	private int getWidth(){
		return width;
	}
	private int getHeight(){
		return height;
	}

	private String[] getSentences( int lev ){
		String[] tmps = new String[countSentence];
		try{
			InputStream is = getClass().getResourceAsStream( "/" + lev + ".txt" );
			DataInputStream dis = new DataInputStream( is ); 
			for( int i=0; i<tmps.length; i++ ){
				tmps[i] = readLine( dis );
			}
			dis.close();
		}
		catch( IOException e ){
			e.printStackTrace();
		}
		return tmps;
	}
	private String readLine( DataInputStream dis ) throws IOException{
		int tmp = dis.read();
		while( (char)tmp != '(' ){
			tmp = dis.read();
		}
		StringBuffer buffer = new StringBuffer();
		tmp = dis.read();
		while( (char)tmp != ')' ){
			buffer.append( (char)tmp );
			tmp = dis.read();
		}

//		System.out.println( buffer.toString() );
		return buffer.toString();
	}

	private void increaseTime(){
		int t = timeIncrease[level/5];
		if( time + t > totalTime ) time = totalTime;
		else						time = time + t;
	}
	private void decreaseTime(){
		int t = timeDecrease[level/5];
		if( time - t < 0 )	time = 0;
		else				time = time - t;
	}
	private boolean isWordTooLong( String s ){
		int c = 0;
		for( int i=0; i<s.length(); i++ ){
			if( s.charAt(s.length()-1-i) == ' ' ){
				break;
			}
			else{
				c++;
			}
		}
		if( c > 15 ) return true;
		else		 return false;
	}

	private boolean isWrong(){
		if( string.length() == 0 ){
			return false;
		}
		else if( string.charAt(string.length()-1) != s0.charAt(string.length()-1) ){
			return true;
		}
		return false;
	}

	private int getTime( String s, int lev ){
		int t = s.length();
		return t*20;
	}

	private void playComplete(){
		strategy.getPlayer().playComplete();
	}
	private void playChangeGirl(){
		strategy.getPlayer().playCross();
	}
	private void playChangeFace(){
		strategy.getPlayer().playGoldEaten();
	}
	private void playStart(){
		strategy.getPlayer().playStart();
	}
	private void playDead(){
		strategy.getPlayer().playDead();
	}

	private void playRightSound(){
		if( level >= 14 ){}
		else if( level%5 == 4 ){
			playChangeGirl();
		}
		else{
			playChangeFace();
		}
	}

	// constants
	/*
	private int width = 128;			// 屏幕的大小
	private int height = 128;
	private int imgWidth = 96;			// 背景图的大小
	private int imgHeight = 80;
	private int line = 2;				// 最多一次可以显示几行
	private int bx = 0;					// 示范字显示的范围
	private int by = 75;
	private int actionX = 0;			// 打字的范围
	private int actionY = 100;
	private int actionWidth = 128;		// 一行字最多有几个像素
	private int countSentence = 2;			// 每个阶段有几句话(15)
	*/
	// constants end

	private GameStrategy strategy;
	Graphics g;

	private String s0 = "";							// 要打的字
	private String string = "";						// 正在打的字
	private int counter;							// 打了多少个字

	private PhoneButton[] buttons = new PhoneButton[19];
	private PhoneButton currentButton;
					
	private Image imgGirl;
	private Image imgFace;
	private Image imgWrong, imgRight, imgStart, imgCross, 
					imgTimeBar, imgBackground, imgTimeHeart, imgNumber, imgFontFrame, imgTime;
	private Image imgComplete;
	private int countLife = 1;
	private int life = countLife;
	private boolean isBlackString = false;
	private boolean isFirst = true;
	private int totalTime = 950;
	private int time = totalTime;
	private int[] timeIncrease = { 150, 120, 100 };
	private int[] timeDecrease = { 100, 100, 100 };
	
	private int lineFrom = 0;			// 示范字从哪行开始显示
	private int lineNumber;				// 每次load的时候算出例句有几行
	private int remainCount;			// 剩下几个字没有打
	private int level = 0;
	private int caseFlag = 0;			// 大小写的flag

	private String[] ss = new String[countSentence];
	private int sentenceCounter;		// 每个阶段有十五句话,记录是到十五句的哪一句, ss[sentenceCounter]
	private final int COLOR_RED = 5;
	private final int COLOR_BLACK = 6;
	
	
	private int testCounter = 0;
}

⌨️ 快捷键说明

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