我们被要求创建一个简单的pacman游戏,忍受我,我是java的新手。所以我在这个任务的这一部分做了很多事情,但是,我被困在我们必须传递点的坐标来收集玩家类的方法。 这是给我们的指示: -
向Player类添加“collect()”方法,以收集指定为参数的点(如果可能)。只有当玩家与点位于同一位置时,玩家才能收集点数。 当玩家收集一个点时,玩家的“gatherDots”计数应该增加1。 当玩家收集一个点时,该点应该消失。 实现这一点,作为一部分 在Player类中的“collect()”方法中,应该调用一个“disappear()”方法 班级点。
下面是游戏类。public class Game {
// instance variables - replace the example below with your own
private Player player;
private Dot dot1;
private Dot dot2;
private Dot dot3;
//Do not touch anything above this mofo.
/**
* Constructor for objects of class Game
*/
public Game(int xPos, int yPos) {
// initialise instance variables
player = new Player(xPos, yPos);
dot1 = new Dot(1, 1);
dot2 = new Dot(2, 2);
dot3 = new Dot(3, 3);
}
//Do not touch anything above this mofo.
public void move(int dx, int dy) {
player.move(dx, dy);
/*everything working above this*/
player.collect(dot1);
player.collect(dot2);
player.collect(dot3);
}
public String toString() {
return player + " " + dot1 + " " + dot2 + " " + dot3;
}
}
玩家类:
public class Player {
private int x;
private int y;
private int collectedDots;
/**
* Constructor for objects of class Player
*/
public Player(int xCoordinate, int yCoordinate) { // initialise instance variables
x = xCoordinate;
y = yCoordinate;
collectedDots = 0;
}
// do not touch anything above this line\
public void move(int dx, int dy) {
x = x + dx;
y = y + dy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void collect(Dot dot1) {
Dot disappearDot = new Dot(x, y);
if (x1 == x && y1 == y) {
collectedDots = 1;
disappearDot.disappear();
} else if (x1 == x && y1 == y) {
collectedDots = 2;
disappearDot.disappear();
} else if (x1 == x && y1 == y) {
collectedDots = 3;
disappearDot.disappear();
}
}
public String toString() {
return "Player[" + collectedDots + "]" + Util.objectStr(x, y, true);
}
}
答案 0 :(得分:0)
在我看来,我们可以在计划中有一些阶段。我们来解决你的问题,例如你的代码。
class Player {
int id;
String name;
int xPos;
int yPos;
public Player(int x, int y){
this.xPos = x;
this.yPos = y;
}
public void collect(Dot d){
}
public void move(int dx, int dy){
}
}
另外,我们需要建立:
class Dot {
int xPos;
int yPos;
public Dot(int x, int y){
this.xPos = x;
this.yPos = y;
}
}
现在,您的代码将传递Player的坐标。
答案 1 :(得分:0)
根据您更新的注释,只需在传递给方法的Dot Object上使用点表示法,即可从Dot对象中获取x和y值。更新了您的“收集”方法,并为通过您要求的方法添加了注释:
//Inside of player class
/**
* Add a “collect()” method to the Player class to collect the dot specified as the parameter
* if that is possible. The player can collect a dot only if the player is at the same position as the dot. When
* the player collects a dot, the player’s “collectedDots” count should be increased by 1. When the player
* collects a dot, the dot should disappear. To implement that, as part of the “collect()” method in the class
* Player, there should be a call to a “disappear()” method in the class Dot.
*/
public void collect(Dot dot){
if(this.xCoordinate == dot.getX() && this.yCoordinate == dot.getY()){
//increment the players count
setCollectedDots(getCollectedDots() + 1);
//remove the dot
dot.disappear();
}
}
添加了消失Dot的方法(假设从游戏中移除点)
public class Dot {
private int x;
private int y;
public Dot(int x, int y){
this.x = x;
this.y = y;
}
/**
* Returns the full coordinates for the Dot[x,y]
* @return
*/
public int[] getCoordinates(){
int[] coordinates = new int[2];
//place our coordinates into the array
coordinates[0] = x;
coordinates[1] = y;
return coordinates;
}
public void disappear(){
//remove from the dot grid by setting to a negative position
setX( -(this.x));
setY(-(this.y));
}
/**
* @return the x
*/
public int getX() {
return x;
}
/**
* @param x the x to set
*/
public void setX(int x) {
this.x = x;
}
/**
* @return the y
*/
public int getY() {
return y;
}
/**
* @param y the y to set
*/
public void setY(int y) {
this.y = y;
}
}