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

📄 nextsquare.java~118~

📁 俄罗斯方块游戏,大学时写的,支持单机及点对点网络对战!同时我还在里面加入了使用技能的功能!不过有一个缺点,呵呵好象代码写的不够严谨,所以现在性能很低,我一直也没有优化它!
💻 JAVA~118~
字号:
package ct;//包
//头文件
import java.awt.*;
import java.awt.event.*;
import java.util.*;

//游戏类
public class nextSquare extends Panel implements Runnable//主游戏类
{
    //每个小方块的大小:宽,高
   int m_nSqLength;
   //游戏区域内的列和行
   static int m_nCols;
   static int m_nRows;


   boolean m_bGameInPlay;
  //游戏窗体大小
   public static int WIDTH=100;
   public static int HEIGHT=125;

  Thread m_theThread=null;//游戏线程
   //当前的界面情况
  int m_nField[][];//本页游戏画面
  int m_nOldField[][];//上页游戏画面

   Image box[]=new Image[8];
   final Color BAKCOLOR=new Color(212,110,46);//背景颜色深蓝
   final Color FORCOLOR=Color.black;//前景颜色
   Square m_prePiece[]=new Square[4];//预览区内的方块
   Square m_oldPiece[]=new Square[4];//预览区内的方块
   Color m_colors[];//方块颜色
   Square m_tranPiece[]=new Square[4];

   Frame1 m_tFrame;//声名主窗体变量
   Tetrics tetric;
   boolean m_bNeedNewPiece;//是否产生新方块
   boolean m_bJustupdating=false;//是否刷新游戏状态

    public nextSquare()
    {//构造游戏方法
        super();//将本窗体设置为容器
        setBackground(BAKCOLOR);//设置背景色
        initParam();//游戏初始化
    }
    public nextSquare(Tetrics ters)
    {//构造游戏方法
        super();//将本窗体设置为容器
         tetric=ters;
        setBackground(BAKCOLOR);//设置背景色
        initParam();//游戏初始化
    }


    public nextSquare(Frame1 tFrame)
    {//重载构造游戏方法
        super();//将本窗体设置为容器
        m_tFrame=tFrame;//将游戏状态传给主窗体
        setBackground(BAKCOLOR);//设置背景色
        initParam();//游戏初始化
    }
   public void initParam()
   {
   //设置初始参数
        m_nSqLength=25;
        m_nCols=10;
        m_nRows=18;
        //定义八种颜色
      /* m_colors=new Color[8];
       m_colors[0]=new Color(31,58,173);//深蓝
       m_colors[1]=new Color(255,0,0);//红
       m_colors[2]=new Color(0,200,0);//绿
       m_colors[3]=new Color(0,200,255);//浅蓝

       m_colors[4]=new Color(255,255,0);//黄
       m_colors[5]=new Color(255,150,0);//橙黄
       m_colors[6]=new Color(210,0,240);//紫
       m_colors[7]=new Color(50,180,150);//浅绿*/
      box[0]=Toolkit.getDefaultToolkit().getImage("images/0.png");
   box[1]=Toolkit.getDefaultToolkit().getImage("images/1.png");
   box[2]=Toolkit.getDefaultToolkit().getImage("images/2.png");
   box[3]=Toolkit.getDefaultToolkit().getImage("images/3.png");
   box[4]=Toolkit.getDefaultToolkit().getImage("images/4.png");
   box[5]=Toolkit.getDefaultToolkit().getImage("images/5.png");
   box[6]=Toolkit.getDefaultToolkit().getImage("images/6.png");
   box[7]=Toolkit.getDefaultToolkit().getImage("images/7.png");

       if(m_bNeedNewPiece){
         newPrePiece();
         newTotran();
       }
       m_bGameInPlay=true;
   }

   public void newTotran(){
     for(int i=0;i<4;i++){
       m_tranPiece[i]=m_prePiece[i];
     }
   }
   /**
   * 用来产生一个新的方块用来做预览
  */
   public void newPrePiece(){
    //定位方块出现位置
    int middle=m_nCols/2;
    int top=m_nRows;
    int m_value=(int)(Math.random()*10);
    switch((int) (Math.random()*7))
       {
           case 0:
               //[][][][] 形
           //方块位置形状
           m_prePiece[0]=new Square(middle-1,top,1,m_value);
           m_prePiece[1]=new Square(middle-2,top,1,m_value);
           m_prePiece[2]=new Square(middle,top,1,m_value);
           m_prePiece[3]=new Square(middle+1,top,1,m_value);
           break;

           case 1:
           //  [][][]    形
           //    []
           //方块位置形状
           m_prePiece[0]=new Square(middle,top-1,5,m_value);
           m_prePiece[1]=new Square(middle,top,5,m_value);
           m_prePiece[2]=new Square(middle-1,top-1,5,m_value);
           m_prePiece[3]=new Square(middle+1,top-1,5,m_value);
           break;

           case 2:
               //[][]
       //          [][]     形
           //方块位置形状
           m_prePiece[0]=new Square(middle,top-1,2,m_value);
           m_prePiece[1]=new Square(middle-1,top,2,m_value);
           m_prePiece[2]=new Square(middle,top,2,m_value);
           m_prePiece[3]=new Square(middle+1,top-1,2,m_value);
           break;

           case 3:
               //[][]  形
       //      [][]
           //方块位置形状
           m_prePiece[0]=new Square(middle,top-1,7,m_value);
           m_prePiece[1]=new Square(middle+1,top,7,m_value);
           m_prePiece[2]=new Square(middle,top,7,m_value);
           m_prePiece[3]=new Square(middle-1,top-1,7,m_value);
           break;

           case 4:
               //[][]
       //        [][]    形
           //方块位置形状
           m_prePiece[0]=new Square(middle,top-1,3,m_value);
           m_prePiece[1]=new Square(middle-1,top,3,m_value);
           m_prePiece[2]=new Square(middle-1,top-1,3,m_value);
           m_prePiece[3]=new Square(middle,top,3,m_value);
           break;

           case 5:
               //[][][]
       //            [] 形
           //方块位置形状
           m_prePiece[0]=new Square(middle,top,6,m_value);
           m_prePiece[1]=new Square(middle-1,top,6,m_value);
           m_prePiece[2]=new Square(middle+1,top,6,m_value);
           m_prePiece[3]=new Square(middle+1,top-1,6,m_value);
           break;

           case 6:
               //[][][]
       //        []      形
           //方块位置形状
           m_prePiece[0]=new Square(middle,top,4,m_value);
           m_prePiece[1]=new Square(middle+1,top,4,m_value);
           m_prePiece[2]=new Square(middle-1,top,4,m_value);
           m_prePiece[3]=new Square(middle-1,top-1,4,m_value);
           break;
       }
  }

//更新游戏界面
   public void update(Graphics g){
           m_bJustupdating=true;//游戏更新中
           paint(g); //绘制新游戏页面
   }
   public void run(){
   }
   public void start(){
      m_theThread=new Thread(this);//声名新线程
      newPrePiece();
      newTotran();
   }

//绘制游戏界面
   public synchronized void paint(Graphics g){ //画预览的方块
      int middle=m_nCols/2;//设置方块显示坐标
      int top=m_nRows;
      g.setColor(BAKCOLOR);//设置区域颜色为背景色
      g.fillRect(0,0,m_nSqLength*4,m_nSqLength*5);//填充一个矩形
      if(m_bGameInPlay){
        for (int i = 0; i < 4; i++) {
          if(m_tranPiece[i]!=null){
           // g.setColor(m_colors[m_prePiece[i].m_nColor]); //设置此方块颜色为m_prePiece()产生方块的颜色
           // g.fill3DRect( (m_tranPiece[i].m_nColumn - 3) * m_nSqLength, //绘制立体效果方块
            //             0 - (m_tranPiece[i].m_nRow - top - 2) * m_nSqLength,
               //          m_nSqLength, m_nSqLength, true);
               g.drawImage(box[m_prePiece[i].getColor()],
                      ((m_prePiece[i].m_nColumn - middle) + 2) * m_nSqLength,
                      0 - (m_prePiece[i].m_nRow - top-2) * m_nSqLength,this);

          }
        }
      }
   }
}

⌨️ 快捷键说明

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