JAVA石头迷阵

石头迷阵

有报错,请大神看看,是不是接口错误

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.sql.JDBCType;
import java.util.Random;

public class MyJFrame extends JFrame implements KeyListener, ActionListener {

    //定义一个二维数组,方便管理数据
    int[][] datas = new int[4][4];

    //定义一个二维数组,元素是最终胜利的元素
    int [][] victoy = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};

    //定义两个变量,用来记录空白位置
    int x0 = 0;
    int y0 = 0;

    //统计走了多少步的变量
    int step = 0;

    //定义两个条目
    JMenuItem jMenuItem1;
    JMenuItem jMenuItem2;

    //构造方法
    public MyJFrame(){


        //初始化界面
        initFrame();

        //初始化菜单
        initMenu();

        //初始化数据
        initData();

        //初始化图片
        initImage();

        //窗体显示
        this.setVisible(true);

    }


    public void initFrame(){
        //窗体宽高设置
        this.setSize(514, 595);

        //窗体标题
        this.setTitle("石头迷阵单机版 v1.0");

        //窗体关闭模式
        this.setDefaultCloseOperation(3);

        //让窗体位于屏幕正中央
        this.setLocationRelativeTo(null);

        //窗体置顶
        this.setAlwaysOnTop(true);

        //让界面添加键盘监听
        this.addKeyListener(this);

        //取消内部居中放置方式
        this.setLayout(null);

    }


    public void initMenu(){
        //创建一个菜单对象
        JMenuBar jMenuBar = new JMenuBar();

        //设置菜单宽高
        jMenuBar.setSize(514, 20);

        //创建一个选项
        JMenu jMenu1 = new JMenu("功能");
        JMenu jMenu2 = new JMenu("关于");

        //创建一个条目
        jMenuItem1 = new JMenuItem("重新游戏");

        jMenuItem2 = new JMenuItem("联系我们");

        //给重新开始游戏这个条目绑定一个事件
        jMenuItem1.addActionListener(this);
        jMenuItem2.addActionListener(this);

        //白条目添加到选项当中
        jMenu1.add(jMenuItem1);
        jMenu2.add(jMenuItem2);

        //把选项添加到菜单中
        jMenuBar.add(jMenu1);
        jMenuBar.add(jMenu2);

        //把菜单添加到最外层的窗体中
        this.setJMenuBar(jMenuBar);
    }


    public void initData(){
        //创建数组
        int [] temp = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        //打乱数组中数据的顺序
        // 1. 遍历数组依次得到数组中的每一个数字
        for (int i = 0; i < temp.length; i++) {
            //temp[i];  --当前遍历到数组中的数字
            // 2. 获取数组中的一个随机索引
            Random r = new Random();
            int index = r.nextInt(temp.length);
            // 3. 将当前遍历到的数字,跟随机索引上的数字两者进行交换
            int number = temp[i];
            temp[i] = temp[index];
            temp[index] = number;
        }
        //把打乱后的数据添加到二维数组中
        //把arr中的16个元素添加到二维数组中
        for (int i = 0; i < temp.length; i++) {
            datas[i / 4][i % 4] = temp[i];
            if (temp[i] == 0){
                x0 = i / 4;
                y0 = i % 4;
            }
        }

    }


    public void initImage(){
        /*
        //添加图片
        ImageIcon imageIcon1 = new ImageIcon("image\\1.png");
        //创建一个JLabel对象, 利用JLabel对象来设置图片的位置和宽高
        JLabel jLabel1 = new JLabel(imageIcon1);
        //设置jLabel的位置和宽高
        //第一个参数,第二个参数表示位置
        //第三个参数,第四个参数表示宽高
        jLabel1.setBounds(0 + 50,0 + 90,100,100);
        //把JLabel添加到整个窗体中
        this.add(jLabel1);

        ImageIcon imageIcon2 = new ImageIcon("image\\2.png");
        JLabel jLabel2 = new JLabel(imageIcon2);
        jLabel2.setBounds(100 + 50,0 + 90,100,100);
        this.add(jLabel2);

         */

        /*
        for (int i = 1; i <= 4; i++){
            ImageIcon imageIcon = new ImageIcon("image\\" + i + ".png");
            JLabel jLabel = new JLabel(imageIcon);
            jLabel.setBounds((i-1) * 100 + 50,0 + 90, 100,100);
            this.add(jLabel);
        }

         */

        //将界面中原来所有的图片全部删除
        this.getContentPane().removeAll();

        JLabel label_step = new JLabel("步数:" + step);
        label_step.setBounds(50,20,100,20);
        this.add(label_step);

        //判断游戏是否胜利
        if (victory()){
            //如果该方法结果是true
            //表示游戏胜利
            ImageIcon imageIcon = new ImageIcon("image\\win.png");
            JLabel jLabel = new JLabel(imageIcon);
            jLabel.setBounds(514/2-266/2, 230,266,88);
            this.add(jLabel);
        }



        for (int i = 0; i < datas.length; i++){
            for (int j = 0; j < datas[i].length; j++) {
                int data = datas[i][j];
                if (data != 0){
                    ImageIcon imageIcon = new ImageIcon("image\\" + data + ".png");
                    JLabel jLabel = new JLabel(imageIcon);
                    jLabel.setBounds(j * 100 + 50,i * 100 + 90,100,100);
                    this.add(jLabel);
                }
                
            }
        }

        //游戏背景
        ImageIcon background = new ImageIcon("image\\background.png");
        JLabel backgroundjLabel = new JLabel(background);
        backgroundjLabel.setBounds(26,30,450,484);
        this.add(backgroundjLabel);

        //将整个界面重新绘制
        this.getContentPane().repaint();
    }


    public boolean victory(){
        //只要判断两个数组中的元素是否相同
        //datas 和 victory
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                if (datas[i][j] != victoy[i][j]){
                    //return -- 可以写在方法当中
                        // 1. 停止方法
                        // 2. 将return后面的数据,返回给调用者
                    return false;
                }
            }
        }
        return true;
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    //按下
    @Override
    public void keyPressed(KeyEvent e) {

    }

    //松开
    @Override
    public void keyReleased(KeyEvent e) {
        //获取按键的数字
        int keyCode = e.getKeyCode();
        move(keyCode);
        //表示重新绘制界面
        initImage();
    }

    public void move(int keyCode){

        if (keyCode == 37){
            //左
            //表示空格右边的图片要向左移动
            datas[x0][y0] = datas[x0][y0 + 1];
            datas[x0][y0 + 1] = 0;
            y0++;
            step++;

        }else if (keyCode == 38){
            //上
            //表示把空格下面的图片向上移动
            datas[x0][y0] = datas[x0 + 1][y0];
            datas[x0 + 1][y0] = 0;
            x0++;
            step++;

        }else if (keyCode == 39){
            //右
            //表示把空格左边的图片向右移动
            datas[x0][y0] = datas[x0][y0 - 1];
            datas[x0][y0 - 1] = 0;
            y0--;
            step++;

        }else if (keyCode == 40){
            //下
            datas[x0][y0] = datas[x0 - 1][y0];
            datas[x0 - 1][y0] = 0;
            x0--;
            step++;

        }else if (keyCode == 87){
            //W  表示作弊吗
            datas = new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};


        }else {
            System.out.println("只能按上下左右键");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //判断
        //如果点击的是重新开始,那么才执行下面的代码
        //如果点击的是关于我们,弹出一个对话框
        if (e.getSource() == jMenuItem1){
            //重新数据打乱数据的顺序
            initData();

            //计步器清零
            step = 0;

            //绘制整个界面
            initImage();
        }else if (e.getSource() == jMenuItem2){
            //点击了联系我们
            //创建了一个弹框对象
            JDialog jDialog = new JDialog();
            //创建了一个图片对象
            ImageIcon imageIcon = new ImageIcon("image\\about.png");
            JLabel jLabel = new JLabel(imageIcon);
            jLabel.setBounds(0,0,344,344);
            //把图片放到弹框当中
            jDialog.add(jLabel);
            //设置弹框大小
            jDialog.setSize(344,344);
            //要把弹框设置为顶层  -- 置顶效果
            jDialog.setAlwaysOnTop(true);
            // 要让jDialog居中显示
            jDialog.setLocationRelativeTo(null);
            //让jDialog显示出来
            jDialog.setVisible(true);



        }else {
            System.out.println("没有这个按键");
        }

        //System.out.println("点击了重新开始按钮");




    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
android 游戏源码,宝石迷阵。 package wealk.android.jewels; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import javax.microedition.khronos.opengles.GL10; import org.anddev.andengine.audio.sound.Sound; import org.anddev.andengine.audio.sound.SoundFactory; import org.anddev.andengine.engine.Engine; import org.anddev.andengine.engine.camera.Camera; import org.anddev.andengine.engine.handler.IUpdateHandler; import org.anddev.andengine.engine.handler.timer.ITimerCallback; import org.anddev.andengine.engine.handler.timer.TimerHandler; import org.anddev.andengine.engine.options.EngineOptions; import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation; import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy; import org.anddev.andengine.entity.scene.Scene; import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener; import org.anddev.andengine.entity.shape.modifier.AlphaModifier; import org.anddev.andengine.entity.shape.modifier.LoopShapeModifier; import org.anddev.andengine.entity.shape.modifier.RotationModifier; import org.anddev.andengine.entity.shape.modifier.ScaleModifier; import org.anddev.andengine.entity.shape.modifier.SequenceShapeModifier; import org.anddev.andengine.entity.sprite.Sprite; import org.anddev.andengine.entity.text.ChangeableText; import org.anddev.andengine.entity.text.Text; import org.anddev.andengine.input.touch.TouchEvent; import org.anddev.andengine.opengl.font.Font; import org.anddev.andengine.opengl.font.FontFactory; import org.anddev.andengine.opengl.texture.Texture; import org.anddev.andengine.opengl.texture.TextureOptions; import org.anddev.andengine.opengl.texture.region.TextureRegion; import org.anddev.andengine.opengl.texture.region.TextureRegionFactory; import org.anddev.andengine.ui.activity.BaseGameActivity; import org.anddev.andengine.util.Debug; import org.anddev.andengine.util.HorizontalAlign; import org.anddev.andengine.util.MathUtils; import wealk.android.jewels.constants.IConstants; import wealk.android.jewels.entity.BackgroundCell; import wealk.android.jewels.entity.BorderSprite; import wealk.android.jewels.entity.JewelSprite; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.graphics.Typeface; import android.os.Handler; import android.os.Message; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.Toast; /** * @author Qingfeng * @time 2010-11-03 ~ 2010 */ public class Jewels extends BaseGameActivity implements IOnSceneTouchListener, IConstants { // =========================================================== // Constants // =========================================================== /**灞忓箷灏哄**/ private static final int CAMERA_WIDTH = 320; private static final int CAMERA_HEIGHT = 480; /**鍦烘櫙鍒嗗眰**/ private static final int LAYER_BACKGROUND = 0; private static final int LAYER_BG_CELL = LAYER_BACKGROUND + 1; private static final int LAYER_JEWELS = LAYER_BG_CELL + 1; private static final int LAYER_SCORE = LAYER_JEWELS + 1; // =========================================================== // Fields // =========================================================== private Camera mCamera;//闀滃ご protected Scene mMainScene;//涓诲満鏅� /**娓告垙妯″紡**/ private String mGameModel; /**娓告垙鐘舵�**/ private boolean mGameRunning;//娓告垙鐨勬�寮�叧(鍙鐞嗘潵鐢点�浠诲姟鍒囨崲绛� private boolean mIsSwaping;//浜ゆ崲鐘舵� private final int MOVE_UP = 1;//涓婄Щ private final int MOVE_DOWN = 2;//涓嬬Щ private final int MOVE_LEFT = 3;//宸︾Щ private final int MOVE_RIGHT = 4;//鍙崇Щ private final int FALL = 5;//涓嬭惤 private final int DEAD = 6;//姝诲眬 private final int CHECK = 0;//鎵ц妫�祴 private int STATE = CHECK;//涓�紑濮嬪氨妫�祴锛屾病鏈夌Щ鍔ㄥ懡浠ょ

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值