esc0rp 发表于 2024-7-11 06:51:14

java实现2048小游戏(附源码)


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">针对</span>java初学者<span style="color: black;">来讲</span>,<span style="color: black;">经过</span><span style="color: black;">有些</span>学习小游戏来对swing学习以及对java<span style="color: black;">基本</span>的学习是一个好的<span style="color: black;">办法</span>,<span style="color: black;">同期</span><span style="color: black;">亦</span>给学习带来了<span style="color: black;">非常多</span>的乐趣,接下来就给<span style="color: black;">大众</span>分享一个java实现的2048小游戏。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">先来看下效果图:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic1.zhimg.com/80/v2-f42d70619cfb457a3152c5c1ff8cb8b4_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">好了,废话不多说,直接上代码。<span style="color: black;">大众</span><span style="color: black;">能够</span>直接复制下面的代码实现体验,<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">微X</span>关注公众号:“程序员的事儿”回复“2048”获取完整代码。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">package</span> <span style="color: black;">cn.hangzhou.demo.testOther</span><span style="color: black;">;</span>
      <span style="color: black;">import</span> <span style="color: black;">java.util.Date</span><span style="color: black;">;</span>
      <span style="color: black;">import</span> <span style="color: black;">java.util.Random</span><span style="color: black;">;</span> <span style="color: black;">// 产生随机数
      </span><span style="color: black;">import</span> <span style="color: black;">java.awt.event.*</span><span style="color: black;">;</span> <span style="color: black;">// <span style="color: black;">供给</span>各类事件的接口和类
      </span><span style="color: black;">import</span> <span style="color: black;">java.awt.Color</span><span style="color: black;">;</span> <span style="color: black;">// <span style="color: black;">供给</span>用于颜色的类
      </span><span style="color: black;">import</span> <span style="color: black;">java.awt.Font</span><span style="color: black;">;</span> <span style="color: black;">// <span style="color: black;">供给</span>与字体<span style="color: black;">关联</span>的类和接口
      </span><span style="color: black;">import</span> <span style="color: black;">java.awt.EventQueue</span><span style="color: black;">;</span> <span style="color: black;">//将来自于<span style="color: black;">基本</span>同位体类和受信任的应用程序类的事件<span style="color: black;">加入</span>队列
      </span><span style="color: black;">import</span> <span style="color: black;">javax.swing.JFrame</span><span style="color: black;">;</span> <span style="color: black;">// 框架
      </span><span style="color: black;">import</span> <span style="color: black;">javax.swing.JPanel</span><span style="color: black;">;</span> <span style="color: black;">// 面板容器 <span style="color: black;">能够</span>加入到 JFrame 中
      </span><span style="color: black;">import</span> <span style="color: black;">javax.swing.JLabel</span><span style="color: black;">;</span> <span style="color: black;">// <span style="color: black;">表示</span>文本、图像或<span style="color: black;">同期</span><span style="color: black;">表示</span>二者
      </span><span style="color: black;">import</span> <span style="color: black;">javax.swing.BorderFactory</span><span style="color: black;">;</span> <span style="color: black;">// 设计边框
      </span><span style="color: black;">import</span> <span style="color: black;">javax.swing.SwingConstants</span><span style="color: black;">;</span>
      <span style="color: black;">import</span> <span style="color: black;">javax.swing.JTextField</span><span style="color: black;">;</span> <span style="color: black;">// 单行文本输入
      </span><span style="color: black;">public</span> <span style="color: black;">class</span> <span style="color: black;">my_2048</span> <span style="color: black;">extends</span> <span style="color: black;">JFrame</span><span style="color: black;">{</span>

      <span style="color: black;">private</span> <span style="color: black;">static</span> <span style="color: black;">final</span> <span style="color: black;">long</span> <span style="color: black;">serialVersionUID</span> <span style="color: black;">=</span> <span style="color: black;">1L</span><span style="color: black;">;</span>
      <span style="color: black;">private</span> <span style="color: black;">JPanel</span> <span style="color: black;">ScoresPanel</span><span style="color: black;">;</span>
      <span style="color: black;">private</span> <span style="color: black;">JPanel</span> <span style="color: black;">MainPanel</span><span style="color: black;">;</span>
      <span style="color: black;">private</span> <span style="color: black;">JPanel</span> <span style="color: black;">TimeSpentPanel</span><span style="color: black;">;</span>
      <span style="color: black;">private</span> <span style="color: black;">JPanel</span> <span style="color: black;">TipsPanel</span><span style="color: black;">;</span>

      <span style="color: black;">private</span> <span style="color: black;">JLabel</span> <span style="color: black;">MaxScoreLabel</span><span style="color: black;">;</span>
      <span style="color: black;">private</span> <span style="color: black;">JLabel</span> <span style="color: black;">CurrentScoreLabel</span><span style="color: black;">;</span> <span style="color: black;">// 当前得分
      </span> <span style="color: black;">private</span> <span style="color: black;">JLabel</span> <span style="color: black;">TipsLabel</span><span style="color: black;">;</span> <span style="color: black;">// 提示
      </span> <span style="color: black;">private</span> <span style="color: black;">JLabel</span> <span style="color: black;">ScoreValueLabel</span><span style="color: black;">;</span> <span style="color: black;">// 当前的分数值
      </span> <span style="color: black;">private</span> <span style="color: black;">JLabel</span><span style="color: black;">[][]</span> <span style="color: black;">Texts</span><span style="color: black;">;</span> <span style="color: black;">// 文本
      </span> <span style="color: black;">private</span> <span style="color: black;">JLabel</span> <span style="color: black;">TimeSpentLabel</span><span style="color: black;">;</span> <span style="color: black;">// <span style="color: black;">表示</span>此次程序运行 经历的时间
      </span> <span style="color: black;">private</span> <span style="color: black;">JLabel</span> <span style="color: black;">TimeSpentValueLabel</span><span style="color: black;">;</span>

      <span style="color: black;">private</span> <span style="color: black;">JTextField</span> <span style="color: black;">MaxScoreField</span><span style="color: black;">;</span> <span style="color: black;">// 记录最大分数文本
      </span><span style="color: black;">// private JTextField TimeSpentField; // 记录此次游戏经历的时间的文本框
      </span>
      <span style="color: black;">private</span> <span style="color: black;">int</span> <span style="color: black;">SurDiamonts</span> <span style="color: black;">=</span> <span style="color: black;">16</span><span style="color: black;">;</span> <span style="color: black;">// <span style="color: black;">暗示</span>剩余方块数目
      </span> <span style="color: black;">private</span> <span style="color: black;">int</span> <span style="color: black;">RecordScores</span> <span style="color: black;">=</span> <span style="color: black;">0</span><span style="color: black;">;</span> <span style="color: black;">// 记录当前的分数
      </span> <span style="color: black;">private</span> <span style="color: black;">String</span> <span style="color: black;">CurrentTime</span> <span style="color: black;">=</span> <span style="color: black;">""</span> <span style="color: black;">;</span> <span style="color: black;">// 当前时间
      </span> <span style="color: black;">private</span> <span style="color: black;">int</span> <span style="color: black;">Mark1</span><span style="color: black;">,</span> <span style="color: black;">Mark2</span><span style="color: black;">,</span> <span style="color: black;">Mark3</span><span style="color: black;">,</span> <span style="color: black;">Mark4</span><span style="color: black;">;</span> <span style="color: black;">// 判断游戏<span style="color: black;">是不是</span>结束
      </span> <span style="color: black;">Font</span> <span style="color: black;">Font1</span> <span style="color: black;">=</span> <span style="color: black;">new</span> <span style="color: black;">Font</span><span style="color: black;">(</span><span style="color: black;">""</span><span style="color: black;">,</span><span style="color: black;">Font</span><span style="color: black;">.</span><span style="color: black;">BOLD</span><span style="color: black;">,</span><span style="color: black;">15</span><span style="color: black;">);</span>
      <span style="color: black;">Font</span> <span style="color: black;">Font2</span> <span style="color: black;">=</span> <span style="color: black;">new</span> <span style="color: black;">Fon</span>
    </div>




jxwysz 发表于 2024-8-25 07:14:52

感谢楼主分享,祝愿外链论坛越办越好!

飘雨的姑娘 发表于 2024-9-8 14:38:49

回顾过去一年,是艰难的一年;展望未来,是辉煌的一年。

bitheerani12500 发表于 2024-9-10 12:16:11

感谢你的精彩评论,为我的思绪打开了新的窗口。

星☆雨 发表于 2024-9-24 12:26:53

期待与你深入交流,共探知识的无穷魅力。

4zhvml8 发表于 2024-10-7 00:58:16

楼主继续加油啊!外链论坛加油!

4lqedz 发表于 2024-10-19 09:26:24

哈哈、笑死我了、太搞笑了吧等。

j8typz 发表于 2024-11-5 20:30:03

顶楼主,说得太好了!
页: [1]
查看完整版本: java实现2048小游戏(附源码)