前言
游戏描述:游戏界面
功能:
技术:JAVA SE,swing库和action event
逻辑:这个拼图游戏是4*4的,定义一个二维数组来存储1~16;每个数字代表一个图片,游戏开始前打乱数组数据顺序,然后往界面插入图片(因为数组是乱序的,图片也是乱序的),图片中有一个是空白图片,图片数字为16,我们只需要移动16这个数字的图片,每次移动,刷新当前界面图片,当我们把拼图完成时,即数组排好序,即获得胜利。
实现游戏视图图框架代码:
private void initJFrame() { this.setSize(603,680); this.setTitle("拼图游戏v1.0"); this.setLocationRelativeTo(null); //窗口居中对齐 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setLayout(null); this.addKeyListener(this); }
实现游戏菜单功能代码:
JMenuBar menuBar = new JMenuBar(); JMenu functionJMenu = new JMenu("功能"); JMenu aboutsJMenu = new JMenu("关于我们"); JMenu changeItem = new JMenu("更换图片"); JMenuItem replayItem = new JMenuItem("重新游戏"); JMenuItem reloadItem = new JMenuItem("重新登录"); JMenuItem closeItem = new JMenuItem("关闭游戏"); JMenuItem animalItem = new JMenuItem("动物"); JMenuItem girlItem = new JMenuItem("美女"); JMenuItem sportItem = new JMenuItem("运动"); JMenuItem accountItem = new JMenuItem("公众号"); private void initMenu() { //菜单 replayItem.addActionListener(this); reloadItem.addActionListener(this); closeItem.addActionListener(this); accountItem.addActionListener(this); animalItem.addActionListener(this); girlItem.addActionListener(this); sportItem.addActionListener(this); functionJMenu.add(changeItem); functionJMenu.add(replayItem); functionJMenu.add(reloadItem); functionJMenu.add(closeItem); changeItem.add(animalItem); changeItem.add(girlItem); changeItem.add(sportItem); aboutsJMenu.add(accountItem); menuBar.add(functionJMenu); menuBar.add(aboutsJMenu); this.setJMenuBar(menuBar); }
实现插入图片的代码:
private void initImage() { this.getContentPane().removeAll(); if (victory()) { JLabel winJlabel = new JLabel(new ImageIcon("puzzlegame\\image\\win.png")); winJlabel.setBounds(203,283,197,73); this.getContentPane().add(winJlabel); } JLabel stepCount = new JLabel("步数"+step); stepCount.setBounds(50,30,100,20); this.getContentPane().add(stepCount); for(int i =0;i<4;i++){//图片导入 for(int j = 0;j<4;j++){ JLabel label = new JLabel(new ImageIcon( path.toString()+"\\"+date[i][j]+".jpg")); label.setBounds(j*105+83,i*105+134,105,105); label.setBorder(new BevelBorder(1)); this.getContentPane().add(label); } } JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png")); background.setBounds(40,40,508,560); this.getContentPane().add(background); // this.add(label); this.getContentPane().repaint(); }
实现键盘操作行为和其逻辑代码:
public void keyReleased(KeyEvent e) { if(victory()){ return; } int code = e.getKeyCode();//左37 上38 右39 下40 if(code == 37){ if(y!=0){ date[x][y]=date[x][y-1]; date[x][y-1]=0; y=y-1; step++; } } else if (code ==39){ if(y!=3){ date[x][y]=date[x][y+1]; date[x][y+1]=0; y=y+1; step++; } } else if (code == 38) { if(x!=0){ date[x][y]=date[x-1][y]; date[x-1][y]=0; x=x-1; step++; } } else if (code == 40) { if(x!=3){ date[x][y]=date[x+1][y]; date[x+1][y]=0; x=x+1; step++; } } else if (code == 65) { initImage(); } else if(code == 87){ this.getContentPane().removeAll(); for(int i =0;i<15;i++){ date[i/4][i%4]=i+1; } date[3][3]=0; }else { return; } this.initImage(); }
实现鼠标操作行为以及功能实现代码:
public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if(source==replayItem){ step=0; initDate(); initImage(); } else if (source==reloadItem) { this.setVisible(false); } else if (source==closeItem) { System.exit(0); } else if (source==animalItem) { path.delete(40,path.length());// D:\stunew\java\javastu1\puzzlegame\image path.append("\\animal\\animal"); if(changeTime>8){ changeTime=1; } if(changeTime==8){ path.append(changeTime); changeTime=1; }else{ path.append(changeTime); changeTime++; } step=0; initDate(); initImage(); } else if (source==girlItem) { path.delete(40,path.length()); path.append("\\girl\\girl"); if(changeTime==13){ path.append(changeTime); changeTime=1; }else{ path.append(changeTime); changeTime++; } step=0; initDate(); initImage(); } else if (source==sportItem) { path.delete(40,path.length());// D:\stunew\java\javastu1\puzzlegame\image path.append("\\sport\\sport"); if(changeTime>10){ changeTime=1; } if(changeTime==10){ path.append(changeTime); changeTime=1; }else{ path.append(changeTime); changeTime++; } step=0; initDate(); initImage(); } else if (source==accountItem) { JFrame frame = new JFrame(); frame.setSize(300,300); frame.setLocationRelativeTo(null); //窗口居中对齐 JLabel aboutJLabel = new JLabel(new ImageIcon("D:\\stunew\\java\\javastu1\\puzzlegame\\image\\about.png")); aboutJLabel.setBounds(20,0,258 ,258); frame.getContentPane().add(aboutJLabel); frame.setLayout(null); frame.setVisible(true); } }
判断胜利条件代码:
public boolean victory(){ for(int x=0;x<4;x++){ for (int y=0;y<4;y++){ if(date[x][y]!=win[x][y]){ return false; } } } return true; }