自己写的2048小游戏,仅支持鼠标操作

主要是我不知道怎么添加键盘监听

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class JF2048 extends JFrame {
  6.  
  7. /**
  8. *
  9. */
  10. private static final long serialVersionUID = 1L;
  11.  
  12. private Ja2048 ja;
  13.  
  14. public JButton b[] = {
  15. new JButton(),
  16. new JButton(),
  17. new JButton(),
  18. new JButton()
  19. };
  20.  
  21. public JButton back = new JButton("back");
  22.  
  23. private ActionListener b0 = new ActionListener(){
  24. public void actionPerformed(ActionEvent e){
  25. ja.cp0();
  26. }};
  27.  
  28. private ActionListener b1 = new ActionListener(){
  29. public void actionPerformed(ActionEvent e){
  30. ja.cp1();
  31. }};
  32.  
  33. private ActionListener b2 = new ActionListener(){
  34. public void actionPerformed(ActionEvent e){
  35. ja.cp2();
  36. }};
  37.  
  38. private ActionListener b3 = new ActionListener(){
  39. public void actionPerformed(ActionEvent e){
  40. ja.cp3();
  41. }};
  42.  
  43. private ActionListener back1 = new ActionListener(){
  44. public void actionPerformed(ActionEvent e){
  45. ja.back();
  46. }};
  47.  
  48. public JLabel[][] la ={
  49. {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
  50. {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
  51. {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
  52. {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
  53. };
  54.  
  55. public JF2048(){
  56.  
  57. super("2048");
  58.  
  59. //this.addKeyListener(x);
  60.  
  61. b[0].setBounds(3,20,16,156);
  62. b[1].setBounds(178,20,16,156);
  63. b[2].setBounds(20,3,156,16);
  64. b[3].setBounds(20,178,156,16);
  65. back.setBounds(3,3,16,16);
  66.  
  67. b[0].addActionListener(b0);
  68. b[1].addActionListener(b1);
  69. b[2].addActionListener(b2);
  70. b[3].addActionListener(b3);
  71. back.addActionListener(back1);
  72.  
  73. for(int i =0;i<4;i++)
  74. for(int j =0;j<4;j++){
  75. la[i][j].setBounds(20+40*i,20+40*j,36,36);
  76. la[i][j].setOpaque(true);
  77. //la[i][j].setFont(new Font("幼圆",1,24));
  78. la[i][j].setHorizontalAlignment(SwingConstants.CENTER);
  79. }
  80.  
  81. this.setSize(217,238);
  82. this.add(b[0]);
  83. this.add(b[1]);
  84. this.add(b[2]);
  85. this.add(b[3]);
  86. this.add(back);
  87. for(int i =0;i<4;i++)
  88. for(int j =0;j<4;j++)
  89. this.add(la[i][j]);
  90. JLabel p = new JLabel();
  91. p.setBackground(new Color(127,127,127));
  92. p.setOpaque(true);
  93. this.add(p);
  94.  
  95. }
  96.  
  97. public static void main(String[] args){
  98. JF2048 jf = new JF2048();
  99. jf.ja=new Ja2048(jf);
  100. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  101. jf.setVisible(true);
  102. }
  103.  
  104. }

界面层代码

  1. import java.awt.*;
  2. public class Ja2048{
  3.  
  4. public static int[][] state=new int[4][4];
  5. public static int[][] bac=new int[4][4];
  6.  
  7. private JF2048 linkF;
  8.  
  9. public Ja2048(JF2048 a){
  10. this.linkF = a;
  11. setNull(state,getRandom());
  12. setNull(state,getRandom());
  13. setState();
  14. }
  15.  
  16. public void cp0(){
  17. boolean bool= false;
  18. for(int i = 1;i<4;i++)
  19. for(int j = 0;j<4;j++)
  20. if(state[i][j]!=0&&(state[i-1][j]==0||state[i-1][j]==state[i][j]))
  21. bool=true;
  22. if(!bool)return;
  23.  
  24. for(int i =0;i<4;i++)
  25. for(int j =0;j<4;j++)
  26. bac[i][j]=state[i][j];
  27.  
  28. int[][] b = new int[4][4];
  29. for(int j=0;j<4;j++){
  30. int[] a ={state[0][j],state[1][j],state[2][j],state[3][j]};
  31. b[j]=LierIntArr.drop(a);
  32. }
  33. setNull(b,getRandom());
  34. int[][] x=new int[4][4];
  35. for(int i=0;i<4;i++)
  36. for(int j=0;j<4;j++)
  37. x[i][j]=b[j][i];
  38. state=x;
  39. setState();
  40. }//向左
  41. public void cp1(){
  42. boolean bool=false;
  43. for(int i=0;i<3;i++)
  44. for(int j=0;j<4;j++)
  45. if(state[i][j]!=0&&(state[i+1][j]==0||state[i+1][j]==state[i][j]))
  46. bool=true;
  47. if(!bool)return;
  48. bac=state;
  49. int[][] b = new int[4][4];
  50. for(int j=0;j<4;j++){
  51. int[] a = {state[3][j],state[2][j],state[1][j],state[0][j]};
  52. b[j]=LierIntArr.drop(a);
  53. }
  54. setNull(b,getRandom());
  55. int[][] x=new int[4][4];
  56. for(int i=0;i<4;i++)
  57. for(int j=0;j<4;j++)
  58. x[i][j]=b[j][3-i];
  59. state=x;
  60. setState();
  61. }//向右
  62. public void cp2(){
  63. boolean bool=false;
  64. for(int i=0;i<4;i++)
  65. for(int j=1;j<4;j++)
  66. if(state[i][j]!=0&&(state[i][j-1]==0||state[i][j-1]==state[i][j]))
  67. bool=true;
  68. if(!bool)return;
  69. bac=state.clone();
  70. int[][] b = new int[4][4];
  71. for(int i=0;i<4;i++)
  72. b[i]=LierIntArr.drop(state[i]);
  73. setNull(b,getRandom());
  74. state=b.clone();
  75. setState();
  76. }//向上
  77. public void cp3(){
  78. boolean bool=false;
  79. for(int i=0;i<4;i++)
  80. for(int j=0;j<3;j++)
  81. if(state[i][j]!=0&&(state[i][j+1]==0||state[i][j+1]==state[i][j]))
  82. bool=true;
  83. if(!bool)return;
  84. bac=state.clone();
  85. int[][] b=new int[4][4];
  86. for(int i=0;i<4;i++){
  87. int[] a ={state[i][3],state[i][2],state[i][1],state[i][0]};
  88. b[i]=LierIntArr.drop(a);
  89. }
  90. setNull(b,getRandom());
  91. int[][] x=new int[4][4];
  92. for(int i=0;i<4;i++)
  93. for(int j=0;j<4;j++)
  94. x[i][j]=b[i][3-j];
  95. state=x;
  96. setState();
  97. }//向下
  98. public void back(){
  99. state=bac.clone();
  100. setState();
  101. }
  102. private void setState(){
  103. for(int i=0;i<4;i++)
  104. for(int j=0;j<4;j++){
  105. if(state[i][j]==0){
  106. linkF.la[i][j].setText("");
  107. linkF.la[i][j].setBackground(new Color(227,227,227));
  108. linkF.la[i][j].setForeground(new Color(0,0,0));
  109. }
  110. else if(state[i][j]==2){
  111. linkF.la[i][j].setText("2");
  112. linkF.la[i][j].setFont(new Font("幼圆",1,20));
  113. linkF.la[i][j].setBackground(new Color(255,255,255));
  114. linkF.la[i][j].setForeground(new Color(0,0,0));
  115. }
  116. else if(state[i][j]==4){
  117. linkF.la[i][j].setText("4");
  118. linkF.la[i][j].setFont(new Font("幼圆",1,20));
  119. linkF.la[i][j].setBackground(new Color(127,227,127));
  120. linkF.la[i][j].setForeground(new Color(0,0,0));
  121. }
  122. else if(state[i][j]==8){
  123. linkF.la[i][j].setText("8");
  124. linkF.la[i][j].setFont(new Font("幼圆",1,20));
  125. linkF.la[i][j].setBackground(new Color(0,127,127));
  126. linkF.la[i][j].setForeground(new Color(255,255,255));
  127. }
  128. else if(state[i][j]==16){
  129. linkF.la[i][j].setText("16");
  130. linkF.la[i][j].setFont(new Font("幼圆",1,20));
  131. linkF.la[i][j].setBackground(new Color(0,255,0));
  132. linkF.la[i][j].setForeground(new Color(255,255,255));
  133. }
  134. else if(state[i][j]==32){
  135. linkF.la[i][j].setText("32");
  136. linkF.la[i][j].setFont(new Font("幼圆",1,20));
  137. linkF.la[i][j].setBackground(new Color(127,127,0));
  138. linkF.la[i][j].setForeground(new Color(255,255,255));
  139. }
  140. else if(state[i][j]==64){
  141. linkF.la[i][j].setText("64");
  142. linkF.la[i][j].setFont(new Font("幼圆",1,20));
  143. linkF.la[i][j].setBackground(new Color(255,0,0));
  144. linkF.la[i][j].setForeground(new Color(255,255,255));
  145. }
  146. else if(state[i][j]==128){
  147. linkF.la[i][j].setText("128");
  148. linkF.la[i][j].setFont(new Font("幼圆",1,20));
  149. linkF.la[i][j].setBackground(new Color(127,255,0));
  150. linkF.la[i][j].setForeground(new Color(255,255,255));
  151. }
  152. else if(state[i][j]==256){
  153. linkF.la[i][j].setText("256");
  154. linkF.la[i][j].setFont(new Font("幼圆",1,20));
  155. linkF.la[i][j].setBackground(new Color(255,255,0));
  156. linkF.la[i][j].setForeground(new Color(0,0,0));
  157. }
  158. else if(state[i][j]==512){
  159. linkF.la[i][j].setText("512");
  160. linkF.la[i][j].setFont(new Font("幼圆",1,20));
  161. linkF.la[i][j].setBackground(new Color(255,255,0));
  162. linkF.la[i][j].setForeground(new Color(0,0,0));
  163. }
  164. else if(state[i][j]==1024){
  165. linkF.la[i][j].setText("1024");
  166. linkF.la[i][j].setFont(new Font("幼圆",1,16));
  167. linkF.la[i][j].setBackground(new Color(63,63,63));
  168. linkF.la[i][j].setForeground(new Color(255,255,255));
  169. }
  170. }//for循环
  171.  
  172. }//setState方法
  173.  
  174. private static int getRandom(){
  175. int a = (int)(1000*Math.random());
  176. if(a%10<3)
  177. return 4;
  178. else
  179. return 2;
  180. }//随机生成一个2或4,可通过调整判断条件中的数字大小来调整2和4所占的比率
  181. /**
  182. * 用于在4x4二维数组中随机挑出一个值为0的元素,并将其赋值为给定整数。特殊地,若该二维数组已满,返回false。
  183. * @param x 该二维数组
  184. * @param y 给定整数
  185. * @return
  186. */
  187. private static boolean setNull(int[][] x,int y){
  188. boolean bool=false;
  189. for(int i=0;i<4;i++)
  190. for(int j=0;j<4;j++)
  191. if(x[i][j]==0)bool=true;
  192. if(!bool)return false;
  193.  
  194. int a = (int)(100*Math.random());
  195. int b = (int)(6+10*Math.random());
  196. int c = a%b;
  197. while(true){
  198. for(int i=0;i<4;i++)
  199. for(int j=0;j<4;j++){
  200. if(x[i][j]==0&&c<=0){
  201. x[i][j]=y;
  202. return true;
  203. }
  204. else if(x[i][j]==0&&c>0)
  205. c--;
  206. i=(i==4?0:i);
  207. j=(j==4?0:j);
  208. }
  209. }
  210. }//boolean setNull(int[][],int)方法用于在4x4二维数组中随机挑出一个值为0的元素,并将其赋值为给定整数。特殊地,若该二维数组已满,返回false。
  211.  
  212. }

算法层代码

  1. /**
  2. *
  3. * @author qliujinming@qq.com
  4. *
  5. * @see http://www.cnblogs.com/liujinming/
  6. *
  7. */
  8. public class LierIntArr{
  9. /**
  10. * 该方法用于接受一个整数数组,对该数组进行drop操作后返回
  11. * 示例:接受 2 0 2 0 5 5,返回4 10 0 0 0 0
  12. * @param 需要进行drop操作的数组
  13. * @return drop操作之后的数组
  14. */
  15. public static int[] drop(int[] a){
  16. int b = a.length;
  17. if(b<=1)return a;
  18. int[] c = new int[b];
  19. int j=0;
  20. for(int i=0;i<b;i++){
  21. if(c[j]==0&&a[i]!=0)
  22. c[j]=a[i];
  23. else if(c[j]!=0&&a[i]==c[j]){
  24. c[j]=2*a[i];
  25. j++;
  26. }
  27. else if(a[i]!=0&&c[j]!=0&&a[i]!=c[j]){
  28. j++;
  29. c[j]=a[i];
  30. }
  31. }
  32. return c;
  33. }
  34. //该方法用于接受一个整数数组,对该数组进行drop操作后返回
  35. //示例:接受 2 0 2 0 5 5,返回4 10 0 0 0 0
  36.  
  37. public static void main(String[] args){
  38. int[] a = {0,2,0,2,4,0,0,4,2,0,2,5,5,0,10};
  39. int[] b = drop(a);
  40. for(int i = 0;i<b.length;i++)
  41. System.out.print(b[i]+",");
  42. }
  43. //输出结果:4,8,4,10,10,0,0,0,0,0,0,0,0,0,0,
  44.  
  45. }

辅助工具

这里的实现了键盘监听

2048小游戏(Java)(swing实现)(一)的更多相关文章

  1. 2048小游戏(Java)(swing实现)(二)

    这里是上一次的成果,只能用鼠标点,没法用键盘 最近扩充了一下知识面,实现了用键盘操控2048小游戏 但是还是不支持同时使用键盘和鼠标同时操作 import javax.swing.*; //impor ...

  2. jQuery实践-网页版2048小游戏

    ▓▓▓▓▓▓ 大致介绍 看了一个实现网页版2048小游戏的视频,觉得能做出自己以前喜欢玩的小游戏很有意思便自己动手试了试,真正的验证了这句话-不要以为你以为的就是你以为的,看视频时觉得看懂了,会写了, ...

  3. C# 开发2048小游戏

    这应该是几个月前,闲的手痒,敲了一上午代码搞出来的,随之就把它丢弃了,当时让别人玩过,提过几条更改建议,但是时至今日,我也没有进行过优化和更改(本人只会作案,不会收场,嘎嘎),下面的建议要给代码爱好的 ...

  4. Swift实战之2048小游戏

    上周在图书馆借了一本Swift语言实战入门,入个门玩一玩^_^正好这本书的后面有一个2048小游戏的实例,笔者跟着实战了一把. 差不多一周的时间,到今天,游戏的基本功能已基本实现,细节我已不打算继续完 ...

  5. 如何在CentOS上安装一个2048小游戏

    如何在centos上安装一个2048小游戏 最近在学习CentOS系统,就琢磨着玩点什么,然后我看到有人在玩2048小游戏,所有我就在想,为啥不装一个2048小游戏搞一下嘞,于是乎,我就开始工作啦 由 ...

  6. js、jQuery实现2048小游戏

    2048小游戏 一.游戏简介:  2048是一款休闲益智类的数字叠加小游戏 二. 游戏玩法: 在4*4的16宫格中,您可以选择上.下.左.右四个方向进行操作,数字会按方向移动,相邻的两个数字相同就会合 ...

  7. 用js实现2048小游戏

    用js实现2048小游戏 笔记仓库:https://github.com/nnngu/LearningNotes 1.游戏简介 2048是一款休闲益智类的数字叠加小游戏.(文末给出源代码和演示地址) ...

  8. 2048小游戏代码解析 C语言版

    2048小游戏,也算是风靡一时的益智游戏.其背后实现的逻辑比较简单,代码量不算多,而且趣味性强,适合作为有语言基础的童鞋来加强编程训练.本篇分析2048小游戏的C语言实现代码. 前言 游戏截图:  游 ...

  9. Docker从0开始之部署一套2048小游戏

    本文记录一下在docker部署一套2048小游戏的过程,在娱乐中熟悉docker的应用部署.docker 安装不在本文讲述之中,参考我的其它博客. 1.获取image镜像. 方法一:daocloud. ...

随机推荐

  1. array_unique() 函数移除数组中的重复的值

    array_unique() 函数移除数组中的重复的值,并返回结果数组. 当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除. 返回的数组中键名不变.

  2. JAVA语法规则总结

    单继承多实现 抽象类  抽象方法 使用关键字:abstract修饰的方法就是抽象方法; 抽象方法的形式:只有方法的声明,没有方法体; 抽象方法一般存在于父类中,相当于强制要求子类必须重写该方法,相当于 ...

  3. Linux 使用静态库注意事项

    1. 静态库一定要放在生成文件后面 gcc main.c -o main libhello.a 2. 使用静态库时一定要连接所有用到的静态库 gcc main.c -o main liba.a lib ...

  4. 22、linux的ssh互信配置

    转载:https://blog.csdn.net/hrn1216/article/details/51568830 https://blog.csdn.net/u013144287/article/d ...

  5. 数据结构_yjjsj(伊姐姐数字游戏)

    问题描述 伊姐姐热衷于各类数字游戏, 24 点. 2048.数独等轻轻松松毫无压力.一日,可爱的小姐姐邀请伊姐姐一起玩一种简单的数字 game,游戏规则如下:一开始桌上放着 n 张数字卡片,从左到右按 ...

  6. Mysql--基本配置

    登录的常用参数 mysql -uroot -p    之后再加上密码 mysql -uroot -p+密码   这个方法不安全 mysql -hlocalhost -uroot -p  之后再加上密码 ...

  7. 【IMOOC学习笔记】多种多样的App主界面Tab实现方法(一)

    1.ViewPager实现Tab 首先实现底部和底部布局 <?xml version="1.0" encoding="utf-8"?> <Li ...

  8. Jmeter-BeanShell的使用介绍

    最近学习使用了jmeter来对接口进行测试.使用jmter进行接口测试,有时候需要编写一些BeanShell脚本语言,或者利用BeanShell调用自己的工具类,来完成jmeter基本功能中无法实现的 ...

  9. winform播放视频(windows media player)

    1.找到windows media player 工具箱常规下边右键,右键弹窗点击“选择项”,选择工具箱窗口点击“COM组件”,找到 Windows Media Player 勾选,点击确定 2.使用 ...

  10. Python学习第三方库Requests: 让 HTTP 服务人类

    转自官方文档:http://cn.python-requests.org/zh_CN/latest/ 快速上手 http://cn.python-requests.org/zh_CN/latest/u ...