有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从一个类到另一个类获取值,其中第一个类有来自第二个类的对象,而不是相反?

我正在用Java制作这个游戏,作为我的CompSci课程课程的一部分。这是一个竞技场风格的游戏,你必须收集硬币和杀死怪物。非常基本

我有一个竞技场a类英雄类和一个硬币类,竞技场有一个英雄物品和硬币物品列表。我想检查我的英雄是否和某枚硬币在同一个地方,我想用某种英雄来做这件事。onCoin()方法,但我不能,因为我的Hero类没有Coin类的任何实例

然后我试着做一些类似的事情:

private List<Coin> getCoins(){
    return coins;
}

所以,我可以在英雄类中有我的硬币列表,但是因为我没有在英雄类中实例化竞技场类,所以我不能。对新手有什么建议吗

这是我的4个职业(英雄类和硬币扩展元素类)

import com.googlecode.lanterna.*;
import com.googlecode.lanterna.graphics.TextGraphics;
import com.googlecode.lanterna.input.KeyStroke;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Arena {

    private final int width, height;
    private final Hero hero = new Hero(20, 10);
    private List<Wall> walls;
    private List<Coin> coins;

    public Arena(int width, int height){
        this.width = width;
        this.height = height;
        this.walls = createWalls();
        this.coins = createCoins();
    }

    public void processKey(KeyStroke key){
        System.out.println(key);
        switch (key.getKeyType()){
            case ArrowUp : moveHero(hero.moveUp()); break;
            case ArrowDown :  moveHero(hero.moveDown()); break;
            case ArrowLeft :  moveHero(hero.moveLeft()); break;
            case ArrowRight :  moveHero(hero.moveRight()); break;
        }
    }

    public void draw(TextGraphics graphics){
        graphics.setBackgroundColor(TextColor.Factory.fromString("#336699"));
        graphics.fillRectangle(new TerminalPosition(0, 0), new TerminalSize(width, height), ' ');

        for (Wall wall : walls){
            wall.draw(graphics, wall.color, wall.model, wall.bold);
        }

        for (Coin coin : coins){
            coin.draw(graphics, coin.color, coin.model, coin.bold);
        }

        hero.draw(graphics, hero.color, hero.model, hero.bold);
    }

    private boolean canHeroMove(Position position) {
        for (Wall wall : walls) {
            if (wall.getPosition().equals(position)) {
                return false;
            }
        }
        return true;
    }

    public void moveHero(Position position) {
        if (canHeroMove(position))
            hero.setPosition(position);
    }

    private List<Wall> createWalls(){
        List<Wall> walls = new ArrayList<>();

        for (int c = 0; c < width; c++){
            walls.add(new Wall(c, 0));
            walls.add(new Wall(c, height - 1));
        }

        for (int r = 1; r < height -1; r++){
            walls.add(new Wall(0, r));
            walls.add(new Wall(width - 1, r));
        }

        return walls;
    }

    private List<Coin> createCoins() {
        Random random = new Random();
        List<Coin> coins = new ArrayList<>();

        for (int i = 0; i < 5; i++) {
            //Create new random coin
            int x = random.nextInt(width - 2) + 1, y = random.nextInt(height - 2) + 1;
            Position p = new Position(x, y);
            boolean new_coin = true;

            //If it's on player's position
            if(hero.getPosition().equals(p)){
                i--;
                continue;
            }
            //If it's on another coin's position
            for (Coin coin : coins){
                if(coin.getPosition().equals(p)){
                    i--;
                    new_coin = false;
                    break;
                }
            }
            //Add coin to coins
            if(new_coin){
                coins.add(new Coin(x, y));
            }

        }
        return coins;
    }

    private List<Coin> getCoins(){
        return coins;
    }

    private void retrieveCoins(){

    }
}
import com.googlecode.lanterna.SGR;
import com.googlecode.lanterna.TerminalPosition;
import com.googlecode.lanterna.TextColor;
import com.googlecode.lanterna.graphics.TextGraphics;

public class Element {

    protected Position position;

    public Element(int x, int y){
        position = new Position(x, y);
    }

    public void draw(TextGraphics graphics, String color, String model, boolean bold){
        graphics.setForegroundColor(TextColor.Factory.fromString(color));
        if(bold){
            graphics.enableModifiers(SGR.BOLD);
        }
        graphics.putString(new TerminalPosition(position.getX(), position.getY()), model);

    }

    public Position getPosition(){
        return position;
    }
}
public class Coin extends Element{

    public final String color = "#ffdb19", model = "O";
    public final boolean bold = true;

    public Coin(int x, int y){
        super(x, y);
    }


}
import java.util.List;

public class Hero extends Element{

    public final String color = "#ff0000", model = "X";
    public final boolean bold = true;

    public Hero(int x, int y){
        super(x, y);
    }

    public Position moveUp(){
        return new Position(position.getX(), position.getY() -1);
    }

    public Position moveDown(){
        return new Position(position.getX(), position.getY() + 1);
    }

    public Position moveLeft(){
        return new Position(position.getX() -1, position.getY());
    }

    public Position moveRight(){
        return new Position(position.getX() + 1, position.getY());
    }

    public void setPosition(Position new_position){
        position.setX(new_position.getX());
        position.setY(new_position.getY());
    }
}

共 (1) 个答案

  1. # 1 楼答案

    将逻辑放入Arena类:

    public boolean heroOnCoin() {
        for (Coin coin: coins) {
            if (hero.getPosition().equals(coin.getPosition())) {
                return true;
            }
        }
        return false;
    }