关于涂鸦跳跃
程序代码:
import java.awt.Frame; import java.awt.event.*; import java.awt.*; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class mainframe extends JFrame implements Runnable { JPanel pnlMain = new JPanel(null);// 启用面板,使用坐标定位而非流式布局 JLabel[] bds = new JLabel[10]; JLabel lblMan = new JLabel(new ImageIcon("images/man.jpg")); private boolean bL = false, bU = false, bR = false;// 确定小人的运动方向 enum Direction { L, LU, R, RU, D, LD, RD, U, STOP };// 8个方向 加上停止 private Direction dir = Direction.STOP;// 定义方向,默认朝下!!!!! int From_x = 30;// 位置变量 int From_y = 30; int Fra_width = 300;// 框的大小 int Fra_height = 600; int board_x; int board_y = 80; int man_x = 0;// 小人的位置 int man_y = 500; int based = man_y - 600;// 小人的绝对高度 public void move() { switch (dir) { case L: man_x -= 5; System.out.println("sdfsadf"); lblMan.setLocation(man_x, man_y); break; case LU: man_x -= 5; man_y -= 5; lblMan.setLocation(man_x, man_y); break; case R: man_x += 5; lblMan.setLocation(man_x, man_y); break; case RU: man_x += 5; man_y -= 5; lblMan.setLocation(man_x, man_y); break; case U: man_y -= 5; lblMan.setLocation(man_x, man_y); break; case D: man_y += 5; lblMan.setLocation(man_x, man_y); break; case LD: man_x -= 5; man_y += 5; lblMan.setLocation(man_x, man_y); break; case RD: man_x += 5; man_y += 5; lblMan.setLocation(man_x, man_y); break; case STOP: lblMan.setLocation(man_x, man_y); break; default: break; } } void locationDirection() {// 方向 if (bL && !bU && !bR) dir = Direction.L; else if (!bL && bU && bR) dir = Direction.LU; else if (!bL && !bU && bR) dir = Direction.R; else if (!bL && bU && bR) dir = Direction.RU; else if (!bL && !bU && !bR) dir = Direction.STOP; // if(bL && !bU && !bR ) dir=Direction.L; // if(bL && !bU && !bR ) dir=Direction.L; // if(bL && !bU && !bR ) dir=Direction.L; } public void lauchFrame() { this.setLocation(From_x, From_y); this.setSize(Fra_width, Fra_height); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // // Thread t=new Thread(){; // public void run(){ // // } // }; // for (int i = 0; i < bds.length; i++) { bds[i] = new JLabel(new ImageIcon("images/board.jpg")); bds[i].setBounds((int) (Math.random() * 250), i * 50, 63, 19);// 启用随机函数设置挡板的位置 pnlMain.add(bds[i]);// 显示 } lblMan.setBounds(man_x, man_y, 65, 63);// 置顶位置以及大小 pnlMain.add(lblMan);// 面板加入小人 this.add(pnlMain); this.setResizable(false); setVisible(true);// 窗体可见 // 确定方向 // 监听键盘 this.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); // if(key==KeyEvent.VK_LEFT){ // man_x-=5; // lblMan.setLocation(man_x, man_y); // } // if(key==KeyEvent.VK_RIGHT){ // man_x+=5; // lblMan.setLocation(man_x, man_y); // } // if(key==KeyEvent.VK_UP){ // man_y-=5; // lblMan.setLocation(man_x, man_y); // } // if(key==KeyEvent.VK_DOWN){ // man_y+=5; // lblMan.setLocation(man_x, man_y); // } switch (key) { case KeyEvent.VK_LEFT: bL = true; move(); // lblMan.setLocation(man_x, man_y); break; case KeyEvent.VK_RIGHT: bR = true; move(); // lblMan.setLocation(man_x, man_y); break; case KeyEvent.VK_UP: bU = true; // lblMan.setLocation(man_x, man_y); break; // case KeyEvent.VK_DOWN: // bD=true; // lblMan.setLocation(man_x, man_y); // break; case KeyEvent.VK_SPACE: bU = true; // lblMan.setLocation(man_x, man_y); break; default: break; } } }); } public static void main(String[] args) { mainframe mf = new mainframe(); mf.lauchFrame(); System.out.println(); } @Override public void run() { } }