floor.png
wall.png
hero.png
public class Test extends JFrame {
public static void main(String[] args) {
Test t = new Test();
t.setDefaultCloseOperation(EXIT_ON_CLOSE);
t.setVisible(true);
}
public Test() {
setTitle("My Game");
MyPanel panel = new MyPanel();
getContentPane().add(panel);
pack();
}
public void go() {
}
public class MyPanel extends JPanel implements KeyListener,Runnable {
public static final int WIDTH = 32*15;
public static final int HEIGHT = 32*15;
public Image floor;
public Image wall;
public Image role;
public static final int LEFT = 1;
public static final int RIGHT = 2;
public static final int UP = 3;
public static final int DOWN = 4;
public static final int CS = 32;
public static final int mapWidth = 15;
public static final int mapHeight = 15;
public int[][] map = {
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,1,1,1,1,1,0,0,0,0,2},
{2,0,0,0,0,1,0,0,0,1,0,0,0,0,2},
{2,0,0,0,0,1,0,0,0,1,0,0,0,0,2},
{2,0,0,0,0,1,0,0,0,1,0,0,0,0,2},
{2,0,0,0,0,1,1,0,1,1,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}};
private int role_X;
private int role_Y;
private int state;
private int direct;
public MyPanel() {
setPreferredSize(new Dimension(WIDTH,HEIGHT));
loadPic();
role_X = 1;
role_Y = 1;
setFocusable(true);
addKeyListener(this);
Thread t = new Thread(this);
t.start();
}
public void loadPic() {
floor = new ImageIcon("image/floor.png").getImage();
wall = new ImageIcon("image/wall.png").getImage();
role = new ImageIcon("image/hero.gif").getImage();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawMap(g);
drawRole(g);
}
public void drawMap(Graphics g) {
for(int i=0; i< mapWidth; ++i) {
for(int j=0; j < mapHeight; ++j) {
switch(map[i][j]) {
case 0:
//g.setColor(Color.green);
//g.drawRect(i*CS, j*CS, CS, CS);
g.drawImage(floor,j*CS,i*CS,this);
break;
case 1:
//g.setColor(Color.red);
//g.drawRect(i*CS, j*CS, CS, CS);
g.drawImage(wall,j*CS,i*CS,this);
break;
}
}
}
}
public void drawRole(Graphics g) {
//g.setColor(Color.yellow);
//g.drawRect(role_X*CS, role_Y*CS, CS, CS);
g.drawImage(role, role_X*CS, role_Y*CS, role_X*CS+CS, role_Y*CS+CS,
state*CS, 0, state*CS+CS, CS,this);
}
public Color randomColor() {
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
return new Color(red, green, blue);
}
public boolean canGo(int x, int y) {
if(map[y][x]==0)
return true;
return false;
}
public void moveRole(int direct) {
switch(direct) {
case LEFT:
if(canGo(role_X-1,role_Y))
--role_X;
break;
case RIGHT:
if(canGo(role_X+1,role_Y))
++role_X;
break;
case UP:
if(canGo(role_X,role_Y-1))
--role_Y;
break;
case DOWN:
if(canGo(role_X,role_Y+1))
++role_Y;
break;
}
repaint();
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
int keycode = arg0.getKeyCode();
switch(keycode) {
case KeyEvent.VK_LEFT:
moveRole(LEFT);
break;
case KeyEvent.VK_RIGHT:
moveRole(RIGHT);
break;
case KeyEvent.VK_UP:
moveRole(UP);
break;
case KeyEvent.VK_DOWN:
moveRole(DOWN);
break;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
if (state == 0)
state = 1;
else if(state == 1)
state = 0;
repaint();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}