neozf
03-19-2008, 06:37 PM
Hmm, this is the third one so far isn't it.
So far I've got it so you can move around, you can change maps by walking off the side of one (Which map leads to which is defined in a file). I'm going to add a piece of code soon so that you can't walk on things like tiles etc.
The code is currently quite messy and needs to be cleaned up I know.
And I might start working on the Map Maker soon.
import java.applet.*;
import java.awt.*;
import java.io.*;
public class Test extends Applet implements Runnable
{
//Map array and tiles
private int[][] map = new int[5][5];
private int[]exit = new int[4];
private Image tileGrass;
private Image tileWall;
private Image player;
private int locX=2;
private int locY=2;
private int oldLocX;
private int oldLocY;
private int gridX=100;
private int gridY=100;
private int mapName=1;
private int mapDat =1;
public void init()
{
setBackground(Color.black);
}
public void start()
{
//map=new int[5][5];
mapLoad();
tileGrass=getImage(getCodeBase(),"grass.GIF");
tileWall=getImage(getCodeBase(),"wall.GIF");
player=getImage(getCodeBase(),"player.PNG");
Thread thread = new Thread(this);
thread.start();
}
public void stop(){}
public void destory(){}
public void mapLoad()
{
try{
FileReader map = new FileReader("Maps\\"+mapName+".map");
FileReader dat = new FileReader("Maps\\"+mapDat+".dat");
BufferedReader buffer = new BufferedReader(map);
BufferedReader buffer2 = new BufferedReader(dat);
String mapTile=null;
String mapData=null;
int x=0; int y=0;
while ((mapTile=buffer.readLine())!=null)
{
mapBuild(x,y,Integer.parseInt(mapTile));
x++;
if(x==5){y++;x=0;}
}
x=0;
while ((mapData=buffer2.readLine())!=null)
{
exitBuild(x,Integer.parseInt(mapData));
x++;
}
}
catch (Exception e){}
}
public void mapBuild(int x,int y,int tile){ map[x][y]=tile; }
public void exitBuild(int x,int exitLoc){ exit[x]=exitLoc; }
public void mapChange(char direction)
{
if(direction=='N') {mapName=exit[0]; mapDat=exit[0];}
if(direction=='E') {mapName=exit[1]; mapDat=exit[1];}
if(direction=='S') {mapName=exit[2]; mapDat=exit[2];}
if(direction=='W') {mapName=exit[3]; mapDat=exit[3];}
mapLoad();
}
public void run()
{
Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);
while(true)
{
repaint();
try
{
Thread.sleep(20);
}
catch(InterruptedException ex){}
Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);
}
}
public void update (Graphics g)
{
paint(g);
}
public boolean keyDown(Event e, int key)
{
oldLocX=locX;
oldLocY=locY;
if(key==Event.LEFT){locX-=1;}
if(key==Event.RIGHT){locX+=1;}
if(key==Event.UP){locY-=1;}
if(key==Event.DOWN){locY+=1;}
if(locX<0&&exit[3]!=0){mapChange('W');locX=4;}else if(locX<0&&exit[3]==0) locX=oldLocX;
if(locX>4&&exit[1]!=0){mapChange('E');locX=0;}else if(locX>4&&exit[1]==0) locX=oldLocX;
if(locY<0&&exit[0]!=0){mapChange('N');locY=4;}else if(locY<0&&exit[0]==0) locY=oldLocY;
if(locY>4&&exit[2]!=0){mapChange('S');locY=0;}else if(locY>4&&exit[2]==0) locY=oldLocY;
if(map[locX][locY]==2) {locX=oldLocX;locY=oldLocY;}
return true;
}
public void paint(Graphics g)
{
Image tile=tileWall;
for (int x=0;x!=5;x++)
{
for (int y=0;y!=5;y++)
{
if(map[x][y]==1) tile = tileGrass;
if(map[x][y]==2) tile = tileWall;
g.drawImage (tile,x*50+gridX,y*50+gridY,this);
}
}
g.drawImage(player,locX*50+gridX,locY*50+gridY,thi s);
}
}Edit: Changed the way it loads the maps and changed it so you can't walk on walls anymore.
So far I've got it so you can move around, you can change maps by walking off the side of one (Which map leads to which is defined in a file). I'm going to add a piece of code soon so that you can't walk on things like tiles etc.
The code is currently quite messy and needs to be cleaned up I know.
And I might start working on the Map Maker soon.
import java.applet.*;
import java.awt.*;
import java.io.*;
public class Test extends Applet implements Runnable
{
//Map array and tiles
private int[][] map = new int[5][5];
private int[]exit = new int[4];
private Image tileGrass;
private Image tileWall;
private Image player;
private int locX=2;
private int locY=2;
private int oldLocX;
private int oldLocY;
private int gridX=100;
private int gridY=100;
private int mapName=1;
private int mapDat =1;
public void init()
{
setBackground(Color.black);
}
public void start()
{
//map=new int[5][5];
mapLoad();
tileGrass=getImage(getCodeBase(),"grass.GIF");
tileWall=getImage(getCodeBase(),"wall.GIF");
player=getImage(getCodeBase(),"player.PNG");
Thread thread = new Thread(this);
thread.start();
}
public void stop(){}
public void destory(){}
public void mapLoad()
{
try{
FileReader map = new FileReader("Maps\\"+mapName+".map");
FileReader dat = new FileReader("Maps\\"+mapDat+".dat");
BufferedReader buffer = new BufferedReader(map);
BufferedReader buffer2 = new BufferedReader(dat);
String mapTile=null;
String mapData=null;
int x=0; int y=0;
while ((mapTile=buffer.readLine())!=null)
{
mapBuild(x,y,Integer.parseInt(mapTile));
x++;
if(x==5){y++;x=0;}
}
x=0;
while ((mapData=buffer2.readLine())!=null)
{
exitBuild(x,Integer.parseInt(mapData));
x++;
}
}
catch (Exception e){}
}
public void mapBuild(int x,int y,int tile){ map[x][y]=tile; }
public void exitBuild(int x,int exitLoc){ exit[x]=exitLoc; }
public void mapChange(char direction)
{
if(direction=='N') {mapName=exit[0]; mapDat=exit[0];}
if(direction=='E') {mapName=exit[1]; mapDat=exit[1];}
if(direction=='S') {mapName=exit[2]; mapDat=exit[2];}
if(direction=='W') {mapName=exit[3]; mapDat=exit[3];}
mapLoad();
}
public void run()
{
Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);
while(true)
{
repaint();
try
{
Thread.sleep(20);
}
catch(InterruptedException ex){}
Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);
}
}
public void update (Graphics g)
{
paint(g);
}
public boolean keyDown(Event e, int key)
{
oldLocX=locX;
oldLocY=locY;
if(key==Event.LEFT){locX-=1;}
if(key==Event.RIGHT){locX+=1;}
if(key==Event.UP){locY-=1;}
if(key==Event.DOWN){locY+=1;}
if(locX<0&&exit[3]!=0){mapChange('W');locX=4;}else if(locX<0&&exit[3]==0) locX=oldLocX;
if(locX>4&&exit[1]!=0){mapChange('E');locX=0;}else if(locX>4&&exit[1]==0) locX=oldLocX;
if(locY<0&&exit[0]!=0){mapChange('N');locY=4;}else if(locY<0&&exit[0]==0) locY=oldLocY;
if(locY>4&&exit[2]!=0){mapChange('S');locY=0;}else if(locY>4&&exit[2]==0) locY=oldLocY;
if(map[locX][locY]==2) {locX=oldLocX;locY=oldLocY;}
return true;
}
public void paint(Graphics g)
{
Image tile=tileWall;
for (int x=0;x!=5;x++)
{
for (int y=0;y!=5;y++)
{
if(map[x][y]==1) tile = tileGrass;
if(map[x][y]==2) tile = tileWall;
g.drawImage (tile,x*50+gridX,y*50+gridY,this);
}
}
g.drawImage(player,locX*50+gridX,locY*50+gridY,thi s);
}
}Edit: Changed the way it loads the maps and changed it so you can't walk on walls anymore.
