java 华容道游戏下载_Java初学之华容道游戏

1 packagehhuarongdao;2 import java.awt.*;3 import javax.swing.*;4

5 import java.awt.event.*;6 public class Hua_Rong_Road extends JFrame implementsMouseListener,KeyListener,ActionListener7 {8 Person [] person = new Person[10]; //person 为自定义的一个变量类型

9 JButton left,right,above,below ;10 JButton restart = new JButton("重新开始");11 publicHua_Rong_Road()12 {13 init();14 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);15 setBounds(100,100,320,500); //设置窗口初始位置以及大小的一个函数

16 setVisible(true); //窗口是否可见

17 validate(); //使用 validate 方法会使容器再次布置其子组件。确保布局有效。

18 }19 public voidinit()20 {21 setLayout(null);22 add(restart);23 restart.addActionListener(this);24 String name [] = {"曹操","关","张","刘","周","黄","兵","兵","兵","兵"};25 for(int i=0;i

43 left=newJButton();44 right=newJButton();45 above=newJButton();46 below = newJButton();47 add(left);48 add(right);49 add(above);50 add(below);51 left.setBounds(49, 49, 5, 260);52 right.setBounds(254,49,5,260);53 above.setBounds(49,49,210,5);54 below.setBounds(49,304,210,5);55 validate(); //确保布局有效...这条语句不要也不会对程序有太大的影响

56 }57 public voidkeyTyped(KeyEvent e){}58 public voidkeyReleased(KeyEvent e){}59 public voidkeyPressed(KeyEvent e)60 {61 Person man=(Person)e.getSource();62 if(e.getKeyCode()==KeyEvent.VK_DOWN)63 go(man,below);64 if(e.getKeyCode()==KeyEvent.VK_UP)65 go(man,above);66 if(e.getKeyCode()==KeyEvent.VK_LEFT)67 go(man,left);68 if(e.getKeyCode()==KeyEvent.VK_RIGHT)69 go(man,right);70 }71 public voidmousePressed(MouseEvent e)72 {73 Person man=(Person)e.getSource();74 int x=-1,y=-1;75 x=e.getX();76 y=e.getY();77 int w=man.getBounds().width;78 int h=man.getBounds().height;79 if(y>h/2) go(man,below);80 if(yw/2) go(man,right);83 }84 public voidmouseReleased(MouseEvent e){}85 public voidmouseEntered(MouseEvent e){}86 public voidmouseExited(MouseEvent e){}87 public voidgo(Person man, JButton direction) {88 //TODO Auto-generated method stub

89 boolean move=true;90 Rectangle manRect=man.getBounds();91 int x=man.getBounds().x;92 int y=man.getBounds().y;93 if(direction==below) y+=50;94 else if(direction==above)y-=50;95 else if(direction==left)x-=50;96 else if(direction==right)x+=50;97 manRect.setLocation(x, y);98 Rectangle directionRect=direction.getBounds();99 for(int k=0;k<10;k++)100 {101 Rectangle personRect=person[k].getBounds();102 if(manRect.intersects(personRect)&&man.number!=k)103 move=false;104 }105 if(manRect.intersects(directionRect))106 move=false;107 if(move==true) man.setLocation(x,y);108 }109 @Override110 public voidmouseClicked(MouseEvent e) {111 //TODO Auto-generated method stub

112

113 }114 public voidactionPerformed(ActionEvent e)115 {116 dispose(); //注销

117 newHua_Rong_Road();118 }119

120 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值