C#语言实例源码系列-游戏-实现俄罗斯方块

news/2024/11/25 17:56:52/
专栏分享
  • 点击跳转=>Unity3D特效百例
  • 点击跳转=>案例项目实战源码
  • 点击跳转=>游戏脚本-辅助自动化
  • 点击跳转=>Android控件全解手册

👉关于作者

众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣 !!!
专注于Android/Unity和各种游戏开发技巧,以及各种资源分享(网站、工具、素材、源码、游戏等)
有什么需要欢迎私我,交流群让学习不再孤单

在这里插入图片描述

👉实践过程

😜效果

在这里插入图片描述

😜代码

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";//显示去除的行数label4.Text = "0";//显示分数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();//实例化RandomCakeNO = rand.Next(1, 8);//获取随机数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();//实例化RandomCakeNO = rand.Next(1, 8);//获取随机数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 = "继续";ispause = false;textBox1.Focus();//获取焦点}else{timer1.Start();//继续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);//获取各方块的绘制区域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 = "行数:";// // 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 = "分数:";// // 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();}#endregionprivate 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;//记录列数public static int conHeight = 0;//记录行数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];//方块的数组Point[] Arryfront = new Point[4];//前一个方块的数组int Cake = 20;//定义方块的大小int Convertor = 0;//变换器Control Mycontrol = new Control();//实例化Controlpublic Label Label_Linage = new Label();//实例化Label,用于显示去除的行数public Label Label_Fraction = new Label();//实例化Label,用于显示分数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)//根据标识设置方块的样式{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();//创建背景控件的Graphics类Rectangle rect = new Rectangle(0, 0, Mycontrol.Width, Mycontrol.Height);//获取背景的区域MyPaint(g, new SolidBrush(Color.Black), rect);//用背景色填充背景}}/// <summary>/// 清空当前方块的区域/// </summary>public void ConvertorDelete(){Graphics g = Mycontrol.CreateGraphics();//创建背景控件的Graphics类for (int i = 0; i < ArryPoi.Length; i++)//遍历方块的各个子方块{Rectangle rect = new Rectangle(ArryPoi[i].X, ArryPoi[i].Y, 20, 20);//获取各子方块的区域MyPaint(g, new SolidBrush(Color.Black), rect);//用背景色填充背景}}/// <summary>/// 变换当前方块的样式/// </summary>public void MyConvertorMode(){ConvertorDelete();//清空当前方块的区域ConvertorMode(Convertor);//设置方块的变换样式Protract(Mycontrol);//绘制变换后的组合方块}/// <summary>/// 设置方块的变换样式/// </summary>/// <param n="int">标识,判断变换的样式</param>public void ConvertorMode(int n){Point[] tem_ArrayPoi = new Point[4];//定义一个临时数组Point tem_Poi = firstPoi;//获取方块的起始位置int tem_n = n;//记录方块的下一个变换样式//将当前方块的位置存入到临时数组中for (int i = 0; i < tem_ArrayPoi.Length; i++)tem_ArrayPoi[i] = ArryPoi[i];switch (n)//根据标识变换方块的样式{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();//创建背景控件的Graphics类//绘制方块的各个子方块for (int i = 0; i < ArryPoi.Length; i++){Rectangle rect = new Rectangle(ArryPoi[i].X + 1, ArryPoi[i].Y + 1, 19, 19);//获取子方块的区域MyPaint(g, new SolidBrush(ConColor), rect);//绘制子方块}}/// <summary>/// 对方块的单个块进行绘制/// </summary>/// <param g="Graphics">封装一个绘图的类对象</param>/// <param SolidB="SolidBrush">画刷</param>/// <param rect="Rectangle">绘制区域</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();//清空当前方块的区域//获取移动后方块的位置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();//实例化Randomint CakeNO = rand.Next(1, 8);//获取随机数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();//创建背景控件的Graphics类int tem_max = Max / 20;//获取方块的最大位置在多少行int tem_min = Min / 20;//获取方块的最小位置在多少行bool tem_bool = false;//初始化记录刷新行的数组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;//记录最小行数for (int i = (tem_Array.Length - 1); i >= 0; i--)//遍历记录刷新行的数组{if (tem_Array[i])//如果是刷新行{Trow = Min / 20 + i;//记录最小行数//将刷新行到背景顶端的区域下移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;//记录刷新的行数}}//在背景中绘制刷新后的方块图案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);//获取各方块的区域MyPaint(g, new SolidBrush(PlaceColor[i, j]), rect);//绘制已落下的方块}}//显示当前的刷新行数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;//获取背景的总行数conHeight = Mycontrol.Height / 20;//获取背景的总列数Place = new bool[conWidth, conHeight];//定义记录各方块位置的数组PlaceColor = new Color[conWidth, conHeight];//定义记录各方块颜色的数组//对各方块的信息进行初始化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;}}

