你好,我有一个游戏,你可以四处走动,寻找发生了什么事的线索。 我有一个map.txt,上面有代表每个 block 的数字。如果数量等于 5,则创建一个表。我想把线索摆在桌面上。我想知道 map 中创建了多少个表,并为每个表分配一个 id,但所有 id 都不能相同,并且 id 编号不能大于该 map 的表数量。这是到目前为止我的代码:
表类
public class TableWithClue {
public int x, y;
public boolean search = false;
public Rectangle r, r1;
public int time = 0;
public int timer = 100;
public int aftertime = 0;
public int aftertimer = 100;
public boolean foundsomething = false;
public TableWithClue(int x, int y,int id) {
this.x = x;
this.y = y;
r = new Rectangle(x - play.camx, y - play.camy, 20, 30);
}
public void tick() {
r = new Rectangle(x - play.camx, y - play.camy, 20, 30);
if (r.intersects(play.p.r)) {
if(Keys.isPressed(Keys.e)){
search = true;
Sound.snap.play();
}
}
}
public void render(Graphics g) {
Image img;
if (search) {
ImageIcon i62 = new ImageIcon("res/tiles/table1.png");
img = i62.getImage();
g.drawImage(img, x - play.camx, y - play.camy, null);
} else {
ImageIcon i62 = new ImageIcon("res/tiles/table1.png");
img = i62.getImage();
g.drawImage(img, x - play.camx, y - play.camy, null);
if(r.intersects(play.p.r)){
ImageIcon i162 = new ImageIcon("res/gui/openstuff.png");
img = i162.getImage();
g.drawImage(img, Comp.size.width /2 - 95, 50, null);
g.setColor(Color.white);
g.setFont(new Font("italic", Font.BOLD,15 ));
g.drawString(" E to search ", Comp.size.width /2 - 32, 88);
}
}
if(search && r.intersects(play.p.r)){
if(time >=timer ){
if(aftertime >= aftertimer){
search = false;
time= 0;
aftertime = 0;
}else{
aftertime++;
}
}else {
time++;
}
g.setColor(Color.white);
g.fillRect((int) play.p.x - 55, (int) play.p.y - 70, 120, 40);
g.setColor(Color.black);
g.drawRect((int) play.p.x - 55, (int) play.p.y - 70, 120, 40);
if(time <timer && time != 0){
g.setColor(Color.black);
g.setFont(new Font("italic", Font.BOLD,15 ));
g.drawString("Searching",(int) play.p.x - 30, (int) play.p.y - 55);
}
g.setColor(Color.gray);
g.fillRect((int) play.p.x - 35, (int) play.p.y - 50, (80 * time) / timer, 10);
g.setColor(new Color(0, 0, 0));
g.drawRect((int) play.p.x - 35, (int) play.p.y - 50, 80, 10);
if(aftertime<=aftertimer && time >= timer){
g.setColor(Color.black);
g.setFont(new Font("italic", Font.BOLD,12 ));
g.drawString("Found Nothing",(int) play.p.x - 34, (int) play.p.y - 55);
}
}else{
search = false;
}
if(play.debug){
g.setColor(Color.red);
g.drawRect(r.x, r.y, r.width, r.height);
g.setColor(Color.yellow);
}else{
}
}
}
这些表是在名为 block 的不同类中创建的:
public void interactables() {
if (id == 5) {
screens.interactables.t1.add(new TableWithClue(x ,y));
}
}
这是 map 类:
public class map {
private String path;
private int width, height;
private static block[][] blocks;
private boolean open = false;;
public Image i2;
public map(String path) {
this.path = path;
loadMap();
}
public void loadMap() {
InputStream is = this.getClass().getResourceAsStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
width = Integer.parseInt(br.readLine());
height = Integer.parseInt(br.readLine());
blocks = new block[height][width];
for (int y = 0; y < height; y++) {
String line = br.readLine();
String[] tokens = line.split("\\s+");
for (int x = 0; x < width; x++) {
blocks[y][x] = new block(x * block.blocksize, y * block.blocksize, Integer.parseInt(tokens[x]));
}
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void draw(Graphics g) {
for (int i = 0; i < blocks.length; i++) {
for (int j = 0; j < blocks[0].length; j++) {
if (blocks[i][j].x + 50 > play.camx && blocks[i][j].x - 50 < play.camx + Comp.size.width + 10 && blocks[i][j].y + 50 > play.camy && blocks[i][j].y - 50 < play.camy + Comp.size.height + 10) {
blocks[i][j].render(g);
}
}
}
}
public static block[][] getBlocks() {
return blocks;
}
public void interact() {
for (int i = 0; i < blocks.length; i++) {
for (int j = 0; j < blocks[0].length; j++) {
blocks[i][j].interactables();
}
}
}
public void addNPC() {
for (int i = 0; i < blocks.length; i++) {
for (int j = 0; j < blocks[0].length; j++) {
blocks[i][j].NPC();
}
}
}
}
提前谢谢您:)
最佳答案
首先,您需要将表的 ID 存储在 TableWithClue
类中。然后,您将需要在您的 block
类中使用 static int。该变量将存储下一个表的ID;每次,下一个 ID 都会传递给表的构造函数,然后变量就会递增。因此加载的第一个表将是表 0,第二个表将是表 1,依此类推。
关于Java 游戏随机 id 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28000080/