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

📄 mousesprite.java

📁 《Java程序设计实践教程》代码
💻 JAVA
字号:
/***************************************************
*  程序文件名称: MouseSprite.java
*  功能:构造地鼠精灵
***************************************************/
 import java.io.*;
 import java.util.*; 
 import javax.microedition.lcdui.*;
 import javax.microedition.lcdui.game.*;
 
 public class MouseSprite extends Sprite implements Runnable
 {
 	private int 	level;
 	private long 	score, recordScore;
 	private int 	shuX, shuY, count;
 	private int 	[]shunXu;
 	private Random 	rand;
 	private boolean sleeping, gameOver, newLevel;
 	private Thread 	mouseT;
 	private int 	beatMouse;
 	
 	public MouseSprite( Image image, int frameWidth, int frameHeight )
 	{
 		//地鼠精灵贴片图像及精灵的宽度与高度
 		super(image, frameWidth, frameHeight );
		//设置初始值
		initGame();
 	}
 
	//确定地鼠出现的初始位置
 	private void initS()
 	{
 		//定义数组
 		shunXu = new int[10*level];
 		//确定随机数,作为地鼠出现的位置
 		for( int i = 0; i < 10*level; i++ )
 		{
 			shunXu[i] = rand.nextInt()%5;			
 		}
 		
 	}
 
 //过关卡	
 private void checkPass()
 {
  if ( beatMouse >= ( 8*level ) &&  count == 10*level )
 	{
      level++;
   	  newLevel = true;
 			
 	  gameOver = false;
	}
  else
 	{
  	  gameOver = true;
 			
 	  newLevel = false;
 		
 	  sleeping = true;
 	}
  }
 	
	//设置初始值
  public void initGame()
 	{
 	    //定义线程
	    mouseT = new Thread(this);
 		//确定地鼠精灵x坐标
 		shuX = 42;
 		//确定地鼠精灵y坐标
 		shuY = 110;
 		//计数回零
 		count = 0;
		//第1关
		level = 1;
		//累计得分回零
		score = 0;
		//打中地鼠的次数
		beatMouse = 0;
		//地鼠精灵的状态(是否可见)
		sleeping = false;
		//允许进行游戏
		gameOver = false;
		//新的一关
		newLevel = false;
		//地鼠精灵坐标
		this.setPosition( shuX, shuY);
		//设定地鼠精灵不可见
		this.setVisible(false);
		//随机数
		rand = new Random();
 	}
 	
    //启动线程
 	public void start()
 	{
 		if(mouseT.isAlive())
 		{
 			sleeping = false;
 		}
 		else
 		{
 			mouseT.start();
 		}
 	}

	//运行线程
  public void run()
   {
 	while(!sleeping)
 	 {
 	  //取初始位置
	  initS();
 	  //计数回零
	  count = 0;
	  //改变地鼠出现的位置
 	  for ( int i = 0; i < 10*level; i++ )
 		{
 		 //移动地鼠出现的位置
		 moveTo(shunXu[i]);
 		  //根据关卡数加快地鼠出现的频率
 		 sleep((2000-100*(level-1)));
         //计数
 		 count++;	 			
 		}
 		//过关	
 	  checkPass();
 		//记录得分	
 	  recordScore = score;
 	}
  }
 	
 public void sleep( long time )
 {
   try
 	 {
 	   Thread.sleep(time);
     }
   catch( InterruptedException ie )
 	{ }
 }
 	
 	//移动地鼠精灵到指定的坐标位置
 public void moveTo( int s )
  {
 	//地鼠不可见
	this.setVisible(false);
	//暂停500毫秒
	this.sleep( 500 );
	//根据随机数,指定坐标位置
	switch(s)
	{
  	 case 1:
  	    shuX = 42;
  		shuY = 93;	
  		break;
  	 case 3:
  		shuX = 110;
  		shuY = 95;
  	 	break;
  	 case 0:
  		shuX = 180;
  		shuY = 92;
  		break;
  	 case 2:
  		shuX = 100;
  		shuY = 134;
  		break;
  	 case 4:
  		shuX = 120;
  		shuY = 73;
  		break;
  	}
  		//地鼠出现的位置		
  		this.setPosition( shuX, shuY);
  		//地鼠可见
  		this.setVisible(true);
  	}	
 	
 	//开始第1关
 	public void setLevel( int l )
 	{
 		level = l;
 	}
 	
    //打中地鼠的次数归零
 	public void initBeatMouse()
 	{
 		beatMouse = 0;
 	}
 	
	//打中了地鼠(精灵碰撞)
 	public void checkBeat()
 	{
 		this.setVisible(false);
 		beatMouse++;
 	}
 	
	//计分
 	public void setScore()
 	{
 		score = beatMouse*100; 
 	}
 	
	//过关
 	public int getLevel()
 	{
 		return level;
 	}
 	
 	//得分
	public long getScore()
 	{
 		return score;
 	}
 	
 	//继续游戏
	public boolean isGameOver( )
 	{
 		return gameOver;
 	}
 	
 	//新的1关
 	public boolean isNewLevel()
 	{ 
 		return newLevel;
 	}
 	
	//增加1关
 	public void setNewLevel(boolean b)
 	{
 		newLevel = b;	
 	}
 	
 	//暂停
 	public void stop()
 	{
 		sleeping = true;
 	}
 			
 }
 

⌨️ 快捷键说明

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