移动游戏的对象

时间:2013-01-03 17:25:04

标签: java

Okey我对编程比较陌生(在编程中只完成了一个学期),我正在尝试创建我的版本的Space Invaders游戏(http://www.aeonity.com/ab/games/arcade-classics /space-invaders.php)。我不想将它编程为applet,我使用java中的StdDraw库来绘制所有内容。

现在我已经完成了绘制所有内容,但我对“宇宙飞船/敌人”的动作有问题我已经想出如何移动一个敌人但是不能让他们一次移动。

我认为我创造敌人的功能并不是那么好,因为我必须自己写下每48个人的位置...我会让我的代码跟随你所以你可以看看。 提前谢谢。

/** 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);

}
}

1 个答案:

答案 0 :(得分:2)

首先让它们更好地实例化。

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

现在因为它们都在列表中,所以可以更新它们

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

然后绘制

for(Enemy e : enemies) {
  e.draw();
}