【问题标题】:How to randomly generate tiles in java code如何在java代码中随机生成图块
【发布时间】:2014-09-12 04:34:55
【问题描述】:

我对在 java 中随机生成精灵表图块有疑问。每当我尝试使用 for 循环时,随机生成每毫秒或更快地发生变化。我很困惑,不知道从哪里开始。如果有人能想出一些简单的代码让初学者掌握随机生成的概念!

这是我用来生成平面地图的代码/

public class Map {

    Random random = new Random();

    public static int mapxDirection;
    public static int mapx = 1;
    public static int bgSpeed = 20;
    public static int grass = 16;
    public static int dirt = 0;
    public static int stone = 1;
    public static int waterup = 2;
    public static int waterside = 18;
    public static int glass = 17;
    public static int chicken = 32;
    public static int steak = 33;
    public static int mapSpeed = 3;

    public static BufferedImage[] sprites;

    public void renderMap(Graphics g) {

        final int width = 32;
        final int height = 32;
        final int rows = 16;
        final int cols = 16;

        sprites = new BufferedImage[rows * cols];

        BufferedImage spritesheet = null;
        try {
            spritesheet = ImageIO.read(new File("res/SpriteSheet.png"));
        } catch (IOException e) {

            e.printStackTrace();
        }

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                sprites[(i * cols) + j] = spritesheet.getSubimage(i * width, j
                        * height, width, height);
            }
        }

        for (int i = 0; i < 2000; i += 32) {
            g.drawImage(sprites[grass], Map.mapx + i, 259, null);
        }

        for (int i = 0; i < 2000; i += 32) {
            for (int j = 291; j <= 508; j += 32) {
                g.drawImage(sprites[dirt], Map.mapx + i, j, null);
            }
        }
        for (int i = 0; i < 2000; i += 32) {
            for (int j = 508; j <= 540; j += 32) {
                g.drawImage(sprites[stone], Map.mapx + i, j - 3, null);
            }
        }
    }
}

【问题讨论】:

  • 我看到你在初始化之后没有在任何地方使用random 对象。你到底在这里做什么?
  • 您是否尝试生成随机数量的图块?还是瓷砖的随机位置?还是随机大小的瓷砖?
  • 我正在尝试随机生成图块的位置

标签: java random generator


【解决方案1】:

有多种方法可以做到这一点,它还取决于你想要实现的随机生成类型,例如,你是做什么来完全随机化每个图块,还是做什么来加载随机块。热门游戏《我的世界》使用随机卡盘生成的方法。

如果您是游戏编程新手,我建议您查看RealTutsGML Game Programming Tutorials。他教您一种简单而有效的加载精灵表的方法,还教您如何加载和创建绘画关卡。尽管他正在制作平台游戏,但您可以轻松地将相同的方法转换为自上而下的游戏。

以下是我将如何创建随机生成图。首先,我将创建一个名为 GameObject 的抽象类。你创建的每个对象都会扩展这个类。

public abstract class GameObject {

    protected float x, y;
    protected ObjectId id;
    protected float velX= 0, velY = 0;

    protected boolean falling = true;
    protected boolean jumping = false;

    public GameObject(float x, float y, ObjectId id) {
        this.x = x;
        this.y = y;
        this.id = id;
    }

    public abstract void tick(LinkedList<GameObject> object);
    public abstract void render(Graphics g);
    public abstract Rectangle getBounds();

    public float getX() { return x; }
    public float getY() { return y; }
    public float getVelX() { return velX; }
    public float getVelY() { return velY; }
    public boolean isFalling() { return falling; }
    public boolean isJumping() { return jumping; }

    public void setX(float x) { this.x = x; }
    public void setY(float y) { this.y = y; }
    public void setVelX(float velX) { this.velX = velX; }
    public void setVelY(float velY) { this.velY = velY; }
    public void setFalling(boolean falling) { this.falling = falling; }
    public void setJumping(boolean jumping) { this.jumping = jumping; }

    public ObjectId getId() { return id; }
}

例如,如果我想创建一个名为 Block 的对象,我将创建一个名为 Block 的类并像这样扩展 GameObject。

public class Block extends GameObject {

    private boolean animate = true;

    Texture tex = Game.getInstance();
    private int type;

    public Block(float x, float y, int type, ObjectId id) {
        super(x, y, id);
        this.type = type;
    }

    public void tick(LinkedList<GameObject> object) {
        bAnimate.runAnimation();
    }

    public void render(Graphics g) {
        if (type == 0) g.drawImage(tex.red_block[0], (int)x, (int)y, null); // Red Block
        if (type == 1) g.drawImage(tex.green_block[0], (int)x, (int)y, null); // Blue Block
        if (type == 2) g.drawImage(tex.blue_block[0], (int)x, (int)y, null); // Green Block
        if (type == 3) g.drawImage(tex.yellow_block[0], (int)x, (int)y, null); // Yellow Block
        if (type == 4) g.drawImage(tex.orange_block[0], (int)x, (int)y, null); // Orange Block
        if (type == 5) g.drawImage(tex.pink_block[0], (int)x, (int)y, null); // Pink Block
    }

    public Rectangle getBounds() {
        return new Rectangle((int)x, (int)y, 32, 32);
    }

}

然后我像这样创建一个 LinkedList。还可以创建 2 个方法来为列表添加和删除对象。

protected LinkedList<GameObject> object = new LinkedList<GameObject>();

public void addObject(GameObject object) {
    this.object.add(object);
}

public void remove(GameObject object) {
    this.object.remove(object);
}

然后我会像这样简单地将对象添加到列表中。

addObject(new Block(x * 32, y * 32, random.nextInt(6), ObjectId.Block)); 

感觉这篇文章有点大了,我会把剩下的留给你,如果你喜欢我可以看到我正在使用的项目的源代码。这也是“RealTutsGML”在他的教程中使用的相同方法。

我看到你使用Map.mapx 而不是只写变量mapx 不需要Map。此外,您的数据应该始终是私有的或受保护的。我还建议像这样创建一个保存地图大小的变量。

private static int mapSize = 2000;

只是这样,如果您想更改地图大小,您不必替换多个数字。

希望对你有所帮助。

这是java programming books的列表。

【讨论】:

    猜你喜欢
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 2011-07-27
    • 1970-01-01
    相关资源
    最近更新 更多