import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*; //need this for the timer to work.
///public class DoubleBuffering extends Applet implements MouseMotionListener
class Ship
{
private int xpos, ypos, height, width, speed, xdir, ydir;
private Ellipse2D.Double c;
public Ship(int x, int y, int w, int h, int s, int xd)
{
xpos = x;
ypos = y;
width = w;
height = h;
speed = s;
xdir = xd;
ydir = 0;
c = new Ellipse2D.Double (xpos, ypos, width, height);
}
public void move()
{
//not completed, only moves in x-axis
xpos = xpos + speed*xdir;
ypos = ypos + speed*ydir;
c.setFrame(xpos, ypos, width, height);
} //not completed, only moves in x-axis
public int getX() { return xpos;}
public int getY() { return ypos ;}
public int getHeight() { return height; }
public int getWidth() { return width ;}
public int getXDir() { return xdir ;}
public int getYDir() { return ydir ;}
public void setDir(int x, int y)
{
xdir = x;
ydir = y;
}
public boolean intersects(int x, int y, int w, int h)
{
return c.intersects(x, y, w, h);
}
public void paint(Graphics2D g)
{
g.fill(c);
}
}
class Alien
{
private int xpos, ypos, height, width, speed, xdir, ydir;
private Ellipse2D.Double c;
public Alien(int x, int y, int w, int h, int s, int xd)
{
xpos = x;
ypos = y;
width = w;
height = h;
speed = s;
xdir = xd;
ydir = 0;
c = new Ellipse2D.Double (xpos, ypos, width, height);
}
public void move()
{
//not completed, only moves in x-axis
xpos = xpos + speed*xdir;
ypos = ypos + speed*ydir;
c.setFrame(xpos, ypos, width, height);
} //not completed, only moves in x-axis
public int getX() { return xpos;}
public int getY() { return ypos ;}
public int getHeight() { return height; }
public int getWidth() { return width ;}
public int getXDir() { return xdir ;}
public int getYDir() { return ydir ;}
public void setDir(int x, int y)
{
xdir = x;
ydir = y;
}
public boolean intersects(int x, int y, int w, int h)
{
return c.intersects(x, y, w, h);
}
public void paint(Graphics2D g)
{
g.fill(c);
}
}
class lazer
{
private int xpos, ypos, height, width, speed, xdir, ydir;
private Ellipse2D.Double c;
public lazer(int x, int y, int w, int h, int s, int xd
)
{
xpos = x;
ypos = y;
width = w;
height = h;
speed = s;
xdir = 0;
ydir = xd;
c = new Ellipse2D.Double (xpos, ypos, width, height);
}
public void move()
{
//not completed, only moves in x-axis
xpos = xpos + speed*xdir;
ypos = ypos + speed*ydir;
c.setFrame(xpos, ypos, width, height);
} //not completed, only moves in x-axis
public int getX() { return xpos;}
public int getY() { return ypos ;}
public int getHeight() { return height; }
public int getWidth() { return width ;}
public int getXDir() { return xdir ;}
public int getYDir() { return ydir ;}
public void setDir(int x, int y)
{
xdir = x;
ydir = y;
}
public boolean intersects(int x, int y, int w, int h)
{
return c.intersects(x, y, w, h);
}
public void paint(Graphics2D g)
{
g.fill(c);
}
// public void fire()
// {
//
// c = new Ellipse2D.Double (xpos, ypos, width, height);
//
// }
}
public class space_invaders extends Applet implements KeyListener {
private lazer l;
private Ship s;
//Alien []a;
private Alien a;
private Timer t;
private Alien [][] aliens = new Alien [5][16];
public void init()
{
s=new Ship(20, 250, 20, 20, 3, 0);
a=new Alien(200, 100, 20, 20, 5, 0);
l=new lazer(39, 100,15, 40, 2, -1);
//a = new Alien[80];
addKeyListener(this);
setFocusable(true);
for(int j=0;j<5;j++)
for(int k=0;k<16;k++)
aliens[j][k] = new Alien(300 + 50*k, 100 + 30*j, 20, 20, 5, 0);
ActionListener z = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
s.move();
a.move();
l.move();
if (s.intersects(a.getX(), a.getY(), a.getWidth(), a.getHeight()))
{
s.setDir(-s.getXDir(), 0);
}
if (l.intersects(a.getX(), a.getY(), a.getWidth(), a.getHeight()))
{
{
a= new Alien(0, 0, 0, 0, 0, 0);
}
}
repaint();
}
};
t=new Timer(10, z);
t.start();
// a = new Alien[470];
}
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == e.VK_DOWN)
{
s.setDir(0,1); //change direction of the paddle to positive
}
if (e.getKeyCode() == e.VK_UP)
{
s.setDir(0,-1); //change direction of the paddle to negative
}
if (e.getKeyCode() == e.VK_LEFT)
{
s.setDir(-1,0);
}
if(e.getKeyCode() ==e.VK_RIGHT)
{
s.setDir(1,0);
}
if(e.getKeyCode() ==e.VK_SPACE)
{
System.out.println(s.getX());
l=new lazer(s.getX(), s.getY(), 30, 40, 15, -1);
}
}
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode() == e.VK_DOWN)
{
s.setDir(0,0); //change direction of the paddle to positive
}
if (e.getKeyCode() == e.VK_UP)
{
s.setDir(0,0); //change direction of the paddle to negative
}
if (e.getKeyCode() == e.VK_LEFT)
{
s.setDir(0,0);
}
if(e.getKeyCode() ==e.VK_RIGHT)
{
s.setDir(0,0);
}
}
public void keyTyped(KeyEvent e) {} //not needed but has to be here
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
{ s.paint(g2);
a.paint(g2);
l.paint(g2);
for(Alien[] row: aliens)
for(Alien A: row)
A.paint(g2);
}
}
}
Hi guys;
So above is my program for my java space invaders attempt. I am very new to java, so my knowledge is still quite limited..but I am having a major problem here. Basically, my lazer should ideally shoot all the aliens in the array causing them to dissappear. Except I cannot include the array when I get to the
if (l.intersects(a.getX(), a.getY(), a.getWidth(), a.getHeight()))
{
a= new Alien(0, 0, 0, 0, 0, 0);
}
because I know the "a" is not including the array. But my array is called aliens and I do not know how to incorporate it into there. Please help me make my aliens in my array disappear. I have tried for a long time only to get nowhere.