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

📄 bomb.java

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

import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.*;//要用到graphics 
import java.util.*;//要用到Random
public class Bomb extends Sprite {
	private Graphics g;
	private int bomb[][];
	private Random rd=new Random();
	private int win_width,win_height;
	public int spExp_count=0;
    public Bomb(Graphics g,Image img_bomb,int num,int win_width,int win_height)
    {
    	super(img_bomb,16,16);
    	this.g=g;
    	this.win_width=win_width;
    	this.win_height=win_height;
    	bomb=new int [num][5];
    	initBomb();
    	
    }
    //完成炸弹的初始化
    public void initBomb()
    {
    	for(int i=0;i<bomb.length;i++)
    	{
    		bomb[i][0]=(rd.nextInt()>>>1)%4;
    		switch(bomb[i][0])
    		{
    		  case 0:
    			  bomb[i][1]=0;
    			  bomb[i][2]=(rd.nextInt()>>>1)%win_height;
    			  bomb[i][3]=(rd.nextInt()>>>1)%5+1;
    			  bomb[i][4]=(rd.nextInt()>>>1)%5;
    			  break;
    		  case 1:
    			  bomb[i][1]=win_width;
    			  bomb[i][2]=(rd.nextInt()>>>1)%win_height;
    			  bomb[i][3]=((rd.nextInt()>>>1)%5+1)*-1;
    			  bomb[i][4]=(rd.nextInt()>>>1)%5;
    			  break;
    		  case 2:
    			  bomb[i][1]=(rd.nextInt()>>>1)%win_width;
    			  bomb[i][2]=0;
    			  bomb[i][3]=(rd.nextInt()>>>1)%5;
    			  bomb[i][4]=(rd.nextInt()>>>1)%5+1;
    			  break;
    		  case 3:
    			  bomb[i][1]=(rd.nextInt()>>>1)%win_width;
    			  bomb[i][2]=win_height;
    			  bomb[i][3]=(rd.nextInt()>>>1)%5;
    			  bomb[i][4]=((rd.nextInt()>>>1)%5+1)*-1;
    			  break;
    			  
    		}
    	}
    }
    //完成炸弹轨迹更新
    public void updateBomb(int i)
    {
    	bomb[i][1]+=bomb[i][3];
    	bomb[i][2]+=bomb[i][4];
    	if(bomb[i][1]<0||bomb[i][1]>win_width)
    	{
    		bomb[i][3]*=-1;
    	}
    	if(bomb[i][2]<0||bomb[i][2]>win_height)
    	{
    		bomb[i][4]*=-1;
    	}
    }
    // 完成炸弹的绘制
    public void paintBomb(Sprite sp_plane)
    {
    	for(int i=0;i<bomb.length;i++)
    	{
    		if(this.collidesWith(sp_plane,true))
    		{
    			
    			sp_plane.setFrame(1);
    			sp_plane.nextFrame();
    			spExp_count++;
    			//sp_plane.paint(g);
    		}
    		else
    		{
    			this.setPosition(bomb[i][1],bomb[i][2]);
    			this.paint(g);
    			updateBomb(i);
    		}
    	}
    }
}

⌨️ 快捷键说明

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