1

Okey I am relatively new to programming (have only completed one semester in programming) and I am trying to create my version of the Space Invaders game (http://www.aeonity.com/ab/games/arcade-classics/space-invaders.php). I don't want to program it as an applet and I use the StdDraw library in java to draw everything.

Now I have already finished drawing everything but I am having problem with the movements of my "spaceships/enemies" I have figured out how to move one enemy but can not get them to move all at once.

I think my function for creating enemies is not that great since I had to write the position of every 48 of them myself... I am going to let my code follow so you can take a look. Thanks in advance.

/** Draw Enemies*/
public class Enemy{
    //yr and xr positions for rectangle
    private double x1, y1, r1, w, h, xr, yr;
    private double x2, y2, r2, x3, y3, r3;
    public Enemy(double x, double y, double r, double width, double height, double xRec, double yRec,
    double xSec, double ySec, double rSec, double xThird, double yThird, double rThird)
    {x1 = x; y1 = y; r1 = r; w = width; h = height; xr = xRec; yr = yRec;
     x2 = xSec; y2 = ySec; r2 = rSec; x3 = xThird; y3 = yThird; r3 = rThird;}

public void DRAW_ENEMY(){
    StdDraw.setPenColor(StdDraw.PINK);
    StdDraw.filledCircle(x1, y1, r1);
    StdDraw.filledCircle(x2, y2, r2);
    StdDraw.filledCircle(x3, y3, r3);
    StdDraw.filledRectangle(xr, yr, w, h);
}
public void MOVE_ENEMY(Enemy e11, Enemy e12) {
    double vx = 0.005;

    while(true){
        if(e11.x2 > e12.x2 || e11.x1 < 0.004) {
            vx = -vx;
        }
        //Update position
        e11.x1 = e11.x1 + vx;
        e11.x2 = e11.x2 + vx;
        e11.x3 = e11.x3 + vx;
        e11.xr = e11.xr + vx;

        // clear the background
        StdDraw.setPenColor(StdDraw.BLACK);
        StdDraw.filledRectangle(0.5, 0.67, 0.6,0.18 );

        // draw ball on the screen
        StdDraw.setPenColor(StdDraw.PINK); 
        e11.DRAW_ENEMY();

        // display and pause for 20 ms
         StdDraw.show(80);
    }
}


//Test client
public static void main(String[] args){
    // create every object
    Enemy e11 = new  Enemy(0.10, 0.84, 0.01, 0.007, 0.022, 0.112, 0.82, 0.124, 0.84, 0.01, 0.112, 0.8, 0.009);
    e11.DRAW_ENEMY();
    Enemy e12 = new  Enemy(0.17, 0.84, 0.01, 0.007, 0.022, 0.182, 0.82, 0.194, 0.84, 0.01, 0.182, 0.8, 0.009);
    e12.DRAW_ENEMY();
    Enemy e13 = new  Enemy(0.24, 0.84, 0.01, 0.007, 0.022, 0.252, 0.82, 0.264, 0.84, 0.01, 0.252, 0.8, 0.009);
    e13.DRAW_ENEMY();
    Enemy e14 = new  Enemy(0.31, 0.84, 0.01, 0.007, 0.022, 0.322, 0.82, 0.334, 0.84, 0.01, 0.322, 0.8, 0.009);
    e14.DRAW_ENEMY();
    Enemy e15 = new  Enemy(0.38, 0.84, 0.01, 0.007, 0.022, 0.392, 0.82, 0.404, 0.84, 0.01, 0.392, 0.8, 0.009);
    e15.DRAW_ENEMY();
    Enemy e16 = new  Enemy(0.45, 0.84, 0.01, 0.007, 0.022, 0.462, 0.82, 0.474, 0.84, 0.01, 0.462, 0.8, 0.009);
    e16.DRAW_ENEMY();
    Enemy e17 = new  Enemy(0.52, 0.84, 0.01, 0.007, 0.022, 0.532, 0.82, 0.544, 0.84, 0.01, 0.532, 0.8, 0.009);
    e17.DRAW_ENEMY();
    Enemy e18 = new  Enemy(0.59, 0.84, 0.01, 0.007, 0.022, 0.602, 0.82, 0.614, 0.84, 0.01, 0.602, 0.8, 0.009);
    e18.DRAW_ENEMY();
    Enemy e19 = new  Enemy(0.66, 0.84, 0.01, 0.007, 0.022, 0.672, 0.82, 0.684, 0.84, 0.01, 0.672, 0.8, 0.009);
    e19.DRAW_ENEMY();
    Enemy e110 = new Enemy(0.73, 0.84, 0.01, 0.007, 0.022, 0.742, 0.82, 0.754, 0.84, 0.01, 0.742, 0.8, 0.009);
    e110.DRAW_ENEMY();
    Enemy e111 = new Enemy(0.80, 0.84, 0.01, 0.007, 0.022, 0.812, 0.82, 0.824, 0.84, 0.01, 0.812, 0.8, 0.009);
    e111.DRAW_ENEMY();
    Enemy e112 = new Enemy(0.87, 0.84, 0.01, 0.007, 0.022, 0.882, 0.82, 0.894, 0.84, 0.01, 0.882, 0.8, 0.009);
    e112.DRAW_ENEMY();

    Enemy e21 = new  Enemy(0.10, 0.74, 0.01, 0.007, 0.022, 0.112, 0.72, 0.124, 0.74, 0.01, 0.112, 0.7, 0.009);
    e21.DRAW_ENEMY();
    Enemy e22 = new  Enemy(0.17, 0.74, 0.01, 0.007, 0.022, 0.182, 0.72, 0.194, 0.74, 0.01, 0.182, 0.7, 0.009);
    e22.DRAW_ENEMY();
    Enemy e23 = new  Enemy(0.24, 0.74, 0.01, 0.007, 0.022, 0.252, 0.72, 0.264, 0.74, 0.01, 0.252, 0.7, 0.009);
    e23.DRAW_ENEMY();
    Enemy e24 = new  Enemy(0.31, 0.74, 0.01, 0.007, 0.022, 0.322, 0.72, 0.334, 0.74, 0.01, 0.322, 0.7, 0.009);
    e24.DRAW_ENEMY();
    Enemy e25 = new  Enemy(0.38, 0.74, 0.01, 0.007, 0.022, 0.392, 0.72, 0.404, 0.74, 0.01, 0.392, 0.7, 0.009);
    e25.DRAW_ENEMY();
    Enemy e26 = new  Enemy(0.45, 0.74, 0.01, 0.007, 0.022, 0.462, 0.72, 0.474, 0.74, 0.01, 0.462, 0.7, 0.009);
    e26.DRAW_ENEMY();
    Enemy e27 = new  Enemy(0.52, 0.74, 0.01, 0.007, 0.022, 0.532, 0.72, 0.544, 0.74, 0.01, 0.532, 0.7, 0.009);
    e27.DRAW_ENEMY();
    Enemy e28 = new  Enemy(0.59, 0.74, 0.01, 0.007, 0.022, 0.602, 0.72, 0.614, 0.74, 0.01, 0.602, 0.7, 0.009);
    e28.DRAW_ENEMY();
    Enemy e29 = new  Enemy(0.66, 0.74, 0.01, 0.007, 0.022, 0.672, 0.72, 0.684, 0.74, 0.01, 0.672, 0.7, 0.009);
    e29.DRAW_ENEMY();
    Enemy e210 = new Enemy(0.73, 0.74, 0.01, 0.007, 0.022, 0.742, 0.72, 0.754, 0.74, 0.01, 0.742, 0.7, 0.009);
    e210.DRAW_ENEMY();
    Enemy e211 = new Enemy(0.80, 0.74, 0.01, 0.007, 0.022, 0.812, 0.72, 0.824, 0.74, 0.01, 0.812, 0.7, 0.009);
    e211.DRAW_ENEMY();
    Enemy e212 = new Enemy(0.87, 0.74, 0.01, 0.007, 0.022, 0.882, 0.72, 0.894, 0.74, 0.01, 0.882, 0.7, 0.009);
    e212.DRAW_ENEMY();

    Enemy e31 = new  Enemy(0.10, 0.64, 0.01, 0.007, 0.022, 0.112, 0.62, 0.124, 0.64, 0.01, 0.112, 0.6, 0.009);
    e31.DRAW_ENEMY();
    Enemy e32 = new  Enemy(0.17, 0.64, 0.01, 0.007, 0.022, 0.182, 0.62, 0.194, 0.64, 0.01, 0.182, 0.6, 0.009);
    e32.DRAW_ENEMY();
    Enemy e33 = new  Enemy(0.24, 0.64, 0.01, 0.007, 0.022, 0.252, 0.62, 0.264, 0.64, 0.01, 0.252, 0.6, 0.009);
    e33.DRAW_ENEMY();
    Enemy e34 = new  Enemy(0.31, 0.64, 0.01, 0.007, 0.022, 0.322, 0.62, 0.334, 0.64, 0.01, 0.322, 0.6, 0.009);
    e34.DRAW_ENEMY();
    Enemy e35 = new  Enemy(0.38, 0.64, 0.01, 0.007, 0.022, 0.392, 0.62, 0.404, 0.64, 0.01, 0.392, 0.6, 0.009);
    e35.DRAW_ENEMY();
    Enemy e36 = new  Enemy(0.45, 0.64, 0.01, 0.007, 0.022, 0.462, 0.62, 0.474, 0.64, 0.01, 0.462, 0.6, 0.009);
    e36.DRAW_ENEMY();
    Enemy e37 = new  Enemy(0.52, 0.64, 0.01, 0.007, 0.022, 0.532, 0.62, 0.544, 0.64, 0.01, 0.532, 0.6, 0.009);
    e37.DRAW_ENEMY();
    Enemy e38 = new  Enemy(0.59, 0.64, 0.01, 0.007, 0.022, 0.602, 0.62, 0.614, 0.64, 0.01, 0.602, 0.6, 0.009);
    e38.DRAW_ENEMY();
    Enemy e39 = new  Enemy(0.66, 0.64, 0.01, 0.007, 0.022, 0.672, 0.62, 0.684, 0.64, 0.01, 0.672, 0.6, 0.009);
    e39.DRAW_ENEMY();
    Enemy e310 = new Enemy(0.73, 0.64, 0.01, 0.007, 0.022, 0.742, 0.62, 0.754, 0.64, 0.01, 0.742, 0.6, 0.009);
    e310.DRAW_ENEMY();
    Enemy e311 = new Enemy(0.80, 0.64, 0.01, 0.007, 0.022, 0.812, 0.62, 0.824, 0.64, 0.01, 0.812, 0.6, 0.009);
    e311.DRAW_ENEMY();
    Enemy e312 = new Enemy(0.87, 0.64, 0.01, 0.007, 0.022, 0.882, 0.62, 0.894, 0.64, 0.01, 0.882, 0.6, 0.009);
    e312.DRAW_ENEMY();

    Enemy e41 = new  Enemy(0.10, 0.54, 0.01, 0.007, 0.022, 0.112, 0.52, 0.124, 0.54, 0.01, 0.112, 0.5, 0.009);
    e41.DRAW_ENEMY();
    Enemy e42 = new  Enemy(0.17, 0.54, 0.01, 0.007, 0.022, 0.182, 0.52, 0.194, 0.54, 0.01, 0.182, 0.5, 0.009);
    e42.DRAW_ENEMY();
    Enemy e43 = new  Enemy(0.24, 0.54, 0.01, 0.007, 0.022, 0.252, 0.52, 0.264, 0.54, 0.01, 0.252, 0.5, 0.009);
    e43.DRAW_ENEMY();
    Enemy e44 = new  Enemy(0.31, 0.54, 0.01, 0.007, 0.022, 0.322, 0.52, 0.334, 0.54, 0.01, 0.322, 0.5, 0.009);
    e44.DRAW_ENEMY();
    Enemy e45 = new  Enemy(0.38, 0.54, 0.01, 0.007, 0.022, 0.392, 0.52, 0.404, 0.54, 0.01, 0.392, 0.5, 0.009);
    e45.DRAW_ENEMY();
    Enemy e46 = new  Enemy(0.45, 0.54, 0.01, 0.007, 0.022, 0.462, 0.52, 0.474, 0.54, 0.01, 0.462, 0.5, 0.009);
    e46.DRAW_ENEMY();
    Enemy e47 = new  Enemy(0.52, 0.54, 0.01, 0.007, 0.022, 0.532, 0.52, 0.544, 0.54, 0.01, 0.532, 0.5, 0.009);
    e47.DRAW_ENEMY();
    Enemy e48 = new  Enemy(0.59, 0.54, 0.01, 0.007, 0.022, 0.602, 0.52, 0.614, 0.54, 0.01, 0.602, 0.5, 0.009);
    e48.DRAW_ENEMY();
    Enemy e49 = new  Enemy(0.66, 0.54, 0.01, 0.007, 0.022, 0.672, 0.52, 0.684, 0.54, 0.01, 0.672, 0.5, 0.009);
    e49.DRAW_ENEMY();
    Enemy e410 = new Enemy(0.73, 0.54, 0.01, 0.007, 0.022, 0.742, 0.52, 0.754, 0.54, 0.01, 0.742, 0.5, 0.009);
    e410.DRAW_ENEMY();
    Enemy e411 = new Enemy(0.80, 0.54, 0.01, 0.007, 0.022, 0.812, 0.52, 0.824, 0.54, 0.01, 0.812, 0.5, 0.009);
    e411.DRAW_ENEMY();
    Enemy e412 = new Enemy(0.87, 0.54, 0.01, 0.007, 0.022, 0.882, 0.52, 0.894, 0.54, 0.01, 0.882, 0.5, 0.009);
    e412.DRAW_ENEMY();
    e11.MOVE_ENEMY(e11,e12);

}
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
gthb7
  • 87
  • 2
  • 5
  • See [here](http://stackoverflow.com/questions/13999506/threads-with-key-bindings/14001011#14001011) for some example game logic and [this](http://stackoverflow.com/questions/13825515/java-rectangle-collision-detection-confusion/13827649#13827649) variation and [another](http://stackoverflow.com/questions/13739693/java-swing-based-game-framework-any-advice/13740162#13740162) and lastly [here](http://stackoverflow.com/questions/13791614/lag-spike-when-moving-player/13792012#13792012) – David Kroukamp Jan 03 '13 at 17:26
  • you should reformat your code so its easier for us to read – Daniel Jan 03 '13 at 17:28
  • 1
    A few tips: put all your items (at least all your enemies) in a `Collection` -v.g. a `java.util.ArrayList`- to handle all of them together. Also, your code does not show anything other than `e11` moving, if you do not change the other enemies values they will not change alone. – SJuan76 Jan 03 '13 at 17:31
  • [Here](http://stackoverflow.com/questions/14068472/java-mouselistener-action-event-in-paintcomponent/14070147#14070147) is another much simpler example too. And that while loop must be blocking the EDT/Thread. – David Kroukamp Jan 03 '13 at 17:31

1 Answers1

2

First lets instantiate them a little better.

List<Enemy> enemies = new ArrayList<Enemy>();

for(int y = 0; y < 4; y++) {
  for(int x = 0; x < 12; x++) {
    Enemy enemy = new Enemy(x,y); //all other variables can be coded in constructor
    enemies.add(enemy);
  }
}

now as they are all in a list lets update them

for(Enemy e : enemies) {
  e.update(); //move a single enemy in the usual pattern ignoring his buddies
}

and then draw

for(Enemy e : enemies) {
  e.draw();
}
AbstractChaos
  • 4,211
  • 1
  • 19
  • 28