关于2048

         做任何产品之前首先先想好要这款游戏能实现什么功能?对界面有什么要求?当有了大概的一个想法之后就可以按想法去一点一点实现它了。首先说一下我做2048的想法,首先功能要实现,其次是界面。在做这个游戏之前我自己玩的时候就觉得好无聊,所以在游戏进行的时候加入了节奏很欢快的歌曲。上下移动时由键盘控制,所以要加一个键盘监听器。
     先写界面:

         

  public void init(){
		this.setTitle("2048");
		this.setLayout(null);
		this.setDefaultCloseOperation(3);
		this.setSize(510,700);
		
		MyListener my=new MyListener(this);
		this.setResizable(false);
		this.addKeyListener(my);
		this.setLocationRelativeTo(null);
		this.setFocusable(true);
		this.getContentPane().setBackground(Color.WHITE);
		
		
		
		jt=new JTextField("当前分数");
		jt.setBounds(61,580,102,66);
		jt.setFont(new Font("宋体", 1, 20));
		this.add(jt);
		jt1=new JTextField("0");
		jt1.setBounds(247,583,165,66);
		jt1.setFont(new Font("宋体", 1, 40));
		this.add(jt1);
		jt1.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.BLACK));
		jt.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.BLACK));
		
		

		mainPane = new JPanel();//创建游戏主面板
		mainPane.setBounds(20, 70, 460, 500);//设置主面板位置尺寸

		this.getContentPane().add(mainPane);
		mainPane.setLayout(null);//设置空布局
		
		texts = new JLabel[4][4];//创建文本框二维数组

                for(int i = 0; i < 4; i++){//遍历数组
			for(int j = 0; j < 4; j++){
				aa[i][j]=1;
				texts[i][j] = new JLabel();//创建标签
				texts[i][j].setBackground(Color.PINK);
				texts[i][j].setFont(font2);
				texts[i][j].setHorizontalAlignment(SwingConstants.CENTER);
				texts[i][j].setText("");
				texts[i][j].setBounds(120 * j, 120 * i, 100, 100);//设置方块的大小位置
				//	setColor(i, j, "");
				texts[i][j].setFont(new Font("宋体", 1, 40));  //设置字体颜色
				texts[i][j].setForeground(Color.black);
				texts[i][j].setOpaque(true);//透明
				texts[i][j].setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.BLACK));//设置方块边框颜色
				mainPane.add(texts[i][j]);//将创建的文本框放在游戏主面板上
			}
			
		}
		
		 int i=ra.nextInt(4);                //首先界面上出现一个2!!!!
	       int j=ra.nextInt(4);
	       int suijishu = ra.nextInt(2);
	       str = texts[i][j].getText();
    	 
	       setColor(i, j, 2);
	       texts[i][j].setText("2");
    		 
		  shixian();
		 this.setVisible(true);
                 g=(Graphics2D)this.getGraphics();
		
		g.setStroke(new BasicStroke(6.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
		
		g.setColor(Color.PINK);
		

	
	}

    !!!! 其次是让界面随机出现两个数。
   

 public void  shixian  (){      
		 int i=ra.nextInt(4);
	     int j=ra.nextInt(4);
	     int suijishu = ra.nextInt(2);
	     str = texts[i][j].getText();
    	 
	     if(str.equals("")){
	    	 if(suijishu==1){
	    //		 setColor(i, j, 2);
	    		 texts[i][j].setText("2");
//	    		 setColor(x, y, z)
	    	 }
	    	 else{
	    		 texts[i][j].setText("4");
	    	//	 setColor(i, j, 4);
	    	 }
	     
	     }
	     else{    
	    	 
	    	 int count = 0;
	    	 for(int n=0;n<4;n++  ){
	  	 		 for(int m=0;m<4;m++){
	  	 			if(texts[n][m].getText().equals("")){	    				
	    				count++;	    				
	    			} 
	    		 }
	    	 }  

	    	 if(count != 0)     
	    	 shixian() ;      
	     }
	     
	}	

  再次,实现2048的功能,两个相同的数字相加的功能。可以上下左右移动相加,最要注意的是不要让它三连加!下面只写一个移动的方法!

  

 public void shangyi(){
		                          //实际是shang移的算法
		   for(int k=0;k<3;k++)
				  for(int y=0;y<4;y++){
					  for(int x=1;x<4;x++){
						 
						  if(!(texts[x][y].getText().equals(""))){  //判断上面文本框不为空
							String str=texts[x][y].getText();
							String str1=texts[x-1][y].getText();
					///!!!!!!!!!!			  
				   			 if(aa[x-1][y]==1&&aa[x][y]==1){   //加之前判断标志
									  
								  if(str.equals(str1)){
							  		int num=Integer.parseInt(str);//把字符转换成数字
							  		
							  		jt1.setText((Integer.parseInt(jt1.getText())+2*num)+"");
							  		texts[x-1][y].setText(2*num+"");
							  		texts[x][y].setText("");
								    aa[x-1][y]=0;
								        
								  }
								  else 
									  if(str1.equals("")){
								  
										  int num=Integer.parseInt(str);										  
										  texts[x-1][y].setText(num+"");
										  texts[x][y].setText("");
										  aa[x-1][y]=aa[x][y];
								  
									  }
								}
					  
					  
						  }
					  }
				  }
		   for(int i=0;i<4;i++){
			   for(int j=0;j<4;j++){
				   aa[i][j]=1;
			   }
			   
		   }
		   shixian() ;
	   }
    
     














猜你喜欢

转载自627648914.iteye.com/blog/2108290