it have 3 classes.
main class = run every thing
player class = create player and create his moves
level class = render map and player
when i run this code below it render a map and two players.
1st player in redering in main when can move. if user hit right or left key it moves fine. (its moves bc of a method player_class.player_move(). i am calling this method in main)
but problem is in 2nd player which is redering in level class. it does render but it doesnt move bc player_move() method is not appliet on 2nd player.
i want some way so player_move() method is applied on 2nd player. which will make him move too.
/*** main.java ***/
public class main exted JApplet implement ActionListener
{
player player_class = new player();
level level_class = new level();
....
//useing timer and this is a main game loop
public void actionPerformed(ActionEvent e)
{
player_class.player_move() ;
repaint();
}
public void paint()
{
super.paint(g);
player_class.paint(g);
level_class.paint(g);
}
}
/*** player.java/
public class player
{
....
public void player_move()
{
//if user hit right key than move player right
//if user hit left key than move player left
//x+=2; or x-=2
}
//my player which is just a box
public void paint(Graphics g)
{
g.drawIRect(x, y, width, height);
}
}
/*** level.java ***/
public class level
{
private int level01[][] = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0,0,2,0,0,0},
{2,2,2,2,2,2,2,0,0,2,2,2,2,2,2} };
int image_tile_size = 32; //32 by 32 image
public level()
{
}
@Override
public void paint(Graphics g)
{
//render map.
//draw green box where its '2'
//draw player where its '1'
//draw blue box for sky where its '0'
for(int y = 0; y < level01.length; y++) //rows
{
for(int x = 0; x < level01[y].length; x++) //cols
{
if(level01[y][x] == 0)
{
g.setColor(Color.blue);
g.fillRect(x*image_tile_size, y*image_tile_size, image_tile_size, image_tile_size); //bc image_title_size is the image size
}
if(level01[y][x] == 1)
{
//draw player where there is '1'
player player_class = new player(x*image_tile_size, y*image_tile_size);
player_class.paint(g);
}
if(level01[y][x] == 2)
{
g.setColor(Color.green);
g.fillRect(x*image_tile_size, y*image_tile_size, image_tile_size, image_tile_size);
}
}
}//end of main for loop
}//end of paint method
}//end of level class