2048小游戏(Java)(swing实现)(一)
自己写的2048小游戏,仅支持鼠标操作
主要是我不知道怎么添加键盘监听
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class JF2048 extends JFrame {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private Ja2048 ja;
- public JButton b[] = {
- new JButton(),
- new JButton(),
- new JButton(),
- new JButton()
- };
- public JButton back = new JButton("back");
- private ActionListener b0 = new ActionListener(){
- public void actionPerformed(ActionEvent e){
- ja.cp0();
- }};
- private ActionListener b1 = new ActionListener(){
- public void actionPerformed(ActionEvent e){
- ja.cp1();
- }};
- private ActionListener b2 = new ActionListener(){
- public void actionPerformed(ActionEvent e){
- ja.cp2();
- }};
- private ActionListener b3 = new ActionListener(){
- public void actionPerformed(ActionEvent e){
- ja.cp3();
- }};
- private ActionListener back1 = new ActionListener(){
- public void actionPerformed(ActionEvent e){
- ja.back();
- }};
- public JLabel[][] la ={
- {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
- {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
- {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
- {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
- };
- public JF2048(){
- super("2048");
- //this.addKeyListener(x);
- b[0].setBounds(3,20,16,156);
- b[1].setBounds(178,20,16,156);
- b[2].setBounds(20,3,156,16);
- b[3].setBounds(20,178,156,16);
- back.setBounds(3,3,16,16);
- b[0].addActionListener(b0);
- b[1].addActionListener(b1);
- b[2].addActionListener(b2);
- b[3].addActionListener(b3);
- back.addActionListener(back1);
- for(int i =0;i<4;i++)
- for(int j =0;j<4;j++){
- la[i][j].setBounds(20+40*i,20+40*j,36,36);
- la[i][j].setOpaque(true);
- //la[i][j].setFont(new Font("幼圆",1,24));
- la[i][j].setHorizontalAlignment(SwingConstants.CENTER);
- }
- this.setSize(217,238);
- this.add(b[0]);
- this.add(b[1]);
- this.add(b[2]);
- this.add(b[3]);
- this.add(back);
- for(int i =0;i<4;i++)
- for(int j =0;j<4;j++)
- this.add(la[i][j]);
- JLabel p = new JLabel();
- p.setBackground(new Color(127,127,127));
- p.setOpaque(true);
- this.add(p);
- }
- public static void main(String[] args){
- JF2048 jf = new JF2048();
- jf.ja=new Ja2048(jf);
- jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- jf.setVisible(true);
- }
- }
界面层代码
- import java.awt.*;
- public class Ja2048{
- public static int[][] state=new int[4][4];
- public static int[][] bac=new int[4][4];
- private JF2048 linkF;
- public Ja2048(JF2048 a){
- this.linkF = a;
- setNull(state,getRandom());
- setNull(state,getRandom());
- setState();
- }
- public void cp0(){
- boolean bool= false;
- for(int i = 1;i<4;i++)
- for(int j = 0;j<4;j++)
- if(state[i][j]!=0&&(state[i-1][j]==0||state[i-1][j]==state[i][j]))
- bool=true;
- if(!bool)return;
- for(int i =0;i<4;i++)
- for(int j =0;j<4;j++)
- bac[i][j]=state[i][j];
- int[][] b = new int[4][4];
- for(int j=0;j<4;j++){
- int[] a ={state[0][j],state[1][j],state[2][j],state[3][j]};
- b[j]=LierIntArr.drop(a);
- }
- setNull(b,getRandom());
- int[][] x=new int[4][4];
- for(int i=0;i<4;i++)
- for(int j=0;j<4;j++)
- x[i][j]=b[j][i];
- state=x;
- setState();
- }//向左
- public void cp1(){
- boolean bool=false;
- for(int i=0;i<3;i++)
- for(int j=0;j<4;j++)
- if(state[i][j]!=0&&(state[i+1][j]==0||state[i+1][j]==state[i][j]))
- bool=true;
- if(!bool)return;
- bac=state;
- int[][] b = new int[4][4];
- for(int j=0;j<4;j++){
- int[] a = {state[3][j],state[2][j],state[1][j],state[0][j]};
- b[j]=LierIntArr.drop(a);
- }
- setNull(b,getRandom());
- int[][] x=new int[4][4];
- for(int i=0;i<4;i++)
- for(int j=0;j<4;j++)
- x[i][j]=b[j][3-i];
- state=x;
- setState();
- }//向右
- public void cp2(){
- boolean bool=false;
- for(int i=0;i<4;i++)
- for(int j=1;j<4;j++)
- if(state[i][j]!=0&&(state[i][j-1]==0||state[i][j-1]==state[i][j]))
- bool=true;
- if(!bool)return;
- bac=state.clone();
- int[][] b = new int[4][4];
- for(int i=0;i<4;i++)
- b[i]=LierIntArr.drop(state[i]);
- setNull(b,getRandom());
- state=b.clone();
- setState();
- }//向上
- public void cp3(){
- boolean bool=false;
- for(int i=0;i<4;i++)
- for(int j=0;j<3;j++)
- if(state[i][j]!=0&&(state[i][j+1]==0||state[i][j+1]==state[i][j]))
- bool=true;
- if(!bool)return;
- bac=state.clone();
- int[][] b=new int[4][4];
- for(int i=0;i<4;i++){
- int[] a ={state[i][3],state[i][2],state[i][1],state[i][0]};
- b[i]=LierIntArr.drop(a);
- }
- setNull(b,getRandom());
- int[][] x=new int[4][4];
- for(int i=0;i<4;i++)
- for(int j=0;j<4;j++)
- x[i][j]=b[i][3-j];
- state=x;
- setState();
- }//向下
- public void back(){
- state=bac.clone();
- setState();
- }
- private void setState(){
- for(int i=0;i<4;i++)
- for(int j=0;j<4;j++){
- if(state[i][j]==0){
- linkF.la[i][j].setText("");
- linkF.la[i][j].setBackground(new Color(227,227,227));
- linkF.la[i][j].setForeground(new Color(0,0,0));
- }
- else if(state[i][j]==2){
- linkF.la[i][j].setText("2");
- linkF.la[i][j].setFont(new Font("幼圆",1,20));
- linkF.la[i][j].setBackground(new Color(255,255,255));
- linkF.la[i][j].setForeground(new Color(0,0,0));
- }
- else if(state[i][j]==4){
- linkF.la[i][j].setText("4");
- linkF.la[i][j].setFont(new Font("幼圆",1,20));
- linkF.la[i][j].setBackground(new Color(127,227,127));
- linkF.la[i][j].setForeground(new Color(0,0,0));
- }
- else if(state[i][j]==8){
- linkF.la[i][j].setText("8");
- linkF.la[i][j].setFont(new Font("幼圆",1,20));
- linkF.la[i][j].setBackground(new Color(0,127,127));
- linkF.la[i][j].setForeground(new Color(255,255,255));
- }
- else if(state[i][j]==16){
- linkF.la[i][j].setText("16");
- linkF.la[i][j].setFont(new Font("幼圆",1,20));
- linkF.la[i][j].setBackground(new Color(0,255,0));
- linkF.la[i][j].setForeground(new Color(255,255,255));
- }
- else if(state[i][j]==32){
- linkF.la[i][j].setText("32");
- linkF.la[i][j].setFont(new Font("幼圆",1,20));
- linkF.la[i][j].setBackground(new Color(127,127,0));
- linkF.la[i][j].setForeground(new Color(255,255,255));
- }
- else if(state[i][j]==64){
- linkF.la[i][j].setText("64");
- linkF.la[i][j].setFont(new Font("幼圆",1,20));
- linkF.la[i][j].setBackground(new Color(255,0,0));
- linkF.la[i][j].setForeground(new Color(255,255,255));
- }
- else if(state[i][j]==128){
- linkF.la[i][j].setText("128");
- linkF.la[i][j].setFont(new Font("幼圆",1,20));
- linkF.la[i][j].setBackground(new Color(127,255,0));
- linkF.la[i][j].setForeground(new Color(255,255,255));
- }
- else if(state[i][j]==256){
- linkF.la[i][j].setText("256");
- linkF.la[i][j].setFont(new Font("幼圆",1,20));
- linkF.la[i][j].setBackground(new Color(255,255,0));
- linkF.la[i][j].setForeground(new Color(0,0,0));
- }
- else if(state[i][j]==512){
- linkF.la[i][j].setText("512");
- linkF.la[i][j].setFont(new Font("幼圆",1,20));
- linkF.la[i][j].setBackground(new Color(255,255,0));
- linkF.la[i][j].setForeground(new Color(0,0,0));
- }
- else if(state[i][j]==1024){
- linkF.la[i][j].setText("1024");
- linkF.la[i][j].setFont(new Font("幼圆",1,16));
- linkF.la[i][j].setBackground(new Color(63,63,63));
- linkF.la[i][j].setForeground(new Color(255,255,255));
- }
- }//for循环
- }//setState方法
- private static int getRandom(){
- int a = (int)(1000*Math.random());
- if(a%10<3)
- return 4;
- else
- return 2;
- }//随机生成一个2或4,可通过调整判断条件中的数字大小来调整2和4所占的比率
- /**
- * 用于在4x4二维数组中随机挑出一个值为0的元素,并将其赋值为给定整数。特殊地,若该二维数组已满,返回false。
- * @param x 该二维数组
- * @param y 给定整数
- * @return
- */
- private static boolean setNull(int[][] x,int y){
- boolean bool=false;
- for(int i=0;i<4;i++)
- for(int j=0;j<4;j++)
- if(x[i][j]==0)bool=true;
- if(!bool)return false;
- int a = (int)(100*Math.random());
- int b = (int)(6+10*Math.random());
- int c = a%b;
- while(true){
- for(int i=0;i<4;i++)
- for(int j=0;j<4;j++){
- if(x[i][j]==0&&c<=0){
- x[i][j]=y;
- return true;
- }
- else if(x[i][j]==0&&c>0)
- c--;
- i=(i==4?0:i);
- j=(j==4?0:j);
- }
- }
- }//boolean setNull(int[][],int)方法用于在4x4二维数组中随机挑出一个值为0的元素,并将其赋值为给定整数。特殊地,若该二维数组已满,返回false。
- }
算法层代码
- /**
- *
- * @author qliujinming@qq.com
- *
- * @see http://www.cnblogs.com/liujinming/
- *
- */
- public class LierIntArr{
- /**
- * 该方法用于接受一个整数数组,对该数组进行drop操作后返回
- * 示例:接受 2 0 2 0 5 5,返回4 10 0 0 0 0
- * @param 需要进行drop操作的数组
- * @return drop操作之后的数组
- */
- public static int[] drop(int[] a){
- int b = a.length;
- if(b<=1)return a;
- int[] c = new int[b];
- int j=0;
- for(int i=0;i<b;i++){
- if(c[j]==0&&a[i]!=0)
- c[j]=a[i];
- else if(c[j]!=0&&a[i]==c[j]){
- c[j]=2*a[i];
- j++;
- }
- else if(a[i]!=0&&c[j]!=0&&a[i]!=c[j]){
- j++;
- c[j]=a[i];
- }
- }
- return c;
- }
- //该方法用于接受一个整数数组,对该数组进行drop操作后返回
- //示例:接受 2 0 2 0 5 5,返回4 10 0 0 0 0
- public static void main(String[] args){
- int[] a = {0,2,0,2,4,0,0,4,2,0,2,5,5,0,10};
- int[] b = drop(a);
- for(int i = 0;i<b.length;i++)
- System.out.print(b[i]+",");
- }
- //输出结果:4,8,4,10,10,0,0,0,0,0,0,0,0,0,0,
- }
辅助工具
2048小游戏(Java)(swing实现)(一)的更多相关文章
- 2048小游戏(Java)(swing实现)(二)
这里是上一次的成果,只能用鼠标点,没法用键盘 最近扩充了一下知识面,实现了用键盘操控2048小游戏 但是还是不支持同时使用键盘和鼠标同时操作 import javax.swing.*; //impor ...
- jQuery实践-网页版2048小游戏
▓▓▓▓▓▓ 大致介绍 看了一个实现网页版2048小游戏的视频,觉得能做出自己以前喜欢玩的小游戏很有意思便自己动手试了试,真正的验证了这句话-不要以为你以为的就是你以为的,看视频时觉得看懂了,会写了, ...
- C# 开发2048小游戏
这应该是几个月前,闲的手痒,敲了一上午代码搞出来的,随之就把它丢弃了,当时让别人玩过,提过几条更改建议,但是时至今日,我也没有进行过优化和更改(本人只会作案,不会收场,嘎嘎),下面的建议要给代码爱好的 ...
- Swift实战之2048小游戏
上周在图书馆借了一本Swift语言实战入门,入个门玩一玩^_^正好这本书的后面有一个2048小游戏的实例,笔者跟着实战了一把. 差不多一周的时间,到今天,游戏的基本功能已基本实现,细节我已不打算继续完 ...
- 如何在CentOS上安装一个2048小游戏
如何在centos上安装一个2048小游戏 最近在学习CentOS系统,就琢磨着玩点什么,然后我看到有人在玩2048小游戏,所有我就在想,为啥不装一个2048小游戏搞一下嘞,于是乎,我就开始工作啦 由 ...
- js、jQuery实现2048小游戏
2048小游戏 一.游戏简介: 2048是一款休闲益智类的数字叠加小游戏 二. 游戏玩法: 在4*4的16宫格中,您可以选择上.下.左.右四个方向进行操作,数字会按方向移动,相邻的两个数字相同就会合 ...
- 用js实现2048小游戏
用js实现2048小游戏 笔记仓库:https://github.com/nnngu/LearningNotes 1.游戏简介 2048是一款休闲益智类的数字叠加小游戏.(文末给出源代码和演示地址) ...
- 2048小游戏代码解析 C语言版
2048小游戏,也算是风靡一时的益智游戏.其背后实现的逻辑比较简单,代码量不算多,而且趣味性强,适合作为有语言基础的童鞋来加强编程训练.本篇分析2048小游戏的C语言实现代码. 前言 游戏截图: 游 ...
- Docker从0开始之部署一套2048小游戏
本文记录一下在docker部署一套2048小游戏的过程,在娱乐中熟悉docker的应用部署.docker 安装不在本文讲述之中,参考我的其它博客. 1.获取image镜像. 方法一:daocloud. ...
随机推荐
- array_unique() 函数移除数组中的重复的值
array_unique() 函数移除数组中的重复的值,并返回结果数组. 当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除. 返回的数组中键名不变.
- JAVA语法规则总结
单继承多实现 抽象类 抽象方法 使用关键字:abstract修饰的方法就是抽象方法; 抽象方法的形式:只有方法的声明,没有方法体; 抽象方法一般存在于父类中,相当于强制要求子类必须重写该方法,相当于 ...
- Linux 使用静态库注意事项
1. 静态库一定要放在生成文件后面 gcc main.c -o main libhello.a 2. 使用静态库时一定要连接所有用到的静态库 gcc main.c -o main liba.a lib ...
- 22、linux的ssh互信配置
转载:https://blog.csdn.net/hrn1216/article/details/51568830 https://blog.csdn.net/u013144287/article/d ...
- 数据结构_yjjsj(伊姐姐数字游戏)
问题描述 伊姐姐热衷于各类数字游戏, 24 点. 2048.数独等轻轻松松毫无压力.一日,可爱的小姐姐邀请伊姐姐一起玩一种简单的数字 game,游戏规则如下:一开始桌上放着 n 张数字卡片,从左到右按 ...
- Mysql--基本配置
登录的常用参数 mysql -uroot -p 之后再加上密码 mysql -uroot -p+密码 这个方法不安全 mysql -hlocalhost -uroot -p 之后再加上密码 ...
- 【IMOOC学习笔记】多种多样的App主界面Tab实现方法(一)
1.ViewPager实现Tab 首先实现底部和底部布局 <?xml version="1.0" encoding="utf-8"?> <Li ...
- Jmeter-BeanShell的使用介绍
最近学习使用了jmeter来对接口进行测试.使用jmter进行接口测试,有时候需要编写一些BeanShell脚本语言,或者利用BeanShell调用自己的工具类,来完成jmeter基本功能中无法实现的 ...
- winform播放视频(windows media player)
1.找到windows media player 工具箱常规下边右键,右键弹窗点击“选择项”,选择工具箱窗口点击“COM组件”,找到 Windows Media Player 勾选,点击确定 2.使用 ...
- Python学习第三方库Requests: 让 HTTP 服务人类
转自官方文档:http://cn.python-requests.org/zh_CN/latest/ 快速上手 http://cn.python-requests.org/zh_CN/latest/u ...