太空侵略者娱乐:外星人向下移动时继续横向移动

时间:2015-11-22 22:40:11

标签: java animation repaint

对于编程类,我必须在java中重新创建游戏Space Invaders。我已经走得很远,实际上差不多已经完成,但是当我的外星人中队降级时会出现问题。有趣的是它发生在我的外星人2D阵列的前两列。当中队作为一组下降时,第一次整个顶行向右移动,第二行的第一个外星人向左移动。在随后的所有跌落中,只有第二排的第一个外星人继续向左移动越来越远。最初我编写代码,以便每个外星人的x和y位置先改变,然后我调用重绘方法。认为这是问题我删除了双循环之外的y位置的变化。所以现在每个外星人的x位置都会发生变化,屏幕会被重新绘制,因此会向左或向右移动。然后,如果drop ready boolean(在for循环中为double)设置为true,则每个外星人的y位置向下移动,屏幕再次重新绘制。因此,对每种类型的更改都有一个单独的repaint()调用。尽管如此,这对我来说并没有帮助。我的Alien类和Squadron类的代码如下所示。有谁知道为什么会这样?

异形课程:

public class Alien extends Invader{
    private int alienType;//denotes the type of alien
    private GamePanel gamePanel;
private Rectangle rectangle;//used to set bounds of each alien for collision

public Alien(int xLoc, int yLoc, GamePanel gp){
    this();
    gamePanel=gp;
    rectangle = new Rectangle();
    alienType=0;
}
public Alien(){
    alienType = 0;
    rectangle = new Rectangle();
    //inherited properties
    this.setSpeed(10);//number of pixels aliens move
    this.setHeight(20);
    this.setWidth(20);
    this.setVisible(true);
    this.setColor(Color.white);
    this.setDirection(Direction.East);//initial direction
}
//moves the alien left
public void moveLeft(){
    getLocation().x += getSpeed();
}
//moves the alien right
public void moveRight(){
    getLocation().x -= getSpeed();
}
//moves the alien downward
public void moveDown(){
    getLocation().y += 2*getSpeed();
    //resets their direction
    if(getDirection()==Direction.East)
        setDirection(Direction.West);
    else
        setDirection(Direction.East);
}
//draws the different type of aliens
@Override
public void draw(Graphics g){
    getRectangle().setBounds(getLocation().x, getLocation().y, getWidth(), getHeight());//setting bounds of rectangle to dimensions of alien
    //determines visiblity of aliens
    if(isVisible()){
        g.setColor(getColor());
        switch(getAlienType()){
            case 1:
                //type 1 alien is a circle with two smaller circles under it
                g.fillOval(getLocation().x, getLocation().y, getWidth(), getHeight());
                g.fillOval(getLocation().x, getLocation().y+getHeight(), getWidth()/2, getHeight()/2);
                g.fillOval(getLocation().x + (getWidth()-getWidth()/2), getLocation().y+getHeight(), getWidth()/2, getHeight()/2);
                break;
            case 2:
                //type 2 alien is a square with two oval on the bottom
                g.fillRect(getLocation().x, getLocation().y, getWidth(), getHeight());
                g.fillOval(getLocation().x, getLocation().y+getHeight(), getWidth(), getHeight()/4);
                g.fillOval(getLocation().x+(getWidth()/2), getLocation().y+getHeight()+(getHeight()/4), getWidth()/2, getHeight()/4);
                break;
            case 3:
                //type 3 alien is a triangle with a circle on top
                Polygon triangle = new Polygon();
                triangle.addPoint(getLocation().x+(getWidth()/2), getLocation().y+5);
                triangle.addPoint(getLocation().x,getLocation().y+getHeight());
                triangle.addPoint(getLocation().x+getWidth(), getLocation().y+getHeight());
                g.fillPolygon(triangle);
                g.drawOval(getLocation().x +(getWidth()/2), getLocation().y-5, getWidth()/2, getHeight()/2);
        }
    }
}
    //mutator and accessor methods
}

中队班:

public class Squadron implements ActionListener{

private Alien[][] aliens = new Alien[5][12];//2D array of invading aliens
private Timer moveTimer;//controls movement of squadron
private int numVisible;//current number of aliens that have not been destroyed
private int moveCount;//denotes the number of squad moves
private GamePanel gamePanel;
private boolean dropReady;//determines if squadron is ready to drop

public Squadron(GamePanel gp){
    dropReady = false;
    moveCount=0;
    //initializes all aliens
   for(int i=0; i<aliens.length; i++){
            for(int j=0; j<aliens[i].length; j++){
                aliens[i][j] = new Alien(0, 0, gp);
                //sets location based on position in the array
                aliens[i][j].getLocation().x = (j*2) * (aliens[i][j].getWidth() + aliens[i][j].getWidth()/2);
                aliens[i][j].getLocation().y = (i*2) * ((aliens[i][j].getHeight() + aliens[i][j].getHeight()/2));
                aliens[i][j].getLocation().y+= gp.getHeight()/6;//shifts aliens downward away from spaceship
                //sets the type of alien based in row position
                if(i==0){
                    aliens[i][j].setAlienType(1);
                    aliens[i][j].setPointValue(40);
                }    
                else if(i>0 && i<3){
                    aliens[i][j].setAlienType(2);
                    aliens[i][j].setPointValue(20);
                }    
                else if(i>=3){
                    aliens[i][j].setAlienType(3);
                    aliens[i][j].setPointValue(10);
                }    
            }
        }
   moveTimer = new Timer(1000,this);
   numVisible = 60;//set to initial number of aliens
   gamePanel = gp;

   moveTimer.start();
}

public void actionPerformed(ActionEvent e){
    if(gamePanel.isEnabled()){
        loop: for(int i=0; i<getAliens().length; i++){
                for(int j =0; j<getAliens()[i].length; j++){
                    //alien lateral and vertical movements are based on if any aliens in a given row/column are visible
                    //moves aliens to the east
                    if(getAliens()[i][j].getDirection() == Direction.East && !dropReady && aliens[i][j].isVisible()){
                        getAliens()[i][j].moveLeft();
                    }
                    //moves alien to the west
                    else if(getAliens()[i][j].getDirection() == Direction.West && !dropReady && aliens[i][j].isVisible()){
                        getAliens()[i][j].moveRight();
                    }
                    //moves alien down
                    if(getAliens()[i][j].getLocation().x>= getGamePanel().getWidth()-aliens[i][j].getWidth() || getAliens()[i][j].getLocation().x<0){
                        dropReady=true;
                    }
                    //stops when alien reaches the bottom
                    if(getAliens()[i][j].getLocation().y+getAliens()[i][j].getHeight() >= getGamePanel().getHeight()-gamePanel.getLaserCanon().getHeight()){
                        getMoveTimer().stop();
                        gamePanel.gameOver();
                    }
                }
            }
            moveCount++;
            getGamePanel().repaint();
            if(dropReady){
                drop();
                gamePanel.repaint();
            }
            //increases speed every 4 lateral moves
            if(moveCount%4==0){
                faster();
            }
    }    
}
//reduces delay time in order to increase speed
public void faster(){
   int delay = moveTimer.getDelay();
        delay -=25;
        if(delay>0)
        moveTimer.setDelay(delay); 
}
//calls move down method for each alien and resets dropReady flag
public void drop(){
    for(int i=0; i<aliens.length; i++){
        for(int j=0; j<aliens[i].length; j++){
            aliens[i][j].moveDown();
        }
    }
    dropReady=false;
}
//used when reset button is pressed. Sets aliens their initial position and redraws all aliens
public void resetPos(){
    for(int i=0; i<aliens.length; i++){
        for(int j=0;j<aliens[i].length; j++){
        aliens[i][j].getLocation().x = (j*2) * (aliens[i][j].getWidth() + aliens[i][j].getWidth()/2);
        aliens[i][j].getLocation().y = (i*2) * ((aliens[i][j].getHeight() + aliens[i][j].getHeight()/2));
        aliens[i][j].getLocation().y+= gamePanel.getHeight()/6;
        aliens[i][j].setVisible(true);
        }
    }  
    gamePanel.repaint();
}
//mutator and accessor methods

}

我意识到另一个与我相似的question已经被问到了,但是在阅读它时我觉得我已经实施了他们的建议无济于事,所以其他东西必然会导致问题。任何帮助表示赞赏

0 个答案:

没有答案