欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#游戲開發(fā)之實現(xiàn)俄羅斯方塊游戲

 更新時間:2023年01月05日 08:41:04   作者:芝麻粒兒  
這篇文章主要為大家詳細介紹了C#如何實現(xiàn)經(jīng)典俄羅斯方塊游戲,文中的示例代碼講解詳細,對我們學習C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下

實踐過程

效果

代碼

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    Russia MyRussia = new Russia();//實例化Russia類,用于操作游戲
    Russia TemRussia = new Russia();//實例化Russia類,用于生成下一個方塊樣式
    public static int CakeNO = 0;//記錄下一個方塊樣式的標識
    public static bool become = false;//判斷是否生成下一個方塊的樣式
    public static bool isbegin = false;//判斷當前游戲是否開始
    public bool ispause = true;//判斷是否暫停游戲
    public Timer timer = new Timer();

    private void button1_Click(object sender, EventArgs e)
    {
        MyRussia.ConvertorClear();//清空整個控件
        MyRussia.firstPoi = new Point(140, 20);//設置方塊的起始位置
        label3.Text = "0";//顯示去除的行數(shù)
        label4.Text = "0";//顯示分數(shù)
        MyRussia.Label_Linage = label3;//將label3控件加載到Russia類中
        MyRussia.Label_Fraction = label4;//將label4控件加載到Russia類中
        timer1.Interval = 500;//下移的速度
        timer1.Enabled = false;//停止計時
        timer1.Enabled = true;//開始計時
        Random rand = new Random();//實例化Random
        CakeNO = rand.Next(1, 8);//獲取隨機數(shù)
        MyRussia.CakeMode(CakeNO);//設置方塊的樣式
        MyRussia.Protract(panel1);//繪制組合方塊
        beforehand();//生成下一個方塊的樣式
        MyRussia.PlaceInitialization();//初始化Random類中的信息
        isbegin = true;//判斷是否開始
        ispause = true;
        MyRussia.timer = timer1;

        button2.Text = "暫停";
        ispause = true;
        textBox1.Focus();//獲取焦點
    }

    /// <summary>
    /// 生成下一個方塊的樣式
    /// </summary>
    public void beforehand()
    {
        Graphics P3 = panel3.CreateGraphics();
        P3.FillRectangle(new SolidBrush(Color.Black), 0, 0, panel3.Width, panel3.Height);
        Random rand = new Random();//實例化Random
        CakeNO = rand.Next(1, 8);//獲取隨機數(shù)
        TemRussia.firstPoi = new Point(50, 30);//設置方塊的起始位置
        TemRussia.CakeMode(CakeNO);//設置方塊的樣式
        TemRussia.Protract(panel3);//繪制組合方塊
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (!isbegin)//如果沒有開始游戲
            return;
        if (!ispause)//如果游戲暫停
            return;
        if (e.KeyCode == Keys.Up)//如果當前按下的是↑鍵
            MyRussia.MyConvertorMode();//變換當前方塊的樣式
        if (e.KeyCode == Keys.Down)//如果當前按下的是↓鍵
        {
            timer1.Interval = 300;//增加下移的速度
            MyRussia.ConvertorMove(0);//方塊下移
        }
        if (e.KeyCode == Keys.Left)//如果當前按下的是←鍵
            MyRussia.ConvertorMove(1);//方塊左移
        if (e.KeyCode == Keys.Right)//如果當前按下的是→鍵
            MyRussia.ConvertorMove(2);//方塊右移
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        MyRussia.ConvertorMove(0);//方塊下移
        if (become)//如果顯示新的方塊
        {
            beforehand();//生成下一個方塊
            become = false;
        }
        textBox1.Focus();//獲取焦點
    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        if (!isbegin)//如果游戲沒有開始
            return;
        if (!ispause)//如果暫停游戲
            return;
        if (e.KeyCode == Keys.Down)//如果當前松開的是↓鍵
        {
            timer1.Interval = 500;//恢復下移的速度
        }
        textBox1.Focus();//獲取焦點
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (timer1.Enabled == true)
        {
            timer1.Stop();//暫停
            button2.Text = "繼續(xù)";
            ispause = false;
            textBox1.Focus();//獲取焦點
        }
        else
        {
            timer1.Start();//繼續(xù)
            button2.Text = "暫停";
            ispause = true;
            textBox1.Focus();//獲取焦點
        }
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        if (isbegin)//如是游戲開始
        {
            //重繪背景上的方塊
            for (int i = 0; i <= (panel1.Width / 20 - 1); i++)
            {
                for (int j = 0; j <= (panel1.Height / 20 - 1); j++)
                {
                    Rectangle rect = new Rectangle(i * 20 + 1, j * 20 + 1, 19, 19);//獲取各方塊的繪制區(qū)域
                    e.Graphics.FillRectangle(new SolidBrush(Russia.PlaceColor[i, j]), rect);//繪制方塊
                }
            }
        }
    }

    private void panel3_Paint(object sender, PaintEventArgs e)
    {
        if (isbegin)//如果游戲開始
        {
            TemRussia.firstPoi = new Point(50, 30);//設置方塊的起始位置
            TemRussia.CakeMode(CakeNO);//設置方塊的樣式
            TemRussia.Protract(panel3);//繪制組合方塊
        }
    }
}
partial class Form1
{
    /// <summary>
    /// 必需的設計器變量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的資源。
    /// </summary>
    /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗體設計器生成的代碼

