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

📄 seconds_gamecanvas.java

📁 一个经典的射击游戏
💻 JAVA
字号:
package seconds;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.*;//yao yong dao graphics 
import javax.microedition.lcdui.game.*;//yao yong dao  TiledLayer


public class Seconds_GameCanvas extends GameCanvas implements Runnable,
		CommandListener {
	private Seconds_MIDlet sm;
	private Graphics g=this.getGraphics();
	private Image img_back;
	private Image img_bullet;
	private Image img_plane;
	private Image img_mesg;
	private Image img_bomb;
	private TiledLayer tl_back;
	private Sprite sp_plane;
	private Bullet bullet;
	private DrawMesg dm;
	private Bomb bomb;
	private int win_width=this.getWidth();
	private int win_height=this.getHeight();
	private boolean game_over=false;//控制游戏运行状态
	private int start_time;
	private int end_time=0;
	private int run_time;//运行时间
	//private int num_count;//炸弹个数
	private  int planeExp_count;//飞机爆炸个数
	private ImageTools it;
	private boolean running;
	private Command exitCommand;
	private Command startCommand;
	private Command restartCommand;
	private int plane[]={0,1,2,3};
	
    public Seconds_GameCanvas(Seconds_MIDlet sm)
    {
    	super(true);//不响应键盘按键
    	running=false;
    	this.sm=sm;
    	exitCommand=new Command("Exit",Command.EXIT,0);
    	startCommand=new Command("Start",Command.OK,1);
    	restartCommand=new Command("Restart",Command.OK,1);
    	 this.addCommand(exitCommand);
    	 this.addCommand(startCommand);
    	setCommandListener(this);
    	it=new ImageTools("/img_back.PNG");
    	 img_back=it.getImage();
    	  int img_back_width=img_back.getWidth();
    	 int img_back_height=img_back.getHeight();
    	int backCols=win_width/img_back_width+1;
    	int backCows=win_height/img_back_height+1;
       tl_back=new TiledLayer(backCols,backCows,img_back,img_back_width,img_back_height);
       for(int i=0;i<backCols*backCows;i++)
       {
    	   tl_back.setCell(i%backCols,i/backCols,1);
       }
        tl_back.paint(g);
        //子弹的绘制
        it=new ImageTools("/img_bullet.PNG");
        img_bullet=it.getImage();
        bullet=new Bullet(g,img_bullet,win_width,win_height);
        //完成信息绘制的 初始化
        it=new ImageTools("/img_mesg.PNG");
        img_mesg=it.getImage();
        dm=new DrawMesg(img_mesg,g);
        //完成炸弹的初始化
        it=new ImageTools("/img_bomb.PNG");
        img_bomb=it.getImage();
        bomb=new Bomb(g,img_bomb,20,win_width,win_height);
        //完成飞机的初始化
        it=new ImageTools("/img_plane.PNG");
        img_plane=it.getImage();
        sp_plane=new Sprite(img_plane,32,32);
        sp_plane.setFrameSequence(plane);
        sp_plane.setFrame(0);
        sp_plane.setPosition((win_width-32)/2,(win_height-32)/2);
        this.flushGraphics();
       
    	//startThread();
    	
    	
    }
    //创建一个启动线程的函数
    public void startThread()
    {
    	running=true;
    	Thread tt=new Thread(this);
    	tt.start();
    	
    }
    //创建一个结束线程的函数
    public void stopThread()
    {
    	running=false;
    }
	public void run() {
		// TODO 自动生成方法存根
		long st=0;
		long ed=0;
		long diff=0;
		int rate=110;
		
		while(true)
		{
			if(!game_over)
			
			{
				if(running=false)
					{
						break;
					}
					st=System.currentTimeMillis();
					
					render(g);
					gameKeyInput();
					bullet.paintBullet(sp_plane);
					bomb.paintBomb(sp_plane);
					this.planeExp_count=bullet.spExp_count+bomb.spExp_count;
					if(planeExp_count>20)
					{
						game_over=true;
					}
					end_time=(int)System.currentTimeMillis()/1000;
					run_time=end_time-start_time;
					System.out.print("run_time::");
					System.out.println(run_time);
				    if(run_time>90)
				    {
				    	game_over=true;
				    }
					dm.drawTime(run_time,3);
					dm.drawNum(planeExp_count);
					
					this.flushGraphics();
				
			}
			else
			{
				 for(int j=0;j<3;j++)
				 {
					 
					 sp_plane.nextFrame();
					 this.flushGraphics();
				 }
				 String tempStr="";
				 if(run_time<5)
				 {
					 tempStr="5秒就不到,太龌磋了吧?";
				 }
				 else if(run_time<10)
				 {
					 tempStr="10秒就不到,也太不好意思了吧? ";
				 }
				 else if(run_time<15)
				 {
					 tempStr="勉强10几秒,加油吧 ";
				 }
				 else if(run_time<20)
				 {
					 tempStr="是男人就努力突破20秒吧 ";
				 }
				 else if(run_time<90)
				 {
					 tempStr="你是个真正的男人 ";
				 }
				 g.setColor(0xff0000);
				 g.drawString(tempStr,win_width/3,win_height/3,0);
				 this.flushGraphics();
				 
			}
			ed=System.currentTimeMillis();
			diff=ed-st;
			if(diff<rate)
			{
				try
				{
					Thread.sleep(rate-diff);
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
			}
			
		}
		

	}
	//定义一个绘制背景的函数 
	public void render(Graphics g)
	{
		g.setColor(0x000000);
		g.fillRect(0,0,win_width,win_height);
		//sp_plane.nextFrame();
		
		sp_plane.paint(g);
		
		this.flushGraphics();
		
	}
	//定义一个响应键盘的函数
	public void gameKeyInput()
	{
		sp_plane.setFrame(0);
		int keyStates=this.getKeyStates();
		if((keyStates&UP_PRESSED)!=0)
		{
			if(sp_plane.getY()<20)
			{
				sp_plane.move(0,-sp_plane.getY());
			}
			else
			{
				sp_plane.move(0,-20);
			}
		}
		else if((keyStates&DOWN_PRESSED)!=0)
		{
			if(sp_plane.getY()+sp_plane.getHeight()+20>win_height)
			{
				sp_plane.move(0, win_height-sp_plane.getY()-sp_plane.getHeight());
			}
			else
			{
				sp_plane.move(0,20);
			}
		}
		else if((keyStates&LEFT_PRESSED)!=0)
		{
			if(sp_plane.getX()<20)
			{
				sp_plane.move(-sp_plane.getX(),0);
			}
			else
			{
				sp_plane.move(-20,0);
			}
		}
			
		else if((keyStates&RIGHT_PRESSED)!=0)
		{
			if(sp_plane.getX()+sp_plane.getWidth()+20>win_width)
			{
				sp_plane.move(win_width-sp_plane.getX()-sp_plane.getWidth(),0);
			}
			else
			{
				sp_plane.move(20,0);
			}
			
		}
		sp_plane.setFrame(0);
	}

	///实现游戏的初始化
	public void initGame()
	{
		 start_time=(int)System.currentTimeMillis()/1000;
		 run_time=0;
		 this.game_over=false;
		 this.planeExp_count=0;
		 //this.num_count=3;
		 
	}
	public void commandAction(Command c, Displayable arg1) {
		// TODO 自动生成方法存根
     if(c.getLabel().equalsIgnoreCase("Exit"))
     {
    	 stopThread();
    	 sm.quitApp();
     }
     else if(c==startCommand)
     {
    	 this.removeCommand(startCommand);
    	 this.addCommand(restartCommand);
    	 this.serviceRepaints();
    	 initGame();
    	 startThread();
     }
     else 
     {
    	 this.flushGraphics();
    	 //stopThread();
    	 initGame();
    	 
    	// startThread();
    	 
     }
	}

}

⌨️ 快捷键说明

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