如何在java代码中随机生成tile
我有一个关于java中随机生成sprite表瓷砖的问题。每当我尝试使用for循环时,随机生成每毫秒或更快地改变一次。我很困惑,不知道从哪里开始。如果有人能想出一些简单的代码让乞丐掌握随机生成的概念 下面是我用来生成平面图的代码/如何在java代码中随机生成tile,java,random,generator,Java,Random,Generator,我有一个关于java中随机生成sprite表瓷砖的问题。每当我尝试使用for循环时,随机生成每毫秒或更快地改变一次。我很困惑,不知道从哪里开始。如果有人能想出一些简单的代码让乞丐掌握随机生成的概念 下面是我用来生成平面图的代码/ public class Map { Random random = new Random(); public static int mapxDirection; public static int mapx = 1; public
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);
}
}
}
}
有多种方法可以做到这一点,这还取决于您想要实现的随机生成的类型。例如,您需要完全随机化每个磁贴还是加载随机块。流行的游戏Minecraft使用随机卡盘生成的方法 如果你对游戏编程不熟悉,我建议你去看看。他教你一个简单而好的方法来加载精灵表,还教你如何加载和创建一个绘画水平。尽管他正在制作一个平台,你可以很容易地将同样的方法转换成自上而下的游戏 下面是我如何创建一个随机生成的地图。首先,我将创建一个名为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; }
}
感觉到这篇文章越来越大,我将把其余的留给你,如果你喜欢,我可以看到你的源代码,我正在使用的项目。这也是RealTutsGML在其教程中使用的相同方法
我发现您可以使用Map.mapx而不是只编写变量mapx,而不需要映射。此外,您的数据应始终是私有的或受保护的。我还建议创建一个变量,这样保存贴图大小
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);
}
}
addObject(new Block(x * 32, y * 32, random.nextInt(6), ObjectId.Block));
private static int mapSize = 2000;
这只是为了,如果你想改变地图的大小,你不必替换多个数字
我希望这对你有帮助
这是一个列表。我看到您在初始化随机对象后没有在任何地方使用过它。你到底在这里做什么?你是在尝试生成随机数量的瓷砖吗?还是随机放置瓷砖?或者随机大小的瓷砖?我正在尝试随机生成瓷砖的位置
private static int mapSize = 2000;