    /// <summary>
    /// 設計器支持所需的方法 - 不要
    /// 使用代碼編輯器修改此方法的內容。
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.panel1 = new System.Windows.Forms.Panel();
        this.button1 = new System.Windows.Forms.Button();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.button2 = new System.Windows.Forms.Button();
        this.panel2 = new System.Windows.Forms.Panel();
        this.panel3 = new System.Windows.Forms.Panel();
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.label3 = new System.Windows.Forms.Label();
        this.label4 = new System.Windows.Forms.Label();
        this.panel2.SuspendLayout();
        this.SuspendLayout();
        // 
        // panel1
        // 
        this.panel1.BackColor = System.Drawing.SystemColors.WindowText;
        this.panel1.Location = new System.Drawing.Point(4, 5);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(281, 401);
        this.panel1.TabIndex = 0;
        this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(291, 341);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 1;
        this.button1.Text = "開始";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(291, 480);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(10, 21);
        this.textBox1.TabIndex = 2;
        this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
        this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
        // 
        // timer1
        // 
        this.timer1.Interval = 300;
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(291, 370);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(75, 23);
        this.button2.TabIndex = 3;
        this.button2.Text = "暫停";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // panel2
        // 
        this.panel2.BackColor = System.Drawing.Color.Black;
        this.panel2.Controls.Add(this.label4);
        this.panel2.Controls.Add(this.label3);
        this.panel2.Controls.Add(this.label2);
        this.panel2.Controls.Add(this.label1);
        this.panel2.Controls.Add(this.panel3);
        this.panel2.Location = new System.Drawing.Point(291, 5);
        this.panel2.Name = "panel2";
        this.panel2.Size = new System.Drawing.Size(120, 308);
        this.panel2.TabIndex = 4;
        // 
        // panel3
        // 
        this.panel3.Location = new System.Drawing.Point(10, 10);
        this.panel3.Name = "panel3";
        this.panel3.Size = new System.Drawing.Size(100, 100);
        this.panel3.TabIndex = 0;
        this.panel3.Paint += new System.Windows.Forms.PaintEventHandler(this.panel3_Paint);
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Font = new System.Drawing.Font("宋體", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.label1.ForeColor = System.Drawing.SystemColors.Window;
        this.label1.Location = new System.Drawing.Point(4, 148);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(59, 16);
        this.label1.TabIndex = 1;
        this.label1.Text = "行數(shù):";
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Font = new System.Drawing.Font("宋體", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.label2.ForeColor = System.Drawing.Color.White;
        this.label2.Location = new System.Drawing.Point(4, 193);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(59, 16);
        this.label2.TabIndex = 2;
        this.label2.Text = "分數(shù):";
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Font = new System.Drawing.Font("宋體", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.label3.ForeColor = System.Drawing.Color.White;
        this.label3.Location = new System.Drawing.Point(48, 150);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(15, 14);
        this.label3.TabIndex = 3;
        this.label3.Text = "0";
        // 
        // label4
        // 
        this.label4.AutoSize = true;
        this.label4.Font = new System.Drawing.Font("宋體", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.label4.ForeColor = System.Drawing.Color.White;
        this.label4.Location = new System.Drawing.Point(48, 195);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(15, 14);
        this.label4.TabIndex = 4;
        this.label4.Text = "0";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(415, 411);
        this.Controls.Add(this.panel2);
        this.Controls.Add(this.button2);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.panel1);
        this.MaximizeBox = false;
        this.Name = "Form1";
        this.Text = "俄羅斯方塊";
        this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
        this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
        this.panel2.ResumeLayout(false);
        this.panel2.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Timer timer1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Panel panel2;
    private System.Windows.Forms.Panel panel3;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label4;
}
class Russia
{
    public Point firstPoi = new Point(140, 20);//定義方塊的起始位置
    public static Color[,] PlaceColor;//記錄方塊的位置
    public static bool[,] Place;//記錄方塊的位置
    public static int conWidth = 0;//記錄列數(shù)
    public static int conHeight = 0;//記錄行數(shù)
    public static int maxY = 0;//方塊在行中的最小高度
    public static int conMax = 0;//方塊落下后的最大位置
    public static int conMin = 0;//方塊落下后的最小位置
    bool[] tem_Array = { false, false, false, false };//記錄方塊組中那一塊所在行中已滿
    Color ConColor = Color.Coral;
    Point[] ArryPoi = new Point[4];//方塊的數(shù)組
    Point[] Arryfront = new Point[4];//前一個方塊的數(shù)組
    int Cake = 20;//定義方塊的大小
    int Convertor = 0;//變換器
    Control Mycontrol = new Control();//實例化Control
    public Label Label_Linage = new Label();//實例化Label,用于顯示去除的行數(shù)
    public Label Label_Fraction = new Label();//實例化Label,用于顯示分數(shù)
    public static int[] ArrayCent = new int[] { 2, 5, 9, 15 };//記錄加分情況
    public Timer timer = new Timer();

    /// <summary>
    /// 設置方塊的樣式
    /// </summary>
    /// <param n="int">標識,方塊的樣式</param>
    public void CakeMode(int n)
    {
        ArryPoi[0] = firstPoi;//記錄方塊的起始位置
        switch (n)//根據(jù)標識設置方塊的樣式
        {
            case 1://組合“L”方塊
                {
                    ArryPoi[1] = new Point(firstPoi.X, firstPoi.Y - Cake);//設置第二塊方塊的位置
                    ArryPoi[2] = new Point(firstPoi.X, firstPoi.Y + Cake);//設置第三塊方塊的位置
                    ArryPoi[3] = new Point(firstPoi.X + Cake, firstPoi.Y + Cake);//設置第四塊方塊的位置
                    ConColor = Color.Fuchsia;//設置當前方塊的顏色
                    Convertor = 2;//記錄方塊的變換樣式
                    break;
                }
            case 2://組合“Z”方塊
                {
                    ArryPoi[1] = new Point(firstPoi.X, firstPoi.Y - Cake);
                    ArryPoi[2] = new Point(firstPoi.X - Cake, firstPoi.Y - Cake);
                    ArryPoi[3] = new Point(firstPoi.X + Cake, firstPoi.Y);
                    ConColor = Color.Yellow;
                    Convertor = 6;
                    break;
                }
            case 3://組合倒“L”方塊
                {
                    ArryPoi[1] = new Point(firstPoi.X, firstPoi.Y - Cake);
                    ArryPoi[2] = new Point(firstPoi.X, firstPoi.Y + Cake);
                    ArryPoi[3] = new Point(firstPoi.X - Cake, firstPoi.Y + Cake);
                    ConColor = Color.CornflowerBlue;
                    Convertor = 8;
                    break;
                }
            case 4://組合倒“Z”方塊
                {
                    ArryPoi[1] = new Point(firstPoi.X, firstPoi.Y - Cake);
                    ArryPoi[2] = new Point(firstPoi.X + Cake, firstPoi.Y - Cake);
                    ArryPoi[3] = new Point(firstPoi.X - Cake, firstPoi.Y);
                    ConColor = Color.Blue;
                    Convertor = 12;
                    break;
                }
            case 5://組合“T”方塊
                {
                    ArryPoi[1] = new Point(firstPoi.X, firstPoi.Y - Cake);
                    ArryPoi[2] = new Point(firstPoi.X + Cake, firstPoi.Y - Cake);
                    ArryPoi[3] = new Point(firstPoi.X - Cake, firstPoi.Y - Cake);
                    ConColor = Color.Silver;
                    Convertor = 14;
                    break;
                }
            case 6://組合“一”方塊
                {
                    ArryPoi[1] = new Point(firstPoi.X + Cake, firstPoi.Y);
                    ArryPoi[2] = new Point(firstPoi.X - Cake, firstPoi.Y);
                    ArryPoi[3] = new Point(firstPoi.X - Cake*2, firstPoi.Y);
                    ConColor = Color.Red;
                    Convertor = 18;
                    break;
                }
            case 7://組合“田”方塊
                {
                    ArryPoi[1] = new Point(firstPoi.X - Cake, firstPoi.Y);
                    ArryPoi[2] = new Point(firstPoi.X - Cake, firstPoi.Y - Cake);
                    ArryPoi[3] = new Point(firstPoi.X, firstPoi.Y - Cake);
                    ConColor = Color.LightGreen;
                    Convertor = 19;
                    break;
                }
        }
    }

    /// <summary>
    /// 清空游戲背景
    /// </summary>
    public void ConvertorClear()
    {
        if (Mycontrol != null)//如要已載入背景控件
        {
            Graphics g = Mycontrol.CreateGraphics();//創(chuàng)建背景控件的Graphics類
            Rectangle rect = new Rectangle(0, 0, Mycontrol.Width, Mycontrol.Height);//獲取背景的區(qū)域
            MyPaint(g, new SolidBrush(Color.Black), rect);//用背景色填充背景
        }
    }

    /// <summary>
    /// 清空當前方塊的區(qū)域
    /// </summary>
    public void ConvertorDelete()
    {
        Graphics g = Mycontrol.CreateGraphics();//創(chuàng)建背景控件的Graphics類
        for (int i = 0; i < ArryPoi.Length; i++)//遍歷方塊的各個子方塊
        {
            Rectangle rect = new Rectangle(ArryPoi[i].X, ArryPoi[i].Y, 20, 20);//獲取各子方塊的區(qū)域
            MyPaint(g, new SolidBrush(Color.Black), rect);//用背景色填充背景
        }
    }

    /// <summary>
    /// 變換當前方塊的樣式
    /// </summary>
    public void MyConvertorMode()
    {
        ConvertorDelete();//清空當前方塊的區(qū)域
        ConvertorMode(Convertor);//設置方塊的變換樣式
        Protract(Mycontrol);//繪制變換后的組合方塊
    }

    /// <summary>
    /// 設置方塊的變換樣式
    /// </summary>
    /// <param n="int">標識,判斷變換的樣式</param>
    public void ConvertorMode(int n)
    {
        Point[] tem_ArrayPoi = new Point[4];//定義一個臨時數(shù)組
        Point tem_Poi = firstPoi;//獲取方塊的起始位置
        int tem_n = n;//記錄方塊的下一個變換樣式
        //將當前方塊的位置存入到臨時數(shù)組中
        for (int i = 0; i < tem_ArrayPoi.Length; i++)
            tem_ArrayPoi[i] = ArryPoi[i];
        switch (n)//根據(jù)標識變換方塊的樣式
        {
            case 1://設置“L”方塊的起始樣式
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X, tem_Poi.Y - Cake);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X, tem_Poi.Y + Cake);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X + Cake, tem_Poi.Y + Cake);
                    tem_n = 2;//記錄變換樣式的標志
                    break;
                }
            case 2://“L”方塊向旋轉的樣式
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X - Cake, tem_Poi.Y);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X + Cake, tem_Poi.Y);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X + Cake, tem_Poi.Y - Cake);
                    tem_n = 3;
                    break;
                }
            case 3://“L”方塊向旋轉的樣式
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X, tem_Poi.Y - Cake);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X - Cake, tem_Poi.Y - Cake);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X, tem_Poi.Y + Cake);
                    tem_n = 4;
                    break;
                }
            case 4://“L”方塊向旋轉的樣式
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X + Cake, tem_Poi.Y);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X - Cake, tem_Poi.Y);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X - Cake, tem_Poi.Y + Cake);
                    tem_n = 1;//返回方塊的起始樣式
                    break;
                }
            case 5://Z
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X, tem_Poi.Y - Cake);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X - Cake, tem_Poi.Y - Cake);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X + Cake, tem_Poi.Y);
                    tem_n = 6;
                    break;
                }
            case 6:
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X + Cake, tem_Poi.Y);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X + Cake, tem_Poi.Y - Cake);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X, tem_Poi.Y + Cake);
                    tem_n = 5;
                    break;
                }
            case 7://倒L
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X, tem_Poi.Y - Cake);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X, tem_Poi.Y + Cake);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X - Cake, tem_Poi.Y + Cake);
                    tem_n = 8;
                    break;
                }
            case 8:
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X - Cake, tem_Poi.Y);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X + Cake, tem_Poi.Y);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X + Cake, tem_Poi.Y + Cake);
                    tem_n = 9;
                    break;
                }
            case 9:
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X, tem_Poi.Y - Cake);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X, tem_Poi.Y + Cake);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X + Cake, tem_Poi.Y - Cake);
                    tem_n = 10;
                    break;
                }
            case 10:
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X - Cake, tem_Poi.Y);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X + Cake, tem_Poi.Y);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X - Cake, tem_Poi.Y - Cake);
                    tem_n = 7;
                    break;
                }
            case 11://倒Z
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X, tem_Poi.Y - Cake);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X + Cake, tem_Poi.Y - Cake);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X - Cake, tem_Poi.Y);
                    tem_n = 12;
                    break;
                }
            case 12:
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X - Cake, tem_Poi.Y);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X - Cake, tem_Poi.Y - Cake);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X, tem_Poi.Y + Cake);
                    tem_n = 11;
                    break;
                }
            case 13://T
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X, tem_Poi.Y - Cake);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X + Cake, tem_Poi.Y - Cake);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X - Cake, tem_Poi.Y - Cake);
                    tem_n = 14;
                    break;
                }
            case 14:
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X, tem_Poi.Y - Cake);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X, tem_Poi.Y + Cake);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X + Cake, tem_Poi.Y);
                    tem_n = 15;
                    break;
                }
            case 15:
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X - Cake, tem_Poi.Y);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X + Cake, tem_Poi.Y);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X, tem_Poi.Y - Cake);
                    tem_n = 16;
                    break;
                }
            case 16:
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X, tem_Poi.Y - Cake);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X - Cake, tem_Poi.Y);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X, tem_Poi.Y + Cake);
                    tem_n = 13;
                    break;
                }
            case 17://一
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X + Cake, tem_Poi.Y);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X - Cake, tem_Poi.Y);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X - Cake * 2, tem_Poi.Y);
                    tem_n = 18;
                    break;
                }
            case 18:
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X, tem_Poi.Y - Cake);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X, tem_Poi.Y + Cake);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X, tem_Poi.Y + Cake * 2);
                    tem_n = 17;
                    break;
                }
            case 19://田
                {
                    tem_ArrayPoi[1] = new Point(tem_Poi.X - Cake, tem_Poi.Y);
                    tem_ArrayPoi[2] = new Point(tem_Poi.X - Cake, tem_Poi.Y - Cake);
                    tem_ArrayPoi[3] = new Point(tem_Poi.X, tem_Poi.Y - Cake);
                    tem_n = 19;
                    break;
                }
        }
        bool tem_bool = true;//判斷方塊是否可變
        //遍歷方塊的各個子方塊
        for (int i = 0; i < tem_ArrayPoi.Length; i++)
        {
            if (tem_ArrayPoi[i].X / 20 < 0)//變換后是否超出左邊界
            {
                tem_bool = false;//不變換
                break;
            }
            if (tem_ArrayPoi[i].X / 20 >= conWidth)//變換后是否超出右邊界
            {
                tem_bool = false;
                break;
            }
            if (tem_ArrayPoi[i].Y / 20 >= conHeight)//變換后是否超出下邊界
            {
                tem_bool = false;
                break;
            }
            if (Place[tem_ArrayPoi[i].X / 20, tem_ArrayPoi[i].Y / 20])//變換后是否與其他方塊重疊
            {
                tem_bool = false;
                break;
            }
        }
        if (tem_bool)//如果當前方塊可以變換
        {
            //改變當前方塊的樣式
            for (int i = 0; i < tem_ArrayPoi.Length; i++)
                ArryPoi[i] = tem_ArrayPoi[i];
            firstPoi = tem_Poi;//獲取當前方塊的起始位置
            Convertor = tem_n;//獲取方塊下一次的變換樣式
        }
    }

    /// <summary>
    /// 繪制組合方塊
    /// </summary>
    /// <param control="Control">控件</param>
    public void Protract(Control control)
    {
        Mycontrol = control;
        Graphics g = control.CreateGraphics();//創(chuàng)建背景控件的Graphics類
        //繪制方塊的各個子方塊
        for (int i = 0; i < ArryPoi.Length; i++)
        {
            Rectangle rect = new Rectangle(ArryPoi[i].X + 1, ArryPoi[i].Y + 1, 19, 19);//獲取子方塊的區(qū)域
            MyPaint(g, new SolidBrush(ConColor), rect);//繪制子方塊
        }
    }

    /// <summary>
    /// 對方塊的單個塊進行繪制
    /// </summary>
    /// <param g="Graphics">封裝一個繪圖的類對象</param>
    /// <param SolidB="SolidBrush">畫刷</param>
    /// <param rect="Rectangle">繪制區(qū)域</param>
    public void MyPaint(Graphics g, SolidBrush SolidB, Rectangle rect)
    {
        g.FillRectangle(SolidB, rect);//填充一個矩形
    }

    /// <summary>
    /// 方塊移動
    /// </summary>
    /// <param n="int">標識,對左右下進行判斷</param>
    public void ConvertorMove(int n)
    {
        //記錄方塊移動前的位置
        for (int i = 0; i < Arryfront.Length; i++)
            Arryfront[i] = ArryPoi[i];
        switch (n)//方塊的移動方向
        {
            case 0://下移
                {
                    //遍歷方塊中的子方塊
                    for (int i = 0; i < Arryfront.Length; i++)
                        Arryfront[i] = new Point(Arryfront[i].X, Arryfront[i].Y + Cake);//使各子方塊下移一個方塊位
                    break;
                }
            case 1://左移
                {
                    for (int i = 0; i < Arryfront.Length; i++)
                        Arryfront[i] = new Point(Arryfront[i].X - Cake, Arryfront[i].Y);
                    break;
                }
            case 2://右移
                {
                    for (int i = 0; i < Arryfront.Length; i++)
                        Arryfront[i] = new Point(Arryfront[i].X + Cake, Arryfront[i].Y);
                    
                    break;
                }
        }

        bool tem_bool = MoveStop(n);//記錄方塊移動后是否出邊界
        if (tem_bool)//如果沒有出邊界
        {
            ConvertorDelete();//清空當前方塊的區(qū)域
            //獲取移動后方塊的位置
            for (int i = 0; i < Arryfront.Length; i++)
                ArryPoi[i] = Arryfront[i];
            firstPoi = ArryPoi[0];//記錄方塊的起始位置
            Protract(Mycontrol);//繪制移動后方塊
        }
        else//如果方塊到達底部
        {
            if (!tem_bool && n == 0)//如果當前方塊是下移
            {
                conMax = 0;//記錄方塊落下后的頂端位置
                conMin = Mycontrol.Height;//記錄方塊落下后的底端位置
                //遍歷方塊的各個子方塊
                for (int i = 0; i < ArryPoi.Length; i++)
                {
                    if (ArryPoi[i].Y < maxY)//記錄方塊的頂端位置
                        maxY = ArryPoi[i].Y;
                    Place[ArryPoi[i].X / 20, ArryPoi[i].Y / 20] = true;//記錄指定的位置已存在方塊
                    PlaceColor[ArryPoi[i].X / 20, ArryPoi[i].Y / 20] = ConColor;//記錄方塊的顏芭
                    if (ArryPoi[i].Y > conMax)//記錄方塊的頂端位置
                        conMax = ArryPoi[i].Y;
                    if (ArryPoi[i].Y < conMin)//記錄方塊的底端位置
                        conMin = ArryPoi[i].Y;
                }
                if (firstPoi.X == 140 && firstPoi.Y == 20)
                {
                    timer.Stop();
                    Form1.isbegin = false;
                    return;
                }

                Random rand = new Random();//實例化Random
                int CakeNO = rand.Next(1, 8);//獲取隨機數(shù)
                firstPoi = new Point(140, 20);//設置方塊的起始位置
                CakeMode(Form1.CakeNO);//設置方塊的樣式
                Protract(Mycontrol);//繪制組合方塊
                RefurbishRow(conMax,conMin);//去除已填滿的行
                Form1.become = true;//標識,判斷可以生成下一個方塊
            }
        }
    }

    /// <summary>
    /// 去除已添滿的行
    /// </summary>
    public void RefurbishRow(int Max,int Min)
    {
        Graphics g = Mycontrol.CreateGraphics();//創(chuàng)建背景控件的Graphics類
        int tem_max = Max / 20;//獲取方塊的最大位置在多少行
        int tem_min = Min / 20;//獲取方塊的最小位置在多少行
        bool tem_bool = false;
        //初始化記錄刷新行的數(shù)組
        for (int i = 0; i < tem_Array.Length; i++)
            tem_Array[i] = false;
        int tem_n = maxY;//記錄最高行的位置
        for (int i = 0; i < 4; i++)//查找要刷新的行
        {
            if ((tem_min + i) > 19)//如果超出邊界
                break;//退出本次操作
            tem_bool = false;
            //如果當前行中有空格
            for (int k = 0; k < conWidth; k++)
            {
                if (!Place[k, tem_min + i])//如果當前位置為空
                {
                    tem_bool = true;
                    break;
                }
            }
            if (!tem_bool)//如要當行為滿行
            {
                tem_Array[i] = true;//記錄為刷新行
            }
        }

        int Progression = 0;//記錄去除的幾行
        if (tem_Array[0] == true || tem_Array[1] == true || tem_Array[2] == true || tem_Array[3] == true)//如果有刷新行
        {
            int Trow = 0;//記錄最小行數(shù)
            for (int i = (tem_Array.Length - 1); i >= 0; i--)//遍歷記錄刷新行的數(shù)組
            {
                if (tem_Array[i])//如果是刷新行
                {
                    Trow = Min / 20 + i;//記錄最小行數(shù)
                    //將刷新行到背景頂端的區(qū)域下移
                    for (int j = Trow; j >=1 ; j--)
                    {
                        for (int k = 0; k < conWidth; k++)
                        {
                            PlaceColor[k, j] = PlaceColor[k, j - 1];//記錄方塊的位置
                            Place[k, j] = Place[k, j - 1];//記錄方塊的位置
                        }
                    }
                    Min += 20;//方塊的最小位置下移一個方塊位
                    //將背景的頂端清空
                    for (int k = 0; k < conWidth; k++)
                    {
                        PlaceColor[k, 0] = Color.Black;//記錄方塊的位置
                        Place[k, 0] = false;//記錄方塊的位置
                    }
                    Progression += 1;//記錄刷新的行數(shù)
                }
            }

            //在背景中繪制刷新后的方塊圖案
            for (int i = 0; i < conWidth; i++)
            {
                for (int j = 0; j <= Max / 20; j++)
                {
                    Rectangle rect = new Rectangle(i * Cake + 1, j * Cake + 1, 19, 19);//獲取各方塊的區(qū)域
                    MyPaint(g, new SolidBrush(PlaceColor[i, j]), rect);//繪制已落下的方塊
                }
            }
            //顯示當前的刷新行數(shù)
            Label_Linage.Text = Convert.ToString(Convert.ToInt32(Label_Linage.Text) + Progression);
            //顯示當前的得分情況
            Label_Fraction.Text = Convert.ToString(Convert.ToInt32(Label_Fraction.Text) + ArrayCent[Progression - 1]);
        }
    }

    /// <summary>
    /// 對信息進行初始化
    /// </summary>
    public void PlaceInitialization()
    {
        conWidth=Mycontrol.Width / 20;//獲取背景的總行數(shù)
        conHeight = Mycontrol.Height / 20;//獲取背景的總列數(shù)
        Place = new bool[conWidth, conHeight];//定義記錄各方塊位置的數(shù)組
        PlaceColor = new Color[conWidth, conHeight];//定義記錄各方塊顏色的數(shù)組
        //對各方塊的信息進行初始化
        for (int i = 0; i < conWidth; i++)
        {
            for (int j = 0; j < conHeight; j++)
            {
                Place[i, j] = false;//方塊為空
                PlaceColor[i, j] = Color.Black;//與背景色相同
            }
        }
        maxY = conHeight * Cake;//記錄方塊的最大值
    }

    /// <summary>
    /// 判斷方塊移動時是否出邊界
    /// </summary>
    public bool MoveStop(int n)
    {
        bool tem_bool = true;
        int tem_width = 0;
        int tem_height = 0;
        switch (n)
        {
            case 0://下移
                {
                    //遍歷方塊中的各個子方塊
                    for (int i = 0; i < Arryfront.Length; i++)
                    {
                        tem_width = Arryfront[i].X / 20;//獲取方塊的橫向坐標值
                        tem_height = Arryfront[i].Y / 20;//獲取方塊的縱向坐標值
                        if (tem_height == conHeight || Place[tem_width, tem_height])//判斷是否超出底邊界,或是與其他方塊重疊
                            tem_bool = false;//超出邊界
                    }
                    break;
                }
            case 1://左移
                {
                    for (int i = 0; i < Arryfront.Length; i++)
                    {
                        tem_width = Arryfront[i].X / 20;
                        tem_height = Arryfront[i].Y / 20;
                        if (tem_width == -1 || Place[tem_width, tem_height])//判斷是否超出左邊界,或是與其他方塊重疊
                            tem_bool = false;
                    }
                    break;
                }
            case 2://右移
                {
                    for (int i = 0; i < Arryfront.Length; i++)
                    {
                        tem_width = Arryfront[i].X / 20;
                        tem_height = Arryfront[i].Y / 20;
                        if (tem_width == conWidth || Place[tem_width, tem_height])//判斷是否超出右邊界,或是與其他方塊重疊
                            tem_bool = false;
                    }
                    break;
                }
        }
        return tem_bool;
    }
    
}

到此這篇關于C#游戲開發(fā)之實現(xiàn)俄羅斯方塊游戲的文章就介紹到這了,更多相關C#俄羅斯方塊內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C# wpf 實現(xiàn)窗口任意區(qū)域點擊拖動

    C# wpf 實現(xiàn)窗口任意區(qū)域點擊拖動

    在wpf要實現(xiàn)此功能簡單形式還是比較容易的,但是有一些細節(jié)需要專門處理,比如與按鈕的點擊事件沖突問題,解決事件沖突問題后拖動的靈敏度,可復用性等,這篇文章主要介紹了C# wpf 實現(xiàn)窗口任意區(qū)域點擊拖動,需要的朋友可以參考下
    2024-03-03
  • c#編寫的高并發(fā)數(shù)據(jù)庫控制訪問代碼

    c#編寫的高并發(fā)數(shù)據(jù)庫控制訪問代碼

    往往大數(shù)據(jù)量,高并發(fā)時, 瓶頸都在數(shù)據(jù)庫上, 好多人都說用數(shù)據(jù)庫的復制,發(fā)布, 讀寫分離等技術, 但主從數(shù)據(jù)庫之間同步時間有延遲.
    2015-03-03
  • 詳解C#設計模式編程中生成器模式的使用

    詳解C#設計模式編程中生成器模式的使用

    這篇文章主要介紹了詳解C#設計模式編程中生成器模式的使用,生成器模式主張創(chuàng)建對象的過程和對象的表現(xiàn)應該分離開來,需要的朋友可以參考下
    2016-02-02
  • C#實現(xiàn)線段樹的示例代碼

    C#實現(xiàn)線段樹的示例代碼

    線段樹是一種常用來維護區(qū)間信息的數(shù)據(jù)結構,本文主要介紹了C#實現(xiàn)線段樹的示例代碼,具有一定的參考價值,感興趣的可以了解一下
    2023-11-11
  • c#版json數(shù)據(jù)解析示例分享

    c#版json數(shù)據(jù)解析示例分享

    JSON(全稱為JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式。它是基于JavaScript語法標準的一個子集。 JSON采用完全獨立于語言的文本格式,可以很容易在各種網(wǎng)絡、平臺和程序之間傳輸。JSON的語法很簡單,易于人閱讀和編寫,同時也易于機器解析和生成
    2014-03-03
  • C#連接數(shù)據(jù)庫的方法

    C#連接數(shù)據(jù)庫的方法

    ASP.NET連接數(shù)據(jù)庫的技術叫ADO.NET,它是用來向數(shù)據(jù)庫提交sql語句的一堆類。這里連接的是Sql Server 2008數(shù)據(jù)庫,其他數(shù)據(jù)庫用法差不多,就是調用的類名不一樣
    2015-11-11
  • C#中的timer與線程使用

    C#中的timer與線程使用

    這篇文章主要介紹了C#中的timer與線程使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • c#二維碼生成的代碼分享

    c#二維碼生成的代碼分享

    c#生成二維碼實現(xiàn)示例代碼分享,生成方法是調用外網(wǎng)API,為了不直接調用別人的接口,創(chuàng)建一個QrImg.aspx用于顯示二維碼,傳參數(shù)即可
    2013-12-12
  • C#轉換日期類型實例

    C#轉換日期類型實例

    這篇文章主要介紹了C#轉換日期類型的方法,以實例形式分析了將日期格式轉換為Unix時間戳與時區(qū)結合的形式,是比較實用的技巧,具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-12-12
  • C# 反射(Reflection)的用處分析

    C# 反射(Reflection)的用處分析

    反射(Reflection)是C#里很重要的一個特性,其它語言也有這個特性,比如JAVA。反射這個特性是很實用的,如果使用過struts, hibernate, spring等等這些框架的話,便會知道反射這個特性是多么的強大了。在我接觸過的那些框架中,沒有一個框架是不使用反射的。
    2015-03-03

最新評論