GDI学习之俄罗斯方块

时间:2023-03-08 19:02:49

做个玩玩

public Form1()
{
InitializeComponent();
} #region 定义砖块int[i,j,y,x] Tricks:i为那块砖,j为状态,y为列,x为行
private int[, , ,] Tricks = {{
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
}
},
{
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
}
},
{
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
}
},
{
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
}
}}; #endregion private Color[] TrickColor = { Color.Red, Color.Blue, Color.Orange, Color.Green }; private int[,] CurrentTrick = new int[, ]; //当前的砖块
//CurrentTrickNum当前砖块的数目, CurrentStatusNum当前状态 CurrentColor当前颜色
private int CurrentTrickNum, CurrentStatusNum, CurrentColor;
private int TricksNum = ;//方块种类
private int StatusNum = ;//方块状态
private int ColorNum = ;//颜色种类
private Image myImage;
private Random rand = new Random(); private void Form1_Load(object sender, EventArgs e)
{
//初始化
myImage = new Bitmap(panel1.Width, panel1.Height);
for (int y = ; y < ; y++)
{
for (int x = ; x < ; x++)
{
CurrentTrick[y, x] = ;
}
}
Draw();
} protected override void OnPaint(PaintEventArgs e)
{
Draw();
base.OnPaint(e);
} private void Draw()
{
Graphics g = Graphics.FromImage(myImage);
g.Clear(Color.Black);
//绘制当前的图片
for (int y = ; y < ; y++)
{
for (int x = ; x < ; x++)
{
if (CurrentTrick[y, x] == )
{
g.FillRectangle(new SolidBrush(TrickColor[CurrentColor]), x * , y * , , );
g.DrawRectangle(Pens.White, x * , y * , , );
}
}
}
Graphics gg = panel1.CreateGraphics();
gg.DrawImage(myImage, , );
} //随机生成方块
private void BeginTricks()
{
//随机生成砖码和状态码和颜色
CurrentTrickNum = rand.Next(, TricksNum);
CurrentStatusNum = rand.Next(, StatusNum);
CurrentColor = rand.Next(, ColorNum);
//分配数组
for (int y = ; y < ; y++)
{
for (int x = ; x < ; x++)
{
CurrentTrick[y, x] = Tricks[CurrentTrickNum, CurrentStatusNum, y, x];
}
}
} //控制方向
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
ChangeTricks();
Draw();
}
} //变换方块
private void ChangeTricks()
{
if (CurrentStatusNum < )
{
CurrentStatusNum++;
}
else
{
CurrentStatusNum = ;
}
for (int y = ; y < ; y++)
{
for (int x = ; x < ; x++)
{
CurrentTrick[y, x] = Tricks[CurrentTrickNum, CurrentStatusNum, y, x];
}
}
} private void button1_Click(object sender, EventArgs e)
{
BeginTricks();
Draw();
}

GDI学习之俄罗斯方块

GDI学习之俄罗斯方块