Java实现2048小游戏(全代码)

在这里插入图片描述

App包

package com.Allen;

import javax.swing.*;//窗体需要导包

public class App {
   
    public static void main(String[]args)
    {
   
        MainJFrame mainJFrame=new MainJFrame();
    }
}

MainJFrame包

package com.Allen;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

//继承-JFrame
public class MainJFrame extends JFrame implements KeyListener , ActionListener {
   
    //成员变量————初始化一个二维数组来设置图片
    int date[][]=new int[4][4];
    //用于判断失败的图片出现时机
    //1.不展示
    //2.展示
    int loseflag=1;

    //得分
    int score=0;

    //图片资源标识
    String theme="A-";

    //将JMenuItem对象提取到成员变量中,方便其他方法使用
    JMenuItem jMenuItem1=new JMenuItem("经典");
    JMenuItem jMenuItem2=new JMenuItem("炫彩");
    JMenuItem jMenuItem3=new JMenuItem("糖果");
    JMenuItem jMenuItem4=new JMenuItem("重置");

    /**
     * 构造方法
     * */
    public MainJFrame(){
   
        //初始化窗口
        this.InitFrame();

        //初始化菜单
        this.InitMexnu();

        //初始化二维数组数据
        this.InitDate();

        //初始化界面
        this.paintView();

        //为窗体添加键盘监听
        this.addKeyListener(this);

        //调用成员方法,设置窗体可见
        super.setVisible(true);
    }

    /**
     * 初始化菜单
     * */
    public void InitMexnu() {
   
        //创建JMenuBar
        JMenuBar jMenuBar=new JMenuBar();

        //创建栏目对象 JMen(换肤,关于我们)
        JMenu jMenu1=new JMenu("换肤");
        JMenu jMenu2=new JMenu("重新开始游戏");


        //添加换肤和重新游戏到jMenuBar中
        jMenuBar.add(jMenu1);
        jMenuBar.add(jMenu2);

        //重置
        jMenu2.add(jMenuItem4);
        jMenuItem4.addActionListener(this);

        //添加换肤个模式到jMenu1中
        jMenu1.add(jMenuItem1);
        jMenu1.add(jMenuItem2);
        jMenu1.add(jMenuItem3);

        jMenuItem1.addActionListener(this);
        jMenuItem2.addActionListener(this);
        jMenuItem3.addActionListener(this);

        //给窗体对象设置菜单
        super.setJMenuBar(jMenuBar);
    }

    /**
     * 此方法用于初始化二维数组
     * */
    public void InitDate(){
   
        this.GenterNum();
        this.GenterNum();
    }

    /**
     * 此方法用于二维数组反转
     * */
    public void horizontalSwap(){
   
        for (int i = 0; i < date.length; i++) {
   
            //调用reverseArray方法,对每一个一维数组进行反转
            this.reverseArray(date[i]);
        }
    }

    /**
     * 次方法用于一维数组的反转
     * */
    public void reverseArray(int arr[]){
   
        for (int start = 0, end=arr.length-1; start < end; start++,end--) {
   
            int temp=arr[start];
            arr[start]=arr[end];
            arr[end]=temp;
        
  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值