类【推箱子】游戏

floor.png floor.png

wall.png wall.png

hero.png hero.gif

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();
				}
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值