Hello,
I have created this code. This code wil create a tile map on the screen.
Game.java
import org.newdawn.slick.AppGameContainer; import org.newdawn.slick.BasicGame; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.Input; import org.newdawn.slick.SlickException; public class Game extends BasicGame { private map map; private Player player; public Game() { super("one class barebone game"); } public void init(GameContainer container) throws SlickException { map = new map(); player = new Player(); map.createArray(); } public void render(GameContainer container, Graphics g) { for(int y=0;y<14;y++){ for(int x=0;x<14;x++){ if(map.getMap(x, y).equals("g")){ g.drawImage(map.getGrass(), x *32, y * 32); } if(map.getMap(x, y).equals("w")){ g.drawImage(map.getWall(), x *32, y * 32); } } } player.draw(g); } public void update(GameContainer gc, int delta) { Input input = gc.getInput(); if(input.isKeyDown(Input.KEY_LEFT)){ if(map.checkCollision(player.x-1, player.y)) player.x-=1*delta; } if(input.isKeyDown(Input.KEY_RIGHT)){ if(map.checkCollision(player.x-1,player.y)){ player.x+=0.1*delta; } } if(input.isKeyDown(Input.KEY_UP)){ if(map.checkCollision(player.x,player.y-1)){ player.y-=0.1*delta; } } if(input.isKeyDown(Input.KEY_DOWN)){ if(map.checkCollision(player.x,player.y+1)){ player.y+=1*delta; } } } public static void main(String[] args) throws SlickException { AppGameContainer container = new AppGameContainer(new Game(), 14*32, 14*32, false); container.start(); } }
map.java
import java.io.File; import java.util.Scanner; import org.newdawn.slick.Image; public class map { private Scanner scanner; private String[] map= new String[14]; String[][] blockArray= new String[448][448]; private Image grass; private Image wall; public String getMap(int x, int y){ String index = map[y].substring(x,x+1); return index; } public void createArray(){ for(int y=0;y<14;y++){ for(int x=0;x<14;x++){ if(getMap(x, y).equals("w")){ for(int yy=0;yy<32;yy++){ for(int xx=0;xx<32;xx++){ blockArray[x+xx][y+yy] = "w"; } } } } } } public boolean checkCollision(int x, int y){ return blockArray[x][y] != "w"; } public map(){ try{ grass = new Image("data/grass.png"); wall = new Image("data/wall.png"); }catch(Exception e){ System.out.println("Probleem laden afbeeldingen map"); } openFile(); readFile(); closeFile(); } public void openFile(){ try{ scanner = new Scanner(new File("data/map.txt")); }catch(Exception e){ System.out.println("Error loading map"); } } public void readFile(){ while(scanner.hasNext()){ for(int i=0;i<14;i++){ map[i]=scanner.next(); } } } public void closeFile(){ scanner.close(); } public Image getGrass(){ return grass; } public Image getWall(){ return wall; } }
player.java
import org.newdawn.slick.Graphics; import org.newdawn.slick.Image; public class Player { public int x=32,y=32; private Image img; public Player(){ try { img = new Image("data/player.png"); } catch(Exception e){ System.out.println("Probleem afbeelding player"); } } public void draw(Graphics g){ g.drawImage(img,x,y); } public int getX(){ return x; } public int getY(){ return y; } }
But my player wont move unless I remove this bit of coding.
if(map.checkCollision(player.x-1,player.y))
So the error is in that code but I dont know where exactly and I need that code for collision detection.