⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hua_rong_road2.java

📁 华容道游戏
💻 JAVA
字号:
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_Road2 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_Road2 extends Applet implements KeyListener,ActionListener
{   People people[]=new People[10];
    Rectangle left,right,above,below;
    Button restart=new Button("重新开始");
    Button b1,b2,b3,b4,b5;
    Panel p,p1;
    Label l1,l2,l3,l4,l5;
    int flag;
    public void init()
    {   setLayout(null);setBackground(Color.green);
        l1=new Label("未通过");l2=new Label("未通过");l3=new Label("未通过");
        l4=new Label("未通过");l5=new Label("未通过");
        flag=1;
        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);
        p=new Panel(new GridLayout(6,1));p1=new Panel(new GridLayout(6,2));
        p1.setBackground(Color.green);p1.setForeground(Color.blue);
        
        p1.add(new Label("第一关:横刀立马1"));p1.add(l1);
        p1.add(new Label("第二关:横刀立马2"));p1.add(l2);
        p1.add(new Label("第三关:齐头并前"));p1.add(l3);
        p1.add(new Label("第四关:兵分三路"));p1.add(l4);
        p1.add(new Label("第五关:屯兵东路"));p1.add(l5);
        
        b1=new Button("第一关:横刀立马1");b2=new Button("第二关:横刀立马2");b3=new Button("第三关:齐头并前");
        b4=new Button("第四关:兵分三路");b5=new Button("第五关:屯兵东路");
        p.add(restart);p.add(b1);p.add(b2);p.add(b3);p.add(b4);p.add(b5);add(p);add(p1);
        restart.addActionListener(this);b1.addActionListener(this);b2.addActionListener(this);
        b3.addActionListener(this);b4.addActionListener(this);b5.addActionListener(this);
        p.setBounds(350,54,110,120);p1.setBounds(350,200,200,140);
        b1.setBackground(Color.green);b2.setBackground(Color.green);b3.setBackground(Color.green);
        b4.setBackground(Color.green);b5.setBackground(Color.green);restart.setBackground(Color.green);
        b1.setForeground(Color.blue);b2.setForeground(Color.blue);b3.setForeground(Color.blue);
        b4.setForeground(Color.blue);b5.setForeground(Color.blue);restart.setForeground(Color.blue);
        
        
       
        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.blue);
        g.fillRect(49,49,5,260);
        g.fillRect(254,49,5,260);
        g.fillRect(49,304,56,5);
        g.setColor(Color.white);


        g.fillRect(104,304,109,5);
        g.setColor(Color.blue);
        g.fillRect(204,304,55,5);
        g.drawString("单击相应人物,然后按键盘上下左右键移动其位置,使曹操到达指定位置",60,20);
        g.setColor(Color.red);
        g.drawString("曹操逃走的位置",110,300);
    }
    public void genggai(People peo,int a,int b)
    {   peo.left_x=a;peo.left_y=b;
        peo.setLocation(a,b);peo.rect.setLocation(a,b);
    }
    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;
            man.setLocation(man.left_x,man.left_y);
            man.rect.setLocation(man.left_x,man.left_y);
            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;
            man.setLocation(man.left_x,man.left_y);
            man.rect.setLocation(man.left_x,man.left_y);
            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;
            man.setLocation(man.left_x,man.left_y);
            man.rect.setLocation(man.left_x,man.left_y);
            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;
            man.setLocation(man.left_x,man.left_y);
            man.rect.setLocation(man.left_x,man.left_y);
            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);
            } 
        }
        if((people[0].left_x==104)&&(people[0].left_y==204))
        {   switch (flag)
               {case 1:
                    l1.setText("通过");
                    break;
                case 2:
                    l2.setText("通过");
                    break;
                case 3:
                    l3.setText("通过");
                    break;
                case 4:
                    l4.setText("通过");
                    break;
                case 5:
                    l5.setText("通过");
                    break;
               }
        }
       
    }
    public void keyTyped(KeyEvent e){}
    public void keyReleased(KeyEvent e){}
    public void actionPerformed(ActionEvent e)
    {   
        if(e.getSource()==b1)
        {   flag=1;
            
            genggai(people[0],104,54);
            
            genggai(people[1],104,154);
            
            genggai(people[2],54,154);
            
            genggai(people[3],204,154);
            
            genggai(people[4],54,54);
            
            genggai(people[5],204,54);
            
            genggai(people[6],54,254);
            
            genggai(people[7],204,254);
            
            genggai(people[8],104,204);
            
            genggai(people[9],154,204);
        }
        if(e.getSource()==b2)
        {   flag=2;
            genggai(people[0],104,54);
            genggai(people[1],104,154);
            genggai(people[2],54,204);
            genggai(people[3],204,204);
            genggai(people[4],54,54);
            genggai(people[5],204,54);
            genggai(people[6],54,154);
            genggai(people[7],204,154);
            genggai(people[8],104,204);
            genggai(people[9],154,204);
        }
        if(e.getSource()==b3)
        {   flag=3;
            genggai(people[0],104,54);
            genggai(people[1],104,204);
            genggai(people[2],54,204);
            genggai(people[3],204,204);
            genggai(people[4],54,54);
            genggai(people[5],204,54);
            genggai(people[6],54,154);
            genggai(people[7],204,154);
            genggai(people[8],104,154);
            genggai(people[9],154,154);
        }
        if(e.getSource()==b4)
        {   flag=4;
            genggai(people[0],104,54);
            genggai(people[1],104,154);
            genggai(people[2],54,204);
            genggai(people[3],204,204);
            genggai(people[4],54,104);
            genggai(people[5],204,54);
            genggai(people[6],54,54);
            genggai(people[7],204,154);
            genggai(people[8],104,204);
            genggai(people[9],154,204);
        }
        if(e.getSource()==b5)
        {   flag=5;
            genggai(people[0],54,54);
            genggai(people[1],54,154);
            genggai(people[2],54,204);
            genggai(people[3],104,204);
            genggai(people[4],154,54);
            genggai(people[5],204,54);
            genggai(people[6],154,204);
            genggai(people[7],204,154);
            genggai(people[8],154,154);
            genggai(people[9],204,204);
        }
        if(e.getSource()==restart)
        {   l1.setText("未通过");l2.setText("未通过");l3.setText("未通过");
            l4.setText("未通过");l5.setText("未通过");
            flag=1;
            genggai(people[0],104,54);
            
            genggai(people[1],104,154);
            
            genggai(people[2],54,154);
            
            genggai(people[3],204,154);
            
            genggai(people[4],54,54);
            
            genggai(people[5],204,54);
            
            genggai(people[6],54,254);
            
            genggai(people[7],204,254);
            
            genggai(people[8],104,204);
            
            genggai(people[9],154,204);
       
        }
    }
   
       
}
         
               
        

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -