plane fire bullets
the way i want the code to work is to always fire the bullet up at a speed of -15 when the spacebar is pressed but after the first bullet, there are a ton of glitches and when u press the spacebar the bullet just moves in the direction that the plane is going and at the same speed. any suggestions or code changes will help. thx.
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.net.*;
//import javax.swing.JOptionPane;
publicclass TopGun2extends Appletimplements Runnable
{
private Image backImage;
privateint i;
//private int current;
privateint y_speed;
privateint x_speed;
privateint x_pos;
privateint y_pos;
privateboolean active;
privateint width;//= 1000;//getSize().height;
privateint height;
//private boolean track = true;
//private int bulletx;
//private int bullety;
//private int byspeed;
//private int bxspeed;
// = 1100;//getSize().width;
private Random rnd;
private Image [] jets;
private Image [] classics;
private Image [] display;
private Image [] bullets;
private Image bank;
private Image dbImage;
private Graphics dbg;
private Image Paddle;
//private Graphics padd;
private Image bullet;
private Image plane;
private AudioClip blastSound;
private AudioClip debrisSound;
private AudioClip explodeSound;
private AudioClip fireSound;
private AudioClip passingSound;
private AudioClip targetedSound;
privateint countForArrays = 0;
private Image[] orangeBullet =new Image[20];
privateint[] bxspeed =newint[20];
privateint[] byspeed =newint[20];
privateint[] bulletx =newint[20];
privateint[] bullety =newint[20];
privateint keepTrackOfLastCountForArrays = 0;
public TopGun2()
{
i = 0;
active =true;
//bulletx = x_pos + 60;// = 100;
//bullety = y_pos + 50;// = 100;
rnd =new Random ();
jets =new Image[6];
//bullets = new Image[500];
}
publicvoid init()
{
// bank = getImage (getCodeBase(), "f-16bank.png");
// bank2 = getImage (getCodeBase(), "f-16bank2.png");
backImage = getImage (getCodeBase(),"shore.png");
setBackground(Color.black);
/**for (int count = 0; count <= 19; count++)
{
orangeBullet[count] = getImage (getCodeBase(), "bullet.png");
}*/
plane = getImage (getCodeBase(),"f-16.png");
//bullet = getImage (getCodeBase(), "bullet.png");
jets[0] = getImage (getCodeBase(),"f-16.png");
jets[1] = getImage (getCodeBase(),"su-27.png");
jets[2] = getImage (getCodeBase(),"f-22.png");
jets[3] = getImage (getCodeBase(),"f-35.png");
jets[4] = getImage (getCodeBase(),"f-117.png");
jets[5] = getImage (getCodeBase(),"mb339.png");
height = getSize().height;
width = getSize().width;
}
publicvoid loadSounds()
{
// Load all sound clips by playing and immediately stopping them.
try{
blastSound= getAudioClip(new URL("fire.au"));
debrisSound= getAudioClip(new URL("debris.au"));
explodeSound = getAudioClip(new URL("explode.au"));
fireSound= getAudioClip(new URL("fire.au"));
passingSound = getAudioClip(new URL("passing.au"));
targetedSound = getAudioClip(new URL("targeted.au"));
}
catch (MalformedURLException e){}
blastSound.play();blastSound.stop();
debrisSound.play();debrisSound.stop();
explodeSound.play(); explodeSound.stop();
fireSound.play();fireSound.stop();
passingSound.play(); passingSound.stop();
targetedSound.play(); targetedSound.stop();
}
publicvoid start()
{
Thread th =new Thread (this);
th.start ();
}
publicboolean keyDown (Event e,int key)
{
if (key == 120)
{
plane = jets[i];
i++;
if (i > 5)
i = 0;
}
if (key == 32)
{
// bullet = new Bullet(bulletx, bullety);
//bullet.drawBullet();
// bullet.fire();
//drawBullet(Graphics g);
orangeBullet[countForArrays] = getImage (getCodeBase(),"bullet.png");
keepTrackOfLastCountForArrays = countForArrays;
//orangeBullet[countForArrays] = getImage (getCodeBase(), "bullet.png");
bxspeed[countForArrays] = 0;
byspeed[countForArrays] = -15;
//byspeed = -15;
//bxspeed = 0;
countForArrays++;
}
if (key == 109)
{
blastSound.play();
}
if (key == Event.UP)
{
y_speed = -5;
if (byspeed[keepTrackOfLastCountForArrays] < 0 && bxspeed[keepTrackOfLastCountForArrays] == 0)
{}
else
{
byspeed[keepTrackOfLastCountForArrays] = -5;
}
}
if (key == Event.DOWN)
{
y_speed = +5;
if (byspeed[keepTrackOfLastCountForArrays] < 0 && bxspeed[keepTrackOfLastCountForArrays] == 0)
{}
else
{
byspeed[keepTrackOfLastCountForArrays] = +5;
}
}
if (key == Event.RIGHT)
{
x_speed = +5;
if (byspeed[keepTrackOfLastCountForArrays] < 0 && bxspeed[keepTrackOfLastCountForArrays] == 0)
{}
else
{
bxspeed[keepTrackOfLastCountForArrays] = +5;
}
}
if (key == Event.LEFT)
{
x_speed = -5;
if (byspeed[keepTrackOfLastCountForArrays] < 0 && bxspeed[keepTrackOfLastCountForArrays] == 0)
{}
else
{
bxspeed[keepTrackOfLastCountForArrays] = -5;
}
}
returntrue;
}
publicboolean keyUp(Event e,int key)
{
if (key == Event.UP)
{
y_speed = 0;
byspeed[keepTrackOfLastCountForArrays] = 0;
}
if (key == Event.DOWN)
{
y_speed = 0;
byspeed[keepTrackOfLastCountForArrays] = 0;
}
if (key == Event.RIGHT)
{
x_speed = 0;
bxspeed[keepTrackOfLastCountForArrays] = 0;
}
if (key == Event.LEFT)
{
x_speed = 0;
bxspeed[keepTrackOfLastCountForArrays] = 0;
}
returntrue;
}
publicvoid run ()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
//bullet.fire();
x_pos += x_speed;
y_pos += y_speed;
for (int count = 0; count <= 19; count++)
{
bulletx[count] += bxspeed[count];
bullety[count] += byspeed[count];
}
//if (bullety <= -20)
//{
// bulletx = x_pos + (125 / 2);
// bullety = y_pos + 50;
// byspeed = 0;
//}
if (x_pos < 0)
{
x_speed = +5;
bxspeed[keepTrackOfLastCountForArrays] = +5;
}
elseif (x_pos > width)
{
x_speed = -5;
bxspeed[keepTrackOfLastCountForArrays] = -5;
}
elseif (y_pos < 0)
{
y_speed = +5;
}
elseif (y_pos > height)
{
y_speed = -5;
byspeed[keepTrackOfLastCountForArrays] = -5;
}
else//if (bullety < -10)
{
for(int count = 0; count <= 19; count++)
{
if (bullety[count] < -10)
{
bulletx[count] = x_pos + 65;
bullety[count] = y_pos + 50;
byspeed[count] = 0;
}
}
}
repaint();
try
{
Thread.sleep (15);
}
catch (InterruptedException ex)
{
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
publicvoid update(Graphics g)
{
if (dbImage ==null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
dbg.setColor (getForeground());
paint (dbg);
g.drawImage (dbImage, 0, 0,this);
}
publicvoid paint (Graphics g)
{
g.drawImage (backImage, 0, 0,this);
g.setColor (Color.blue);
// bullet.drawBullet(g);
for (int count = 0; count <= 19; count++)
{
//g.drawImage(bullet, bulletx, bullety, this);
g.drawImage(orangeBullet[count], bulletx[count], bullety[count],this);
}
g.drawImage (plane, x_pos, y_pos,this);
}
}

