我正在写一个让两个坦克互相撞击的程序。我已经写了除了结束游戏的火之外的所有方法。到目前为止
public class Tank {
Tank() {
int xPos, yPos;
char direction;
boolean loaded = 0;
}
public void move(Tank currentPlayer) {
if (yPos<=4 && yPos>=0) {
if (xPos<=4 && xPos>=0) {
if (currentPlayer.direction == 'u') {
currentPlayer.yPos--;
}
if (currentPlayer.direction == 'd') {
currentPlayer.yPos++;
}
if (currentPlayer.direction == 'l') {
currentPlayer.xPos--;
}
if (currentPlayer.direction == 'r') {
currentPlayer.xPos++;
}
}
}
}
public void turn(boolean bool, Tank currentPlayer) {
if (currentPlayer.direction == 'u') {
currentPlayer.direction ='r';
}
if (currentPlayer.direction == 'd') {
currentPlayer.direction = 'l';
}
if (currentPlayer.direction == 'l') {
currentPlayer.direction ='u';
}
if (currentPlayer.direction == 'r') {
currentPlayer.direction ='d';
}
}
public void load(Tank currentPlayer) {
currentPlayer.loaded=true;
}
public int fire(Tank currentPlayer, Tank jim) {
// ???
}
}
如何完成我的计划?任何帮助将不胜感激。
答案 0 :(得分:1)
你需要一个吸气剂来测量水箱的x位置,y位置和当前方向。例如:
public int getX();
public int getY();
public char getOrientation();
通过调用这些方法,您应该获得坦克的x位置,y位置和方向。然后检查命中。你知道你面临的方向。因此,如果你正面朝上,看看另一个坦克是否与你在同一个x上,并且他们的y高于你的。
int otherX = jim.getX();
int otherY = jim.getY();
switch (currentPlayer.getOrientation())
{
case 'u':
//Check if you're in same x
//Check if above player
break;
case ... etc.
}
然后在击中的情况下,换句话说,如果满足条件。游戏应以currentPlayer胜利结束。