2013-11-26 59 views
0

我使用slick2d來製作mario類型的克隆遊戲。我們有50x50的圖像,我們正在使用的塊將會在地板上。無論何時將此代碼放入for循環,我都可以看到正在繪製的塊,並一直重繪到指定區域的末尾,而不是每隔50個像素繪製一個塊。使用for循環繪製地面上的塊

package edu.bsu.cs222.finalProject.states; 



public class GameState extends BasicGameState { 

int stateID = 1; 
Image background; 
Image block; 
float x = 0; 
float y = -370; 
float Dx = x + 200; 
float Dy = y + 810; 
private float verticalSpeed = 0.0f; 
private float horizontalSpeed = 1.8f; 
private float imagepositionx=0; 
private float imagepositiony=500; 
boolean jumping = false; 
Animation cowboy, movingLeft, movingRight, movingUp, stoppedLeft, 
     stoppedRight; 
int[] animationDuration = { 200, 200 }; 

public GameState(int stateID) { 
    this.stateID = stateID; 
} 

@Override 
public void init(GameContainer gc, StateBasedGame sbg) 
     throws SlickException { 
    background = new Image("res/ui/background.png"); 
    block = new Image("res/obj/block.png"); 
    Image[] walkLeft = { new Image("res/char/cowboyleft1.png"), 
      new Image("res/char/cowboyleft2.png") }; 
    Image[] walkRight = { new Image("res/char/cowboyright1.png"), 
      new Image("res/char/cowboyright2.png") }; 
    Image[] stopLeft = { new Image("res/char/cowboyleft1.png"), 
      new Image("res/char/cowboyleft2.png") }; 
    Image[] stopRight = { new Image("res/char/cowboyright1.png"), 
      new Image("res/char/cowboyright2.png") }; 
    movingLeft = new Animation(walkLeft, animationDuration, true); 
    movingRight = new Animation(walkRight, animationDuration, true); 
    stoppedLeft = new Animation(stopLeft, animationDuration, false); 
    stoppedRight = new Animation(stopRight, animationDuration, false); 
    cowboy = stoppedRight; 
} 

@Override 
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) 
     throws SlickException { 
    background.draw(x, y); 
    g.drawString("Cowboy's X:" + x + "\nCowboy's Y: " + y, 400, 20); 
    cowboy.draw(Dx, Dy); 


     if(imagepositionx <3000) 
     { 
      g.drawImage(block, imagepositionx, imagepositiony); 
      imagepositionx += 50; 
     } 



      //pillar1 
    block.draw(600, 450); 
    block.draw(600, 400); 
    g.drawImage(block, 600, 400); 
    g.drawImage(block, 600, 350); 
    //3 in air 
    g.drawImage(block, 800, 250); 
    g.drawImage(block, 850, 250); 
    g.drawImage(block, 900, 250); 
    //pillar2 
    g.drawImage(block, 1100, 450); 
    g.drawImage(block, 1100, 400); 
    g.drawImage(block, 1100, 350); 

} 

@Override 
public void update(GameContainer gc, StateBasedGame sbg, int delta) 
     throws SlickException { 
    Input input = gc.getInput(); 
    if (input.isKeyDown(Input.KEY_LEFT)) { 
     cowboy = movingLeft; 
     x += horizontalSpeed; 
     if (x > 0) { 
      x -= delta * horizontalSpeed; 
     } 
    } 

    if (input.isKeyDown(Input.KEY_RIGHT)) { 
     cowboy = movingRight; 
     x -= horizontalSpeed; 
     if (x < -2975.0) { 
      x += delta * horizontalSpeed; 
     } 
    } 

    if (input.isKeyDown(Input.KEY_UP) && !jumping) { 
     verticalSpeed = -3.0f; 
     jumping = true; 
    } 

    if (jumping) { 
     verticalSpeed += .03f * delta; 
    } 

    if (Dy > 470) { 
     jumping = false; 
     verticalSpeed = 0; 
     Dy = 470; 
    } 
    Dy += verticalSpeed; 
} 

@Override 
public int getID() { 
    return stateID; 
} 

} 

也使用此代碼遇到問題,它們在屏幕上移動而不是保持在固定位置。

g.drawImage(block, 600, 400); 
+3

你能發表更多的代碼嗎? –

+0

另外,在單個名稱中大寫單詞是一種很好的做法;例如'imagePositionX'而不是'imagepositionx'。 –

+0

您的圖片有輪廓?可能是它畫的框,但你無法看到。 – CRazyProgrammer

回答

1

如果我理解正確的話,你的意思是正在制訂同一個街區那麼喜歡它被擦除,並在新的位置重繪?你只能看到一個塊直到循環結束?

如果是這種情況,您需要爲每個位置創建一個新塊。

至於第二部分塊沒有留在原地。它留在原地。發生什麼事是每次你這樣稱呼:

g.drawImage(block, 600, 400); 

你的塊被放置在屏幕上的相同的地方。所以技術上你的區塊不會移動。

如果你想要做的就像馬里奧,你的角色向前移動,一切都會倒退,以模仿穿過關卡的旅程,那麼你需要更新一切的位置。

如果你向前移動一定數量的對象的位置設置-50px或類似的東西。如果向後移動,請將對象的位置設置爲+ 50px。

我希望我有道理,這有點奇怪的解釋。

+0

是的,它繪製了屏幕左側的塊,你可以看到它從左到右閃爍,因爲循環被執行,但只有循環中的最終塊座標保留。 –

+0

然後您需要爲每個循環實例創建一個新塊,並且您應該獲得一排塊。 – MGHandy

+0

不知道這是怎麼做的,認爲它會用循環內的代碼來做,但它只是將它移動到屏幕上,而不是畫出新的 –