在 Gridworld 中创建一个具有位置的新 Actor

Create a new Actor with Location in Gridworld

我想创建一个带有一个“汽车”演员和固定生成位置的网格世界:

      package gridworld.blatt3;
    import gridworld.framework.actor.*;
    import gridworld.framework.grid.Grid;
    import gridworld.framework.grid.Location;
    
    public class Traffic {
    
        public static void main(String[] args) {
            TrafficWorld world = new TrafficWorld();
            System.out.println("Anwendung startet...");
            Location loc1 = new Location(0,0);
            Grid.put(loc1, new Car());
            world.show();
        }
    }
    package gridworld.framework.actor;

import gridworld.framework.actor.bugs.Flower;
import gridworld.framework.grid.Grid;
import gridworld.framework.grid.Location;

import java.awt.*;

public class Car extends AbstractActor {
    //Schrittgröße//
    int speed=5;
    int sideLenght=50;
    int step = 0;


    //Richtung //
    public Car () {
    setDirection(Location.EAST);
    setColor(Color.BLUE);
    }

    public void act () {
       move();
    }

    public void move() {
        int speedCount=0;
        //Springt immer um 'speed'-Schritte //
        do {
            step++;
        speedCount++;
            if (step < sideLenght) {
                System.out.println("Step: "+step);
                Location loc = getLocation();
                Location next = loc.getAdjacentLocation(getDirection());
                moveTo(next);
            }
            else {
                System.out.println("car drives off the grid");
                removeSelfFromGrid();
                break;
            }

        } while (speedCount!=speed);
    }

}



   package gridworld.framework.actor;

import gridworld.framework.grid.BoundedGrid;

public class TrafficWorld extends ActorWorld {
    private static final int rowSize = 1;
    private static final int colSize = 50;

    public TrafficWorld() {
        super(new BoundedGrid<Actor>(rowSize, colSize));
    }
}

如何为“汽车”定义特定的生成位置?例如。我希望 Actors 在网格的左上角生成,我创建了一个 Location 对象“loc1”以与 Grid.put() 一起使用,但我得到一个错误:java:非静态方法put(gridworld.framework.grid.Location,E) 不能从静态上下文中引用

我正在使用 openjdk-15.0.1

您可以使用带有 Location 参数的 ActorWorld.add() 方法将角色放置在特定位置:

add

public void add(Location loc, Actor occupant)

Adds an actor to this world at a given location.

在你的情况下它会是这样的:

world.add(new Location(0, 0), new Car());