0

你能帮忙看看这个关卡的无尽背景吗?

我目前正在使用 slick2d 编写一个原始游戏,游戏玩法类似于马里奥。

我有两张图片 - img1 和 img2(均为 1280x480,而屏幕分辨率为 640x480)。最初 img2 的 X 是 == (img1 的 X + img1 的宽度)。即它粘在img1的末端。当 img1 超出屏幕左边框时,它的 X 坐标变为 img2X + imgWidth。

逻辑看起来对我来说是正确的,但有时图片会被过度使用(很多,大约是屏幕的 1/4)。逻辑上有没有错误?方法好吗?也许有更简单和正确的方法来做到这一点?

伪代码如下所示:

class BkgDrawer {


Image img1 = new Image("imgs/background/bkg1.png");
Image img2 = new Image("imgs/background/bkg2.png");

int img1Width = img1.getWidth(); //1280
int img2Width = img2.getWidth(); //1280
int screenResolution = game.getResolution; //640

Vector2f position1 = new Vector2f (0,0);
Vector2f position2 = new Vector2f (position1.x+img1.getWidth(), 0); //initially position2 is glued to the end of img1

public void render(    ) {
  if (position1.x + img1Width < 0) { //the img is over the left border of the screen
      position1.x = position2.x + img2Width; //glue it to the end of  img2
  }
//the same for the img2
  if (position2.x + img2Width < 0) { //the img is over the left border of the screen
      position2.x = position1.x + img2Width; //glue it to the end of  img2
  }
  img1.draw(position1.x, position1.y);
  img2.draw(position2.x, position2.y);
  //move coordinate to get the background moving.
  position1.x -= MOVING_STEP;
  position2.x -= MOVING_STEP;
  }
}

很抱歉有很多文字,谢谢

4

1 回答 1

0

I have found only one bug and it will only have an affect if the two images have different widths: Your two if-statements use the same width img2Width

You may have noticed that you have duplicate code to handle the rendering and repositioning of each background. Might I suggest that you refactor the background code into a Background class that contains the background image, position, and and update method that repositions it by MOVING_STEP. You will avoid mistakes like the one I mentioned above.

于 2012-07-21T22:24:08.983 回答