| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 547 人关注过本帖
标题:华容道小游戏求帮助!!!!!!!!!!!
只看楼主 加入收藏
格雷迪
Rank: 1
来 自:湖北
等 级:新手上路
帖 子:24
专家分:1
注 册:2011-2-21
结帖率:33.33%
收藏
已结贴  问题点数:10 回复次数:1 
华容道小游戏求帮助!!!!!!!!!!!
本人今年学的Java,现在想学着编个简单的华容道小游戏,正确运行后就发现很多bug,希望哪位大神帮忙改下。大家一起来共同学习下!



package HuaRongDao;
import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;

class People extends Button implements FocusListener{
      Rectangle rect=null;
      int leftX,leftY;
      int width,heigth;
      String name;
      int number;
      People(int number,String name,int leftX,int leftY,int width,int heigth,Hua_Rong_Road road){
          super(name);
          this.name=name;
          this.number=number;
          this.leftX=leftX;
          this.leftY=leftY;
          this.width=width;
          this.heigth=heigth;
          rect=new Rectangle(leftX,leftY,width,heigth);
          setBackground(Color.orange);
          setBounds(leftX,leftY,width,heigth);
          road.add(this);
          addKeyListener(road);
          addFocusListener(this);
         
      }
    @Override
    public void focusGained(FocusEvent e) {
        // TODO Auto-generated method stub
        setBackground(Color.CYAN);
    }
    @Override
    public void focusLost(FocusEvent e) {
        // TODO Auto-generated method stub
        setBackground(Color.orange);
    }
}
class Hua_Rong_Road extends JPanel implements KeyListener{
     Rectangle left,right,above,below;
     People people[]=new People[10];
     Hua_Rong_Road(){
         setLayout(null);
        people[0] = new People(0, "张辽",50,50,50,100,this);
        people[1] = new People(1, "曹操",100,50,100,100,this);
        people[2] = new People(2, "曹仁",200,50,50,100,this);
        people[3] = new People(3, "张飞",50,150,50,100,this);
        people[4] = new People(4, "关羽",100,150,100,50,this);
        people[5] = new People(5, "刘备",200,150,50,100,this);
        people[6] = new People(6, "兵",100,200,50,50,this);
        people[7] = new People(7, "兵",150,200,50,50,this);
        people[8] = new People(8, "兵",50,250,50,50,this);
        people[9] = new People(9, "兵",200,250,50,50,this);
        people[1].setForeground(Color.red);
        left=new Rectangle(49,49,1,251);
        right=new Rectangle(251,49,1,251);
        above=new Rectangle(49,49,200,1);
        below=new Rectangle(49,301,200,1);
        setVisible(true);
     }
     public void paint(Graphics g){
         g.setColor(Color.red);
         g.fillRect(49,49,1,251);
         g.fillRect(251,49,1,251);
         g.fillRect(49,49,200,1);
         g.fillRect(49,301,200,1);
         g.drawString("单击对应的人物,然后按键盘上的方向箭头移动",50,30);
         g.setColor(Color.red);
         g.drawString("曹操到达该位置",110,280);
     }
    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
    }
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub   
        People man=(People)e.getSource();
        man.rect.setLocation(man.getBounds().x,man.getBounds().y);
        if(e.getKeyCode()==KeyEvent.VK_DOWN){
            man.leftY=man.leftY+50;
            man.setLocation(man.leftX,man.leftY);
            for(int i=0;i<10;i++){
                if(man.rect.intersects(people[i].rect)&&man.number!=i){
                    man.leftY=man.leftY-50;
                    man.setLocation(man.leftX,man.leftY);
                    man.rect.setLocation(man.leftX,man.leftY);
                }
            }
            if(man.rect.intersects(below)){
                man.leftY=man.leftY-50;
                man.setLocation(man.leftX,man.leftY);
                man.rect.setLocation(man.leftX,man.leftY);
            }
        }
        if(e.getKeyCode()==KeyEvent.VK_UP){
            man.leftY=man.leftY-50;
            man.setLocation(man.leftX,man.leftY);
            man.rect.setLocation(man.leftX,man.leftY);
            for(int i=0;i<10;i++){
                if(man.rect.intersects(people[i].rect)&&man.number!=i){
                    man.leftY=man.leftY+50;
                    man.setLocation(man.leftX,man.leftY);
                    man.rect.setLocation(man.leftX,man.leftY);
                }
            }
            if(man.rect.intersects(above)){
                man.leftY=man.leftY+50;
                man.setLocation(man.leftX,man.leftY);
                man.rect.setLocation(man.leftX,man.leftY);
            }
        }
        if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            man.leftX=man.leftX+50;
            man.setLocation(man.leftX,man.leftY);
            man.rect.setLocation(man.leftX,man.leftY);
            for(int i=0;i<10;i++){
                if(man.rect.intersects(people[i].rect)&&man.number!=i){
                    man.leftX=man.leftX-50;
                    man.setLocation(man.leftX,man.leftY);
                    man.rect.setLocation(man.leftX,man.leftY);
                }
            }
            if(man.rect.intersects(right)){
                man.leftX=man.leftX-50;
                man.setLocation(man.leftX,man.leftY);
                man.rect.setLocation(man.leftX,man.leftY);
            }
        }
        if(e.getKeyCode()==KeyEvent.VK_LEFT){
            man.leftX=man.leftX-50;
            man.setLocation(man.leftX,man.leftY);
            man.rect.setLocation(man.leftX,man.leftY);
            for(int i=0;i<10;i++){
                if(man.rect.intersects(people[i].rect)&&man.number!=i){
                    man.leftX=man.leftX+50;
                    man.setLocation(man.leftX,man.leftY);
                    man.rect.setLocation(man.leftX,man.leftY);
                }
            }
            if(man.rect.intersects(left)){
                man.leftX=man.leftX+50;
                man.setLocation(man.leftX,man.leftY);
                man.rect.setLocation(man.leftX,man.leftY);
            }
        }
    }
    public static void main(String args[]){
        JFrame f=new JFrame();
        f.add("Center",new Hua_Rong_Road());
        f.setVisible(true);
        f.setBounds(5,5,400,400);
    }
}
搜索更多相关主题的帖子: 小游戏 package 华容道 import 
2012-12-27 21:45
w527705090
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:441
专家分:1882
注 册:2011-6-28
收藏
得分:10 
bug还是自己改吧。。。。

有心者,千方百计;无心者,千难万难。
2012-12-27 21:57
快速回复:华容道小游戏求帮助!!!!!!!!!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017523 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved