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

📄 gameview.java

📁 个人开发的第一个手机RPG游戏
💻 JAVA
字号:
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
import java.util.Random;
import javax.microedition.lcdui.*;



public class GameView extends GameCanvas implements Runnable ,CommandListener
{
	protected Graphics g;
	Font f;
	int bl;
	Hero hero;
	GameMap map,map2,mapf,map3,map4,gameover;
	Enemy enemy;
	LayerManager lm,lm2,lm3,lm4;
	Const cs;
	
	
	Command object,record,status,exit,run,magtic,normal;
	StringItem objectstring;
	
	
	Image herop,mapp,mappp;
	
	
	GameFigh gf;
	GameLogo gl;
	GameScene gs;
	
	
	static boolean lg=true;
	boolean ck=true;
	//static boolean commandselect=true;
	static int a;
	String you;
	String you2;
	String you3;

	
	

	public GameView()
	{
		super(true);
		try
		{
			herop=Image.createImage("/player.png");
			mapp=Image.createImage("/map.png");
			mappp=Image.createImage("/dd.png");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		hero=new Hero(herop,herop.getWidth()/3,herop.getHeight()/4);
		//hero2=new Hero(herop,herop.getWidth()/3,herop.getHeight()/4);
		
		enemy=new Enemy(herop,herop.getWidth()/3,herop.getHeight()/4);
		
		map=new GameMap(21,20,mapp,24,24);
		map2=new GameMap(21,20,mapp,24,24);
		mapf=new GameMap(10,14,mapp,24,24);
		map3=new GameMap(21,20,mappp,24,24);
		map4=new GameMap(21,20,mappp,24,24);
		gameover=new GameMap(10,13,mapp,24,24);
		
		
	
		
			 you="物品";
			 you2="状态";
			 you3="记录";
			
		
	
		
		object=new Command(you,Command.OK,1);
		status=new Command(you2,Command.OK,1);
		record=new Command(you3,Command.OK,1);
		
		exit=new Command("退出",Command.EXIT,1);
		
		
		run=new Command("逃跑 下键",Command.OK,1);
		normal=new Command("普通攻击 上键",Command.OK,1);
		magtic=new Command("魔法攻击",Command.OK,1);
		
		
		
		this.addCommand(object);
		this.addCommand(status);
		this.addCommand(record);
		this.addCommand(normal);
		this.addCommand(magtic);
		this.addCommand(run);
		//this.addCommand(magtic);
		this.addCommand(exit);
		
		this.setCommandListener(this);
		
		map.paintmap();
		map2.paintmap2();
		map3.paintmap3();
		map4.paintmap4();
		gameover.paintover();
		
		
		gf=new GameFigh(this);
		gf.cf();
		
		gs=new GameScene(this);
		gl=new GameLogo(this);
		cs=new Const(this);
		
		
		cm3();	
		cm2();
		cm();
		
		
	
		g=this.getGraphics();
	
		Thread t=new Thread(this);
		t.start();

	}
	
	public void cm()
	{
		lm=new LayerManager();
	    lm.append(hero);
	    lm.append(map2);
		lm.append(map);
	
	}
	public void cm2()
	{
		lm2=new LayerManager();
		lm2.append(hero);
		lm2.append(enemy);
		lm2.append(mapf);
	}
	public void cm3()
	{
		lm3=new LayerManager();
		lm3.append(hero);
		lm3.append(map4);
		lm3.append(map3);
		
	}
	public void cm4()
	{
		lm4=new LayerManager();
		lm4.append(gameover);
	}
	
	
	public int backpointx()
	{
		int sx;
		sx=Hero.x-this.getWidth()/2<0?0:Hero.x-this.getWidth()/2;
		sx=Hero.x+this.getWidth()/2+hero.getWidth()/3>480?480-this.getWidth():sx;
		return sx;
	}
	public int backpointy()
	{
		int sy;
		sy=Hero.y-this.getHeight()/2<0?0:Hero.y-this.getHeight()/2;
		sy=Hero.y+this.getHeight()/2+hero.getHeight()/4>480?480-this.getHeight():sy;
		return sy;		
	}
	public boolean collision(int x,int y)
	{
		if(!ck)
			return false;
		hero.setRefPixelPosition(x, y);
		
			
		return hero.collidesWith(backmap(), false);
		
	}
	public GameMap backmap()
	{
		
		if((Hero.x==0&&(Hero.y==234||Hero.y==235||Hero.y==236||Hero.y==237))||(a==1))	
			
				return map2;
			else
		
				return map4;
			
	}

	public LayerManager backmanager()
	{
		
		
		if(((Hero.x==0&&(Hero.y==234||Hero.y==235||Hero.y==236||Hero.y==237)))||(a==1))
                	return lm;
                else
                	
                	return lm3;
		 
	}
	public void backint()
	{
		
		if(backmanager()==lm3)
		
			a=0;
		
		else if(backmanager()==lm)
			a=1;
			
		
	}

	public void chufa()
	{
		int a;
		Random r;
		r=new Random();
		a=r.nextInt();
		
		
		backint();
		
	        if(this.a==1)
	        {
	        	if(a%50==0)
	    			
				{
		
				lg=false;

				}
	        }
			
			
			}
	
	public void run()
	{
		while(true)
		{
			
			if(GameLogo.cg==0)
			{
				
				
				gl.draw();
				gl.keyevent();
				GameLogo.exit=true;
			}
			else if(GameLogo.cg==1)
			{
				GameLogo.exit=true;
				
				chufa();
				if(lg==true)
					
					{
				
					gs.draw();
					gs.keyevent();
					cs.talkman();
						
					}
	
				
				else
					{
					
					gf.draw();
					gf.keyevent();
					gf.define();
					gf.enemyattact();
					gf.shengji();
					
					}
			}
			else if(GameLogo.cg==2)
			{
				
				GameLogo.exit=false;
				cs.statusdraw();
				
			}
			else if(GameLogo.cg==3)
			{
				
				GameLogo.exit=false;
				cs.objectdraw();
				cs.objectevent();
				
			}
			else if(GameLogo.cg==4)
			{
				
				GameLogo.exit=false;
				cs.recorddraw();
				cs.recordkeyevent();
				
			}
			else if(GameLogo.cg==5)
			{
				
				GameLogo.exit=false;
				cs.talkdraw();
			}
			else if(GameLogo.cg==6)
			{
				GameLogo.exit=false;
				
				g.setColor(0xffffff);
				g.fillRect(0, 0, this.getWidth(), this.getHeight());
				f=Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALIC, Font.SIZE_LARGE);
				g.setFont(f);
				g.setColor(0);
				g.drawString("来电等待", 100,100 , Graphics.BOTTOM|Graphics.HCENTER);
				flushGraphics();
			}
			else if(GameLogo.cg==7)
			{
				GameLogo.exit=false;
				g.setColor(0xffffff);
				g.fillRect(0, 0, this.getWidth(), this.getHeight());
				f=Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALIC, Font.SIZE_LARGE);
				g.setFont(f);
				g.setColor(0);
				g.drawString("只能在战斗中使用", 100,100 , Graphics.BOTTOM|Graphics.HCENTER);
				flushGraphics();
				
				
			}
			else if(GameLogo.cg==8)
			{
				gf.draw2();
			}
			else if(GameLogo.cg==9)
		{
			gf.draw3();
		}
			
	
		
		try
		{
			Thread.sleep(100);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		}
	}
	public void commandAction(Command c,Displayable s)
	{
		if(c==exit)
		{
			
			if(GameLogo.exit==true)
			{
				Midlet.ins.destroyApp(false);
				Midlet.ins.notifyDestroyed();
			}
			else
				GameLogo.cg=1;
		}
		else 
		{
			
			
			
		 if(c==object)
		 {
			 GameLogo.cg=3;
			 
		 }
				
		 if(c==status)
		{
			GameLogo.cg=2;
			
		}
		else if(c==record)
		{
			
			GameLogo.cg=4;
			
		}
		else if(c==normal)
		{
			if(lg==false)
			{
				GameLogo.cg=8;
				Enemy.hp=Enemy.hp-Hero.at;
				GameFigh.enemyred=GameFigh.enemyred/2;
				GameFigh.act=false;
				
				
			}
			else
			{
				GameLogo.cg=7;
				
			}
			
		}
		else if(c==magtic)
		{
			if(lg==false)
			{
				if(Hero.mp!=0)
				{
					GameLogo.cg=9;
					Enemy.hp=Enemy.hp-500;
					GameFigh.enemyred=0;
					
					GameFigh.act=false;
					Hero.mp--;
				}
				
				
			}
			else
			{
				GameLogo.cg=7;
				
			}
			
		}
		else if(c==run)
		{
			if(lg==false)
			{
				lg=true;
			}
			else
			{
				GameLogo.cg=7;
				
			}
			
		}
	
	}

		
	}
}
	
	


⌨️ 快捷键说明

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