I by no means claim that I may have not missed something. try this though
public abstract class Body {
public double x, y;
public double distance(Body b) { // This calculates the distance between centers.
double xd = (b.x + b.width() / 2) - (x + width() / 2);
double yd = (b.y + b.height() / 2) - (y + height() / 2);
return Math.sqrt(xd * xd + yd * yd);
}
public Boolean collide(Body b) { // This determines if two bodies have collided.
return pointIn(b.x, b.y) || pointIn(b.x + b.width(), b.y)
|| pointIn(b.x, b.y + b.height())
|| pointIn(b.x + b.width(), b.y + b.height())
|| b.pointIn(x, y);
}
public Boolean pointIn(double xp, double yp) { // This determines if a
// point is within this
// body.
return x < xp && xp < x + width() && y < yp && yp < y + height();
}
// you want to avoid returning static values
public int mapx(Game h, GameComponent canvas, double x) {
return (int) Math.round(x);
}
public int mapy(Game h, GameComponent canvas, double y) {
return (int) Math.round
;
}
public abstract void draw(Game h, GameComponent canvas);
public abstract void step(Game bw, GameComponent canvas);
public abstract double width();
public abstract double height();
}
------------------------------------------------------------------------------------------------------------------------
import java.util.Random;
import javax.swing.ImageIcon;
public class Bullet extends Body {
// if you have different bullet types don't final these. If all bullets are the same
// then change final to static.
// final represents values that are unchangle per instance. static value carries between instance or iteration closure
final ImageIcon bullet = new ImageIcon("bullet.png");
final int width = bullet.getIconWidth();
final int height = bullet.getIconHeight();
public double yVelocity = y-SpaceShip.getInstance().y;
public double xVelocity = x-SpaceShip.getInstance().x;
public double yVelocity = SpaceShip.getInstance().top();
public double yvelocity = Game.ship.getY();// good example of static :)
public double yVelocity = 3.0;
public double xVelocity = 3.0;
public Bullet(double xp, double yp) {
x = xp;
y = yp;
}
public void draw(Game h, GameComponent canvas) {
canvas.drawImage(bullet.getImage(), mapx(h, canvas, x), mapy(h, canvas,));
}
public Boolean offscreen() {
return y > SpaceInvaders.ycanvas;
}
public void step(Game bw, GameComponent canvas) { // All they do is drop.
y += yVelocity;
x += xVelocity;
}
public double width() {
return (double) width;
}
public double height() {
return (double) height;
}
}
-------------------------------------------------------------------------------------------------------------------------
import java.util.Random;
import javax.swing.ImageIcon;
public class SpaceShip extends Body {
private static instance;
static final ImageIcon ship = new ImageIcon("ship.png");
static final int width = ship.getIconWidth();
static final int height = ship.getIconHeight();
public SpaceShip(double xp, double yp) {
x = xp;
y = yp;
instance = this;
}
public SpaceShip getInstance(){ return instance; }
public int clip = 7;
public int ammo = 70;
public void draw(Game h, GameComponent canvas) {
canvas.drawImage(ship.getImage(), mapx(h, canvas, x), mapy(h, canvas, y));
}
public void step(Game bw, GameComponent canvas) {
if (canvas.leftPressed) {
x += -2;
if (x < 0)
x = 0.0;
}
if (canvas.rightPressed) {
x += 2;
if (x > canvas.xdim - width)
x = 1.0 * canvas.xdim - width;
}
if (canvas.upPressed) {
y += -2;
if (y < 0)
y = 0.0;
}
if (canvas.downPressed) {
y += 2;
if (y > canvas.ydim - height)
y = 1.0 * canvas.ydim - height;
}
if (canvas.rPressed) {
canvas.rPressed = false;
ammo = (ammo -7 + clip);
clip += 7;
System.out.println(ammo);
}
if (canvas.spacePressed) {
canvas.spacePressed = false;
if (clip >= 1) {
bw.missiles.add(new Missile(x + width / 2 - Missile.width / 2, y));
clip += -1;
System.out.println(clip);
}
}
}
public double width() {
return (double) width;
}
public double height() {
return (double) height;
}
}