我一直在努力制作一款基于2D Tile的游戏,但在出现问题之前并未走得太远。游戏很好,除了它非常慢,并且瓷砖之间的空间不断出现。我尝试将所有平铺图像放入一个图像中以加载以使其更平滑,但它不起作用。我需要帮助来优化我的游戏以获得更好的fps。
大多数展示类
Player p = new Player();
static Map m = new Map();
Hotbar h = new Hotbar();
static Zombie z = new Zombie();
public static void main(String args[]) {
Thread t1 = new Thread(new Display());
t1.start();
Thread t2 = new Thread(z);
t2.start();
Thread t3 = new Thread(m);
t3.start();
}
@SuppressWarnings("static-access")
public void run() {
while (true) {
try {
oldTime = currentTime;
currentTime = System.currentTimeMillis();
elapsedTime = currentTime - oldTime;
fps = (int) ((1000 / 40) - elapsedTime);
Thread.sleep(fps);
} catch (Exception e) {
e.printStackTrace();
}
if (started) {
p.playerMovement();
p.width = getWidth();
p.height = getHeight();
h.width = getWidth();
h.height = getHeight();
if (p.maptiles != null) {
z.maptiles = new Rectangle[p.maptiles.length];
z.maptiles = p.maptiles;
}
} else {
}
repaint();
}
}
大多数地图类
BufferedImage backLayer;
BufferedImage backLayer2;
@SuppressWarnings("static-access")
public void createBack() {
backLayer = new BufferedImage(mapwidth * 32, mapheight * 32, BufferedImage.TYPE_INT_ARGB);
Graphics g = backLayer.getGraphics();
Graphics2D gd = (Graphics2D) g;
Color daycolor = new Color(0, 150, 255, 255);
Color nightcolor = new Color(0, 50, 100, Math.abs(time / daylength - 510 / 2));
Color backtilecolor = new Color(10, 10, 20, Math.abs(time / daylength - 510 / 4));
g.setColor(daycolor);
g.fillRect(0, 0, p.width, p.height);
g.setColor(nightcolor);
g.fillRect(0, 0, p.width, p.height);
time++;
if (time > 510 * daylength)
time = 0;
if (time < 100 * daylength || time > 410 * daylength) {
z.SpawnZombie();
} else {
}
g.setColor(backtilecolor);
int i = 0;
for (int x = 0; x < mapwidth; x++) {
for (int y = 0; y < mapheight; y++) {
if (mapdatasky[i] == 0) {
p.mapsky[i] = new Rectangle(x * 32 - p.playerX, y * 32 - p.playerY, 32, 32);
}
if (mapdatasky[i] >= 1) {
g.drawImage(tiles[mapdatasky[i]], x * 32 - p.playerX, y * 32 - p.playerY, 32, 32, null);
mapsky[i] = new Rectangle(x * 32 - p.playerX, y * 32 - p.playerY, 32, 32);
p.mapsky[i] = new Rectangle(x * 32 - p.playerX, y * 32 - p.playerY, 32, 32);
gd.fill(mapsky[i]);
}
i++;
}
}
backLayer2 = backLayer;
}
BufferedImage middleLayer;
BufferedImage middleLayer2;
@SuppressWarnings("static-access")
public void createMiddle() {
middleLayer = new BufferedImage(mapwidth * 32, mapheight * 32, BufferedImage.TYPE_INT_ARGB);
Graphics g = middleLayer.getGraphics();
Color fronttilecolor = new Color(20, 20, 40, Math.abs(time / daylength - 510 / 4));
int i = 0;
for (int x = 0; x < mapwidth; x++) {
for (int y = 0; y < mapheight; y++) {
if (mapdata[i] == -1) {
spawnX = x * 32 - p.width;
spawnY = y * 32 - p.height;
p.spawnX = spawnX;
p.spawnY = spawnY;
}
if (mapdata[i] == 0) {
p.mapsky[i] = new Rectangle(x * 32 - p.playerX, y * 32 - p.playerY, 32, 32);
}
if (mapdata[i] > 0) {
g.drawImage(tiles[mapdata[i]], x * 32 - p.playerX, y * 32 - p.playerY, 32, 32, null);
maptiles[i] = new Rectangle(x * 32 - p.playerX, y * 32 - p.playerY, 32, 32);
p.maptiles[i] = new Rectangle(x * 32 - p.playerX, y * 32 - p.playerY, 32, 32);
}
if (!p.breaking) {
tileid = -1;
}
if (tileid == i) {
g.setColor(new Color(100, 0, 0, 100));
g.fillRect(x * 32 - p.playerX + 16 - timer / (breaktime / 16), y * 32 - p.playerY + 16 - timer / (breaktime / 16), (timer / (breaktime / 16)) * 2, (timer / (breaktime / 16)) * 2);
}
i++;
}
}
g.setColor(fronttilecolor);
g.fillRect(0, 0, p.width, p.height);
middleLayer2 = middleLayer;
}
BufferedImage both;
BufferedImage both2;
public void mergeLayers() {
both = new BufferedImage(mapwidth * 32, mapheight * 32, BufferedImage.TYPE_INT_ARGB);
Graphics g = both.getGraphics();
g.drawImage(backLayer2, 0, 0, mapwidth * 32, mapheight * 32, null);
g.drawImage(middleLayer2, 0, 0, mapwidth * 32, mapheight * 32, null);
both2 = both;
}
public void renderMap(Graphics g) {
g.drawImage(both2, 0, 0, mapwidth * 32, mapheight * 32, null);
if (placetimer > 0)
placetimer--;
}
@Override
public void run() {
while (true) {
try {
oldTime = currentTime;
currentTime = System.currentTimeMillis();
elapsedTime = currentTime - oldTime;
fps = (int) ((1000 / 100) - elapsedTime);
Thread.sleep(fps);
if (!mapset) {
setMap();
mapset = true;
}
createBack();
createMiddle();
mergeLayers();
} catch(Exception e) {
e.printStackTrace();
}
}
}
这是瓷砖之间渲染错误的图片
游戏截图:
答案 0 :(得分:2)
我同意这里的审核可能是合适的。
但一般提示:如果你出于性能原因添加了这个“合并”(createBack / createMiddle / mergeLayers):不要这样做!在那里你正在绘制所有的瓷砖(也可能是一些无论如何都不可见的瓷砖,当它们直接用g.drawImage绘制时可以被剪掉)。将许多小图像绘制成大图像,然后在屏幕上绘制大图像,几乎不会比直接在屏幕上直接绘制小图像更快....
如果你添加了这个“合并”来解决出现的“条纹”:不要这样做!条纹来自于与绘制图像的线程不同的线程更改的坐标。您可以通过更改计算切片及其坐标的方式来避免这种情况。代码有点太......“复杂”指出它,所以我将在这里使用一些伪代码:
void paintTiles(Graphics g)
{
for (Tile tile : allTiles)
{
g.drawImage(tile, player.x, player.y, null);
}
}
这里的问题是而绘制线程在所有切片上迭代,另一个线程可能会改变玩家坐标。例如,某些图块可能会使用player.x=10
和player.y=20
进行绘制,然后另一个图块会更改玩家坐标,因此剩余的图块会使用player.x=15
和player.y=25
进行绘制 - 你会发现这是瓷砖之间出现的“条纹”。
在最好的情况下,这可以很容易地解决:
void paintTiles(Graphics g)
{
int currentPlayerX = player.x;
int currentPlayerY = player.y;
for (Tile tile : allTiles)
{
g.drawImage(tile, currentPlayerX, currentPlayerY, null);
}
}
这样,“当前”玩家坐标在迭代瓷砖时保持不变。