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

📄 mysprite.java

📁 游戏超级玛丽的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.*;
import java.util.Vector;

//精灵
public class MySprite
{
	public int Frames=0,curFrame=0;
	public int spwidth,spheight;//一个精灵的宽度,高度
	private int spsrows,spscols;//所有精灵总的行数,列数
	private int cursprow,curspcol;//当前精灵所在的行号,列号
	public int curspX=0,curspY=143;//当前精灵的坐标
	public int spspeed=0;//精灵移动的速度
	//public boolean strong=false;//是big mario还是small mario
	private Image spsImage;
	private boolean visible=true;
	
	private int cos=50000,sin=86603;
	//private int cos=25882,sin=96593;
	//private int cos=0,sin=100000;
	private int v0=5,vx=v0*cos,vy=v0*sin,g=22;
	public boolean bjump=false;//是否正在跳跃

	public int t=0,tTemp=0;
	private int left_u_tile,left_m_tile,left_d_tile,right_u_tile,right_m_tile,right_d_tile,down_l_tile,down_r_tile,up_l_tile,up_r_tile,up_tile;//精灵周围的tile和精灵正上方的tile
	
	public boolean left_canmove=false,right_canmove=false,up_canmove=false,down_canmove=false;
	
	private boolean left_u_canmove=false,left_m_canmove=false,left_d_canmove=false,right_u_canmove=false,right_m_canmove=false,right_d_canmove=false,up_l_canmove=false,up_r_canmove=false,down_l_canmove=false,down_r_canmove=false;
	
	
	public int y1=0,y2=0,y0=-1,stopcurspY=-1;
	public int way=1;
	
	public boolean ding=false;
	public int flashxfirst,flashx,flashy,flashx0,flashy0,whichflash=-1,flashtime=0;
	
	private GameScreen gs;
	
	private Vector coin_bricks,coins;
	private Image Image_debris,Image_coin,Image_num;
	
	private int debris0_x,debris0_y,debris1_x,debris1_y,debris2_x,debris2_y,debris3_x,debris3_y;
	private int debris_x,debris_y,debris_v0=6,debris_cos=20000,debris_sin=86603,debris0_g=8,debris1_g=12,debris_t=0,debris_vx,debris_vy;
	private boolean is_debris=false,is_flash=false,has_coin_bricks=false;//是否砖爆炸,是否闪烁,钱砖vector中是否有这个位置的钱砖记录
	
	public int mario_coins=0;//mario顶到的钱
	
	
	public boolean life=true,xiajiang=false;//mario正在下降
	public boolean mushroom_show=false;//是否出现长大蘑菇
	public int mushroom_x,mushroom_y;//长大蘑菇的坐标
	public int state=0;//mario的状态:0-小,1-大,2-子弹,3-小无敌,4-大无敌,5-小大,6-大小
	public int statetimes;//mario状态的计数器
	public int deadtime=0;//死亡时跳跃时间
	
	public MySprite(Image sourceImage,int width,int height,GameScreen gs)
	{
		this.gs=gs;
		this.spsImage=sourceImage;
		this.spwidth=width;
		this.spheight=height;
		
		if(sourceImage.getWidth()%width==0&&sourceImage.getHeight()%height==0)
		{
			this.spsrows=sourceImage.getHeight()/height;
			this.spscols=sourceImage.getWidth()/width;
			this.Frames=spsrows*spscols;
		}
		else//图片宽度或高度不是精灵宽度或高度的整数倍!
		{
			System.out.println("Can't not create sprite!");
			this.Frames=0;
		}
		try
		{
			Image_debris=Image.createImage("/debris.png");
			Image_coin=Image.createImage("/coin.png");
			Image_num=Image.createImage("/num.png");
		}catch(Exception ec){
			System.out.println(ec);
		}
		debris_vx=debris_v0*debris_cos;
		debris_vy=debris_v0*debris_sin;
		coins=new Vector();
		coin_bricks=new Vector();
	}
	
