Trying to achieve movement like in Legends of Yore (http://legendsofyore.com), not sure how to achieve it. Current code I have is:
package Game;
import java.util.Random;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Input;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class GameState extends BasicGameState {
GameContainer container;
StateBasedGame stateContainer;
Image grassImage;
Image playerImage;
Image treeImage;
Image flowerImage;
Player player;
public Input input;
public final static int tile = 32;
public final static int width = 480;
public final static int height = 320;
final int tileSize = 32;
final int widthInTiles = width/tileSize;
final int heightInTiles = height/tileSize;
//int startTileX = player.x / tileSize - widthInTiles/2;
//int startTileY = player.y / tileSize - heightInTiles/2;
private int[][] level;
public int getID() {
return RPG.GAME_STATE;
}
public void init(GameContainer container, StateBasedGame stateContainer) throws SlickException {
this.container = container;
this.stateContainer = stateContainer;
grassImage = new Image("Images/Backgrounds/grass.png");
playerImage = new Image("Images/Players/defPlayer.png");
treeImage = new Image("Images/Backgrounds/tree.png");
flowerImage = new Image("Images/Backgrounds/flower.png");
input = container.getInput();
level = new int[width/tile][height/tile];
player = new Player(playerImage, 0, 0);
Random r = new Random(3424);
for (int i = 0; i < 3; i++) {
int x = r.nextInt(level.length);
int y = r.nextInt(level[x].length);
level[x][y] = 1;
}
for (int i = 0; i < 2; i++) {
int x = r.nextInt(level.length);
int y = r.nextInt(level[x].length);
level[x][y] = 2;
}
}
public void update(GameContainer container, StateBasedGame stateContainer, int delta) throws SlickException {
if (input.isKeyPressed(Input.KEY_P)) {
stateContainer.enterState(RPG.PAUSED_STATE);
}
if (input.isKeyPressed(Input.KEY_ESCAPE)) {
stateContainer.enterState(RPG.HOME_STATE);
}
if (input.isKeyPressed(Input.KEY_LEFT)) {
player.x -= 32;
if (player.x < 0 || checkCollide()) {
player.x += 32;
}
}
if (input.isKeyPressed(Input.KEY_RIGHT)) {
player.x += 32;
if (player.x > 448 || checkCollide()) {
player.x -= 32;
}
}
if (input.isKeyPressed(Input.KEY_UP)) {
player.y -= 32;
if (player.y < 0 || checkCollide()) {
player.y += 32;
}
}
if (input.isKeyPressed(Input.KEY_DOWN)) {
player.y += 32;
if (player.y > 288 || checkCollide()) {
player.y -= 32;
}
}
}
public boolean checkCollide() {
return level[(int)player.x/tile][(int)player.y/tile] == 1;
}
public void render(GameContainer container, StateBasedGame stateContainer, Graphics g) throws SlickException {
g.setColor(Color.black);
for (int x = 0; x < 480; x += 32) {
for (int y = 0; y < 320; y += 32) {
g.drawImage(grassImage, x, y);
}
}
//background
for(int x = 0; x < level.length; x++)
{
for(int y = 0; y < level[x].length; y++)
{
if(level[x][y] == 1) {
g.drawImage(treeImage, x*tile, y*tile);
}
if (level[x][y] == 2) {
g.drawImage(flowerImage, x*tile, y*tile);
}
}
}
//grid
for (int h = 0; h < height; h += 32) {
g.drawLine(0, h, width, h);
}
for (int w = 0; w < width; w += 32) {
g.drawLine(w, 0, w, height);
}
// draw player
player.draw(g);
}
}
I know I can implement it, but I’m not sure how. If anyone can give me some explanation it would be wonderful.
Shannon.