你能幫助這個關卡的無盡背景嗎?無盡的背景爲水平
我目前正在使用slick2d編寫一個原始遊戲,遊戲類似於Mario。
我有兩張圖片 - 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;
}
}
對不起,大量的文字和感謝
這隻發生在一些時間? –