这是百度到的代码,我只是增加了方块颜色,并稍微改了下
namespace Tetris { public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region 定义砖块int[i,j,y,x] Tricks:i为那块砖,j为状态,y为列,x为行 private int[, , ,] Tricks = {{ { {0,1,0,0}, {0,1,0,0}, {0,1,0,0}, {0,1,0,0} }, { {0,0,0,0}, {0,0,0,0}, {1,1,1,1}, {0,0,0,0} }, { {0,0,1,0}, {0,0,1,0}, {0,0,1,0}, {0,0,1,0} }, { {0,0,0,0}, {1,1,1,1}, {0,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,1,0,0}, {0,1,1,0}, {0,0,1,0}, {0,0,0,0} }, { {0,0,0,0}, {0,0,1,1}, {0,1,1,0}, {0,0,0,0} }, { {0,1,0,0}, {0,1,1,0}, {0,0,1,0}, {0,0,0,0} }, { {0,0,0,0}, {0,0,1,1}, {0,1,1,0}, {0,0,0,0} } }, { { {0,0,1,0}, {0,1,1,0}, {0,1,0,0}, {0,0,0,0} }, { {0,0,0,0}, {1,1,0,0}, {0,1,1,0}, {0,0,0,0} }, { {0,0,1,0}, {0,1,1,0}, {0,1,0,0}, {0,0,0,0} }, { {0,0,0,0}, {1,1,0,0}, {0,1,1,0}, {0,0,0,0} } }, { { {0,1,1,0}, {0,0,1,0}, {0,0,1,0}, {0,0,0,0} }, { {0,0,1,0}, {1,1,1,0}, {0,0,0,0}, {0,0,0,0} }, { {0,1,0,0}, {0,1,0,0}, {0,1,1,0}, {0,0,0,0} }, { {1,1,1,0}, {1,0,0,0}, {0,0,0,0}, {0,0,0,0} } }, { { {0,1,1,0}, {0,1,0,0}, {0,1,0,0}, {0,0,0,0} }, { {1,1,1,0}, {0,0,1,0}, {0,0,0,0}, {0,0,0,0} }, { {0,0,1,0}, {0,0,1,0}, {0,1,1,0}, {0,0,0,0} }, { {1,0,0,0}, {1,1,1,0}, {0,0,0,0}, {0,0,0,0} } }, { { {0,1,0,0}, {1,1,1,0}, {0,0,0,0}, {0,0,0,0} }, { {0,1,0,0}, {1,1,0,0}, {0,1,0,0}, {0,0,0,0} }, { {0,0,0,0}, {1,1,1,0}, {0,1,0,0}, {0,0,0,0} }, { {0,1,0,0}, {0,1,1,0}, {0,1,0,0}, {0,0,0,0} } }}; #endregion #region 定义背景 private int[,] bgGraoud ={ {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} }; #endregion private Color[] TrickColor = { Color.Red, Color.Blue, Color.Orange, Color.Green }; private int[,] CurrentTrick = new int[4, 4]; //当前的砖块 //CurrentTrickNum当前砖块的数目, CurrentStatusNum当前状态, CurrentX当前x, CurrentY当前y, Sorce分数 private int CurrentTrickNum, CurrentStatusNum, CurrentColor, CurrentX, CurrentY, Sorce; private int TricksNum = 4; private int StatusNum = 4; private int ColorNum = 4;//颜色种类 private Image myImage; private Random rand = new Random(); private void Form1_Load(object sender, EventArgs e) { //初始化 myImage = new Bitmap(panel1.Width, panel1.Height); loadGame(); } protected override void OnPaint(PaintEventArgs e) { Draw(); base.OnPaint(e); } private void loadGame() { timer1.Interval = 1000; Sorce = 0; label1.Text = "分数:" + Sorce; for (int y = 0; y < 20; y++) { for (int x = 0; x < 14; x++) { bgGraoud[y, x] = 0; } } BeginTricks(); button2.Text = "开始游戏"; this.Focus(); } /// <summary> /// 随机生成方块和状态 /// </summary> private void BeginTricks() { //随机生成砖码和状态码 CurrentTrickNum = rand.Next(0, TricksNum); CurrentStatusNum = rand.Next(0, StatusNum); CurrentColor = rand.Next(0, ColorNum); //分配数组 for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { CurrentTrick[y, x] = Tricks[CurrentTrickNum, CurrentStatusNum, y, x]; } } CurrentX = 5; CurrentY = 0; //timer1.Start(); } /// <summary> /// 变化方块 /// </summary> private void ChangeTricks() { if (CurrentStatusNum < 3) { CurrentStatusNum++; } else { CurrentStatusNum = 0; } for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { CurrentTrick[y, x] = Tricks[CurrentTrickNum, CurrentStatusNum, y, x]; } } } /// <summary> /// 下落方块 /// </summary> private void DownTricks() { if (CheckIsDown()) { CurrentY++; } else { if (CurrentY == 0) { timer1.Stop(); MessageBox.Show("哈哈,你玩玩了"); loadGame(); return; } //下落完成,修改背景 for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { bgGraoud[CurrentY + y, CurrentX + x] = CurrentColor + 1; } } } CheckSore(); BeginTricks(); } Draw(); } /// <summary> /// 检测是否可以向下了 /// </summary> /// <returns></returns> private bool CheckIsDown() { for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { //超过了背景 if (y + CurrentY + 1 >= 20) { return false; } if (x + CurrentX >= 14) { CurrentX = 13 - x; } if (bgGraoud[y + CurrentY + 1, x + CurrentX] > 0) { return false; } } } } return true; } /// <summary> /// 检测是否可以左移 /// </summary> /// <returns></returns> private bool CheckIsLeft() { for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { if (x + CurrentX - 1 < 0) { return false; } if (bgGraoud[y + CurrentY, x + CurrentX - 1] > 0) { return false; } } } } return true; } /// <summary> /// 检测是否可以右移 /// </summary> /// <returns></returns> private bool CheckIsRight() { for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { if (x + CurrentX + 1 >= 14) { return false; } if (bgGraoud[y + CurrentY, x + CurrentX + 1] > 0) { return false; } } } } return true; } private void Draw() { Graphics g = Graphics.FromImage(myImage); g.Clear(this.BackColor); for (int bgy = 0; bgy < 20; bgy++) { for (int bgx = 0; bgx < 14; bgx++) { if (bgGraoud[bgy, bgx] > 0) { g.FillRectangle(new SolidBrush(TrickColor[bgGraoud[bgy, bgx] - 1]), bgx * 20, bgy * 20, 20, 20); g.DrawRectangle(Pens.White, bgx * 20, bgy * 20, 20, 20); } } } //绘制当前的图片 for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { g.FillRectangle(new SolidBrush(TrickColor[CurrentColor]), (x + CurrentX) * 20, (y + CurrentY) * 20, 20, 20); g.DrawRectangle(Pens.White, (x + CurrentX) * 20, (y + CurrentY) * 20, 20, 20); } } } Graphics gg = panel1.CreateGraphics(); gg.DrawImage(myImage, 0, 0); } private void timer1_Tick(object sender, EventArgs e) { DownTricks(); } private void Form1_KeyDown(object sender, KeyEventArgs e) { if (timer1.Enabled == false) { return; } if (e.KeyCode == Keys.W) { ChangeTricks(); Draw(); } else if (e.KeyCode == Keys.A) { if (CheckIsLeft()) { CurrentX--; } Draw(); } else if (e.KeyCode == Keys.D) { if (CheckIsRight()) { CurrentX++; } Draw(); } else if (e.KeyCode == Keys.S) { timer1.Stop(); timer1.Interval = 10; timer1.Start(); } } private void Form1_KeyUp(object sender, KeyEventArgs e) { if (timer1.Enabled == false) { return; } if (e.KeyCode == Keys.S) { timer1.Stop(); timer1.Interval = 1000; timer1.Start(); } } private void CheckSore() { for (int y = 19; y > -1; y--) { bool isFull = true; for (int x = 13; x > -1; x--) { if (bgGraoud[y, x] == 0) { isFull = false; break; } } if (isFull) { //增加积分 Sorce = Sorce + 100; for (int yy = y; yy > 0; yy--) { for (int xx = 0; xx < 14; xx++) { int temp = bgGraoud[yy - 1, xx]; bgGraoud[yy, xx] = temp; } } y++; label1.Text = Sorce.ToString(); ; Draw(); } } } private void button2_Click(object sender, EventArgs e) { if (button2.Text == "暂停游戏") { button2.Text = "开始游戏"; timer1.Stop(); } else { button2.Text = "暂停游戏"; timer1.Start(); } } } }
时间: 2024-10-09 23:54:22