需要的再直接Call我下方卡片,直接发。

👉其他

📢作者:小空和小芝中的小空
📢转载说明-务必注明来源:https://zhima.blog.csdn.net/
📢这位道友请留步☁️,我观你气度不凡,谈吐间隐隐有王者霸气💚,日后定有一番大作为📝!!!旁边有点赞👍收藏🌟今日传你,点了吧,未来你成功☀️,我分文不取,若不成功⚡️,也好回来找我。

温馨提示点击下方卡片获取更多意想不到的资源。
空名先生


http://www.ppmy.cn/news/9474.html

相关文章

responseType几种类型

响应responseType xhr.response的数据类型 responseType值 xhr.response 数据类型 说明 responseType值xhr.response 数据类型说明" "String字符串默认值(在不设置responseType时)textString字符串–documentDocument对象希望返回 XML 格式数据时使用jsonjavascript…

JAVA mongodb 聚合查询

区分先后条件顺序&#xff0c;这是个坑&#xff01; /*** 测试聚合*/Testpublic void testAggregation() {String format_DateTime "yyyy-MM-dd HH:mm:ss";DateTimeFormatter df DateTimeFormatter.ofPattern(format_DateTime);LocalDateTime localDateTime Local…

【网络安全管理员三级鉴定题】二十四

91.UDP协议是一个面向连接的协议 A.正确B. 错误 参考答案:B 92.交换机具备自动识别数据包发送和到达地址的功能。 A.正确B. 错误 参考答案:B 93.计算机的发展按其使用的逻辑器件划分为四代。 A.正确B. 错误 参考答案:A 94.Trunk类型的端口可以允许多个VLAN通过,可以接收和发…

PYNQ-Z2 开发板

1. 官方手册写的挺全&#xff0c;了解一下PYNQ-Z2 设置指南 https://pynq.readthedocs.io/en/latest/getting_started/pynq_z2_setup.htmlPYNQ-Z2 Reference Manual v1.0 https://www.mouser.com/datasheet/2/744/pynqz2_user_manual_v1_0-1525725.pdfpynq&#xff08;Python O…

机器学习--决策树、线性模型、随机梯度下降

目录 一、决策树 二、线性模型 三、随机梯度下降 一、决策树 决策树&#xff08;decision tree&#xff09;&#xff1a;是一种基本的分类与回归方法&#xff0c;此处主要讨论分类的决策树。 在分类问题中&#xff0c;表示基于特征对实例进行分类的过程&#xff0c;可以认为…

怎么把图片转换成excel?原来这么简单

在我们工作中&#xff0c;难免会遇到一些图片中的文字信息整理成Excel表格的情况。这时如果我们是用手输入&#xff0c;还是需要相当一段时间的。事实上&#xff0c;我们身边就有不少的小工具可以实现把图片变成表格&#xff0c;想要知道怎么把图片转换成excel呢?就和小编一起…

UDP协议重点总结(附实例)

文章目录前言一、网络的原生情况二、UDP协议2.1 UDP的特点2.1.1 不可靠性2.1.2 无连接&#xff08;不是缺点&#xff09;2.1.3 面向数据报&#xff08;优点&#xff09;2.1.4 缓冲区2.1.5 大小受限2.2 UDP协议端格式2.3 关于校验和2.4 基于UDP的应用层协议三、UDP总结&#xff…

C++ 使用Socket实现主机间的UDP/TCP通信

前言 全部代码放到github上了&#xff1a;cppSocketDemo 服务器端的代码做了跨平台&#xff08;POSIX和WINDOWS&#xff09;&#xff0c;基于POSIX平台&#xff08;Linux、Mac OS X、PlayStation等&#xff09;使用sys/socket.h库&#xff0c;windows平台使用winsock2.h库。 客…