I’m trying to make my player centered at all times, moving at 20px per keypress, that works. What doesn’t work is the scrolling map, I need the map to scroll to allow the player to be centered at all times whilst exploring the map. The map size is 2000x2000 and the viewport or screen size is 480x320.
I have the following code which I understand doesn’t work, but I hope I’m getting the right idea.
package game;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import entities.Player;
import environment.Grass;
public class GameState extends BasicGameState {
Input input;
Player player;
Grass grass;
Image imagePlayer, imageGrass;
int xOff;
int yOff;
public int getID() {
return Main.GAMESTATE;
}
public void init(GameContainer gc, StateBasedGame sb) throws SlickException {
imagePlayer = new Image("assets/img/player.png");
imageGrass = new Image("assets/img/grass.png");
player = new Player(230, 150, imagePlayer);
grass = new Grass(imageGrass);
input = gc.getInput();
}
public void render(GameContainer gc, StateBasedGame sb, Graphics g) throws SlickException {
grass.draw(g);
player.draw(g);
}
public void update(GameContainer gc, StateBasedGame sb, int delta) throws SlickException {
xOff = (2000 - player.getX());
yOff = (2000 - player.getY());
if (input.isKeyPressed(Input.KEY_LEFT)) {
player.x -= 20;
} else if (input.isKeyPressed(Input.KEY_RIGHT)) {
player.x += 20;
} else if (input.isKeyPressed(Input.KEY_UP)) {
player.y -= 20;
} else if (input.isKeyPressed(Input.KEY_DOWN)) {
player.y += 20;
}
if (player.x < 0) player.x = 0;
if (player.y < 0) player.y = 0;
}
}
Not sure where to go from here.
Shannon