By using this site, you agree to our Privacy Policy and our Terms of Use. Close

Forums - PC - So, who wants to help me with Java?

On second thought, do maybe you should not make it static. You should come up with a way to house all of your game data. Perhaps you should look at the MVC Pattern. Not sure how useful it would be to games, but it helps on applications I have worked on.



Around the Network

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;
  }
}

 

 

 

 



Squilliam: On Vgcharts its a commonly accepted practice to twist the bounds of plausibility in order to support your argument or agenda so I think its pretty cool that this gives me the precedent to say whatever I damn well please.

Your use for SpaceShip.y was dynamic, but you were using it as a Static Context. Also you were as mentioned before using Integer/Double when you should be using int/double

SpaceShip.y
even with a static context .y was not initialized from a straight class call.

by creating and storing an instance of a created class with the ShipClass you can call on the instance that will hold a dynamic value of .y

You can however
public class SpaceShip extends Body{
public static final instance = new SpaceShip(); // maybe this instead
}

though you won't be able to set the constructor parameters. though I don't see this in common practice. So i'm not sure the validity. Also

SpaceShip getInstance(){
  if(instance == null) instance  = new SpaceShip();
  return instance;
}



Squilliam: On Vgcharts its a commonly accepted practice to twist the bounds of plausibility in order to support your argument or agenda so I think its pretty cool that this gives me the precedent to say whatever I damn well please.