From b3ec2b0e1c7eb4acae0cc75360a9691f40ca151f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E6=AC=A3=E5=AE=87?=
<8289379+Sin-Yoo@user.noreply.gitee.com>
Date: Sat, 9 Jan 2021 01:47:50 +0800
Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E4=BA=8E?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Form1.Designer.cs | 317 +++++++++++++++++++++++++
Form1.cs | 593 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 910 insertions(+)
create mode 100644 Form1.Designer.cs
create mode 100644 Form1.cs
diff --git a/Form1.Designer.cs b/Form1.Designer.cs
new file mode 100644
index 0000000..48d9db4
--- /dev/null
+++ b/Form1.Designer.cs
@@ -0,0 +1,317 @@
+namespace TetrisV1._0
+{
+ partial class Form1
+ {
+ ///
+ /// 必需的设计器变量。
+ ///
+ private System.ComponentModel.IContainer components = null;
+
+ ///
+ /// 清理所有正在使用的资源。
+ ///
+ /// 如果应释放托管资源,为 true;否则为 false。
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing && (components != null))
+ {
+ components.Dispose();
+ }
+ base.Dispose(disposing);
+ }
+
+ #region Windows 窗体设计器生成的代码
+
+ ///
+ /// 设计器支持所需的方法 - 不要修改
+ /// 使用代码编辑器修改此方法的内容。
+ ///
+ private void InitializeComponent()
+ {
+ this.components = new System.ComponentModel.Container();
+ this.panel1 = new System.Windows.Forms.Panel();
+ this.panel2 = new System.Windows.Forms.Panel();
+ this.button1 = new System.Windows.Forms.Button();
+ this.button2 = new System.Windows.Forms.Button();
+ 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.label5 = new System.Windows.Forms.Label();
+ this.label6 = new System.Windows.Forms.Label();
+ this.timer1 = new System.Windows.Forms.Timer(this.components);
+ this.menuStrip1 = new System.Windows.Forms.MenuStrip();
+ this.模式ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
+ this.简单ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
+ this.一般ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
+ this.困难ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
+ this.关于ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
+ this.panel3 = new System.Windows.Forms.Panel();
+ this.label8 = new System.Windows.Forms.Label();
+ this.button3 = new System.Windows.Forms.Button();
+ this.label7 = new System.Windows.Forms.Label();
+ this.menuStrip1.SuspendLayout();
+ this.panel3.SuspendLayout();
+ this.SuspendLayout();
+ //
+ // panel1
+ //
+ this.panel1.BackColor = System.Drawing.SystemColors.ButtonFace;
+ this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
+ this.panel1.Location = new System.Drawing.Point(8, 31);
+ this.panel1.Margin = new System.Windows.Forms.Padding(2);
+ this.panel1.Name = "panel1";
+ this.panel1.Size = new System.Drawing.Size(286, 407);
+ this.panel1.TabIndex = 0;
+ //
+ // panel2
+ //
+ this.panel2.BackColor = System.Drawing.SystemColors.ButtonFace;
+ this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
+ this.panel2.Location = new System.Drawing.Point(298, 31);
+ this.panel2.Margin = new System.Windows.Forms.Padding(2);
+ this.panel2.Name = "panel2";
+ this.panel2.Size = new System.Drawing.Size(97, 86);
+ this.panel2.TabIndex = 1;
+ //
+ // button1
+ //
+ this.button1.Location = new System.Drawing.Point(302, 168);
+ this.button1.Margin = new System.Windows.Forms.Padding(2);
+ this.button1.Name = "button1";
+ this.button1.Size = new System.Drawing.Size(88, 24);
+ this.button1.TabIndex = 2;
+ this.button1.Text = "重新开始";
+ this.button1.UseVisualStyleBackColor = true;
+ this.button1.Click += new System.EventHandler(this.button1_Click);
+ //
+ // button2
+ //
+ this.button2.Location = new System.Drawing.Point(302, 208);
+ this.button2.Margin = new System.Windows.Forms.Padding(2);
+ this.button2.Name = "button2";
+ this.button2.Size = new System.Drawing.Size(88, 24);
+ this.button2.TabIndex = 3;
+ this.button2.Text = "暂停游戏";
+ this.button2.UseVisualStyleBackColor = true;
+ this.button2.Click += new System.EventHandler(this.button2_Click);
+ //
+ // label1
+ //
+ this.label1.AutoSize = true;
+ this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+ this.label1.Location = new System.Drawing.Point(299, 140);
+ this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
+ this.label1.Name = "label1";
+ this.label1.Size = new System.Drawing.Size(84, 14);
+ this.label1.TabIndex = 4;
+ this.label1.Text = "游戏得分:0";
+ //
+ // label2
+ //
+ this.label2.AutoSize = true;
+ this.label2.Location = new System.Drawing.Point(317, 259);
+ this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
+ this.label2.Name = "label2";
+ this.label2.Size = new System.Drawing.Size(41, 12);
+ this.label2.TabIndex = 5;
+ this.label2.Text = "操作:";
+ //
+ // label3
+ //
+ this.label3.AutoSize = true;
+ this.label3.Location = new System.Drawing.Point(317, 283);
+ this.label3.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
+ this.label3.Name = "label3";
+ this.label3.Size = new System.Drawing.Size(53, 12);
+ this.label3.TabIndex = 6;
+ this.label3.Text = "左移:←";
+ //
+ // label4
+ //
+ this.label4.AutoSize = true;
+ this.label4.Location = new System.Drawing.Point(317, 308);
+ this.label4.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
+ this.label4.Name = "label4";
+ this.label4.Size = new System.Drawing.Size(53, 12);
+ this.label4.TabIndex = 7;
+ this.label4.Text = "右移:→";
+ //
+ // label5
+ //
+ this.label5.AutoSize = true;
+ this.label5.Location = new System.Drawing.Point(317, 331);
+ this.label5.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
+ this.label5.Name = "label5";
+ this.label5.Size = new System.Drawing.Size(53, 12);
+ this.label5.TabIndex = 8;
+ this.label5.Text = "变换:↑";
+ //
+ // label6
+ //
+ this.label6.AutoSize = true;
+ this.label6.Location = new System.Drawing.Point(317, 355);
+ this.label6.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
+ this.label6.Name = "label6";
+ this.label6.Size = new System.Drawing.Size(77, 12);
+ this.label6.TabIndex = 9;
+ this.label6.Text = "加速下落:↓";
+ //
+ // timer1
+ //
+ this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
+ //
+ // menuStrip1
+ //
+ this.menuStrip1.ImageScalingSize = new System.Drawing.Size(24, 24);
+ this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
+ this.模式ToolStripMenuItem,
+ this.关于ToolStripMenuItem});
+ this.menuStrip1.Location = new System.Drawing.Point(0, 0);
+ this.menuStrip1.Name = "menuStrip1";
+ this.menuStrip1.Padding = new System.Windows.Forms.Padding(4, 1, 0, 1);
+ this.menuStrip1.Size = new System.Drawing.Size(692, 24);
+ this.menuStrip1.TabIndex = 11;
+ this.menuStrip1.Text = "menuStrip1";
+ //
+ // 模式ToolStripMenuItem
+ //
+ this.模式ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
+ this.简单ToolStripMenuItem,
+ this.一般ToolStripMenuItem,
+ this.困难ToolStripMenuItem});
+ this.模式ToolStripMenuItem.Name = "模式ToolStripMenuItem";
+ this.模式ToolStripMenuItem.Size = new System.Drawing.Size(44, 22);
+ this.模式ToolStripMenuItem.Text = "模式";
+ //
+ // 简单ToolStripMenuItem
+ //
+ this.简单ToolStripMenuItem.Name = "简单ToolStripMenuItem";
+ this.简单ToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
+ this.简单ToolStripMenuItem.Text = "简单";
+ this.简单ToolStripMenuItem.Click += new System.EventHandler(this.简单ToolStripMenuItem_Click);
+ //
+ // 一般ToolStripMenuItem
+ //
+ this.一般ToolStripMenuItem.Name = "一般ToolStripMenuItem";
+ this.一般ToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
+ this.一般ToolStripMenuItem.Text = "一般";
+ this.一般ToolStripMenuItem.Click += new System.EventHandler(this.一般ToolStripMenuItem_Click);
+ //
+ // 困难ToolStripMenuItem
+ //
+ this.困难ToolStripMenuItem.Name = "困难ToolStripMenuItem";
+ this.困难ToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
+ this.困难ToolStripMenuItem.Text = "困难";
+ this.困难ToolStripMenuItem.Click += new System.EventHandler(this.困难ToolStripMenuItem_Click);
+ //
+ // 关于ToolStripMenuItem
+ //
+ this.关于ToolStripMenuItem.Name = "关于ToolStripMenuItem";
+ this.关于ToolStripMenuItem.Size = new System.Drawing.Size(44, 22);
+ this.关于ToolStripMenuItem.Text = "关于";
+ this.关于ToolStripMenuItem.Click += new System.EventHandler(this.关于ToolStripMenuItem_Click);
+ //
+ // panel3
+ //
+ this.panel3.BackColor = System.Drawing.SystemColors.Window;
+ this.panel3.Controls.Add(this.label8);
+ this.panel3.Controls.Add(this.button3);
+ this.panel3.Controls.Add(this.label7);
+ this.panel3.Location = new System.Drawing.Point(413, 48);
+ this.panel3.Name = "panel3";
+ this.panel3.Size = new System.Drawing.Size(400, 420);
+ this.panel3.TabIndex = 12;
+ this.panel3.Visible = false;
+ //
+ // label8
+ //
+ this.label8.AutoSize = true;
+ this.label8.Location = new System.Drawing.Point(68, 114);
+ this.label8.Name = "label8";
+ this.label8.Size = new System.Drawing.Size(41, 12);
+ this.label8.TabIndex = 2;
+ this.label8.Text = "向上:";
+ //
+ // button3
+ //
+ this.button3.Location = new System.Drawing.Point(147, 323);
+ this.button3.Name = "button3";
+ this.button3.Size = new System.Drawing.Size(75, 23);
+ this.button3.TabIndex = 1;
+ this.button3.Text = "返回";
+ this.button3.UseVisualStyleBackColor = true;
+ this.button3.Click += new System.EventHandler(this.button3_Click);
+ //
+ // label7
+ //
+ this.label7.AutoSize = true;
+ this.label7.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+ this.label7.Location = new System.Drawing.Point(118, 44);
+ this.label7.Name = "label7";
+ this.label7.Size = new System.Drawing.Size(154, 24);
+ this.label7.TabIndex = 0;
+ this.label7.Text = "游戏操作说明";
+ //
+ // Form1
+ //
+ this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
+ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+ this.BackColor = System.Drawing.SystemColors.Window;
+ this.ClientSize = new System.Drawing.Size(692, 480);
+ this.Controls.Add(this.panel3);
+ this.Controls.Add(this.label6);
+ this.Controls.Add(this.label5);
+ this.Controls.Add(this.label4);
+ this.Controls.Add(this.label3);
+ this.Controls.Add(this.label2);
+ this.Controls.Add(this.label1);
+ this.Controls.Add(this.button2);
+ this.Controls.Add(this.button1);
+ this.Controls.Add(this.panel2);
+ this.Controls.Add(this.panel1);
+ this.Controls.Add(this.menuStrip1);
+ this.ForeColor = System.Drawing.SystemColors.WindowText;
+ this.KeyPreview = true;
+ this.MainMenuStrip = this.menuStrip1;
+ this.Margin = new System.Windows.Forms.Padding(2);
+ this.Name = "Form1";
+ this.Text = "俄罗斯方块";
+ this.Load += new System.EventHandler(this.Form1_Load);
+ this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
+ this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
+ this.menuStrip1.ResumeLayout(false);
+ this.menuStrip1.PerformLayout();
+ this.panel3.ResumeLayout(false);
+ this.panel3.PerformLayout();
+ this.ResumeLayout(false);
+ this.PerformLayout();
+
+ }
+
+ #endregion
+
+ private System.Windows.Forms.Panel panel1;
+ private System.Windows.Forms.Panel panel2;
+ private System.Windows.Forms.Button button1;
+ private System.Windows.Forms.Button button2;
+ private System.Windows.Forms.Label label1;
+ private System.Windows.Forms.Label label2;
+ private System.Windows.Forms.Label label3;
+ private System.Windows.Forms.Label label4;
+ private System.Windows.Forms.Label label5;
+ private System.Windows.Forms.Label label6;
+ private System.Windows.Forms.Timer timer1;
+ private System.Windows.Forms.MenuStrip menuStrip1;
+ private System.Windows.Forms.ToolStripMenuItem 模式ToolStripMenuItem;
+ private System.Windows.Forms.ToolStripMenuItem 简单ToolStripMenuItem;
+ private System.Windows.Forms.ToolStripMenuItem 一般ToolStripMenuItem;
+ private System.Windows.Forms.ToolStripMenuItem 困难ToolStripMenuItem;
+ private System.Windows.Forms.ToolStripMenuItem 关于ToolStripMenuItem;
+ private System.Windows.Forms.Panel panel3;
+ private System.Windows.Forms.Label label7;
+ private System.Windows.Forms.Label label8;
+ private System.Windows.Forms.Button button3;
+ }
+}
+
diff --git a/Form1.cs b/Form1.cs
new file mode 100644
index 0000000..c3509e7
--- /dev/null
+++ b/Form1.cs
@@ -0,0 +1,593 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Data;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+
+namespace TetrisV1._0
+{
+ public partial class Form1 : Form
+ {
+ private int [,] currentBrick=new int[4,4];//当前的方块 序号 形状
+ private int currentBrickNum;//当前的方块序号(哪种方块)
+ private int currentBrickDirec;//该序号的方块的方向
+ private int currentX;//当前方块的位置
+ private int currentY;//当前方块的位置
+
+ private int nextBrickNum;//下一个方块的序号
+ private int nextBrickDirec;//下一个该序号的方块的方向
+ private Image backgroundImage;//得到与panel相同大小的背景图片
+ private Random random=new Random();
+ private int score;
+
+ //定义四维数组Bricks[m,n,y,x]
+ //m:当前方块的序号 n:当前方块的方向 y:当前方块所处的行数 x:当前方块所处的列数
+ private int[,,,] Bricks =
+ {
+ //第一种方块
+ {
+ {
+ {1,0,0,0},
+ {1,0,0,0},
+ {1,0,0,0},
+ {1,0,0,0}
+ },
+ {
+ {1,1,1,1},
+ {0,0,0,0},
+ {0,0,0,0},
+ {0,0,0,0}
+ },
+ {
+ {1,0,0,0},
+ {1,0,0,0},
+ {1,0,0,0},
+ {1,0,0,0}
+ },
+ {
+ {1,1,1,1},
+ {0,0,0,0},
+ {0,0,0,0},
+ {0,0,0,0}
+ },
+ },
+ //第二种方块
+ {
+ {
+ {1,1,0,0},
+ {1,1,0,0},
+ {0,0,0,0},
+ {0,0,0,0}
+ },
+ {
+ {1,1,0,0},
+ {1,1,0,0},
+ {0,0,0,0},
+ {0,0,0,0}
+ },
+ {
+ {1,1,0,0},
+ {1,1,0,0},
+ {0,0,0,0},
+ {0,0,0,0}
+ },
+ {
+ {1,1,0,0},
+ {1,1,0,0},
+ {0,0,0,0},
+ {0,0,0,0}
+ },
+ },
+ //第三种方块
+ {
+ {
+ {1,0,0,0},
+ {1,1,0,0},
+ {0,1,0,0},
+ {0,0,0,0}
+ },
+ {
+ {0,1,1,0},
+ {1,1,0,0},
+ {0,0,0,0},
+ {0,0,0,0}
+ },
+ {
+ {1,0,0,0},
+ {1,1,0,0},
+ {0,1,0,0},
+ {0,0,0,0}
+ },
+ {
+ {0,1,1,0},
+ {1,1,0,0},
+ {0,0,0,0},
+ {0,0,0,0}
+ },
+ },
+ //第四种方块
+ {
+ {
+ {1,1,0,0},
+ {0,1,0,0},
+ {0,1,0,0},
+ {0,0,0,0}
+ },
+ {
+ {0,0,1,0},
+ {1,1,1,0},
+ {0,0,0,0},
+ {0,0,0,0}
+ },
+ {
+ {1,0,0,0},
+ {1,0,0,0},
+ {1,1,0,0},
+ {0,0,0,0}
+ },
+ {
+ {1,1,1,0},
+ {1,0,0,0},
+ {0,0,0,0},
+ {0,0,0,0}
+ },
+ },
+ };
+
+ //定义20*12的背景数组
+ private int[,] Background =
+ {
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+ };
+ public Form1()
+ {
+ InitializeComponent();
+ }
+
+ //将panel作为游戏背景板
+ private void Form1_Load(object sender, EventArgs e)
+ {
+ //得到与panel相同大小的背景图片
+ backgroundImage = new Bitmap(panel1.Width, panel1.Height);
+ score = 0;
+ }
+
+ //产生方块
+ private void CreateBricks()
+ {
+ currentBrickNum = nextBrickNum;
+ currentBrickDirec = nextBrickDirec;
+
+ nextBrickNum = random.Next(0, 4);
+ nextBrickDirec = random.Next(0, 4);
+
+ for(int y=0;y<4;y++)
+ {
+ for(int x=0;x<4;x++)
+ {
+ currentBrick[y, x] = Bricks[currentBrickNum, currentBrickDirec, y, x];
+ }
+ }
+ //方块出现的位置 (7,0)
+ currentX = 7;
+ currentY = 0;
+
+ //计时器开启
+ timer1.Start();
+ }
+ //随机获取颜色
+ public Color GetRandomColor()
+ {
+ Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
+ // 对于C#的随机数,没什么好说的
+ System.Threading.Thread.Sleep(RandomNum_First.Next(50));
+ Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
+ // 为了在白色背景上显示,尽量生成深色
+ int int_Red = RandomNum_First.Next(256);
+ int int_Green = RandomNum_Sencond.Next(256);
+ int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
+ int_Blue = (int_Blue > 255) ? 255 : int_Blue;
+ return Color.FromArgb(int_Red, int_Green, int_Blue);//.Name
+ }
+ //画出当前下落的方块和已经下落的方块
+ private void DrawCurrentBrick()
+ {
+ Color randomColor=GetRandomColor();
+ //创建窗体画布
+ Graphics g = Graphics.FromImage(backgroundImage);
+
+ //清除以前画的图形
+ g.Clear(this.panel1.BackColor);
+
+ //画出已经落下的方块
+ for (int bgy = 0; bgy < 20; bgy++)
+ {
+ for (int bgx = 0; bgx < 14; bgx++)
+ {
+ if (Background[bgy, bgx] == 1)
+ {
+ //定义方块每一个小单元的边长为20
+ g.FillRectangle(new SolidBrush(randomColor), bgx * 20, bgy * 20, 20, 20);//Color.DimGray
+ g.DrawRectangle(new Pen(Color.Black, 1), bgx * 20, bgy * 20, 20, 20);
+ }
+ }
+ }
+
+ //绘制当前正在下落的方块
+ for (int y = 0; y < 4; y++)
+ {
+ for (int x = 0; x < 4; x++)
+ {
+ if (currentBrick[y, x] == 1)
+ {
+ //定义方块每一个小单元的边长为20
+ g.FillRectangle(new SolidBrush(Color.DimGray), (x + currentX) * 20, (y + currentY) * 20, 20, 20);
+ g.DrawRectangle(new Pen(Color.Gainsboro, 1), (x + currentX) * 20, (y + currentY) * 20, 20, 20);
+ }
+ }
+ }
+
+ //获取面板的画布
+ Graphics gg = panel1.CreateGraphics();
+ gg.DrawImage(backgroundImage, 0, 0);
+ }
+
+ //使方块下落
+ private void FallBrick()
+ {
+ //判断是否可以下落
+ if(CheckFallBrick())
+ {
+ currentY++;
+ }
+ else
+ {
+ //如果方块已经达到最上方
+ if (currentY == 0)
+ {
+ timer1.Stop();
+ MessageBox.Show("游戏结束!");
+ return;
+ }
+
+ //下落完成,修改背景数组
+ for(int y=0;y<4;y++)
+ {
+ for(int x=0;x<4;x++)
+ {
+ if(currentBrick[y,x]==1)
+ {
+ Background[y + currentY, x + currentX] = currentBrick[y,x];
+ }
+ }
+ }
+ CalculateScore();
+ //前一个方块到底了才能产生新的方块
+ CreateBricks();
+ DrawNextBrick();
+ }
+ //绘制方块
+ DrawCurrentBrick();
+ }
+
+ //旋转方块
+ private void RotateBrick()
+ {
+ int tempBrickDirec = (currentBrickDirec + 1) % 4;
+ int[,] tempBrick = new int[4, 4];
+ for(int y=0;y<4;y++)
+ {
+ for(int x=0;x<4;x++)
+ {
+ tempBrick[y, x] = Bricks[currentBrickNum, currentBrickDirec, y, x];
+ if(tempBrick[y,x]==1&&(currentY+y+1)>20&&(currentX+x+1)>14)
+ {
+ return;
+ }
+ if(tempBrick[y,x]==1&&Background[currentY+y,currentX+x]==1)
+ {
+ return;
+ }
+ }
+ }
+
+ currentBrick = tempBrick;
+ currentBrickDirec = (currentBrickDirec + 1) % 4;
+ }
+
+ //检测方块是否可以下落
+ private bool CheckFallBrick()
+ {
+ for(int y=0;y<4;y++)
+ {
+ for(int x=0;x<4;x++)
+ {
+ if(currentBrick[y,x]==1)
+ {
+ //方块已经下落到底,不能下落
+ if((currentY+y+1)>=20)
+ {
+ return false;
+ }
+ //下方有别的方块,不能下落
+ if(Background[y+currentY+1,x+currentX]==1)
+ {
+ return false;
+ }
+ }
+ }
+ }
+ return true;
+ }
+
+ //检测方块是否可以左移
+ private bool CheckLeftBrick()
+ {
+ for(int y=0;y<4;y++)
+ {
+ for(int x=0;x<4;x++)
+ {
+ if(currentBrick[y,x]==1)
+ {
+ //左移到边界,不能移动
+ if(x+currentX-1<0)
+ {
+ return false;
+ }
+ //左边有方块,不能移动
+ if(Background[y+currentY,x+currentX-1]==1)
+ {
+ return false;
+ }
+ }
+ }
+ }
+ return true;
+ }
+
+ //检测方块是否可以右移
+ private bool CheckRightBrick()
+ {
+ for(int y=0;y<4;y++)
+ {
+ for(int x=0;x<4;x++)
+ {
+ if(currentBrick[y,x]==1)
+ {
+ //右移到边界,不能移动
+ if(x+currentX+1>=14)
+ {
+ return false;
+ }
+ //右边有方块,不能移动
+ if(Background[y+currentY,x+currentX+1]==1)
+ {
+ return false;
+ }
+ }
+ }
+ }
+ return true;
+ }
+
+ //重新开始
+ private void button1_Click(object sender, EventArgs e)
+ {
+ //初始化背景数组
+ for (int y = 0; y < 20; y++)
+ {
+ for (int x = 0; x < 14; x++)
+ {
+ Background[y, x] = 0;
+ }
+ }
+ timer1.Interval = 1000;
+
+ //生成第一个方块的序号和方向
+ nextBrickNum = random.Next(0, 4);
+ nextBrickDirec = random.Next(0, 4);
+
+ CreateBricks();
+ DrawCurrentBrick(); //绘制方块
+ DrawNextBrick(); //绘制下一个方块
+ this.panel1.Focus(); //将窗口变成输入焦点
+
+ PlayMusic();
+ }
+
+ //暂停游戏
+ private void button2_Click(object sender, EventArgs e)
+ {
+ if(button2.Text=="暂停游戏")
+ {
+ button2.Text = "开始游戏";
+ timer1.Stop();
+ }
+ else
+ {
+ button2.Text = "暂停游戏";
+ timer1.Start();
+ }
+ }
+
+ //画出下一个方块,预览下一个方块
+ private void DrawNextBrick()
+ {
+ Graphics g = this.panel2.CreateGraphics();
+ g.Clear(this.panel2.BackColor); //清除之前的图形
+ for (int y=0; y<4; y++)
+ {
+ for (int x=0; x<4; x++)
+ {
+ //j为横坐标,i为纵坐标
+ if (Bricks[nextBrickNum, nextBrickDirec, y, x] == 1)
+ {
+ g.FillRectangle(new SolidBrush(Color.DimGray), x * 20, y * 20, 20, 20);
+ g.DrawRectangle(new Pen(Color.Gainsboro, 1), x * 20, y * 20, 20, 20);
+ }
+ }
+ }
+ }
+
+ //计算得分,消去整行方块
+ private void CalculateScore()
+ {
+ for (int y=19; y>=0; y--)
+ {
+ bool isFull = true;
+ for (int x=13; x>=0; x--)
+ {
+ //若一行当中有某个单元没有方块,则这一行不能消去
+ if (Background[y,x] == 0)
+ {
+ isFull = false;
+ break;
+ }
+ }
+ if (isFull)
+ {
+ //增加得分
+ score = score + 10;
+ for (int y1=y; y1>0; y1--) //y1>0,消到第二行,第一行肯定没有方块
+ {
+ //消去一行后把这一行上面的所有方块向下移一行
+ for (int x1=0; x1<14; x1++)
+ {
+ Background[y1,x1] = Background[y1-1,x1];
+ }
+ }
+ //纵坐标为消去的这一行 继续判断
+ y++;
+ label1.Text = "游戏得分: " + score.ToString();
+ }
+ }
+ }
+
+ //键盘响应事件
+ private void Form1_KeyDown(object sender, KeyEventArgs e)
+ {
+ if (this.button2.Text.Equals("开始游戏"))
+ {
+ //暂停的时候不响应键盘
+ return;
+ }
+
+ else if (e.KeyValue==38)
+ {
+ //↑键按下
+ //旋转方块
+ RotateBrick();
+ DrawCurrentBrick();
+ }
+ else if (e.KeyValue==37)
+ {
+ //←键按下
+ //方块往左边移动
+ if (CheckLeftBrick())
+ {
+ currentX--;
+ }
+ DrawCurrentBrick();
+ }
+ else if (e.KeyValue==39)
+ {
+ //→键按下
+ //方块向右移动
+ if (CheckRightBrick())
+ {
+ currentX++;
+ }
+ DrawCurrentBrick();
+ }
+ else if (e.KeyValue==40)
+ {
+ //↓键按下
+ //方块加速下落
+ this.timer1.Interval = 20;
+ }
+ }
+
+ //覆盖默认的系统键处理方式,
+ //遇到方向键,则直接返回,
+ //系统不处理,这样键值就会被传递到窗体,触发keydown事件
+ protected override bool ProcessDialogKey(Keys keyData)
+ {
+ if (keyData == Keys.Up || keyData == Keys.Down ||
+ keyData == Keys.Left || keyData == Keys.Right)
+ return false;
+ else
+ return base.ProcessDialogKey(keyData);
+ }
+
+ private void Form1_KeyUp(object sender, KeyEventArgs e)
+ {
+ if(e.KeyCode==Keys.Down)
+ {
+ this.timer1.Interval = 1000;
+ }
+ }
+
+ private void timer1_Tick(object sender, EventArgs e)
+ {
+ FallBrick();
+ }
+
+ //播放音乐
+ private void PlayMusic()
+ {
+ System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
+ sp.SoundLocation = @".\Sound.wav";
+ if (System.IO.File.Exists(@".\Sound.wav")) sp.PlayLooping();
+ }
+
+ private void 简单ToolStripMenuItem_Click(object sender, EventArgs e)
+ {
+ timer1.Interval = 1000;
+ }
+
+ private void 一般ToolStripMenuItem_Click(object sender, EventArgs e)
+ {
+ timer1.Interval = 400;
+ }
+
+ private void 困难ToolStripMenuItem_Click(object sender, EventArgs e)
+ {
+ timer1.Interval = 200;
+ }
+
+ private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
+ {
+ panel3.Visible = true;
+
+ }
+
+ private void button3_Click(object sender, EventArgs e)
+ {
+ panel3.Visible = false;
+ }
+
+ }
+}
--
Gitee