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

📄 gamescreen.java

📁 经典的超级玛丽游戏
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.*;
import java.io.DataInputStream;
import java.io.InputStream;
import java.util.Vector;
import java.lang.System;


public class GameScreen extends Canvas implements Runnable
{
	private supermario theMidlet;
	private World world;
	private Thread thread;
	private int keycode,releasedkeycode;
	
	public int currentViewPosX=90,currentViewPosY=88;//getWidth()/2=180/90,getHeight()/2=177/2;
	
	
	private long upstarttime=-1,upendtime=-1,upduration=-1,last_uptime=0;//开始按跳跃键,松开跳跃键,,按住向上键不放的持续时间
	private boolean upreleased=true;//是否松开跳跃键
	private static final int TIME_JUMP=500;
	private static final int SMALL_JUMP=0;
	private static final int BIG_JUMP=1;
	private boolean last_UP=false;
	private MySprite mariosprite;
	private int movespeed=2;
	
	private boolean uppress=false,downpress=false,firepress=false,rightpress=false,leftpress=false,walkdown=false;//walkdown:是否由于走到空地而向下掉
	
	private Image Image_ghost,Image_duck,Image_num,Image_bigmario,Image_smallmario,Image_bullet,Image_explode,Image_coin;
	public Vector npcs,nums,bullets;
	
	
	private NpcSprite npc_mushroom=null;//长大蘑菇
	
	public static int score=0,coinnum=0,mariolife=3;//分数,钱币数目,mario的生命数目
	
	private int npc_curnum=0,remaintime=400,gameovertime=0,mushroom_times=0,sleeptime=10;//游戏剩余时间
	
	private long starttime=0,endtime=0;
	
	private boolean showgameover=false;
	
