使用for循环在地面上绘制块

时间:2013-11-26 16:57:39

标签: java loops slick2d side-scroller

我正在使用slick2d制作马里奥式克隆游戏。我们使用的50x50图像作为将要在地板上的块。每当我将此代码放入for循环中时,我都可以看到正在绘制的块并重新绘制到指定区域的末尾,而不是每隔50个像素绘制一个块。

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);

1 个答案:

答案 0 :(得分:1)

如果我理解正确的话,你的意思是同一块被绘制然后被删除并重新绘制在新位置?你只看到一个块直到循环结束?

如果是这种情况,您需要为每个位置创建一个新区块。

关于区块未停留的第二部分。它留在原地。每当你打电话时都会发生什么:

g.drawImage(block, 600, 400);

您的区块被放置在屏幕上的相同位置。所以从技术上讲,你的区块并没有移动。

如果你想要做的就像马里奥,你的角色向前移动,一切都倒退,模仿穿越关卡的外观,那么你需要更新一切的位置。

如果向前移动一定量,则将对象的位置设置为-50px或类似的值。如果向后移动,请将对象的位置设置为+ 50px。

我希望我有道理,解释起来有点奇怪。