这是关于我在Space Invaders上进行的项目的第二个问题。
我重新组织了我的代码以使用继承和ArrayLists。 在我的项目中,类Ship,Weapon和Bullet扩展了类Entity(其中一个实体是任何" thing"可以在游戏屏幕上出现,具有坐标,方向和一组精灵,由文件名数组表示)
在处理游戏屏幕的类中,我有预先存在的武器套装,可以直接用于他们的统计数据以方便使用。 这些是使用标准的Weapon构造函数创建的,该构造函数包含对Entity的超级构造函数调用。
我的问题来自于Weapon的重载构造函数: 我想将这个武器构造函数与武器所附着的船相关联(注意它是两个独立的武器,并行射击,与船的中心隔开),这样就可以自动创建武器了。与当前游戏场景匹配的属性(船只跟踪要加载的武器,然后在每个帧的屏幕上绘制)。
以下是课程:
父类(实体)
//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;
}
}
子类,船舶和武器(我也有子弹,但如果我解决了武器问题,它也将修复子弹)
SHIP:
//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;
}
}
以下错误来自&#34; super&#34;调用第二个Weapon构造函数,以及它。哈哈,跟随它的任务调用,即整个事情都不起作用 https://drive.google.com/file/d/0B7ye7Ul2JDG2cy0yak82eUZaclE/view?usp=sharing
GameScreen类的引用(本身将引用Graphics) 是这些课程:
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;
}
}
GRAPHICS:
//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);
}
}
总而言之,我试图重载Weapon构造函数,以便使用现有的Weapon配置(在GameScreen中列出,使用第一个Weapon构造函数)来设置自己,其中一个将根据Ship& #39;武器类型状态。 编译器对此抱怨很多,我试图确定为什么&gt;。&lt;
答案 0 :(得分:1)
GameScreen.Weapons
是二维数组,定义如下:
public static final Weapon[][] WEAPONS
这意味着,你有两个类型武器的数组。
如果您使用例如此GameScreen.Weapons[0]
,它将返回数组中的第一行,从而返回一维武器数组。并且数组没有像.getDamage()
答案 1 :(得分:0)
我的问题有两方面:
在GameScreen中,我使WEAPONS成为一个2D数组,而在重载的Weapon构造函数中,我试图只访问一个维度,返回一个数组而不是一个Weapon对象。
其次,由于某些愚蠢的原因,我在Entity中创建了“getSprites”方法而不是public,因此重载的构造函数无法访问它。