	private Font f1 = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font. SIZE_MEDIUM);
	
	private String String_score,String_coinnum,String_mariolife,String_remaintime;//上方显示的内容
	
	public GameScreen(supermario midlet)
	{
		theMidlet=midlet;
		readMap(1);//读取地图
		world=new World(13,13,getWidth(), getHeight());//初始化地图	getWidth()=180	getHeight()=177
		try
		{
			Image_smallmario=Image.createImage("/smallmario.png");
			Image_bigmario=Image.createImage("/bigmario.png");
			Image_ghost=Image.createImage("/gf.png");
			Image_duck=Image.createImage("/duck.png");
			Image_num=Image.createImage("/num.png");
			Image_bullet=Image.createImage("/bullet.png");
			Image_explode=Image.createImage("/explode.png");
			Image_coin=Image.createImage("/coin.png");
		}catch(Exception ec){
			System.out.println(ec);
		}
		mariosprite=new MySprite(Image_smallmario,13,13,this);	
		npcs=new Vector();
		nums=new Vector();
		bullets=new Vector();
		thread = new Thread(this);
		thread.start();
	}
	public void run()
	{
		while(true) 
		{
			if(mariolife>0)//if(mariosprite.life==true)
			{
				if(mariosprite.life==true)//mario活着
				{
					if(endtime-starttime>=500)
					{
						starttime=System.currentTimeMillis();
						remaintime--;
					}
					action();
					npcaction();
					mn_action();
					repaint();
					try{
						Thread.sleep(sleeptime);
					}catch(InterruptedException e){}
					endtime=System.currentTimeMillis();
				}
				else
				{
					if(showgameover==false)
						mariodead();
					repaint();
				}
			}
		}
	}
	public void mariodead()//mario死亡动作
	{
		if(mariosprite.deadtime<10)//碰到怪兽死亡时,先停一下
			mariosprite.deadtime++;
		if(mariosprite.deadtime==10)//当停了10轮以后,设置跳跃效果
		{
			mariosprite.whichjump(3);
			mariosprite.bjump=true;
			mariosprite.t=0;
			mariosprite.deadtime++;
		}
		if(mariosprite.bjump==true)//跳跃
		{
			mariosprite.t++;
			mariosprite.jump();
		}
		
		if(mariosprite.deadtime>=20)//当掉到地下死亡时,停30轮后再减少生命
			mariosprite.deadtime++;
			
		if(mariosprite.deadtime==50||mariosprite.curspY>getHeight())//减少生命
		{
			mariolife--;
			showgameover=true;
			mariosprite.deadtime=15;
			mariosprite.bjump=false;
			mariosprite.xiajiang=false;
			mariosprite.deadtime=0;
			
			if(currentViewPosX<1160)
			{
				mariosprite.curspX=0;
				currentViewPosX=getWidth()/2;
			}
			else
			{
				mariosprite.curspX=38;
				currentViewPosX=1105;
			}
			mariosprite.curspY=143;
			mariosprite.ChangeSize(Image_smallmario,mariosprite.curspX,mariosprite.curspY,13,13,3);//3:纵坐标不变
			
			mariosprite.state=0;
			
			
			if(mariosprite.way==1)
				mariosprite.curFrame=0;
			else if(mariosprite.way==0)
				mariosprite.curFrame=6;
			
			mariosprite.y1=0;
			mariosprite.y2=0;
			mariosprite.y0=-1;
			mariosprite.stopcurspY=-1;
			//last_uptime=0;
					
			npcs.removeAllElements();
			npc_curnum=0;
			
			leftpress=false;
			rightpress=false;
			firepress=false;
			uppress=false;
			
			remaintime=400;
			
			readMap(1);//读取地图
		}
	}
	
	public void readMap(int world)//读取地图数据
	{
		int xx=0,yy=0;
		byte data[];
		byte xy[]=new byte[4];

		try{
			DataInputStream dis =new DataInputStream(getClass().getResourceAsStream("/"+world+".map"));
			//读入map数组的行列数目
			dis.read(xy);
			for(int j=0;j<4;)
			{
				int k=xy[j++];
				if(k<0)
					k=k+256;
				int l=xy[j++];
				if(l<0)
					l+=256;
				if(j==2)
					xx=k+(l<<8);
				else if(j==4)
					yy=k+(l<<8);
			}
			World.tileMap=new byte[xx][yy];
			
			//读取地图数据
			data=new byte[yy];
			for(int i=0;i<xx;i++)
			{
				dis.read(data);
				for(int j=0;j<yy;j++)
					World.tileMap[i][j]=data[j];
			}
			dis.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	protected void paint(Graphics g)
	{
		if(showgameover==false)
		{
			world.setView(currentViewPosX,currentViewPosY);
			g.setColor(255,255,255);
			g.fillRect(0,0,getWidth(),getHeight());
		
			world.render(g);
			mariosprite.setFrame(mariosprite.curFrame);
			mariosprite.draw(g);
		
			DrawNpcs(g);//画除了mario以外的所有角色
		
			DrawTop(g);//画分、钱币、面、时间
		}
		else
			DrawGameOver(g);//画mario死亡后的画面
		
		
	}
	public void DrawTop(Graphics g)
	{
		g.setClip(0,0,getWidth(),25);
		g.setFont(f1);
		
		//显示分
		String_score=Integer.toString(score);
		int len=6-String_score.length();
		for(int i=0;i<len;i++)
			String_score="0"+String_score;
		g.drawString("Score",10,0,Graphics.TOP|Graphics.LEFT);
		g.drawString(String_score,8,10,Graphics.TOP|Graphics.LEFT);
		
		//显示钱币数目
		String_coinnum=Integer.toString(coinnum);
		int coin_len=2-String_coinnum.length();
		for(int i=0;i<coin_len;i++)
			String_coinnum="0"+String_coinnum;
		g.setClip(59,12,50,10);
		g.drawImage(Image_coin,59,12,Graphics.TOP|Graphics.LEFT);
		g.drawString("x",69,10,Graphics.TOP|Graphics.LEFT);
		g.drawString(String_coinnum,77,10,Graphics.TOP|Graphics.LEFT);
		
		//显示生命数目
		String_mariolife=Integer.toString(mariolife);
		int mariolife_len=2-String_mariolife.length();
		for(int i=0;i<mariolife_len;i++)
			String_mariolife="0"+String_mariolife;
		g.setClip(96,10,50,13);
		g.drawImage(Image_smallmario,96,10,Graphics.TOP|Graphics.LEFT);
		g.drawString("x",113,10,Graphics.TOP|Graphics.LEFT);
		g.drawString(String_mariolife,121,10,Graphics.TOP|Graphics.LEFT);
		
		//显示时间
		String_remaintime=Integer.toString(remaintime);
		int remaintime_len=3-String_remaintime.length();
		for(int i=0;i<remaintime_len;i++)
			String_remaintime="0"+String_remaintime;
		g.setClip(145,0,50,25);
		g.drawString("Time",145,0,Graphics.TOP|Graphics.LEFT);
		g.drawString(String_remaintime,150,10,Graphics.TOP|Graphics.LEFT);
			
	}
	
	public void DrawGameOver(Graphics g)
	{
			gameovertime++;
			g.setClip(0,0,getWidth(),getHeight());
			g.setColor(0,0,0);
			g.fillRect(0,0,getWidth(),getHeight());
			g.setColor(255,255,255);
			//关
			g.drawString("World 1-1",getWidth()/2-23,60,Graphics.TOP|Graphics.LEFT);
			
			//生命
			String_mariolife=Integer.toString(mariolife);
			int mariolife_len=2-String_mariolife.length();
			for(int i=0;i<mariolife_len;i++)
				String_mariolife="0"+String_mariolife;
			g.setClip(getWidth()/2-18,90,50,13);
			g.drawImage(Image_smallmario,getWidth()/2-18,90,Graphics.TOP|Graphics.LEFT);
			g.drawString("x",getWidth()/2,90,Graphics.TOP|Graphics.LEFT);
			g.drawString(String_mariolife,getWidth()/2+10,90,Graphics.TOP|Graphics.LEFT);
			try{
				Thread.sleep(1000);
			}catch(InterruptedException e){}
			
			if(gameovertime==3)
			{
				showgameover=false;
				mariosprite.life=true;
				gameovertime=0;
			}
				
	}
	
	public void DrawNpcs(Graphics g)//画除了mario以外的所有角色
	{
		if(mariosprite.mushroom_show==true)//画长大蘑菇或花
		{
			int x=mariosprite.mushroom_x*13-currentViewPosX+getWidth()/2;
			int y=mariosprite.mushroom_y*13;
						
			if(mariosprite.state==0)//如果是small mario则画长大蘑菇
			{
				npc_mushroom=new NpcSprite(Image_ghost,x,y-mushroom_times,13,13,10,currentViewPosX,this);
				npc_mushroom.setFrame(4);
			}
			else if(mariosprite.state==1||mariosprite.state==2)//如果是big mario则画花
			{
				npc_mushroom=new NpcSprite(Image_ghost,x,y-mushroom_times,13,13,8,currentViewPosX,this);
				npc_mushroom.setFrame(6);
				
			}
			npc_mushroom.draw(g);
			
			g.setClip(x,y,13,13);
			g.drawImage(World.Image_tile,x-2*13,y,Graphics.TOP|Graphics.LEFT);
			
			mushroom_times++;
			if(mushroom_times==14)
			{
				mariosprite.mushroom_show=false;
				mushroom_times=0;
				npc_mushroom.npc_way=1;
				
				if(npc_mushroom.npc_kind==8)//如果是花
					npc_mushroom.npc_life=false;
				else//如果是长大蘑菇
					npc_mushroom.npc_life=true;
				npcs.addElement(npc_mushroom);
			}
		}		
		for(int i=0;i<npcs.size();i++)//画npc
		{
			NpcSprite npc=(NpcSprite)npcs.elementAt(i);
			if(npc.npc_dead>0)
				npc.draw(g);
			if(npc.npc_life==false&&npc.npc_kind==0)
				npc.npc_dead--;
		}
		for(int i=0;i<nums.size();i++)//画num
		{
			num num=(num)nums.elementAt(i);
			if(num.fentimes<=10)
				num.draw(g);
			else
				nums.removeElementAt(i);
		}
		
		for(int i=0;i<bullets.size();i++)//画子弹
		{
			bullet bul=(bullet)bullets.elementAt(i);
			bul.CanMove(currentViewPosX-getWidth()/2+bul.curspX,currentViewPosY-getHeight()/2+bul.curspY);
			if(bul.right_u_canmove==false||bul.left_u_canmove==false)
				bul.bul_life=false;
			else
				bul.shot(this);
			if(bul.bul_life==false)
				bul.explode_time-=6;
			if(bul.explode_time==0||bul.curspX<0||bul.curspX>getWidth()||bul.curspY>160||bul.curspY<0)
				bullets.removeElementAt(i);

			bul.draw(g,currentViewPosX);
		}
	}
	protected void keyPressed(int code)
	{
		keycode=getGameAction(code);
		
		if(mariosprite.life==true)
		{
		
			last_UP=false;
		
			if(keycode==Canvas.RIGHT)
			{
				rightpress=true;
				mariosprite.way=1;
			}
			if(keycode==Canvas.LEFT)
			{
				leftpress=true;
				mariosprite.way=0;
			}
			
			if(keycode==Canvas.UP&&mariosprite.bjump==false)
			{
				upstarttime=System.currentTimeMillis();
				last_uptime=0;
				upreleased=false;
				last_UP=true;
			}
			else
				last_UP=false;
		
			if(keycode==Canvas.FIRE)
			{
				firepress=true;
				if(bullets.size()<2&&mariosprite.state==2)
				{
					bullet bul=new bullet(Image_bullet,Image_explode,mariosprite.curspX,mariosprite.curspY,mariosprite.way,currentViewPosX);
					bullets.addElement(bul);
				}
			
			}
			if(keycode==Canvas.DOWN)
			{
				downpress=true;
			
				if(sleeptime==30)
					sleeptime=500;
				else
					sleeptime=30;
			}
			if(keycode==Canvas.UP)
			{
				uppress=true;
			}
		}
		
	}
	protected void keyReleased(int code)
	{
	 	keycode=-1000;
	 	releasedkeycode=getGameAction(code);
	 	
	 	if(mariosprite.life==true)
	 	{
		 	if(releasedkeycode==Canvas.UP&&upreleased==false)
		 	{
	 			upendtime=System.currentTimeMillis();
	 			upreleased=true;
		 		last_UP=false;
		 		if(mariosprite.bjump==false)

⌨️ 快捷键说明

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