⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myeluosi.java

📁 完全自己做的俄罗斯方块
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    	
    	twopoint1.setBounds(240,0,60,60);
    	if(r == 0){
    		twopoint2.setBounds(300,0,60,60);//两横点
    		this.setTitle("俄罗斯方块   两横点"+"      现在积分:"+(scores*11));
    	}
    	else{
    		twopoint2.setBounds(240,60,60,60);		//两竖点
    		this.setTitle("俄罗斯方块   两竖点"+"      现在积分:"+(scores*11));
    	}
    	
    	twopoint1.setBackground(Color.red);
    	twopoint2.setBackground(Color.red);
    	c.add(twopoint1);
    	c.add(twopoint2);
    	twopoint1.requestFocus();
    	
    	final Thread thread=new Thread(){
			public void run(){
				x = twopoint1.getX();
				y = twopoint1.getY();
				y2 = twopoint2.getY();
				x2 = twopoint2.getX();
				
				while( y<600&&y2<600&&c.getComponentAt(x,y+60).isEnabled()&&
						c.getComponentAt(x2,y2+60).isEnabled() ){
					x = twopoint1.getX(); y = twopoint1.getY()+60;
					x2 = twopoint2.getX(); y2 = twopoint2.getY()+60;
        			try{
            			Thread.sleep(slep);
            			twopoint1.setLocation(x,y);
            			twopoint2.setLocation(x2,y2);
        			}catch(InterruptedException e){}      
        		}
        		//当方块遇到障碍物时不可再用
                int twoy1 = twopoint1.getY()+60;
                int twox1 = twopoint1.getX();
                int twoy2= twopoint2.getY()+60;
                int twox2 = twopoint2.getX();
                
                if(twoy1==660||!c.getComponentAt(twox1,twoy1).isEnabled()){
                	twopoint1.setBackground(Color.magenta);
                	twopoint2.setBackground(Color.magenta);
                	twopoint1.setEnabled(false);
                	twopoint2.setEnabled(false);
                	ClearRows();
                	getRandomDiamonds();
                }
                else if(twoy2==660||!c.getComponentAt(twox2,twoy2).isEnabled()){
                	twopoint1.setBackground(Color.magenta);
                	twopoint2.setBackground(Color.magenta);
                	twopoint1.setEnabled(false);
                	twopoint2.setEnabled(false);
                	ClearRows();
                	getRandomDiamonds();
                }
                DefineOver();
			}
		};
		thread.start();
		
    	KeyAdapter k;
    	
    	twopoint1.addKeyListener(k = new KeyAdapter(){
            public void keyPressed(KeyEvent e){
                int key = e.getKeyCode();
                x = twopoint1.getX();y = twopoint1.getY();
                x2 = twopoint2.getX();y2 = twopoint2.getY();
    			int xx2 = x2,yy2 = y2;
                
                //使twopoint相应移动
                if(e.getKeyText(key).equals("Right")){
                	xx2 = x2;
                	xx = x;
                	x+=60;
                	x2+=60;
                	int twoy1 = twopoint1.getY();
                	int twox1 = twopoint1.getX()+60;
                	int twoy2 = twopoint2.getY();
                	int twox2 = twopoint2.getX()+60;  
                	 
                	if( x>=0&&x<=600&&x2>=0&&x2<=600&&
                		c.getComponentAt(twox1,twoy1).isEnabled()&&
                		c.getComponentAt(twox2,twoy2).isEnabled() )
                		{
                		twopoint1.setLocation(x,y);
                		twopoint2.setLocation(x2,y2);
                	}
                	else{
                		x = xx;
                		x2 = xx2;
                	}
                }
                else if(e.getKeyText(key).equals("Left")){
                	xx2 = x2;
                	xx = x;
                	x-=60;
                	x2-=60;
                	int twoy1 = twopoint1.getY();
                	int twox1 = twopoint1.getX()-60;
                	int twoy2 = twopoint2.getY();
                	int twox2 = twopoint2.getX()-60;  
                	 
                	if( x>=0&&x<=600&&x2>=0&&x2<=600&&
                		c.getComponentAt(twox1,twoy1).isEnabled()&&
                		c.getComponentAt(twox2,twoy2).isEnabled() )
                		{
                		twopoint1.setLocation(x,y);
                		twopoint2.setLocation(x2,y2);
                	}
                	else{
                		x = xx;
                		x2 = xx2;
                	}
                }
                else if(e.getKeyText(key).equals("Down")){
                	yy2 = y2;
                	yy = y;
                	y+=60;
                	y2+=60;
                	if(y<=600&&y2<=600){
                		twopoint1.setLocation(x,y);
                		twopoint2.setLocation(x2,y2);
                	}
                	else{
                		y = yy;
                		y2 = y2;
                	}
                }
                //找一正方区域再进行变形
                else if(e.getKeyText(key).equals("Space")){
                	int x22 = twopoint1.getX();
                	int y22 = twopoint1.getY();
                	int xx22 = twopoint2.getX();
                	int yy22 = twopoint2.getY();
                	int left = x22>xx22 ? xx22:x22;
                	int right = x22>xx22 ? x22:xx22;
                	int top = y22>yy22 ? yy22:y22;
                	int bottom = y22>yy22 ? y22:yy22;
                	int max = (bottom-top)>(right-left) ? (bottom-top):(right-left);
                	
                	//判断是否可以变形
                	if(left>=0&&bottom>=max&&bottom<=600&&(
                			(right==left&&right<600)||
                			(right==left+60&&right<=600))){
                		if(top == bottom){
                			twopoint1.setLocation(left,bottom);
                			twopoint2.setLocation(left,bottom-60);
                			x = left; y = bottom;
                			x2 = left;y2 = bottom-60;
                		}
                		else{
                			twopoint1.setLocation(left,bottom);
                			twopoint2.setLocation(left+60,bottom);
                			x = left; y = bottom;
                			x2 = left+60;y2 = bottom;
                		}
                	}
                }
                //快速下降
                else if(e.getKeyText(key).equals("F")){
                	int twox1 = twopoint1.getX();
                	int twoy1 = twopoint1.getY();
                	int twox2 = twopoint2.getX();
                	int twoy2 = twopoint2.getY();
                	
                	while(c.getComponentAt(twox1,twoy1+60).isEnabled()&&
                			c.getComponentAt(twox2,twoy2+60).isEnabled()&&
                			twoy1<600&&twoy2<600){
                		twoy1+=60;twoy2+=60;
                	}
                	twopoint1.setLocation(twox1,twoy1);
                	twopoint2.setLocation(twox2,twoy2);
                	x = twox1;y = twoy1;
                	x2 = twox2;y2 = twoy2;
                }
                //两点爆炸
                else if(bombFlag&&e.getKeyText(key).equals("B")){
                	System.out.println("Bomb!");
                	int towx1 = twopoint1.getX();
                	int towy1 = twopoint1.getY();
                	int towx2 = twopoint2.getX();
                	int towy2 = twopoint2.getY();
                	
                	int zonex1 = towx1-60;
                	int zoney1 = towy1-60;
                	int zonex2 = towx1;
                	int zoney2 = towy1-60;
                	int zonex3 = towx1+60;
                	int zoney3 = towy1-60;
                	int zonex4 = towx1-60;
                	int zoney4 = towy1;
                	int zonex5 = towx1;
                	int zoney5 = towy1;
                	int zonex6 = towx1+60;
                	int zoney6 = towy1;
                	int zonex7 = towx1-60;
                	int zoney7 = towy1+60;
                	int zonex8 = towx1;
                	int zoney8 = towy1+60;
                	int zonex9 = towx1+60;
                	int zoney9 = towy1+60;
                	
                	//使两点不可用
                	twopoint1.setEnabled(false);
                	twopoint2.setEnabled(false);
                	
                	
                	if(zonex1>=0&&zonex1<=600&&zoney1>=0&&zoney1<=600
                		&&!c.getComponentAt(zonex1,zoney1).isEnabled()){
            			c.remove(c.getComponentAt(zonex1,zoney1));
            		}
            		if(zonex2>=0&&zonex2<=600&&zoney2>=0&&zoney2<=600
            			&&!c.getComponentAt(zonex2,zoney2).isEnabled()){
            			c.remove(c.getComponentAt(zonex2,zoney2));
            		}
            		if(zonex3>=0&&zonex3<=600&&zoney3>=0&&zoney3<=600
            			&&!c.getComponentAt(zonex3,zoney3).isEnabled()){
            			c.remove(c.getComponentAt(zonex3,zoney3));
            		}
            		if(zonex4>=0&&zonex4<=600&&zoney4>=0&&zoney4<=600
            			&&!c.getComponentAt(zonex4,zoney4).isEnabled()){
            			c.remove(c.getComponentAt(zonex4,zoney4));
            		}
            		if(zonex5>=0&&zonex5<=600&&zoney5>=0&&zoney5<=600
            			&&!c.getComponentAt(zonex5,zoney5).isEnabled()){
            			c.remove(c.getComponentAt(zonex5,zoney5));
            		}
            		if(zonex6>=0&&zonex6<=600&&zoney6>=0&&zoney6<=600
            			&&!c.getComponentAt(zonex6,zoney6).isEnabled()){
            			c.remove(c.getComponentAt(zonex6,zoney6));
            		}
            		if(zonex7>=0&&zonex7<=600&&zoney7>=0&&zoney7<=600
            			&&!c.getComponentAt(zonex7,zoney7).isEnabled()){
            			c.remove(c.getComponentAt(zonex7,zoney7));
            		}
            		if(zonex8>=0&&zonex8<=600&&zoney8>=0&&zoney8<=600
            			&&!c.getComponentAt(zonex8,zoney8).isEnabled()){
            			c.remove(c.getComponentAt(zonex8,zoney8));
            		}
            		if(zonex9>=0&&zonex9<=600&&zoney9>=0&&zoney9<=600
            			&&!c.getComponentAt(zonex9,zoney9).isEnabled()){
            			c.remove(c.getComponentAt(zonex9,zoney9));
            		}
            		getRandomDiamonds();
                }
            }
        });
    }
    
