Java 太空入侵者娱乐:外星人向下移动时继续横向移动
对于编程类,我必须用java重新创建游戏空间入侵者。我已经走了相当远的路,实际上几乎完成了,但是当我的外星人中队下降一级时,有一个问题。有趣的是,它出现在我的2D数组的前两列中。当中队第一次集体下降时,整个第一排移向右侧,第二排的第一个外星人移向左侧。在随后的所有下降中,只有第二排的第一个外星人继续向左移动越来越远。最初,我编写代码,以便首先更改每个外星人的x和y位置,然后调用repait方法。考虑到这就是问题所在,我删除了双for循环外y位置的更改。因此,现在每个外星人的x位置都会改变,屏幕会重新绘制,以便向左或向右移动。然后,如果drop ready boolean(在double for循环中)设置为true,则每个外星人的y位置将下移,并再次重新绘制屏幕。因此,每种类型的更改都有一个单独的repaint()调用。不过,这也没用,我也束手无策。下面列出了我的外星人级和中队级代码。有人知道为什么会这样吗 外国人类别:Java 太空入侵者娱乐:外星人向下移动时继续横向移动,java,animation,repaint,Java,Animation,Repaint,对于编程类,我必须用java重新创建游戏空间入侵者。我已经走了相当远的路,实际上几乎完成了,但是当我的外星人中队下降一级时,有一个问题。有趣的是,它出现在我的2D数组的前两列中。当中队第一次集体下降时,整个第一排移向右侧,第二排的第一个外星人移向左侧。在随后的所有下降中,只有第二排的第一个外星人继续向左移动越来越远。最初,我编写代码,以便首先更改每个外星人的x和y位置,然后调用repait方法。考虑到这就是问题所在,我删除了双for循环外y位置的更改。因此,现在每个外星人的x位置都会改变,屏幕会
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
}
公共类中队实现ActionListener{
私人外星人[][]外星人=新外星人[5][12];//入侵外星人的2D数组
专用定时器moveTimer;//控制中队的移动
private int numVisible;//当前未被销毁的外星人数量
private int moveCount;//表示团队移动的次数
私人游戏面板游戏面板;
私有布尔dropReady;//确定中队是否准备好放下
公共中队(游戏面板总成){
dropReady=false;
moveCount=0;
//初始化所有外星人
对于(int i=0;我也被问到了类似于我的问题,但在仔细阅读时,我觉得我已经实现了他们的建议,但没有任何效果,所以一定是其他原因造成了问题。感谢您的任何帮助我发现很难检查您当前发布的代码,我想游戏中涉及到更多的类,并且不容易看到发生了什么乍一看是这样。作为一个小提示,我建议您检查代码中横向移动alliens的每一点。然后调试代码,看看它们从哪里被调用。我一直在这样做,但还没有注意到任何东西。我将再次尝试,并试着更仔细地查看。但唯一的其他自制类是GamePanel,它是一个JPanel,包含所有游戏对象,如宇宙飞船、外星人、底部的佳能等。paintComponent()方法在该类中。