我正在尝试编写一个简单的横向卷轴游戏。如果玩家在某个方向上移动得太远,我有这个代码可以让屏幕重新居中。
有没有更好的写法?我觉得我在滥用语言,但效果很好,我认为它可能没问题。
public void adjustFrameIfNecessary()
{
int dx, dy;
if ((dx = (GAME_WIDTH - GAME_WIDTH / 3) - player.x) < 0 || (dx = GAME_WIDTH / 3 - player.x) > 0 || (dx = 0) == 0);
if ((dy = (GAME_HEIGHT - GAME_HEIGHT / 3) - player.y) < 0 || (dy = GAME_HEIGHT / 3 - player.y) > 0 || (dy = 0) == 0);
if(dx != 0 || dy != 0)
{
for (Drawable shiftMe : drawables)
{
shiftMe.unconditionalShift(dx, dy);
}
}
}
编辑
关于大家的意见,为了使其更具可读性,我已将其更改为
public void adjustFrameIfNecessary()
{
int dx, dy;
assignX:
{
dx = (GAME_WIDTH - GAME_WIDTH / 3) - player.x;
if(dx < 0) break assignX;
dx = GAME_WIDTH / 3 - player.x;
if(dx > 0) break assignX;
dx = 0;
}
assignY:
{
dy = (GAME_HEIGHT - GAME_HEIGHT / 3) - player.y;
if(dy < 0) break assignY;
dy = GAME_HEIGHT / 3 - player.y;
if(dy > 0) break assignY;
dy = 0;
}
if (dx != 0 || dy != 0)
{
for (Drawable shiftMe : drawables)
{
shiftMe.unconditionalShift(dx, dy);
}
}
}
这是否更好?
编辑 2
public void adjustFrameIfNecessary()
{
int dx = calculateShift(GAME_WIDTH, frameReference.x);
int dy = calculateShift(GAME_HEIGHT, frameReference.y);
if (dx != 0 || dy != 0)
{
for (Drawable shiftMe : drawables)
{
shiftMe.unconditionalShift(dx, dy);
}
}
}
我认为现在很清楚。谢谢大家。