简体   繁体   English

如何在Java代码中随机生成图块

[英]How to randomly generate tiles in java code

I have question about random generation of sprite sheet tiles in java. 我对在Java中随机生成的Sprite Sheet Tile有疑问。 Whenever I try to use a for loop the random generation changes every millisecond or faster. 每当我尝试使用for循环时,随机生成都会每毫秒或更快速地更改一次。 I am very confused and have no idea where to start from here. 我很困惑,不知道从哪里开始。 If anyone can come up with some easy code for a begginer to grasp the concept of random generation! 如果有人可以为入门者提供一些简单的代码来掌握随机数生成的概念!

Here is the code I am working with for generating a flat map / 这是我正在使用的用于生成平面地图的代码/

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);
            }
        }
    }
}

There are multiple ways this can be done, it also depends on what type on random generation you want to achieve for example, do you what to completely randomize every tile or do you what to load random chunks. 可以通过多种方式完成此操作,这还取决于要实现的随机生成的类型,例如,要完全随机化每个图块还是加载随机块? The popular game Minecraft uses the method of random chuck generation. 热门游戏《我的世界》使用随机卡盘生成方法。

If your new to game programming, i recommend checking out RealTutsGML Game Programming Tutorials . 如果您是游戏编程的新手,我建议您查阅RealTutsGML游戏编程教程 He teaches you a easy and good way of loading in sprite sheets and also teaches you how to load in and create a level in paint. 他教给您一种简单而又好用的加载Sprite表的方法,还教您如何加载和创建涂料级别。 All though he is making a platformer, you can easily convert the same method into a top down game. 尽管他正在制作平台游戏,但您可以轻松地将相同的方法转换为自上而下的游戏。

Here is how i would go about creating a random generation map. 这就是我将如何创建随机生成图。 First i would create a abstract class called GameObject. 首先,我将创建一个称为GameObject的抽象类。 Every object you make will extend this class. 您制作的每个对象都将扩展此类。

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; }
}

For example if i want to make a object called Block, i would create a class called Block and extends GameObject like this. 例如,如果我要创建一个名为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);
    }

}

I then create a LinkedList like this. 然后,我像这样创建一个LinkedList。 An also create 2 methods for adding and removing objects for the list. 还创建了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);
}

I would then simply add objects to the list like this. 然后,我将像这样简单地将对象添加到列表中。

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

Sense this post is getting kinda big i will leave the rest to you, if you like i can see you the source code of the project i am using. 感觉到这篇文章变得越来越大,如果您愿意,我会把剩下的一切留给您,如果我能看到您我正在使用的项目的源代码。 This is also the same method that "RealTutsGML" uses in his tutorials. 这也是“ RealTutsGML”在其教程中使用的相同方法。

I see that you and using Map.mapx instead of that just write the variable mapx no need for the Map . 我看到您在使用Map.mapx而不是那只写变量mapx而不需要Map Also your data should always be private or protected. 此外,您的数据应始终为私有或受保护的。 I also recommend making a variable that holds your map size like this. 我还建议创建一个这样的变量来保存您的地图大小。

private static int mapSize = 2000;

It is just so that if you want to change the map size you don't have to replace more than one number. 就是这样,如果您想更改地图大小,则不必替换多个数字。

I hope this helps you out. 我希望这能够帮到你。

Here is a list of java programming books . 这是Java编程书籍的清单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM