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

📄 mystrategyimplement.java

📁 情话宝典
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.microedition.lcdui.*;
import java.io.*;

public class MyStrategyImplement implements StrategyImplement, Constants{
	public MyStrategyImplement( GameStrategy s ){
		strategy = s;
		initButton();
		try{
			imgWrong = Image.createImage( "/wrong.png" );
			imgRight = Image.createImage( "/right.png" );
			imgStart = Image.createImage( "/start.png" );
//			imgCross = Image.createImage( "/cross.png" );
			imgTimeBar = Image.createImage( "/timebar.png" );
			imgBackground = Image.createImage( "/bg_1.png" );
			imgTimeHeart = Image.createImage( "/timeHeart.png" );
			imgNumber = Image.createImage( "/number.png" );
			imgFontFrame = Image.createImage( "/fontFrame.png" );
			imgTime = Image.createImage( "/time.png" );
		}catch( Exception e ){
			e.printStackTrace();
		}
	}
	public void keyPressed( int keyCode ){
		try{

		if( keyCode == '1' ){
			releaseCurrentButton();
			if( caseFlag == 0 ) caseFlag = 11;
			else				caseFlag = 0;
			repaint();
		}
		else if( keyCode == '#' ){		// cancel
			counter = 0;
			releaseCurrentButton();
			if( string.length() == 0 ) return;
			string = string.substring( 0, string.length()-1 );
			isBlackString = false;
			repaint();
		}
		else if( isWordTooLong( string ) ){
			return;
		}
		else if( keyCode == '0' ){
			counter = 0;
			if( string.length() == 0 ) return;
			if( string.charAt(string.length()-1) == ' ' ) return;
			if( isWrong() ) return;
			releaseCurrentButton();
			string = string + " ";
			isBlackString = false;
			repaint();
		}
		else if( (keyCode >= '2' && keyCode <= '9') || keyCode == '*' ){
			counter = 0;
			if( currentButton.getNumber() != getButton(keyCode).getNumber() ){
				if( isWrong() ) return;
				currentButton = getButton( keyCode );
				currentButton.clear();
				string = string + currentButton.getChar();
				isBlackString = true;
				repaint();
			}
			else{
				currentButton.press();
				string = string.substring(0,string.length()-1) + currentButton.getChar();
				isBlackString = true;
				repaint();
			}
		}
		}
		catch( Exception e ){
			e.printStackTrace();
		}
	}
	public void keyReleased( int keyCode ){
	}
	public void init( int lev ){
		level = lev*5;
		initData( level );
		strategy.prepareOK();
	}
	public void deadStartReset(){
		time = totalTime;
		string = "";
	}
	public void restartReset(){
		life = countLife;
	}
	public void gameStart( Graphics g, int c ){
		if( c == 1 ) playStart();
		this.g = g;
		g.setFont( Font.getFont( 0, 0, 8 ) );
		drawBackground();
		drawTypingWord();
		drawBackgroundWord();
//		g.setColor( 255, 0, 0 );
//		String s = "Start";
//		int x = (width - g.getFont().stringWidth(s))/2;
//		int y = (height - g.getFont().getHeight() )/2;
//		g.drawString( s, x, y, 4|16 );
		g.drawImage( imgStart, (width-imgStart.getWidth())/2, (height-imgStart.getHeight())/2, 4|16 );
	}
	public void gameDead( Graphics g, int c ){
		if( c == 1 ) playDead();
		this.g = g;
		drawBackground();
		drawTypingWord();
		drawBackgroundWord();
		g.setColor( 255, 0, 0 );
		String s = "游戏失败";
		int x = (width - g.getFont().stringWidth(s))/2;
		int y = (height - g.getFont().getHeight() )/2;
		g.drawString( s, x, y, 4|16 );
	}
	public void gameCross( Graphics g, int c ){
		this.g = g;
//		time = totalTime;
		drawBackground();
		drawTimeBar();
		drawTypingWord();
		drawBackgroundWord();
//		g.drawImage( imgCross, (width-imgCross.getWidth())/2, (height-imgCross.getHeight())/2, 4|16 );
		int tt = 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 );
	}
	public void gameComplete( Graphics g, int c ){
		/*
		this.g = g;
		drawBackground();
		drawTypingWord();
		drawBackgroundWord();
		g.setColor( 255, 0, 0 );
		String s = "Complete";
		int x = (width - g.getFont().stringWidth(s))/2;
		int y = (height - g.getFont().getHeight() )/2;
		g.drawString( s, x, y, 4|16 );
		*/
		imgGirl = null;
		imgFace = null;
		System.gc();
		try{
			imgComplete = Image.createImage( "/complete.png" );
		}catch( Exception e ){
			e.printStackTrace();
		}
		g.drawImage( imgComplete, 0, 0, 4|16 );
	}
	public void gameAllDead( Graphics g, int c ){
		this.g = g;
		drawBackground();
		drawTypingWord();
		drawBackgroundWord();
		g.setColor( 255, 0, 0 );
		String s = "游戏结束";
		int x = (width - g.getFont().stringWidth(s))/2;
		int y = (height - g.getFont().getHeight() )/2;
		g.drawString( s, x, y, 4|16 );
	}
	public void gamePause( Graphics g ){
		this.g = g;
		drawBackground();
		drawTypingWord();
		drawBackgroundWord();
		g.setColor( 0, 0, 255 );
		String s = "游戏暂停";
		int x = (width - g.getFont().stringWidth(s))/2;
		int y = (height - g.getFont().getHeight() )/2;
		g.drawString( s, x, y, 4|16 );
	}
	public void testGamePause( Graphics g, int state ){
		this.g = g;
		drawBackground();
		drawTypingWord();
		drawBackgroundWord();
		g.setColor( 0, 0, 255 );
		testCounter = (testCounter+1)&0xfff;
		String s = "游戏暂停 tmpState:" + state + " counter:" + testCounter;
		int x = (width - g.getFont().stringWidth(s))/2;
		int y = (height - g.getFont().getHeight() )/2;
		g.drawString( s, x, y, 4|16 );
	}
	public void gameLoop( Graphics g, int c ){
		loop();
	}

	private void loop(){
		if( isSelecting() ){
			counter++;
			if( counter >= 8 ){
				counter = 0;
				releaseCurrentButton();
				isBlackString = false;
			}
		}
		time--;
		if( (time & 0x3) == 0 ) repaint();
		if( time <= 0 ){
			dead();
		}
		repaint2();
	}

	private void repaint(){
	}

	private void repaint2(){
		try{
		g = strategy.getGraphics();
		g.setFont( Font.getFont( 0, 0, 8 ) );
		remainCount = s0.length() - string.length();
		if( isFirst ){
			isFirst = false;
			lineNumber = getLineNumber( g, s0, actionWidth );
		}
		if( (remainCount == 0 && !isSelecting()) || remainCount == -1 ){
			if( s0.equals(string) ){
				drawRight();
			}
			else{
				drawWrong();
			}
			return;
		}
		drawBackground();
//		drawIsUpperCase();
//		drawRemainCount();
		drawTimeBar();
		drawHeart();
		drawLife();
		drawTypingWord();
		drawBackgroundWord();
		}
		catch( Exception e ){
			e.printStackTrace();
		}
//		strategy.repaint();
//		strategy.serviceRepaints();
	}

	private void drawRight(){
//		increaseTime();
		System.out.println( "right" );
		g = strategy.getGraphics();
		drawBackground();
		drawTypingWord();
		drawBackgroundWord();
		drawTimeBar();
		drawLife();
		g.drawImage( imgRight, width-imgRight.getWidth(), actionY, 4|16 );
		sentenceCounter++;
		strategy.repaint();
		strategy.serviceRepaints();
		
		playRightSound();
		try{
			Thread.sleep( 1000 );
		}catch( InterruptedException e ){}

		if( sentenceCounter >= countSentence ){
//			level++;
			cross( level );
		}
		else{
//			sentenceComplete( sentenceCounter );
			string = "";
			lineFrom = 0;
			s0 = ss[sentenceCounter];
			totalTime = getTime( s0, level );
			time = totalTime;
		}
	}
	private void drawWrong(){
//		decreaseTime();
		System.out.println( "wrong" );
		g = strategy.getGraphics();
		drawBackground();
		drawTypingWord();
		drawBackgroundWord();
		drawTimeBar();
		drawLife();
		g.drawImage( imgWrong, width-imgWrong.getWidth(), actionY, 4|16 );
		strategy.repaint();
		strategy.serviceRepaints();
		try{
			Thread.sleep( 1000 );
		}catch( InterruptedException e ){}

//		sentenceComplete( sentenceCounter );
		string = string.substring( 0, string.length()-1 );
	}

	private void cross( int lev ){
		if( lev >= 14 ){
			strategy.complete();
			playComplete();
		}
		else if( level%5 == 4 ){
//			playChangeGirl();
			strategy.cross( lev/5 );
			
		}
		else{
			level++;
			lev = level;
//			playChangeFace();
			initData( lev );
		}
	}
	private void dead(){
		life--;
		if( life <= 0 ){
			strategy.allDead();
		}
		else{
			strategy.dead();
		}
	}

	private void initButton(){
		buttons[0] = new PhoneButton( '2', "abc" );		// 2
		buttons[1] = new PhoneButton( '3', "def" );		// 3
		buttons[2] = new PhoneButton( '4', "ghi" );		// 4
		buttons[3] = new PhoneButton( '5', "jkl" );		// 5
		buttons[4] = new PhoneButton( '6', "mno" );		// 6
		buttons[5] = new PhoneButton( '7', "pqrs" );	// 7
		buttons[6] = new PhoneButton( '8', "tuv" );		// 8
		buttons[7] = new PhoneButton( '9', "wxyz" );	// 9
		buttons[8] = new PhoneButton( '*', ",.!?\":';" );		// *
		buttons[9] = new PhoneButton( '0', " " );		// 0
		buttons[10] = new PhoneButton( '$', ":)" );		// tmp
		buttons[11] = new PhoneButton( '2', "ABC" );
		buttons[12] = new PhoneButton( '3', "DEF" );
		buttons[13] = new PhoneButton( '2', "GHI" );
		buttons[14] = new PhoneButton( '3', "JKL" );
		buttons[15] = new PhoneButton( '2', "MNO" );
		buttons[16] = new PhoneButton( '3', "PQRS" );
		buttons[17] = new PhoneButton( '2', "TUV" );
		buttons[18] = new PhoneButton( '3', "WXYZ" );
		currentButton = buttons[10];
	}
	private PhoneButton getButton( int i ){
		switch( i ){
			case '2':	return buttons[0+caseFlag];
			case '3':	return buttons[1+caseFlag];
			case '4':	return buttons[2+caseFlag];
			case '5':	return buttons[3+caseFlag];
			case '6':	return buttons[4+caseFlag];
			case '7':	return buttons[5+caseFlag];
			case '8':	return buttons[6+caseFlag];
			case '9':	return buttons[7+caseFlag];
			case '*':	return buttons[8];
			case '0':	return buttons[9];
			default:	return buttons[10];
		}
	}

	private void initData( int lev ){
		System.out.println( "initData: " + lev );
		try{

			System.gc();
			System.out.print( "before: " );
			System.out.println( Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory() );
			imgGirl = null;
			imgFace = null;
			System.gc();
			imgFace = Image.createImage( "/face_" + ((lev%10)+1) + ".png" );
			imgGirl = Image.createImage( "/girl_" + ((lev/5)+1) + ".png" );
			System.out.print( "after: " );
			System.out.println( Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory() );
			Thread.sleep( 500 );
		}
		catch( Exception e ){
			e.printStackTrace();
		}
		string = "";
		counter = 0;
		sentenceCounter = 0;
		ss = getSentences( lev );
		s0 = ss[0];
		totalTime = getTime( s0, lev );
		time = totalTime;
		lineFrom = 0;
		isBlackString = false;
		isFirst = true;
		caseFlag = 0;
		releaseCurrentButton();
	}
	
//	private void sentenceComplete( int c ){
//		string = "";
//		s0 = ss[c]; 
//	}

	private void releaseCurrentButton(){
		currentButton = buttons[10];
	}

⌨️ 快捷键说明

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