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

📄 worldcanvas.java

📁 J2me横版动作游戏源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	protected Image itemp;
	protected ImageInfo iitemp;
	
	protected int getType(long l){
		return (int)l/100000;
	}
	protected static ImageInfo findImageInfo(long id){
		ImageInfo temp=(ImageInfo)GameRes.find(id);
		if(temp!=null){
			return temp;
		}
		else{
			return null;
		}
	}
	public static ActionInfo findAction(long id){
		ActionInfo temp=(ActionInfo)GameRes.find(id);
		if(temp!=null){
			return temp;
		}
		else{
			return (ActionInfo)GameRes.find(id-(id%10));
		}
	}
	protected Image creature=null;
	protected void LoadCreatureImage(){
			if((resstate&CreatureImages)!=0){
				return;
			}
			else{
				resstate|=CreatureImages;
			}
			try {
				creature=Image.createImage(pngpath+"creature"+".png");
			} catch (IOException e) {
				e.printStackTrace();
			}
			int imagex=creature.getWidth();
			int imagey=creature.getHeight();
			long id=0;
			int xmod=0;
			int ymod=0;
			int width=0;
			int height=0;
			int centermod=0;
			byte Btemp[]=readFile(respath,"creimginfo");
			char cha=0;
			int scount=0;
			boolean key=false;
			for(int i=0;i<Btemp.length-1;i+=2){
				cha=CharOP.readUnicode(Btemp[i],Btemp[i+1]);
				if(cha==32){
					scount++;
					key=true;
				}
				else if(i<Btemp.length-1&&cha==13&&CharOP.readUnicode(Btemp[i+2],Btemp[i+3])==10){
					scount++;
					i+=2;
					key=true;
				}
				else if(cha==35){
					break;
				}
				if(key==true){
					switch(scount){
					case 1:
						id=CharOP.getIong();
						break;
					case 2:
						xmod=CharOP.getInt();
						break;
					case 3:
						ymod=CharOP.getInt();
						break;
					case 4:
						width=CharOP.getInt();
						break;
					case 5:
						height=CharOP.getInt();
						break;
					case 6:
						centermod=CharOP.getInt();
						scount=0;
						GameRes.insert(new ImageInfo(id,xmod,ymod,width,height,centermod-xmod));
						GameRes.insert(new ImageInfo(id-100,imagex-xmod-width,ymod+imagey/2,width,height,imagex-centermod-(imagex-xmod-width)));
						break;
					}
					key=false;
				}
				else{
					CharOP.record(cha);
				}
			}
		
	}
	protected Image ground=null;
	protected Image effect=null;
	protected void LoadGroundImage(){
			if((resstate&GroundImages)!=0){
				return;
			}
			else{
				resstate|=GroundImages;
			}
			try {
				effect=Image.createImage(pngpath+"effect"+".png");
				ground=Image.createImage(pngpath+"ground"+".png");
			} catch (IOException e) {
				e.printStackTrace();
			}
			long id=0;
			int xmod=0;
			int ymod=0;
			int width=0;
			int height=0;
			int centermod=0;
			byte Btemp[]=readFile(respath,"groeffimginfo");
			char cha=0;
			int scount=0;
			boolean key=false;
			for(int i=0;i<Btemp.length-1;i+=2){
				cha=CharOP.readUnicode(Btemp[i],Btemp[i+1]);
				if(cha==32){
					scount++;
					key=true;
				}
				else if(i<Btemp.length-1&&cha==13&&CharOP.readUnicode(Btemp[i+2],Btemp[i+3])==10){
					scount++;
					i+=2;
					key=true;
				}
				else if(cha==35){
					break;
				}
				if(key==true){
					switch(scount){
					case 1:
						id=CharOP.getIong();
						break;
					case 2:
						xmod=CharOP.getInt();
						break;
					case 3:
						ymod=CharOP.getInt();
						break;
					case 4:
						width=CharOP.getInt();
						break;
					case 5:
						height=CharOP.getInt();
						break;
					case 6:
						centermod=CharOP.getInt();
						scount=0;
						GameRes.insert(new ImageInfo(id,xmod,ymod,width,height,centermod-xmod));
						break;
					}
					key=false;
				}
				else{
					CharOP.record(cha);
				}
			}
		
	}
	protected void LoadGameAction(){
			if((resstate&Actions)!=0){
				return;
			}
			else{
				resstate|=Actions;
			}
			byte Btemp[]=readFile(respath,"action");
			char cha=0;
			int scount=-1;
			int ifend=0;
			long temp[]=new long[10];
			boolean key=false;
			for(int i=0;i<Btemp.length-1;i+=2){
				cha=CharOP.readUnicode(Btemp[i],Btemp[i+1]);
				if(cha==32){
					scount++;
					key=true;
				}
				else if(i<Btemp.length-1&&cha==13&&CharOP.readUnicode(Btemp[i+2],Btemp[i+3])==10){
					scount++;
					ifend=1;
					i+=2;
					key=true;
				}
				else if(cha==35){
					break;
				}
				if(key==true){
					switch(ifend){
					case 0:
						temp[scount]=CharOP.getIong();
						break;
					case 1:
						temp[scount]=CharOP.getIong();
						long action1[]=new long[scount];
						long action2[]=new long[scount];
						for(int n=0;n<scount;++n){
							action1[n]=temp[n+1];
							action2[n]=temp[n+1]-100;
						}
						GameRes.insert(new ActionInfo(temp[0],action1));
						GameRes.insert(new ActionInfo(temp[0]-100,action2));
						scount=-1;
						ifend=0;
						break;
					}
					key=false;
				}
				else{
					CharOP.record(cha);
				}
			}
	}
	protected void releaseGameRes(){
		ground=null;
		creature=null;
		GameRes.clear();
		resstate=0;
		System.gc();
	}
	protected static int wordnum=20;
	protected static String syswords[]=null;
	protected void loadwords(){
		if(syswords==null){
			syswords=new String[wordnum];
			byte temp[]=readFile(respath,"string");
			char cha=0;
			int scount=0;
			int ncount=0;
			boolean key=false;
			for(int i=0;i<temp.length-1;i+=2){
				cha=CharOP.readUnicode(temp[i],temp[i+1]);
				if(cha==32){
					scount++;
					key=true;
				}
				else if(i<temp.length-1&&cha==13&&CharOP.readUnicode(temp[i+2],temp[i+3])==10){
					scount++;
					i+=2;
					key=true;
				}
				else if(cha==35){
					return;
				}
				if(key==true){
					switch(scount){
					case 1:
						CharOP.getInt();
						break;
					case 2:
						syswords[ncount]=new String(CharOP.getChars());
						scount=0;
						ncount++;
						if(ncount>=wordnum){
							System.out.println("words out of bouns");
							return;
						}
						break;
					}
					key=false;
				}
				else{
					CharOP.record(cha);
				}
			}
		}
	}
	
	public static String getString(int id){
		if(id<wordnum){
			return syswords[id];
		}
		else{
			return "";
		}
	}
	protected void perpare_Res(){
		loadwords();
		GameRes=new AVLTree();
	}
	
	protected InputStream is;
	protected final String respath="/resourse/"; 
	protected final String mappath="/map/";
	protected final String objectpath="/object/";
	protected byte buff[];
	public final byte[] readFile(String path,final String sname){
		if(buff==null){
			buff=new byte[2048];
		}
		is=getClass().getResourceAsStream(path+sname+".txt");
		try {
			is.read(buff);
			is.close();
			is=null;
			System.gc();
		} catch (IOException e) {
			e.printStackTrace();
		}
		for(int i=2;i<buff.length;){
			if(buff[i]==0&&buff[i+1]==0){
				buff[i-2]=buff[i];
				buff[i-1]=buff[i+1];
				break;
			}
			buff[i-2]=buff[i];
			buff[i-1]=buff[i+1];
			i+=2;
		}
		return buff;
	}
	
	//TODO Sound
	boolean enable_Sound=false;
//	public void perpare_Sound(){
//		
//	}
	//TODO Render
	protected Canvas can=null;
	protected static Image ImageBuff=null;
	protected Graphics GIB=null;
	
	protected void clear_Ren(){
		GIB.setClip(0,0,ImageBuffWidth,ImageBuffHeight);
		GIB.setColor(R,G,B);
		GIB.fillRect(0,0,ImageBuffWidth,ImageBuffHeight);
	}
	protected void refashionImageBuff(){
		if(ImageBuff==null){
			ImageBuff=Image.createImage(ImageBuffWidth,ImageBuffHeight);
			GIB=ImageBuff.getGraphics();
			GIB.setColor(this.R,this.G,this.B);
			GIB.fillRect(0,0,ImageBuffWidth,ImageBuffHeight);
		}
	}
	protected static void flushGraphics(Graphics g){
		g.setClip(0,0,ImageBuffWidth,ImageBuffHeight);
		g.drawImage(ImageBuff,0,0,0);
	}
	protected void perpare_Ren(){
		can=this;
		refashionImageBuff();
	}
	protected void repaint_Ren(){
		can.repaint();
		can.serviceRepaints();
	}
	int drawx=0;
	int drawy=0;
	//protected void 
	protected void drawgame(long mapid,int x,int y){
		int type=getType(mapid);
		switch(type){
		case 1:
			itemp=creature;
			break;
		case 2:
			itemp=ground;
			break;
		case 3:
			itemp=effect;
			break;
		default:
			return;
		}
		iitemp=findImageInfo(mapid);
		if(iitemp==null){
			System.out.println(mapid+" "+"not"+" "+"found");
			return;
		}
		drawx=x-screenx-iitemp.centermod;
		drawy=y-screeny-iitemp.height;
		GIB.setClip(drawx,drawy,iitemp.width,iitemp.height);
		GIB.drawImage(itemp,drawx-iitemp.xmod,drawy-iitemp.ymod,0);
		itemp=null;
	}
	protected void drawsys(long mapid,int x,int y){
		int type=getType(mapid);
		switch(type){
		case 1:
			itemp=creature;
			break;
		case 2:
			itemp=ground;
			break;
		case 3:
			itemp=effect;
			break;
		default:
			return;
		}
		iitemp=findImageInfo(mapid);
		if(iitemp==null){
			System.out.println(mapid+" "+"not"+" "+"found");
			return;
		}
		drawx=x-iitemp.centermod;
		drawy=y-iitemp.height;
		GIB.setClip(drawx,drawy,iitemp.width,iitemp.height);
		GIB.drawImage(itemp,drawx-iitemp.xmod,drawy-iitemp.ymod,0);
		itemp=null;
	}
	//定义
	protected static int ImageBuffWidth=176;
	protected static int ImageBuffHeight=208;
	protected int R=222;
	protected int G=222;
	protected int B=214;
	//TODO EngineSetting
	protected void perform_EngineSetting(){
		control_EngineSetting();
		draw_EngineSetting();
	}
	protected void draw_EngineSetting(){
		GIB.setColor(160,160,147);
		GIB.fillRect(36,73,100,26);
		Font_big(GIB,222,222,214);
		GIB.drawString(getString(0),45,76,0);
		Font_middle(GIB,160,160,147);
		GIB.drawString(getString(1),4,188,0);
		GIB.drawString(getString(2),146,188,0);
	
	}
	protected void control_EngineSetting(){
		int i=get_Key();
		if(enableControl){
			if(keyIs(i,LSB_G)){
				pressed_Button(1,0);
			}
			else if(keyIs(i,RSB_G)){
				pressed_Button(2,0);
			}
		}
		
	}
	//TODO game
	protected int Level=0;//0-2
	protected static byte State_Game=0;
	protected final byte Logo_Game=0;
	protected final byte MineMenu_Game=1;
	protected final byte Load_Game=2;
	protected final byte Start_Game=3;
	protected final byte Pause_Game=4;
	protected final byte List_Game=5;
	protected final byte System_Game=6;
	protected boolean enableControl=true;
	protected int recordtime=0;
	protected void init_Game(){
		State_Game=Start_Game;
		Level=0;
	}
	protected void release_Game(){
		release_Object();
	}
	protected void control_Game(){
		int i=get_Key();
		if(enableControl){
			switch(State_Game){
			case Logo_Game:
				control_Logo(i);
				break;
			case MineMenu_Game:
				control_MineMenu(i);
				break;
			case Load_Game:
				control_LoadGame(i);
				break;
			case Start_Game:
				control_player(i);
				break;
			case Pause_Game:
				Pause_Control(i);
				break;
			case List_Game:
				break;
			case System_Game:
				GScontrol(i);
				break;
			}
		}
	}
	protected void perform_Game(){
		switch(State_Game){
		case Logo_Game:
			perform_Logo();
			break;
		case MineMenu_Game:
			perform_MineMenu();
			break;
		case Load_Game:
			LoadGame();
			break;
		case Start_Game:
			perform_background();
			perform_Object();
			damageProcess();
			showDam();
			perform_player();
			break;
		case Pause_Game:
			Pause_Show();
			break;
		case List_Game:
			break;
		case System_Game:
			GSshow();
			break;
		}
	}
	//TODO gamesys
	protected byte gsnum=0;
	protected byte gsbegin=0;
	protected byte gsend=3;
	
	protected void GSenter(){
		if(player.hp<=0){
			gsbegin=1;
			gsnum=1;
		}
		else{
			gsbegin=0;
			gsnum=0;
		}
		State_Game=System_Game;
	}
	protected void GSresume(){
		State_Game=Start_Game;
	}
	protected void GSshow(){
		switch(gsnum){
		case 0:GIB.setColor(138,138,138);GIB.fillRect(61,128,50,12);break;
		case 1:GIB.setColor(138,138,138);GIB.fillRect(61,142,50,12);break;
		case 2:GIB.setColor(138,138,138);GIB.fillRect(61,156,50,12);break;
		case 3:GIB.setColor(138,138,138);GIB.fillRect(61,170,50,12);break;
		}
		Font_middle(GIB,82,82,82);
		GIB.drawString(getString(10),63,126,0);
		Font_middle(GIB,82,82,82);
		GIB.drawString(getString(11),63,140,0);
		Font_middle(GIB,82,82,82);
		GIB.drawString(getString(5),63,154,0);
		Font_middle(GIB,82,82,82);
		GIB.drawString(getString(13),63,168,0);
	}
	
	protected void GScontrol(int i){
		if(keyIs(i,UP_G)){
			gsnum--;
			if(gsnum<gsbegin){
				gsnum=gsbegin;
			}
		}
		if(keyIs(i,DOWN_G)){
			gsnum++;
			if(gsnum>gsend){
				gsnum=gsend;
			}	
		}
		if(keyIs(i,FIRE_G)){
			switch(gsnum){
			case 0:pressed_Button(11,0);break;
			case 1:pressed_Button(12,0);break;
			case 2:break;
			case 3:pressed_Button(13,0);break;
			}
		}
	}
	//TODO pause
	protected byte scopy=0;
	protected byte cp=0;
	public void Pause(){
		if(State_Engine!=S_Game){
			return;
		}
		scopy=State_Game;
		cp=0;
		State_Game=Pause_Game;
	}
	public void resume(){

⌨️ 快捷键说明

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