通过继承使用构造函数重载和预先存在的静态对象
Using constructor overloading and pre-existing static objects with inheritance
这是我关于 Space Invaders 项目的第二个问题。
我重新组织了我的代码以使用继承和 ArrayList。
在我的项目中,classes Ship、Weapon 和 Bullet 扩展了 class 实体(其中实体是可以出现在游戏屏幕上的任何 "thing",具有坐标,方向和由文件名数组表示的精灵集)
在处理游戏屏幕的 class 中,我有预先存在的武器组,为了方便起见,可以直接使用它们的统计数据。
这些是使用标准 Weapon 构造函数创建的,其中包含对 Entity 的超级构造函数调用。
我的问题来自 Weapon 的重载构造函数:
我想将此武器构造器与武器所连接的飞船相关联(请注意,这是两个平行发射的独立武器,与飞船的中心隔开),以便自动创建具有与当前游戏场景匹配的属性的武器( ship 跟踪要装载的武器,然后为每一帧在屏幕上绘制)。
这里是 classes:
父级 class(实体)
//IMMUTABLE
public class Entity extends Object
{
//FIELDS
private final double x;
private final double y;
private final double orientation;
private final String[] sprites;
//CONSTRUCTOR
public Entity(double x, double y, double orientation, String[] sprites)
{
this.x = x;
this.y = y;
this.orientation = orientation;
this.sprites = sprites;
}
//ACCESSORS
public double getX()
{
return this.x;
}
public double getY()
{
return this.y;
}
public double getOrientation()
{
return this.orientation;
}
private String[] getSprites()
{
return this.sprites;
}
}
Sub-classes,飞船和武器(我也有子弹,但如果我解决了武器问题,它也会修复子弹)
船:
//IMMUTABLE
public class Ship extends Entity
{
//GLOBAL VARIABLES
public static final double SHIP_MOVE_INT = 1;
//100 hp, weapon 0, not moving, no thrusters
public static final State defState = new State(100, 0, false, false, 0);
//FIELDS
private final State state;
//CONSTRUCTOR
public Ship(double x, double y, double orientation, String[] sprites, State state)
{
super(x, y, orientation, sprites);
this.state = state;
}
//ACCESSORS
public State getState()
{
return this.state;
}
//METHODS
public void moveLeft()
{
if (this.x > 15*SHIP_MOVE_INT)
{
this.x -= SHIP_MOVE_INT;
}
}
public void moveRight()
{
if (this.x < Graphics.X_SCALE - 15*SHIP_MOVE_INT)
{
this.x += SHIP_MOVE_INT;
}
}
//Works, but revise
public void invaderPattern(double gameClock)
{
double stage1Bound = 0.3*Graphics.X_SCALE;
double stage2Bound = stage1Bound + 0.05*Graphics.Y_SCALE;
double stage3Bound = stage2Bound + stage1Bound;
double stage4Bound = stage3Bound + 0.05*Graphics.Y_SCALE;
if (gameClock < stage1Bound)
{
//Move right
this.state.setMovingRight(true);
this.state.setMovingLeft(false);
this.x += SHIP_MOVE_INT;
}
else if (gameClock >= stage1Bound && gameClock < stage2Bound)
{
//Move down
this.state.setMovingRight(false);
this.state.setMovingLeft(false);
this.y -= SHIP_MOVE_INT;
}
else if (gameClock >= stage2Bound && gameClock < stage3Bound)
{
//Move left
this.state.setMovingLeft(true);
this.state.setMovingRight(false);
this.x -= SHIP_MOVE_INT;
}
else
{
//Move down
this.state.setMovingRight(false);
this.state.setMovingLeft(false);
this.y -= SHIP_MOVE_INT;
}
}
}
武器:
//IMMUTABLE
public class Weapon extends Entity
{
//FIELDS
private final String type;
private final int damage;
private final int rof; //Rate of fire
private final int firingStage;
//CONSTRUCTORS
public Weapon(double x, double y, double orientation, String[] sprites, String type, int damage, int rof, int firingStage)
{
super(x, y, orientation, sprites);
this.type = type;
this.damage = damage;
this.rof = rof;
this.firingStage = firingStage;
}
public Weapon(double x, Ship defender, double orientation)
{
super(x, defender.getY(), orientation, GameScreen.WEAPONS[defender.getState().getWeapon()].getSprites());
this.type = GameScreen.WEAPONS[defender.getState().getWeapon()].getType();
this.damage = GameScreen.WEAPONS[defender.getState().getWeapon()].getDamage();
this.rof = GameScreen.WEAPONS[defender.getState().getWeapon()].getRof();
this.firingStage = GameScreen.WEAPONS[defender.getState().getWeapon()].getFiringStage();
}
//END OF CONSTRUCTORS
//ACCESSORS
public String getType()
{
return this.type;
}
public int getDamage()
{
return this.damage;
}
public int getRof()
{
return this.rof;
}
public int getFiringStage()
{
return this.firingStage;
}
}
随后的错误来自第二个 Weapon 构造函数中的 "super" 调用,以及 this.紧随其后的分配调用,即整个事情都不起作用哈哈
https://drive.google.com/file/d/0B7ye7Ul2JDG2cy0yak82eUZaclE/view?usp=sharing
对 class GameScreen 的引用(它本身将引用 Graphics)
这些是 classes:
GameScreen(我还得加Mutators)
//MUTABLE - NB
public class GameScreen
{
//GLOBAL VARIABLES
public static final String HIGH_SCORE_FILE = "highScore.txt";
//FIELDS
private Ship defender;
private Weapon[] weapons;
private ArrayList invaders;
private ArrayList bullets;
private int score;
private int lives;
private int highscore;
private double gameClock;
//AVAILABLE WEAPONS
public static final Weapon[][] WEAPONS = new Weapon[][]
{
{
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[0], "Machinegun L", 10, 20, 0),
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[0], "Machinegun R", 10, 20, 0)
},
{
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[1], "Plasma MG L", 20, 20, 0),
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[1], "Plasma MG L", 20, 20, 0)
},
{
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[2], "Photon Cannon L", 40, 5, 0),
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[2], "Photon Cannon R", 40, 5, 0)
},
{
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[3], "Alien Destabilizer L", 60, 10, 0),
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[3], "Alien Destabilizer L", 60, 10, 0)
}
};
//AVAILABLE BULLETS
public static final Bullet[] BULLETS = new Bullet[] //Correspond to WEAPONS above
{
new Bullet(0, 0, 0, Graphics.BULLET_SPRITES[0], WEAPONS[0][0].getDamage()),
new Bullet(0, 0, 0, Graphics.BULLET_SPRITES[1], WEAPONS[1][0].getDamage()),
new Bullet(0, 0, 0, Graphics.BULLET_SPRITES[2], WEAPONS[2][0].getDamage()),
new Bullet(0, 0, 0, Graphics.BULLET_SPRITES[3], WEAPONS[3][0].getDamage())
};
//CONSTRUCTOR
public GameScreen(Ship defender, Weapon[] weapons, ArrayList invaders, ArrayList bullets, int score, int lives)
{
this.defender = defender;
this.weapons = weapons;
this.invaders = invaders;
this.bullets = bullets;
this.score = score;
this.lives = lives;
this.loadHighscore();
this.gameClock = 0;
}
//METHODS
public void clk()
{
this.gameClock++;
//Should only be called when the game itself is being played, not menus
}
public void loadHighscore()
{
try
{
Scanner sc = new Scanner(new File(HIGH_SCORE_FILE));
this.highscore = Integer.parseInt(sc.next());
sc.close();
}
catch(FileNotFoundException fnf)
{
System.out.println(fnf);
this.highscore = 0;
}
}
public void saveHighScore(int highscore)
{
try
{
FileWriter write = new FileWriter(HIGH_SCORE_FILE);
PrintWriter pw = new PrintWriter(write);
pw.print(this.highscore);
pw.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
//ACCESSORS
public Ship getDefender()
{
return this.defender;
}
public Weapon[] getWeapons()
{
return this.weapons;
}
public ArrayList getInvaders()
{
return this.invaders;
}
public ArrayList getBullets()
{
return this.bullets;
}
public int getScore()
{
return this.score;
}
public int getHighscore()
{
return this.highscore;
}
public int getLives()
{
return this.lives;
}
public double getGameClock()
{
return this.gameClock;
}
}
图形:
//LIBRARY
public class Graphics
{
//GLOBAL VARIABLES
public static final int REFRESH_RATE = 20; //delay in milliseconds
public static final double X_SCALE = 100;
public static final double Y_SCALE = 100;
public static final int X_SIZE = 512;
public static final int Y_SIZE = 624;
//Cycles of 4 stage motions
public static final double gameClockMax = X_SCALE*(0.6) + Y_SCALE*(0.1);
//SPRITES
public static final String[][] SHIP_SPRITES =
{
{"BasicShip_TRANS.png"}, //Defender [0][...]
{"BasicInvader_TRANS.png"} //Invader [1][...]
};
public static final String[][] WEAPON_SPRITES =
{
{"MG_L_TRANS.png", "MG_R_TRANS.png"}, //Machine Gun [0][...]
{"PMG_L_TRANS.png", "PMG_R_TRANS.png"}, //Plasma Machine Gun [1][...]
{"PC_L_TRANS.png", "PC_R_TRANS.png"}, //Photon Cannon [2][...]
{"AD_L_TRANS.png", "AD_R_TRANS.png"} //Alien Destabilizer [3][...]
};
public static final String[][] BULLET_SPRITES =
{
{"MG_PROJ_TRANS.png"},
{"PMG_PROJ_TRANS.png"},
{"PC_PROJ_TRANS.png"},
{"AD_PROJ.png"}
};
//END OF SPRITES
//FUNCTIONS
public static void drawMenu()
{
StdDraw.clear(StdDraw.GRAY);
StdDraw.text(50, 80, "THE SWARM");
StdDraw.text(50, 50, "P - Play");
StdDraw.text(50, 40, "Q - Quit");
}
public static void init()
{
StdDraw.setCanvasSize(X_SIZE, Y_SIZE);
StdDraw.setXscale(0.0, X_SCALE);
StdDraw.setYscale(0.0, Y_SCALE);
drawMenu();
}
public static void drawShip(int type, Ship ship) // type: 0 Defender , 1 Invader
{
if (type > 1 || type < 0)
{
System.out.println("Invalid ship type");
return;
}
int hp = ship.getState().getHealth();
if (hp > 80) StdDraw.picture(ship.getX(), ship.getY(), SHIP_SPRITES[type][0]);
//TODO
}
public static double orientation(Ship defender) //Determine weapon orientation to mouse pointer direction
{
//Clockwise rotation thus 270 + theta
if (defender.getX() < StdDraw.mouseX())
{
if (defender.getY() < StdDraw.mouseY())
{
return 270 + Math.toDegrees(Math.atan((StdDraw.mouseY() - defender.getY())/(StdDraw.mouseX() - defender.getX())));
}
else
{
return 270;
}
}
else if (defender.getX() > StdDraw.mouseX())
{
if (defender.getY() < StdDraw.mouseY())
{
return (180) + (270 + Math.toDegrees(Math.atan((StdDraw.mouseY() - defender.getY())/(StdDraw.mouseX() - defender.getX()))));
}
else
{
return 90;
}
}
else
{
return 0;
}
}
public static void drawWeapons(Weapon[] weapons)
{
//Left
StdDraw.picture
(
weapons[0].getX(),
weapons[0].getY(),
weapons[0].getSprites()[0],
weapons[0].getOrientation()
);
//Right
StdDraw.picture
(
weapons[1].getX(),
weapons[1].getY(),
weapons[1].getSprites()[1],
weapons[1].getOrientation()
);
}
public static void drawBullet(Bullet bullet)
{
StdDraw.picture
(
bullet.getX(),
bullet.getY(),
bullet.getSprites()[0],
bullet.getOrientation()
);
}
//Primary function
public void animate(GameScreen gs)
{
//TODO
//accept display of stats (hp, lives, score, etc)
/* Order of drawing:
* 1 - Background
* 2 - Borders
* 3 - Stats
* 4 - Thrusters
* 5 - Weapons
* 6 - Ships
* 7 - Bullets
* 8 - Effects
* 9 - Notifications (Combo indicators etc)
*/
//1
StdDraw.clear(StdDraw.GRAY);
//5
drawWeapons(gs.getWeapons());
//6
drawShip(gs.getDefender());
for (int i = 0; i < gs.getInvaders().size(); i++)
{
gs.getInvaders().get(i).invaderPattern(gs.getGameClock());
drawShip(gs.getInvaders().get(i));
}
//REFRESH RATE DELAY
StdDraw.show(REFRESH_RATE);
}
}
总而言之,我正在尝试重载武器构造函数,以便它使用现有的武器配置(在 GameScreen 中列出,使用第一个武器构造函数制作)进行自我设置,其中一个将根据船舶的武器类型状态。
编译器对此抱怨很多,我试图确定为什么 >.<
GameScreen.Weapons
是这样定义的二维数组:
public static final Weapon[][] WEAPONS
这意味着,你有武器类型的二维数组。
例如,如果您使用这个 GameScreen.Weapons[0]
它 returns 数组的第一行,因此是武器的一维数组。而且数组没有像 .getDamage()
这样的方法
PS : 我想你误解了什么是多维数组,我建议找一些教程(也许图片就足够了,很简单,二维数组就是矩阵)
我的问题有两个:
在 GameScreen 中,我将 WEAPONS 设为二维数组,而在重载的 Weapon 构造函数中,我试图仅访问一维,返回一个数组而不是单个 Weapon 对象。
其次,出于某些愚蠢的原因,我将 Entity 中的 "getSprites" 方法设为私有而不是 public,因此重载的构造函数无法访问它。
这是我关于 Space Invaders 项目的第二个问题。
我重新组织了我的代码以使用继承和 ArrayList。 在我的项目中,classes Ship、Weapon 和 Bullet 扩展了 class 实体(其中实体是可以出现在游戏屏幕上的任何 "thing",具有坐标,方向和由文件名数组表示的精灵集)
在处理游戏屏幕的 class 中,我有预先存在的武器组,为了方便起见,可以直接使用它们的统计数据。 这些是使用标准 Weapon 构造函数创建的,其中包含对 Entity 的超级构造函数调用。
我的问题来自 Weapon 的重载构造函数: 我想将此武器构造器与武器所连接的飞船相关联(请注意,这是两个平行发射的独立武器,与飞船的中心隔开),以便自动创建具有与当前游戏场景匹配的属性的武器( ship 跟踪要装载的武器,然后为每一帧在屏幕上绘制)。
这里是 classes:
父级 class(实体)
//IMMUTABLE
public class Entity extends Object
{
//FIELDS
private final double x;
private final double y;
private final double orientation;
private final String[] sprites;
//CONSTRUCTOR
public Entity(double x, double y, double orientation, String[] sprites)
{
this.x = x;
this.y = y;
this.orientation = orientation;
this.sprites = sprites;
}
//ACCESSORS
public double getX()
{
return this.x;
}
public double getY()
{
return this.y;
}
public double getOrientation()
{
return this.orientation;
}
private String[] getSprites()
{
return this.sprites;
}
}
Sub-classes,飞船和武器(我也有子弹,但如果我解决了武器问题,它也会修复子弹)
船:
//IMMUTABLE
public class Ship extends Entity
{
//GLOBAL VARIABLES
public static final double SHIP_MOVE_INT = 1;
//100 hp, weapon 0, not moving, no thrusters
public static final State defState = new State(100, 0, false, false, 0);
//FIELDS
private final State state;
//CONSTRUCTOR
public Ship(double x, double y, double orientation, String[] sprites, State state)
{
super(x, y, orientation, sprites);
this.state = state;
}
//ACCESSORS
public State getState()
{
return this.state;
}
//METHODS
public void moveLeft()
{
if (this.x > 15*SHIP_MOVE_INT)
{
this.x -= SHIP_MOVE_INT;
}
}
public void moveRight()
{
if (this.x < Graphics.X_SCALE - 15*SHIP_MOVE_INT)
{
this.x += SHIP_MOVE_INT;
}
}
//Works, but revise
public void invaderPattern(double gameClock)
{
double stage1Bound = 0.3*Graphics.X_SCALE;
double stage2Bound = stage1Bound + 0.05*Graphics.Y_SCALE;
double stage3Bound = stage2Bound + stage1Bound;
double stage4Bound = stage3Bound + 0.05*Graphics.Y_SCALE;
if (gameClock < stage1Bound)
{
//Move right
this.state.setMovingRight(true);
this.state.setMovingLeft(false);
this.x += SHIP_MOVE_INT;
}
else if (gameClock >= stage1Bound && gameClock < stage2Bound)
{
//Move down
this.state.setMovingRight(false);
this.state.setMovingLeft(false);
this.y -= SHIP_MOVE_INT;
}
else if (gameClock >= stage2Bound && gameClock < stage3Bound)
{
//Move left
this.state.setMovingLeft(true);
this.state.setMovingRight(false);
this.x -= SHIP_MOVE_INT;
}
else
{
//Move down
this.state.setMovingRight(false);
this.state.setMovingLeft(false);
this.y -= SHIP_MOVE_INT;
}
}
}
武器:
//IMMUTABLE
public class Weapon extends Entity
{
//FIELDS
private final String type;
private final int damage;
private final int rof; //Rate of fire
private final int firingStage;
//CONSTRUCTORS
public Weapon(double x, double y, double orientation, String[] sprites, String type, int damage, int rof, int firingStage)
{
super(x, y, orientation, sprites);
this.type = type;
this.damage = damage;
this.rof = rof;
this.firingStage = firingStage;
}
public Weapon(double x, Ship defender, double orientation)
{
super(x, defender.getY(), orientation, GameScreen.WEAPONS[defender.getState().getWeapon()].getSprites());
this.type = GameScreen.WEAPONS[defender.getState().getWeapon()].getType();
this.damage = GameScreen.WEAPONS[defender.getState().getWeapon()].getDamage();
this.rof = GameScreen.WEAPONS[defender.getState().getWeapon()].getRof();
this.firingStage = GameScreen.WEAPONS[defender.getState().getWeapon()].getFiringStage();
}
//END OF CONSTRUCTORS
//ACCESSORS
public String getType()
{
return this.type;
}
public int getDamage()
{
return this.damage;
}
public int getRof()
{
return this.rof;
}
public int getFiringStage()
{
return this.firingStage;
}
}
随后的错误来自第二个 Weapon 构造函数中的 "super" 调用,以及 this.紧随其后的分配调用,即整个事情都不起作用哈哈 https://drive.google.com/file/d/0B7ye7Ul2JDG2cy0yak82eUZaclE/view?usp=sharing
对 class GameScreen 的引用(它本身将引用 Graphics) 这些是 classes:
GameScreen(我还得加Mutators)
//MUTABLE - NB
public class GameScreen
{
//GLOBAL VARIABLES
public static final String HIGH_SCORE_FILE = "highScore.txt";
//FIELDS
private Ship defender;
private Weapon[] weapons;
private ArrayList invaders;
private ArrayList bullets;
private int score;
private int lives;
private int highscore;
private double gameClock;
//AVAILABLE WEAPONS
public static final Weapon[][] WEAPONS = new Weapon[][]
{
{
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[0], "Machinegun L", 10, 20, 0),
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[0], "Machinegun R", 10, 20, 0)
},
{
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[1], "Plasma MG L", 20, 20, 0),
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[1], "Plasma MG L", 20, 20, 0)
},
{
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[2], "Photon Cannon L", 40, 5, 0),
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[2], "Photon Cannon R", 40, 5, 0)
},
{
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[3], "Alien Destabilizer L", 60, 10, 0),
new Weapon(0, 0, 0, Graphics.WEAPON_SPRITES[3], "Alien Destabilizer L", 60, 10, 0)
}
};
//AVAILABLE BULLETS
public static final Bullet[] BULLETS = new Bullet[] //Correspond to WEAPONS above
{
new Bullet(0, 0, 0, Graphics.BULLET_SPRITES[0], WEAPONS[0][0].getDamage()),
new Bullet(0, 0, 0, Graphics.BULLET_SPRITES[1], WEAPONS[1][0].getDamage()),
new Bullet(0, 0, 0, Graphics.BULLET_SPRITES[2], WEAPONS[2][0].getDamage()),
new Bullet(0, 0, 0, Graphics.BULLET_SPRITES[3], WEAPONS[3][0].getDamage())
};
//CONSTRUCTOR
public GameScreen(Ship defender, Weapon[] weapons, ArrayList invaders, ArrayList bullets, int score, int lives)
{
this.defender = defender;
this.weapons = weapons;
this.invaders = invaders;
this.bullets = bullets;
this.score = score;
this.lives = lives;
this.loadHighscore();
this.gameClock = 0;
}
//METHODS
public void clk()
{
this.gameClock++;
//Should only be called when the game itself is being played, not menus
}
public void loadHighscore()
{
try
{
Scanner sc = new Scanner(new File(HIGH_SCORE_FILE));
this.highscore = Integer.parseInt(sc.next());
sc.close();
}
catch(FileNotFoundException fnf)
{
System.out.println(fnf);
this.highscore = 0;
}
}
public void saveHighScore(int highscore)
{
try
{
FileWriter write = new FileWriter(HIGH_SCORE_FILE);
PrintWriter pw = new PrintWriter(write);
pw.print(this.highscore);
pw.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
//ACCESSORS
public Ship getDefender()
{
return this.defender;
}
public Weapon[] getWeapons()
{
return this.weapons;
}
public ArrayList getInvaders()
{
return this.invaders;
}
public ArrayList getBullets()
{
return this.bullets;
}
public int getScore()
{
return this.score;
}
public int getHighscore()
{
return this.highscore;
}
public int getLives()
{
return this.lives;
}
public double getGameClock()
{
return this.gameClock;
}
}
图形:
//LIBRARY
public class Graphics
{
//GLOBAL VARIABLES
public static final int REFRESH_RATE = 20; //delay in milliseconds
public static final double X_SCALE = 100;
public static final double Y_SCALE = 100;
public static final int X_SIZE = 512;
public static final int Y_SIZE = 624;
//Cycles of 4 stage motions
public static final double gameClockMax = X_SCALE*(0.6) + Y_SCALE*(0.1);
//SPRITES
public static final String[][] SHIP_SPRITES =
{
{"BasicShip_TRANS.png"}, //Defender [0][...]
{"BasicInvader_TRANS.png"} //Invader [1][...]
};
public static final String[][] WEAPON_SPRITES =
{
{"MG_L_TRANS.png", "MG_R_TRANS.png"}, //Machine Gun [0][...]
{"PMG_L_TRANS.png", "PMG_R_TRANS.png"}, //Plasma Machine Gun [1][...]
{"PC_L_TRANS.png", "PC_R_TRANS.png"}, //Photon Cannon [2][...]
{"AD_L_TRANS.png", "AD_R_TRANS.png"} //Alien Destabilizer [3][...]
};
public static final String[][] BULLET_SPRITES =
{
{"MG_PROJ_TRANS.png"},
{"PMG_PROJ_TRANS.png"},
{"PC_PROJ_TRANS.png"},
{"AD_PROJ.png"}
};
//END OF SPRITES
//FUNCTIONS
public static void drawMenu()
{
StdDraw.clear(StdDraw.GRAY);
StdDraw.text(50, 80, "THE SWARM");
StdDraw.text(50, 50, "P - Play");
StdDraw.text(50, 40, "Q - Quit");
}
public static void init()
{
StdDraw.setCanvasSize(X_SIZE, Y_SIZE);
StdDraw.setXscale(0.0, X_SCALE);
StdDraw.setYscale(0.0, Y_SCALE);
drawMenu();
}
public static void drawShip(int type, Ship ship) // type: 0 Defender , 1 Invader
{
if (type > 1 || type < 0)
{
System.out.println("Invalid ship type");
return;
}
int hp = ship.getState().getHealth();
if (hp > 80) StdDraw.picture(ship.getX(), ship.getY(), SHIP_SPRITES[type][0]);
//TODO
}
public static double orientation(Ship defender) //Determine weapon orientation to mouse pointer direction
{
//Clockwise rotation thus 270 + theta
if (defender.getX() < StdDraw.mouseX())
{
if (defender.getY() < StdDraw.mouseY())
{
return 270 + Math.toDegrees(Math.atan((StdDraw.mouseY() - defender.getY())/(StdDraw.mouseX() - defender.getX())));
}
else
{
return 270;
}
}
else if (defender.getX() > StdDraw.mouseX())
{
if (defender.getY() < StdDraw.mouseY())
{
return (180) + (270 + Math.toDegrees(Math.atan((StdDraw.mouseY() - defender.getY())/(StdDraw.mouseX() - defender.getX()))));
}
else
{
return 90;
}
}
else
{
return 0;
}
}
public static void drawWeapons(Weapon[] weapons)
{
//Left
StdDraw.picture
(
weapons[0].getX(),
weapons[0].getY(),
weapons[0].getSprites()[0],
weapons[0].getOrientation()
);
//Right
StdDraw.picture
(
weapons[1].getX(),
weapons[1].getY(),
weapons[1].getSprites()[1],
weapons[1].getOrientation()
);
}
public static void drawBullet(Bullet bullet)
{
StdDraw.picture
(
bullet.getX(),
bullet.getY(),
bullet.getSprites()[0],
bullet.getOrientation()
);
}
//Primary function
public void animate(GameScreen gs)
{
//TODO
//accept display of stats (hp, lives, score, etc)
/* Order of drawing:
* 1 - Background
* 2 - Borders
* 3 - Stats
* 4 - Thrusters
* 5 - Weapons
* 6 - Ships
* 7 - Bullets
* 8 - Effects
* 9 - Notifications (Combo indicators etc)
*/
//1
StdDraw.clear(StdDraw.GRAY);
//5
drawWeapons(gs.getWeapons());
//6
drawShip(gs.getDefender());
for (int i = 0; i < gs.getInvaders().size(); i++)
{
gs.getInvaders().get(i).invaderPattern(gs.getGameClock());
drawShip(gs.getInvaders().get(i));
}
//REFRESH RATE DELAY
StdDraw.show(REFRESH_RATE);
}
}
总而言之,我正在尝试重载武器构造函数,以便它使用现有的武器配置(在 GameScreen 中列出,使用第一个武器构造函数制作)进行自我设置,其中一个将根据船舶的武器类型状态。 编译器对此抱怨很多,我试图确定为什么 >.<
GameScreen.Weapons
是这样定义的二维数组:
public static final Weapon[][] WEAPONS
这意味着,你有武器类型的二维数组。
例如,如果您使用这个 GameScreen.Weapons[0]
它 returns 数组的第一行,因此是武器的一维数组。而且数组没有像 .getDamage()
PS : 我想你误解了什么是多维数组,我建议找一些教程(也许图片就足够了,很简单,二维数组就是矩阵)
我的问题有两个:
在 GameScreen 中,我将 WEAPONS 设为二维数组,而在重载的 Weapon 构造函数中,我试图仅访问一维,返回一个数组而不是单个 Weapon 对象。
其次,出于某些愚蠢的原因,我将 Entity 中的 "getSprites" 方法设为私有而不是 public,因此重载的构造函数无法访问它。