JAVA开发的华容道游戏
来源:互联网 发布:社交网络平台有哪些 编辑:程序博客网 时间:2024/04/30 13:10
import java.awt.*;import java.applet.*;import java.awt.event.*;
class People extends Button implements FocusListener //代表华容道人物的类。
{ Rectangle rect=null;
int left_x,left_y;//按扭的左上角坐标.
int width,height; //按扭的宽和高.
String name; int number;
People(int number,String s,int x,int y,int w,int h,Hua_Rong_Road road)
{ super(s);
name=s;this.number=number;
left_x=x;left_y=y;
width=w;height=h;setBackground(Color.orange);
road.add(this); addKeyListener(road);
setBounds(x,y,w,h);addFocusListener(this);
rect=new Rectangle(x,y,w,h);
}
public void focusGained(FocusEvent e)
{ setBackground(Color.red);
}
public void focusLost(FocusEvent e)
{ setBackground(Color.orange);
}
}
public class Hua_Rong_Road extends Applet implements KeyListener,ActionListener
{ People people[]=new People[10];
Rectangle left,right,above ,below;//华容道的边界 .
Button restart=new Button("重新开始");
public void init()
{ setLayout(null); add(restart);
restart.setBounds(5,5,80,25);
restart.addActionListener(this);
people[0]=new People(0,"曹操",104,54,100,100,this);
people[1]=new People(1,"关羽",104,154,100,50,this);
people[2]=new People(2,"张飞",54, 154,50,100,this);
people[3]=new People(3,"刘备",204,154,50,100,this);
people[4]=new People(4,"张辽",54, 54, 50,100,this);
people[5]=new People(5,"曹仁",204, 54, 50,100,this);
people[6]=new People(6,"兵 ",54,254,50,50,this);
people[7]=new People(7,"兵 ",204,254,50,50,this);
people[8]=new People(8,"兵 ",104,204,50,50,this);
people[9]=new People(9,"兵 ",154,204,50,50,this);
people[9].requestFocus();
left=new Rectangle(49,49,5,260);
people[0].setForeground(Color.white) ;
right=new Rectangle(254,49,5,260);
above=new Rectangle(49,49,210,5);
below=new Rectangle(49,304,210,5);
}
public void paint(Graphics g)
{ //画出华容道的边界:
g.setColor(Color.cyan);
g.fillRect(49,49,5,260);//left.
g.fillRect(254,49,5,260);//right.
g.fillRect(49,49,210,5); //above.
g.fillRect(49,304,210,5);//below.
//提示曹操逃出位置和按键规则:
g.drawString("点击相应的人物,然后按键盘上的上下左右箭头移动",100,20);
g.setColor(Color.red);
g.drawString("曹操到达该位置",110,300);
}
public void keyPressed(KeyEvent e)
{ People man=(People)e.getSource();//获取事件源.
man.rect.setLocation(man.getBounds().x, man.getBounds().y);
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{ man.left_y=man.left_y+50; //向下前进50个单位。
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其它人物或下边界出现重叠,如果出现重叠就退回50个单位距离。
for(int i=0;i<10;i++)
{if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{ man.left_y=man.left_y-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(below))
{ man.left_y=man.left_y-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{ man.left_y=man.left_y-50; //向上前进50个单位。
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其它人物或上边界出现重叠,如果出现重叠就退回50个单位距离。
for(int i=0;i<10;i++)
{ if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{ man.left_y=man.left_y+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(above))
{ man.left_y=man.left_y+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{ man.left_x=man.left_x-50; //向左前进50个单位。
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其它人物或左边界出现重叠,如果出现重叠就退回50个单位距离。
for(int i=0;i<10;i++)
{ if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{ man.left_x=man.left_x+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(left))
{ man.left_x=man.left_x+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{ man.left_x=man.left_x+50; //向右前进50个单位。
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其它人物或右边界出现重叠,如果出现重叠就退回50个单位距离。
for(int i=0;i<10;i++)
{ if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{ man.left_x=man.left_x-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(right))
{ man.left_x=man.left_x-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void actionPerformed(ActionEvent e)
{ this.removeAll();
this.init();
}
}
class People extends Button implements FocusListener //代表华容道人物的类。
{ Rectangle rect=null;
int left_x,left_y;//按扭的左上角坐标.
int width,height; //按扭的宽和高.
String name; int number;
People(int number,String s,int x,int y,int w,int h,Hua_Rong_Road road)
{ super(s);
name=s;this.number=number;
left_x=x;left_y=y;
width=w;height=h;setBackground(Color.orange);
road.add(this); addKeyListener(road);
setBounds(x,y,w,h);addFocusListener(this);
rect=new Rectangle(x,y,w,h);
}
public void focusGained(FocusEvent e)
{ setBackground(Color.red);
}
public void focusLost(FocusEvent e)
{ setBackground(Color.orange);
}
}
public class Hua_Rong_Road extends Applet implements KeyListener,ActionListener
{ People people[]=new People[10];
Rectangle left,right,above ,below;//华容道的边界 .
Button restart=new Button("重新开始");
public void init()
{ setLayout(null); add(restart);
restart.setBounds(5,5,80,25);
restart.addActionListener(this);
people[0]=new People(0,"曹操",104,54,100,100,this);
people[1]=new People(1,"关羽",104,154,100,50,this);
people[2]=new People(2,"张飞",54, 154,50,100,this);
people[3]=new People(3,"刘备",204,154,50,100,this);
people[4]=new People(4,"张辽",54, 54, 50,100,this);
people[5]=new People(5,"曹仁",204, 54, 50,100,this);
people[6]=new People(6,"兵 ",54,254,50,50,this);
people[7]=new People(7,"兵 ",204,254,50,50,this);
people[8]=new People(8,"兵 ",104,204,50,50,this);
people[9]=new People(9,"兵 ",154,204,50,50,this);
people[9].requestFocus();
left=new Rectangle(49,49,5,260);
people[0].setForeground(Color.white) ;
right=new Rectangle(254,49,5,260);
above=new Rectangle(49,49,210,5);
below=new Rectangle(49,304,210,5);
}
public void paint(Graphics g)
{ //画出华容道的边界:
g.setColor(Color.cyan);
g.fillRect(49,49,5,260);//left.
g.fillRect(254,49,5,260);//right.
g.fillRect(49,49,210,5); //above.
g.fillRect(49,304,210,5);//below.
//提示曹操逃出位置和按键规则:
g.drawString("点击相应的人物,然后按键盘上的上下左右箭头移动",100,20);
g.setColor(Color.red);
g.drawString("曹操到达该位置",110,300);
}
public void keyPressed(KeyEvent e)
{ People man=(People)e.getSource();//获取事件源.
man.rect.setLocation(man.getBounds().x, man.getBounds().y);
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{ man.left_y=man.left_y+50; //向下前进50个单位。
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其它人物或下边界出现重叠,如果出现重叠就退回50个单位距离。
for(int i=0;i<10;i++)
{if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{ man.left_y=man.left_y-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(below))
{ man.left_y=man.left_y-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{ man.left_y=man.left_y-50; //向上前进50个单位。
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其它人物或上边界出现重叠,如果出现重叠就退回50个单位距离。
for(int i=0;i<10;i++)
{ if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{ man.left_y=man.left_y+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(above))
{ man.left_y=man.left_y+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{ man.left_x=man.left_x-50; //向左前进50个单位。
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其它人物或左边界出现重叠,如果出现重叠就退回50个单位距离。
for(int i=0;i<10;i++)
{ if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{ man.left_x=man.left_x+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(left))
{ man.left_x=man.left_x+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{ man.left_x=man.left_x+50; //向右前进50个单位。
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其它人物或右边界出现重叠,如果出现重叠就退回50个单位距离。
for(int i=0;i<10;i++)
{ if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{ man.left_x=man.left_x-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(right))
{ man.left_x=man.left_x-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void actionPerformed(ActionEvent e)
{ this.removeAll();
this.init();
}
}
- JAVA开发的华容道游戏
- 游戏华容道的历史
- java编程华容道智力游戏
- AndEngine开发的华容道
- J2me游戏开发实例讲解(华容道)
- 华容道游戏
- 华容道 游戏
- 华容道游戏
- J2Me华容道游戏的实现(转载)
- 《华容道》的历史和游戏解法
- 发芽网的在线华容道游戏
- Java 华容道
- Java-华容道
- Java华容道
- 【java】华容道游戏设计与搜索算法实现
- 类似华容道游戏源码
- aauto华容道开发
- 28连局华容道游戏
- 对抽象和泛化区别的思考
- 三步学会Java Socket编程
- Java Socket编程(一)Socket传输模式
- 统计报表分析柱状图
- 懷疑 ——一品軒的麵包 用糖精
- JAVA开发的华容道游戏
- 解决WEB页面上"焦点控制"一法
- Java中的传值与传引用之我见
- 两则vim广告 ^_^
- 从普通程序员到CTO所要培养的六种能力
- ASP.NET事件回传机制
- Java Socket编程(二)Java面向连接的类
- Java Socket编程(三)服务器Sockets
- JAVA基础:随机整数的生成