Java 使用构造函数重载和带有继承的预先存在的静态对象
这是关于我正在做的一个关于太空入侵者的项目的第二个问题 我已经重新组织了代码以使用继承和ArrayList。 在我的项目中,类Ship、wearm和Bullet扩展了类实体(其中实体是可以出现在游戏屏幕上的任何“东西”,具有坐标、方向和一组由文件名数组表示的精灵) 在处理游戏屏幕的类中,我有一套预先存在的武器,可以直接用于它们的统计数据,以方便使用。 这些是使用标准武器构造函数创建的,其中包含对实体的超级构造函数调用 我的问题来自于武器的重载构造函数: 我想把这个武器构装器和武器所连接的飞船联系起来(注意,这是两个独立的武器,平行发射,与飞船中心隔开),这样武器就会自动创建,其属性与当前游戏场景相匹配(飞船跟踪装载哪种武器,然后在屏幕上绘制每一帧) 以下是课程: 父类(实体) 子类,飞船和武器(我也有子弹,但如果我解决了武器问题,它也会解决子弹) 船舶: 接下来的错误来自第二个武器构造函数中的“super”调用,以及随后的this.assignment调用,即整个过程都不起作用哈哈 对GameScreen类的引用(其本身将引用图形) 这些类别包括: GameScreen(我仍然需要添加变种) 图形:Java 使用构造函数重载和带有继承的预先存在的静态对象,java,inheritance,parameters,constructor,overloading,Java,Inheritance,Parameters,Constructor,Overloading,这是关于我正在做的一个关于太空入侵者的项目的第二个问题 我已经重新组织了代码以使用继承和ArrayList。 在我的项目中,类Ship、wearm和Bullet扩展了类实体(其中实体是可以出现在游戏屏幕上的任何“东西”,具有坐标、方向和一组由文件名数组表示的精灵) 在处理游戏屏幕的类中,我有一套预先存在的武器,可以直接用于它们的统计数据,以方便使用。 这些是使用标准武器构造函数创建的,其中包含对实体的超级构造函数调用 我的问题来自于武器的重载构造函数: 我想把这个武器构装器和武器所连接的飞船联系
//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);
}
}
//库
公共类图形
{
//全局变量
public static final int REFRESH_RATE=20;//延迟(毫秒)
公共静态最终双X_标度=100;
公共静态最终双Y_标度=100;
公共静态最终整数X_SIZE=512;
公共静态最终int Y_尺寸=624;
//四级运动循环
公共静态最终双游戏时钟最大值=X_刻度*(0.6)+Y_刻度*(0.1);
//精灵
公共静态最终字符串[][]SHIP_精灵=
{
{“BasicShip_TRANS.png”},//Defender[0][…]
{“BasicInvader_TRANS.png”}//入侵者[1][…]
};
公共静态最终字符串[][]武器精灵=
{
{“MG_L_TRANS.png”,“MG_R_TRANS.png”},//机枪[0][…]
{“PMG_L_TRANS.png”,“PMG_R_TRANS.png”},//等离子机枪[1][…]
{“PC_L_TRANS.png”,“PC_R_TRANS.png”},//光子炮[2][…]
{“AD_L_TRANS.png”,“AD_R_TRANS.png”}//外来不稳定因素[3][…]
};
公共静态最终字符串[][]项目符号\u精灵=
{
{“MG_PROJ_TRANS.png”},
{“PMG_PROJ_TRANS.png”},
{“PC_PROJ_TRANS.png”},
{“AD_PROJ.png”}
};
//精灵的尽头
//功能
公共静态菜单()
{
透明(灰色);
StdDraw.text(50,80,“蜂群”);
StdDraw.text(50,50,“P-Play”);
StdDraw.text(50,40,“Q-Quit”);
}
公共静态void init()
{
StdDraw.setCanvasSize(X_大小,Y_大小);
标准刻度(0.0,X_刻度);
标准设定标度(0.0,Y_标度);
drawMenu();
}
public static void drawShip(int类型,Ship Ship)//类型:0防御者,1入侵者
{
如果(类型>1 | |类型<0)
{
System.out.println(“无效船型”);
返回;
}
int hp=ship.getState().getHealth();
如果(hp>80)StdDraw.picture(ship.getX()、ship.getY()、ship_精灵[type][0]);
//待办事项
}
公共静态双方向(Ship defender)//确定武器方向到鼠标指针方向
{
//顺时针旋转270+θ
if(defender.getX()StdDraw.mouseX())
{
if(defender.getY()//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;
}
}
//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);
}
}
public static final Weapon[][] WEAPONS