我对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);
}
}
}
}
答案 0 :(得分:0)
有多种方法可以做到这一点,它还取决于您想要实现的随机生成类型,例如,您可以完全随机化每个磁贴或者加载随机块的内容。流行的游戏Minecraft使用随机吸盘生成方法。
如果您是游戏编程新手,我建议您查看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。还创建了两种添加和删除列表对象的方法。
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));
感觉这篇文章变得有点大我会把剩下的留给你,如果你愿意,我可以看到你正在使用的项目的源代码。这也是&#34; RealTutsGML&#34;在他的教程中使用。
我看到你并使用Map.mapx
代替只是编写变量mapx
而不需要Map
。您的数据也应始终是私有或受保护的。我还建议制作一个保存地图大小的变量。
private static int mapSize = 2000;
如果您想更改地图尺寸,则不必更换多个数字。
我希望这可以帮助你。
以下是java programming books的列表。