0

I am using slick2d to make a mario type clone game. We have 50x50 images we are using as blocks that was going to be on the floor. 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.

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);
Sam Haito
  • 113
  • 1
  • 1
  • 6

1 Answers1

1

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.

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.

If you move forward a certain amount set the position of objects back -50px or something like that. If you move backwards, set the position of objects forward +50px.

I hope I make sense, it's kinda weird to explain.

MGHandy
  • 363
  • 1
  • 3
  • 11
  • Yes it draws the block at the left of the screen and you can see it blinking left to right as the loop is executed but only the final block coordinates in the loop remain. – Sam Haito Nov 26 '13 at 17:53
  • Then you do need to create a new block for each instance of the loop and you should get a row of blocks. – MGHandy Nov 27 '13 at 22:19
  • not sure how that is done, thought it would do it with the code inside the loop but it just moves it across the screen rather then draw new ones – Sam Haito Nov 28 '13 at 03:31
  • it redraws the current one because block is referring to a single variable object. make the variable called block like you have. Then every instance of the loop create a new one, block = new Image(); – MGHandy Dec 02 '13 at 19:45
  • If this doesn't work, try creating a class for game objects, they have an image obviously, and a position. create a new block every instance that way. – MGHandy Dec 02 '13 at 19:56