	public void ChangeSize(Image sourceImage,int x,int y,int width,int height,int how)//mario改变大小;how(0:变小,1:变大)
	{
		this.spsImage=sourceImage;
		this.spwidth=width;
		this.spheight=height;
		if(sourceImage.getWidth()%width==0&&sourceImage.getHeight()%height==0)
		{
			this.spsrows=sourceImage.getHeight()/height;
			this.spscols=sourceImage.getWidth()/width;
			this.Frames=spsrows*spscols;
		}
		else//图片宽度或高度不是精灵宽度或高度的整数倍!
		{
			System.out.println("Can't not create sprite!");
			this.Frames=0;
		}
		if(how==0)
			this.curspY+=13;
		else if(how==1)
		{
			if(up_canmove==true)
				this.curspY-=13;
			else
			{
				this.curspY=y;
				t=-3;
				y1=0;
				y2=0;
				y0=-1;
				xiajiang=false;
				bjump=false;
			}
		}
	}
	
	public void whichjump(int i)//设置精灵跳跃的幅度
	{
		switch(i)
		{
			case(0)://小跳
				this.v0=8;
				this.g=42;
			break;
			
			case(1)://大跳
				this.v0=5;
				this.g=22;
			break;
			
			case(2)://踩怪物弹起
				this.v0=6;
				this.g=60;
			break;
							
			case(3)://死亡弹起
				this.v0=2;
				this.g=26;
			break;
		}
	}
	
	public void jump()//mario 跳跃
	{
		if(life==true)
		{
			switch(way)
			{
				case(1):
					curFrame=5;
				break;
				case(0):
					curFrame=11;
				break;
			}
		}
		if(t==2&&up_canmove==false)//当mario被两个不能通过的tile夹在中间时
		{
			t=-3;
			if(way==1)
				curFrame=5;
			else if(way==0)
				curFrame=11;
		}
		else
		{
			if(y0==-1)
				y0=curspY;
			y1=y2;
			y2=(vy*t-g*t*t*400)/100000;	
		
			stopcurspY=-1;//上升时可以跳到的最大高度(在大不能向上走了)
			if((y2-y1)>0)//上升
			{
				xiajiang=false;
				for(int i=0;i<(y2-y1);i++)
				{
					CanMove(gs.currentViewPosX-gs.getWidth()/2+curspX,gs.currentViewPosY-gs.getHeight()/2+curspY);
				
					if(up_canmove==true)
						curspY--;
					else
					{
						if(stopcurspY==-1)
							stopcurspY=curspY;
						while(curspY<=stopcurspY)
						{
							t+=2;
							y1=y2;
							y2=(vy*t-g*t*t*400)/100000;	
							curspY-=(y2-y1);
						}
					}
				}
			}
			else//下降
			{
				xiajiang=true;
				for(int i=0;i<(y1-y2);i++)
				{
					CanMove(gs.currentViewPosX-gs.getWidth()/2+curspX,gs.currentViewPosY-gs.getHeight()/2+curspY);
				
					if(down_canmove==true)
						curspY++;
					else
					{
						t=-3;
						y1=0;
						y2=0;
						y0=-1;
						xiajiang=false;
						
						if(curspY>143&&life==true)
							curspY=143;
						if(way==1)
							curFrame=0;
						else if(way==0)
							curFrame=6;
						break;
					}
				}
			}
		}
	}
	
	public void CanMove(int x,int y)
	{
		if((y+spheight)>168)
			y=168-spheight;
			
			
		//达到sprite周围的tile
		
		/*严丝合缝
		this.left_u_tile=World.tileMap[y/World.TILE_HEIGHT][(x-1)/World.TILE_WIDTH];
		this.left_d_tile=World.tileMap[(y+spheight-1)/World.TILE_HEIGHT][(x-1)/World.TILE_WIDTH];
		
		this.right_u_tile=World.tileMap[y/World.TILE_HEIGHT][(x+spwidth)/World.TILE_WIDTH];
		this.right_d_tile=World.tileMap[(y+spheight-1)/World.TILE_HEIGHT][(x+spwidth)/World.TILE_WIDTH];
		
		this.up_l_tile=World.tileMap[(y-1)/World.TILE_HEIGHT][x/World.TILE_WIDTH];
		this.up_r_tile=World.tileMap[(y-1)/World.TILE_HEIGHT][(x+spwidth-1)/World.TILE_WIDTH];
		
		this.down_l_tile=World.tileMap[(y+spheight)/World.TILE_HEIGHT][x/World.TILE_WIDTH];
		this.down_r_tile=World.tileMap[(y+spheight)/World.TILE_HEIGHT][(x+spwidth-1)/World.TILE_WIDTH];
		*/
		
		//上下各减少一个像素
		this.left_u_tile=World.tileMap[y/World.TILE_HEIGHT][(x-1)/World.TILE_WIDTH];
		this.left_m_tile=World.tileMap[(y+spheight/2-1)/World.TILE_HEIGHT][(x-1)/World.TILE_WIDTH];
		this.left_d_tile=World.tileMap[(y+spheight-1)/World.TILE_HEIGHT][(x-1)/World.TILE_WIDTH];
		
		this.right_u_tile=World.tileMap[y/World.TILE_HEIGHT][(x+spwidth)/World.TILE_WIDTH];
		this.right_m_tile=World.tileMap[(y+spheight/2-1)/World.TILE_HEIGHT][(x+spwidth)/World.TILE_WIDTH];
		this.right_d_tile=World.tileMap[(y+spheight-1)/World.TILE_HEIGHT][(x+spwidth)/World.TILE_WIDTH];
		
		this.up_l_tile=World.tileMap[(y-1)/World.TILE_HEIGHT][(x+1)/World.TILE_WIDTH];
		this.up_r_tile=World.tileMap[(y-1)/World.TILE_HEIGHT][(x+spwidth-2)/World.TILE_WIDTH];
		
		this.down_l_tile=World.tileMap[(y+spheight)/World.TILE_HEIGHT][(x+1)/World.TILE_WIDTH];
		this.down_r_tile=World.tileMap[(y+spheight)/World.TILE_HEIGHT][(x+spwidth-2)/World.TILE_WIDTH];
		
		//假设各个方向均不能移动
		this.left_canmove=false;
		this.left_u_canmove=false;
		this.left_m_canmove=false;
		this.left_d_canmove=false;
		
		this.right_canmove=false;
		this.right_u_canmove=false;
		this.right_m_canmove=false;
		this.right_d_canmove=false;
		
		this.up_canmove=false;
		this.up_l_canmove=false;
		this.up_r_canmove=false;
		
		this.down_canmove=false;
		this.down_l_canmove=false;
		this.down_r_canmove=false;
		
		//判断那些方向可以移动
		for(int i=0;i<World.CANPASS_TILE.length;i++)
		{
			
			if(this.left_u_tile==World.CANPASS_TILE[i])
				this.left_u_canmove=true;
			if(this.left_m_tile==World.CANPASS_TILE[i])
				this.left_m_canmove=true;
			if(this.left_d_tile==World.CANPASS_TILE[i])
				this.left_d_canmove=true;
			
			if(this.right_u_tile==World.CANPASS_TILE[i])
				this.right_u_canmove=true;
			if(this.right_m_tile==World.CANPASS_TILE[i])
				this.right_m_canmove=true;
			if(this.right_d_tile==World.CANPASS_TILE[i])
				this.right_d_canmove=true;
				
			if(this.up_l_tile==World.CANPASS_TILE[i])
				this.up_l_canmove=true;
			if(this.up_r_tile==World.CANPASS_TILE[i])
				this.up_r_canmove=true;
			
			if(this.down_l_tile==World.CANPASS_TILE[i])
				this.down_l_canmove=true;
			if(this.down_r_tile==World.CANPASS_TILE[i])
				this.down_r_canmove=true;
		}
		if(left_u_canmove==true&&left_m_canmove==true&&left_d_canmove==true)
			this.left_canmove=true;
		if(right_u_canmove==true&&right_m_canmove==true&&right_d_canmove==true)
			this.right_canmove=true;
		if(up_l_canmove==true&&up_r_canmove==true&&curspY>0)
			up_canmove=true;
		if(down_l_canmove==true&&down_r_canmove==true)
			down_canmove=true;
		
		
		if(up_canmove==false&&life==true)//如果mario的上方的tile不能通过,并且mario是活的时
		{
			//得到mario正上方的tile
			this.up_tile=World.tileMap[(y-1)/World.TILE_HEIGHT][(x+1+6)/World.TILE_WIDTH];
			if(ding==false&&bjump==true)
			{
				if(this.up_tile==1||this.up_tile==2||this.up_tile==17||this.up_tile==28)//1:砖,2:钱,17:钱砖,28:长大蘑菇或花
				{
					whichflash=this.up_tile;
			
					flashx0=(x+1+6)/World.TILE_WIDTH;
					flashy0=(y-1)/World.TILE_HEIGHT;
				
					flashxfirst=((x+1+6)/World.TILE_WIDTH)*13;
					flashy=((y-1)/World.TILE_HEIGHT)*13-gs.currentViewPosY+gs.getHeight()/2;

⌨️ 快捷键说明

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