gpt4 book ai didi

java - Swing简单叠加

转载 作者:搜寻专家 更新时间:2023-10-31 19:44:44 25 4
gpt4 key购买 nike

<分区>

我正在制作一款 2D 游戏,其中玩家在迷宫中漫游。 screenshot of the GUI

我想实现某种“黑暗”,甚至是像被黑色包围的玩家周围的透明形状这样简单的东西,如下所示: mock-up screenshot

我在使用 Swing 时发现的问题是,虽然这是可能的,但这意味着必须重绘所有内容,这会在每次发生时产生恼人的“闪烁”效果。有没有一种方法可以进行某种覆盖,或者只是在 Swing 中通常这样做的好方法?我现在对 GUI/视觉的东西不是很有经验,所以如果可能的话我想坚持使用 Swing。

编辑:这是我绘制背景的方法,即地板、墙壁和导出:

    public final void paintBG(Graphics g){
g.setColor(Color.LIGHT_GRAY); // Screen background
g.fillRect(0, 0, getWidth(), getHeight());
// Draw the Walls of the maze
// scalex and y are for scaling images/walls within the maze since I let users specify how big they want the maze
for (int j = 0; j < this.height; j++, y += scaley) {
x = 20;
for (int i = 0; i < this.width; i++, x += scalex) {
if (!(maze[j][i].northwall.isBroken())) // If the north wall isn't broken
{
g.drawImage(walltile, x, y, scalex, scaley / 5, null); // Draw a wall there (image, xpos, ypos, width, height, observer)
}
if (!(maze[j][i].eastwall.isBroken())) // etc
{
g.drawImage(walltile, x + scalex, y, scalex / 5, scaley, null);
}
if (!(maze[j][i].southwall.isBroken())) {
g.drawImage(walltile, x, y + scaley, scalex, scaley / 5, null);
}
if (!(maze[j][i].westwall.isBroken())) {
g.drawImage(walltile, x, y, scalex / 5, scaley, null);
}

if ((j == mazeinfo.getTargetM()) && (i == mazeinfo.getTargetN())) {
// Draw the exit
g.drawImage(jeep, x + (scalex / 2), y + (scaley / 2), cx, cy, null);
g.setColor(Color.LIGHT_GRAY);
if (maze[j][i].northwall.isEdge()) {
// Paint over the edge creating a 'way out'
g.fillRect(x, y, scalex, scaley / 4);
} else if (maze[j][i].eastwall.isEdge()) {
g.fillRect(x + scalex, y, scalex / 4, scaley);
} else if (maze[j][i].southwall.isEdge()) {
g.fillRect(x, y + scaley, scalex, scaley / 4);
} else if (maze[j][i].westwall.isEdge()) {
g.fillRect(x, y, scalex / 4, scaley);
}
}
}
}
}

然后我使用“paintPlayer”和“paintEnemy”方法在每次移动时绘制这些 Sprite 。背景仅在开始时绘制一次。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com