Java初学之华容道游戏

Java初学之华容道游戏

1 package hhuarongdao;
2 
3 public class example {
4  public static void main(String args[])
5  {
6     new Hua_Rong_Road();
7  }
8  }

  1 package hhuarongdao;
  2 import java.awt.*;
  3 import javax.swing.*;
  4 
  5 import java.awt.event.*;
  6 public class Hua_Rong_Road extends JFrame implements MouseListener,KeyListener,ActionListener
  7 {
  8     Person [] person = new Person[10];   //person 为自定义的一个变量类型
  9     JButton left,right,above,below ;
 10     JButton restart = new JButton("重新开始");
 11     public Hua_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 void init()
 20     {
 21         setLayout(null); 
 22         add(restart);
 23         restart.addActionListener(this);
 24         String name [] = {"曹操","关","张","刘","周","黄","兵","兵","兵","兵"};
 25         for(int i=0;i<name.length;i++)
 26         {
 27           person[i]= new Person(i,name[i]);
 28           person[i].addMouseListener(this);
 29           person[i].addKeyListener(this);
 30           add(person[i]);
 31         }
 32         person[0].setBounds(104, 54, 100, 100);
 33         person[1].setBounds(104,154,100,50);
 34         person[2].setBounds(54,154,50,100);
 35         person[3].setBounds(204,54,50,100);
 36         person[4].setBounds(54,54,50,100);
 37         person[5].setBounds(204, 54, 50, 100);
 38         person[6].setBounds(54,254,50,50);
 39         person[7].setBounds(204,254,50,50);
 40         person[8].setBounds(104,204,50,50);
 41         person[9].setBounds(154,204,50,50);
 42         person[9].requestFocus();   //将焦点放在控件上面
 43         left=new JButton();
 44         right=new JButton();
 45         above=new JButton();
 46         below = new JButton();
 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 void keyTyped(KeyEvent e){}
 58     public void keyReleased(KeyEvent e){}
 59     public void keyPressed(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 void mousePressed(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(y<h/2) go(man,above);
 81          if(x<w/2) go(man,left);
 82          if(x>w/2) go(man,right);
 83     }
 84     public void mouseReleased(MouseEvent e){}
 85     public void mouseEntered(MouseEvent e){}
 86     public void mouseExited(MouseEvent e){}
 87     public void go(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     @Override
110     public void mouseClicked(MouseEvent e) {
111         // TODO Auto-generated method stub
112         
113     }    
114     public void actionPerformed(ActionEvent e)
115     {
116        dispose();  //注销
117        new Hua_Rong_Road();
118     }
119     
120 }

 1 package hhuarongdao;
 2 import javax.swing.*;
 3 import java.awt.*;
 4 import java.awt.event.*;
 5 public class Person extends JButton implements FocusListener
 6 {
 7     
 8     int number ;
 9     Color c= new Color(255,245,170);
10     Font font=new Font("宋体",Font.BOLD,12);
11     Person(int number,String s)
12     {
13         super(s);
14         setBackground(c);
15         setFont(font);
16         this.number=number;
17         c=getBackground();
18         addFocusListener(this);
19     }
20     public void focusGained(FocusEvent e)
21     {
22         setBackground(Color.red);
23     }
24     public void focusLost(FocusEvent e)
25     {
26        setBackground(c);        
27     }
28 }

效果图:

Java初学之华容道游戏

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109772.html原文链接:https://javaforall.net

(0)
上一篇 2021年8月30日 下午5:00
下一篇 2021年8月30日 下午5:00


相关推荐

  • 微信小程序云开发入门详细教程

    微信小程序云开发入门详细教程本篇是 从零开始 Python 微信小程序开发 专栏第九篇 主要介绍最新最全的云开发入门教程 微信小程序云开发 云函数 云数据库学习 微信小程序云开发扩展功能学习 希望大家能够一起学习 互相交流 一 认识小程序云开发 1 云开发简介小程序云开发是微信团队联合腾讯云推出的专业的小程序开发服务 开发者可以使用云开发快速开发小程序 小游戏 公众号网页等 并且原生打通微信开放能力 开发者无需搭建服务器 可免鉴权直接使用平台提供的 API 进行业务开发 云开发提供三大基础能力 帮助开发者快速开发

    2026年3月19日
    2
  • python画等边三角形_四边形的画法

    python画等边三角形_四边形的画法python是编程语言,学习它只是因为要搞深度学习,其实语言类只要精通一种即可,但一定是精通,像我就是啥都知道,啥都不精,到最终一事无成。在学Python的时候,无意间看到网上有小游戏开发,于是乎就想自己调试下。第一个接触的例程是画国旗的。画国旗必然要画框,画框也就是画四边形,要画五角星,而五角星就是也是由三角形组成的,因此画一面很完美的五星红旗,则基础需要画四边形和三角形。OK,让我们一起来玩

    2026年1月29日
    5
  • 阿里千问新帅周浩:DeepMind大牛能否扛起开源大旗?

    阿里千问新帅周浩:DeepMind大牛能否扛起开源大旗?

    2026年3月12日
    2
  • 在MFC下面实际演示CCriticalSection 的使用

    在MFC下面实际演示CCriticalSection 的使用Q:CCriticalSection是什么?A:CCriticalSection是一种线程同步策略或者说技术或者方法总之呢就是这么个意思。。。。参考资料:http://blog.csdn.ne

    2022年7月4日
    30
  • supervisor学习记录

    supervisor学习记录1 supervisor 简介 Supervisor 是用 Python 开发的一个 client server 服务 是 Linux Unix 系统下的一个进程管理工具 不支持 Windows 系统 它可以很方便的监听 启动 停止 重启一个或多个进程 用 Supervisor 管理的进程 当一个进程意外被杀死 supervisort 监听到进程死后 会自动将它重新拉起 很方便的做到进程自动恢复的功能 不再需要自己写 s

    2026年3月16日
    2
  • Redis集群搭建(非常详细)

    Redis集群搭建(非常详细)https blog csdn net article details redis 集群搭建在开始 redis 集群搭建之前 我们先简单回顾一下 redis 单机版的搭建过程 下载 redis 压缩包 然后解压压缩文件 进入到解压缩后的 redis 文件目录 此时可以看到 Makefile 文件 编译 redis 源文件 把编译好的 redis 源文件安装到 usr local redis 目录下 如果 local 目录下没有 redis 目录 会自动新建 r

    2025年10月28日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号