📄 bullet.java
字号:
import javax.microedition.lcdui.*;
public class Bullet
{
int bullet[][];
Image bulletImage;
Map b_map;
public Bullet()
{
b_map = new Map();
bullet = new int [20][5];
for(int i = 0;i < bullet.length;i++)
{
bullet[i][0] = 0; // 状态
bullet[i][1] = 0; // x
bullet[i][2] = 0; // y
bullet[i][3] = 5; // 速度
bullet[i][4] = 0; //方向
}
try
{
bulletImage = Image.createImage("/bullet.png");
}
catch(Exception e){}
}
public void paintBullet(Graphics g) //画子弹
{
for(int i = 0;i < bullet.length;i++)
{
if(bullet[i][0] == 1)
{
g.drawImage(bulletImage,bullet[i][1],bullet[i][2],0);
}
}
}
public void creatBullet(int pX,int pY,int dir) // 产生子弹.参数为坦克横纵坐标,行驶方向
{
int bulletID = 0;
do
{
if(bulletID >= 20) break;
if(bullet[bulletID][0] == 0)
{
bullet[bulletID][0] = 1;
switch(dir)
{
case 0:
bullet[bulletID][4] = 0;
bullet[bulletID][1] = pX+4;
bullet[bulletID][2] = pY;
break;
case 1:
bullet[bulletID][4] = 1;
bullet[bulletID][1] = pX+13;
bullet[bulletID][2] = pY+4;
break;
case 2:
bullet[bulletID][4] = 2;
bullet[bulletID][1] = pX+4;
bullet[bulletID][2] = pY+13;
break;
case 3:
bullet[bulletID][4] = 3;
bullet[bulletID][1] = pX;
bullet[bulletID][2] = pY+4;
break;
}
break;
}
else bulletID++;
}
while(true);
}
public void bulletMove() // 子弹运动
{
for(int i = 0;i < bullet.length;i++)
{
if(bullet[i][0] == 1)
{
switch(bullet[i][4])
{
case 0:
if(bullet[i][2] > 0) bullet[i][2] -= bullet[i][3];
else bullet[i][0] = 0;
break;
case 1:
if(bullet[i][1] < 176) bullet[i][1] += bullet[i][3];
else bullet[i][0] = 0;
break;
case 2:
if(bullet[i][2] < 208) bullet[i][2] += bullet[i][3];
else bullet[i][0] = 0;
break;
case 3:
if(bullet[i][2] > 0) bullet[i][1] -= bullet[i][3];
else bullet[i][0] = 0;
break;
}
}
}
}
public boolean bullet_collides()throws ArrayIndexOutOfBoundsException //与地图碰撞检测
{
for(int j = 0;j < bullet.length;j++)
{
if(bullet[j][0] == 1)
{
if(bullet[j][4] == 0) // up
{
int x1 = (bullet[j][1] - b_map.mapX) / 16;
int x2 = (bullet[j][1] + 5 - b_map.mapX) / 16;
int y = (bullet[j][2] - bullet[j][3] - b_map.mapY) / 16;
for(int i = x1;i <= x2;i++)
{
if(b_map.mapdata[y][i] != 4)
System.out.println(b_map.mapdata[y][i]);
return true;
}
}
}
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -