• 2048小游戏(Java)(swing实现)(二)


    这里是上一次的成果,只能用鼠标点,没法用键盘

    最近扩充了一下知识面,实现了用键盘操控2048小游戏

    但是还是不支持同时使用键盘和鼠标同时操作

      1 import javax.swing.*;
      2 
      3 //import com.sun.glass.events.KeyEvent;
      4 
      5 import java.awt.*;
      6 import java.awt.event.*;
      7 
      8 public class JF2048 extends JFrame {
      9 
     10 /**
     11      * 
     12      */
     13     private static final long serialVersionUID = 1L;
     14 
     15 private Ja2048 ja;
     16 
     17 public JButton b[] = {
     18 new JButton(),
     19 new JButton(),
     20 new JButton(),
     21 new JButton()
     22 };
     23 
     24 public JButton back = new JButton("back");
     25 
     26 private ActionListener b0 = new ActionListener(){
     27 public void actionPerformed(ActionEvent e){
     28 ja.cp0();
     29 }};
     30 
     31 private ActionListener b1 = new ActionListener(){
     32 public void actionPerformed(ActionEvent e){
     33 ja.cp1();
     34 }};
     35 
     36 private ActionListener b2 = new ActionListener(){
     37 public void actionPerformed(ActionEvent e){
     38 ja.cp2();
     39 }};
     40 
     41 private ActionListener b3 = new ActionListener(){
     42 public void actionPerformed(ActionEvent e){
     43 ja.cp3();
     44 }};
     45 
     46 private ActionListener back1 = new ActionListener(){
     47 public void actionPerformed(ActionEvent e){
     48 ja.back();
     49 }};
     50 
     51 public JLabel[][] la ={
     52 {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
     53 {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
     54 {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
     55 {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
     56 };
     57 
     58 
     59 public JF2048(){
     60 
     61 super("2048");
     62 
     63 //this.addKeyListener(x);
     64 
     65 b[0].setBounds(3,20,16,156);
     66 b[1].setBounds(178,20,16,156);
     67 b[2].setBounds(20,3,156,16);
     68 b[3].setBounds(20,178,156,16);
     69 back.setBounds(3,3,16,16);
     70 
     71 b[0].addActionListener(b0);
     72 b[1].addActionListener(b1);
     73 b[2].addActionListener(b2);
     74 b[3].addActionListener(b3);
     75 back.addActionListener(back1); 
     76 
     77 
     78 for(int i =0;i<4;i++)
     79 for(int j =0;j<4;j++){
     80 la[i][j].setBounds(20+40*i,20+40*j,36,36);
     81 la[i][j].setOpaque(true);
     82 //la[i][j].setFont(new Font("幼圆",1,24));
     83 la[i][j].setHorizontalAlignment(SwingConstants.CENTER);
     84 }
     85 
     86 this.setSize(217,238);
     87 //this.add(b[0]);
     88 //this.add(b[1]);
     89 //this.add(b[2]);
     90 //this.add(b[3]);
     91 //this.add(back);
     92 for(int i =0;i<4;i++)
     93 for(int j =0;j<4;j++)
     94 this.add(la[i][j]);
     95 JLabel p = new JLabel();
     96 p.setBackground(new Color(127,127,127));
     97 p.setOpaque(true);
     98 this.add(p);
     99 this.addKeyListener(new KeyAdapter()//键盘监听按钮
    100         {
    101              public void keyPressed(KeyEvent e)
    102              {
    103                  switch(e.getKeyCode()){
    104                  case KeyEvent.VK_UP:ja.cp2();break;
    105                  case KeyEvent.VK_DOWN:ja.cp3();break;
    106                  case KeyEvent.VK_RIGHT:ja.cp1();break;
    107                  case KeyEvent.VK_LEFT:ja.cp0();break;
    108                  case KeyEvent.VK_BACK_SPACE:ja.back();
    109                  }
    110              }
    111              
    112          });
    113 
    114 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    115 this.setVisible(true);
    116 }
    117 
    118 public static void main(String[] args){
    119 JF2048 jf = new JF2048();
    120 jf.ja=new Ja2048(jf);
    121 }
    122 
    123 }
    界面部分代码
      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 
     10 public Ja2048(JF2048 a){
     11 this.linkF = a;
     12 setNull(state,getRandom());
     13 setNull(state,getRandom());
     14 setState();
     15 } 
     16 /**
     17  * 向左
     18  */
     19 public void cp0(){
     20 boolean bool= false;
     21 for(int i = 1;i<4;i++)
     22 for(int j = 0;j<4;j++)
     23 if(state[i][j]!=0&&(state[i-1][j]==0||state[i-1][j]==state[i][j]))
     24 bool=true;
     25 if(!bool)return;
     26 
     27 for(int i =0;i<4;i++)
     28 for(int j =0;j<4;j++)
     29 bac[i][j]=state[i][j];
     30 
     31 int[][] b = new int[4][4];
     32 for(int j=0;j<4;j++){
     33 int[] a ={state[0][j],state[1][j],state[2][j],state[3][j]};
     34 b[j]=LierIntArr.drop(a);
     35 }
     36 setNull(b,getRandom());
     37 int[][] x=new int[4][4];
     38 for(int i=0;i<4;i++)
     39 for(int j=0;j<4;j++)
     40 x[i][j]=b[j][i];
     41 state=x;
     42 setState();
     43 }//向左
     44 /**
     45  * 向右
     46  */
     47 public void cp1(){
     48 boolean bool=false;
     49 for(int i=0;i<3;i++)
     50 for(int j=0;j<4;j++)
     51 if(state[i][j]!=0&&(state[i+1][j]==0||state[i+1][j]==state[i][j]))
     52 bool=true;
     53 if(!bool)return;
     54 bac=state;
     55 int[][] b = new int[4][4];
     56 for(int j=0;j<4;j++){
     57 int[] a = {state[3][j],state[2][j],state[1][j],state[0][j]};
     58 b[j]=LierIntArr.drop(a);
     59 }
     60 setNull(b,getRandom());
     61 int[][] x=new int[4][4];
     62 for(int i=0;i<4;i++)
     63 for(int j=0;j<4;j++)
     64 x[i][j]=b[j][3-i];
     65 state=x;
     66 setState();
     67 }//向右
     68 /**
     69  * 向上
     70  */
     71 public void cp2(){
     72 boolean bool=false;
     73 for(int i=0;i<4;i++)
     74 for(int j=1;j<4;j++)
     75 if(state[i][j]!=0&&(state[i][j-1]==0||state[i][j-1]==state[i][j]))
     76 bool=true;
     77 if(!bool)return;
     78 bac=state.clone();
     79 int[][] b = new int[4][4];
     80 for(int i=0;i<4;i++)
     81 b[i]=LierIntArr.drop(state[i]);
     82 setNull(b,getRandom());
     83 state=b.clone();
     84 setState();
     85 }//向上
     86 /**
     87  * 向下
     88  */
     89 public void cp3(){
     90 boolean bool=false;
     91 for(int i=0;i<4;i++)
     92 for(int j=0;j<3;j++)
     93 if(state[i][j]!=0&&(state[i][j+1]==0||state[i][j+1]==state[i][j]))
     94 bool=true;
     95 if(!bool)return;
     96 bac=state.clone();
     97 int[][] b=new int[4][4];
     98 for(int i=0;i<4;i++){
     99 int[] a ={state[i][3],state[i][2],state[i][1],state[i][0]};
    100 b[i]=LierIntArr.drop(a);
    101 }
    102 setNull(b,getRandom());
    103 int[][] x=new int[4][4];
    104 for(int i=0;i<4;i++)
    105 for(int j=0;j<4;j++)
    106 x[i][j]=b[i][3-j];
    107 state=x;
    108 setState();
    109 }//向下
    110 /**
    111  * 回滚
    112  */
    113 public  void back(){
    114 state=bac.clone();
    115 setState();
    116 }
    117 private void setState(){
    118 for(int i=0;i<4;i++)
    119 for(int j=0;j<4;j++){
    120 if(state[i][j]==0){
    121 linkF.la[i][j].setText("");
    122 linkF.la[i][j].setBackground(new Color(227,227,227));
    123 linkF.la[i][j].setForeground(new Color(0,0,0));
    124 }
    125 else if(state[i][j]==2){
    126 linkF.la[i][j].setText("2");
    127 linkF.la[i][j].setFont(new Font("幼圆",1,20));
    128 linkF.la[i][j].setBackground(new Color(255,255,255));
    129 linkF.la[i][j].setForeground(new Color(0,0,0));
    130 }
    131 else if(state[i][j]==4){
    132 linkF.la[i][j].setText("4");
    133 linkF.la[i][j].setFont(new Font("幼圆",1,20));
    134 linkF.la[i][j].setBackground(new Color(127,227,127));
    135 linkF.la[i][j].setForeground(new Color(0,0,0));
    136 }
    137 else if(state[i][j]==8){
    138 linkF.la[i][j].setText("8");
    139 linkF.la[i][j].setFont(new Font("幼圆",1,20));
    140 linkF.la[i][j].setBackground(new Color(0,127,127));
    141 linkF.la[i][j].setForeground(new Color(255,255,255));
    142 }
    143 else if(state[i][j]==16){
    144 linkF.la[i][j].setText("16");
    145 linkF.la[i][j].setFont(new Font("幼圆",1,20));
    146 linkF.la[i][j].setBackground(new Color(0,255,0));
    147 linkF.la[i][j].setForeground(new Color(255,255,255));
    148 }
    149 else if(state[i][j]==32){
    150 linkF.la[i][j].setText("32");
    151 linkF.la[i][j].setFont(new Font("幼圆",1,20));
    152 linkF.la[i][j].setBackground(new Color(127,127,0));
    153 linkF.la[i][j].setForeground(new Color(255,255,255));
    154 }
    155 else if(state[i][j]==64){
    156 linkF.la[i][j].setText("64");
    157 linkF.la[i][j].setFont(new Font("幼圆",1,20));
    158 linkF.la[i][j].setBackground(new Color(255,0,0));
    159 linkF.la[i][j].setForeground(new Color(255,255,255));
    160 }
    161 else if(state[i][j]==128){
    162 linkF.la[i][j].setText("128");
    163 linkF.la[i][j].setFont(new Font("幼圆",1,20));
    164 linkF.la[i][j].setBackground(new Color(127,255,0));
    165 linkF.la[i][j].setForeground(new Color(255,255,255));
    166 }
    167 else if(state[i][j]==256){
    168 linkF.la[i][j].setText("256");
    169 linkF.la[i][j].setFont(new Font("幼圆",1,20));
    170 linkF.la[i][j].setBackground(new Color(255,255,0));
    171 linkF.la[i][j].setForeground(new Color(0,0,0));
    172 }
    173 else if(state[i][j]==512){
    174 linkF.la[i][j].setText("512");
    175 linkF.la[i][j].setFont(new Font("幼圆",1,20));
    176 linkF.la[i][j].setBackground(new Color(255,255,0));
    177 linkF.la[i][j].setForeground(new Color(0,0,0));
    178 }
    179 else if(state[i][j]==1024){
    180 linkF.la[i][j].setText("1024");
    181 linkF.la[i][j].setFont(new Font("幼圆",1,16));
    182 linkF.la[i][j].setBackground(new Color(63,63,63));
    183 linkF.la[i][j].setForeground(new Color(255,255,255));
    184 }
    185 }//for循环
    186 
    187 }//setState方法
    188 
    189 private static int getRandom(){
    190 int a = (int)(1000*Math.random());
    191 if(a%10<3)
    192 return 4;
    193 else
    194 return 2;
    195 }//随机生成一个2或4,可通过调整判断条件中的数字大小来调整2和4所占的比率
    196 /**
    197  * 用于在4x4二维数组中随机挑出一个值为0的元素,并将其赋值为给定整数。特殊地,若该二维数组已满,返回false。
    198  * @param x    该二维数组
    199  * @param y    给定整数
    200  * @return 
    201  */
    202 private static boolean setNull(int[][] x,int y){
    203 boolean bool=false;
    204 for(int i=0;i<4;i++)
    205 for(int j=0;j<4;j++)
    206 if(x[i][j]==0)bool=true;
    207 if(!bool)return false;
    208 
    209 int a = (int)(100*Math.random());
    210 int b = (int)(6+10*Math.random());
    211 int c = a%b;
    212 while(true){
    213 for(int i=0;i<4;i++)
    214 for(int j=0;j<4;j++){
    215 if(x[i][j]==0&&c<=0){
    216 x[i][j]=y;
    217 return true;
    218 }
    219 else if(x[i][j]==0&&c>0)
    220 c--;
    221 i=(i==4?0:i);
    222 j=(j==4?0:j);
    223 }
    224 }
    225 }//boolean setNull(int[][],int)方法用于在4x4二维数组中随机挑出一个值为0的元素,并将其赋值为给定整数。特殊地,若该二维数组已满,返回false。
    226 
    227 
    228 }
    算法部分代码
     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 }
    工具
  • 相关阅读:
    理解HTTP协议
    节点操作,节点属性的操作及DOM event事件
    jQuery的属性,事件及操作
    jQuery中的选择器及筛选器
    javascipt中的DOM对象
    javascript中的BOM对象
    javascript中的Date对象和Math对象
    javascript中的字符串对象
    javascript基础
    CSS核心内容之浮动
  • 原文地址:https://www.cnblogs.com/liujinming/p/7612281.html
Copyright © 2020-2023  润新知