/*---------------------------------------------------------------------------*/
    //三点
    public void getThreePoints(int r){
    	final JButton threepoint1 = new JButton("1");
    	final JButton threepoint2 = new JButton("2");
    	final JButton threepoint3 = new JButton("3");
    	
    	threepoint1.setBounds(240,0,60,60);
    	
    	if(r == 0){		//三横点
    		threepoint2.setBounds(300,0,60,60);
    		threepoint3.setBounds(360,0,60,60);
    		this.setTitle("俄罗斯方块   三横点"+"      现在积分:"+(scores*11));
    	}
    	else if(r==1){	//三竖点
    		threepoint2.setBounds(240,60,60,60);
    		threepoint3.setBounds(240,120,60,60);
    		this.setTitle("俄罗斯方块   三竖点"+"      现在积分:"+(scores*11));
    	}
    	else if(r == 2){//上二点右下一点
    		threepoint2.setBounds(300,0,60,60);
    		threepoint3.setBounds(300,60,60,60);
    		this.setTitle("俄罗斯方块   三点之上二点右下一点"+"      现在积分:"+(scores*11));
    	}
    	else{			//上二点左下一点
    		threepoint2.setBounds(300,0,60,60);
    		threepoint3.setBounds(240,60,60,60);
    		this.setTitle("俄罗斯方块   三点之上二点左下一点"+"      现在积分:"+(scores*11));
    	}
    	
    	threepoint1.setBackground(Color.red);
    	threepoint2.setBackground(Color.red);
    	threepoint3.setBackground(Color.red);
    	c.add(threepoint1);
    	c.add(threepoint2);
    	c.add(threepoint3);
    	threepoint1.requestFocus();
    	
    	final Thread thread=new Thread(){
			public void run(){
				x = threepoint1.getX();
				y = threepoint1.getY();
				y2 = threepoint2.getY();
				x2 = threepoint2.getX();
				y3 = threepoint3.getY();
				x3 = threepoint3.getX();
				
				while( y<600&&y2<600&&y3<600&&
						c.getComponentAt(x,y+60).isEnabled()&&
						c.getComponentAt(x2,y2+60).isEnabled()&&
						c.getComponentAt(x3,y3+60).isEnabled() ){
					x = threepoint1.getX(); y = threepoint1.getY()+60;
					x2 = threepoint2.getX(); y2 = threepoint2.getY()+60;
					x3 = threepoint3.getX(); y3 = threepoint3.getY()+60;
        			try{
            			Thread.sleep(slep);
            			threepoint1.setLocation(x,y);
            			threepoint2.setLocation(x2,y2);
            			threepoint3.setLocation(x3,y3);
        			}catch(InterruptedException e){}      
        		}
        		//当方块遇到障碍物时不可再用
                int threey1 = threepoint1.getY()+60;
                int threex1 = threepoint1.getX();
                int threey2 = threepoint2.getY()+60;
                int threex2 = threepoint2.getX();
                int threey3 = threepoint3.getY()+60;
                int threex3 = threepoint3.getX();
                
                if(threey1==660||!c.getComponentAt(threex1,threey1).isEnabled()){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -