简体   繁体   English

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

[英]Using for loop to draw blocks on ground

I am using slick2d to make a mario type clone game. 我正在使用slick2d制作马里奥型克隆游戏。 We have 50x50 images we are using as blocks that was going to be on the floor. 我们有50x50的图像被用作地板上的块。 Whenever I put this code into the for loop, I can see the block being drawn and redrawn all the way to the end of the specified area, instead of drawing a block every 50 pixels. 每当我将此代码放入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;
}

}

Also running into a problem using this code they move on the screen rather then stay in a fixed position. 使用此代码也遇到问题,它们在屏幕上移动,而不是停留在固定位置。

g.drawImage(block, 600, 400);

If i understand you correctly, you mean the same one block is being drawn then like its being erased and redrawn in the new position? 如果我正确理解您的意思,您是说正在绘制相同的图块,然后就像在新位置将其擦除并重新绘制一样? You only see the one block until the end of the loop? 您只看到一个块,直到循环结束?

If that's the case you need to create a NEW block for every position. 如果是这种情况,则需要为每个位置创建一个NEW块。

As for the second part where the block is not staying in place. 至于第二部分,其中的块没有停留在原地。 It is staying in place. 它留在原地。 Whats happening is every time you call this: 每次您打电话时发生的事情是:

g.drawImage(block, 600, 400);

Your block is placed in the same place on the screen. 您的块将放置在屏幕上的同一位置。 So technically your block isn't moving. 因此,从技术上讲,您的障碍物不会移动。

If what you are trying to do is like mario where your character moves forward and everything goes backwards to emulate the look of traveling through the level then you need to update the positions of everything. 如果您要尝试做的事情像马里奥(Mario),您的角色向前移动,而一切向后移动,以模仿穿越关卡的外观,那么您需要更新所有位置。

If you move forward a certain amount set the position of objects back -50px or something like that. 如果向前移动一定数量,则将对象的位置设置为-50px或类似的值。 If you move backwards, set the position of objects forward +50px. 如果向后移动,请将对象的位置向前设置+ 50px。

I hope I make sense, it's kinda weird to explain. 我希望我有道理,这有点奇怪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM