我正在尝试从PNG文件渲染(600/448)屏幕中的级别(1600/448)。它尝试渲染但它没有成功,我认为它会在这样做时停止游戏循环。而且我认为这种情况正在发生,因为有许多瓷砖,但我真的不知道如何绕过它。
public void loadLevel(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
for (int xx = 0; xx < width; xx++) { //width = 1600
for (int yy = 0; yy < height; yy++) { //height = 448;
Color c = new Color(image.getRGB(xx,yy));
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();
if (red == 225 && blue == 225 & green == 225) {
objectHandler.add(new GrassRight(xx * 32, yy * 32, ObjectId.GrassRight));
} else if (red == 129 && blue == 250 & green == 212) {
objectHandler.add(new Player(xx * 32, yy * 32, objectHandler, ObjectId.Player));
} else if (red == 0 && blue == 0 && green == 0) {
objectHandler.add(new Dirt(xx * 32, yy * 32, ObjectId.Dirt));
} else if (red == 144 && blue == 144 & green == 213) {
objectHandler.add(new Grass(xx * 32, yy * 32, ObjectId.Grass));
} else if (red == 38 && blue == 56 & green == 50) {
objectHandler.add(new GrassLeft(xx * 32, yy * 32, ObjectId.GrassLeft));
} else if (red == 62 && blue == 35 & green == 39) {
objectHandler.add(new SlideRight(xx * 32, yy * 32, ObjectId.SlideRight));
} else if (red == 191 && blue == 12 & green == 54) {
objectHandler.add(new SlideLeft(xx * 32, yy * 32, ObjectId.SlideLeft));
} else if (red == 127 && blue == 194 & green == 87) {
objectHandler.add(new DownSlideLeft(xx * 32, yy * 32, ObjectId.DownSlideLeft));
} else if (red == 49 && blue == 146 & green == 27) {
objectHandler.add(new DownSlideRight(xx * 32, yy * 32, ObjectId.DownSlideRight));
}
}
}
}
public void init() {
System.out.println("Loading...");
LoadingHandler loadingHandler = new LoadingHandler();
level = loadingHandler.loadImage("/Level1.png");
objectHandler = new ObjectHandler();
cameraHandler = new CameraHandler(0, 0);
this.addKeyListener(new KeyHandler(objectHandler));
loadLevel(level);
System.out.println("Done!");
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
Graphics2D g2d = (Graphics2D) g;
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g2d.translate(cameraHandler.getX(), cameraHandler.getY());
System.out.println("Rendering!");
objectHandler.render(g);
System.out.println("Rendered!");
g2d.translate(-cameraHandler.getX(), -cameraHandler.getY());
g.dispose();
bs.show();
}
public class ObjectHandler {
public LinkedList<ObjectManager> object = new LinkedList<>();
ObjectManager tempObject;
public void tick() {
for (int i = 0; i < object.size(); i++) {
tempObject = object.get(i);
tempObject.tick(object);
}
}
public void render(Graphics g) {
for (int i = 0; i < object.size(); i++) {
tempObject = object.get(i);
tempObject.render(g);
}
}
public void add(ObjectManager object) {
this.object.add(object);
}
它给出了这个输出:
Loading...
Done!
Rendering!
PNG:
答案 0 :(得分:3)
不是为每个像素(即716'800个对象)创建一个对象,而是为每个矩形区域(15个对象)创建一个对象。
更新:
在ObjectManager类中,添加两个int字段:width和height,然后渲染整个矩形而不是单个像素(或32x32 square)。您仍然可以使用PNG初始化对象,但使用更智能的算法,不会为每个像素重新创建新对象。