总流程
是最后一节捏!
由于我在讲解的时候的代码不全,我会把源码放在附录里(新人博主这么认真,点个关注吧!)
首先是关定时器和画棋盘
timer1.Enabled = false;
DrawBoard();
生成对象
if(tstart)//生成随机枚举类型控制游戏对象类型
{
Random rd = new Random();
random=rd.Next(0,7);
angle = 1;
tstart = false;
}
GO gO = (GO)random;
临时下落对象的初始化
byte tempAngle;
bool tangle = false;
bool tfall = false;
tempAngle = angle;
Tpoint tempcenturePoint = new Tpoint();
tempcenturePoint.x = centureX;
tempcenturePoint.y = centureY;
按键检测(敲黑板!!)
if (Keypress!=Keypress.NULL)//检验按键是否按下
{
response = 0;
switch (Keypress)
{
case Keypress.A: tempcenturePoint.x--; break;
case Keypress.D: tempcenturePoint.x++; break;
case Keypress.J: tempAngle++;tangle = true; break;
default:break;
}
if (tangle && tempcenturePoint.x == 0)
{
if (tempAngle == 1 && gO == GO.I)
tempcenturePoint.x += 2;
else
tempcenturePoint.x++;
}
if (tangle && tempcenturePoint.x == 9)
{
if (tempAngle == 1 && gO == GO.I)
tempcenturePoint.x -= 2;
else
tempcenturePoint.x--;
}
Objectgame tempobjectgame = new Objectgame();
Tpoint[] temppoint4 = tempobjectgame.GetPoint(gO, tempcenturePoint, (byte)(tempAngle % 4 + 3));
if (CrachCheck(temppoint4))//碰撞检测
{
centureX = (byte)tempcenturePoint.x;
centureY = (byte)tempcenturePoint.y;
angle = tempAngle;
}
Keypress = Keypress.NULL;
}
下落检测
tempcenturePoint.y++;
Objectgame tempobjectgame1 = new Objectgame();
Tpoint[] temppoint41 = tempobjectgame1.GetPoint(gO, tempcenturePoint, (byte)(tempAngle % 4 + 3));
if (FallCheck(temppoint41))
centureY = (byte)tempcenturePoint.y;
else
tfall = true;
检测完之后的绘制
Tpoint centurePoint = new Tpoint();
centurePoint.x = centureX;
centurePoint.y = centureY;
Objectgame objectgame = new Objectgame();
Tpoint[] point4 = objectgame.GetPoint(gO, centurePoint, (byte)(angle % 4 + 3));
MoveBlock(gO, point4);
if (tfall)
{
if(!GameoverCheck(point4))
{
timer = false;
label1.Text = "GAMEOVER";
goto to;
}
else
{
FixBlock(point4);
centureX = 5;
centureY = 0;
tstart = true;
}
}
StaticBlock(staticBlock);
if (tfall)
{
ClearBlock();
StaticBlock(staticBlock);
}
label2.Text = score.ToString("d3");
to:
timer1.Enabled = timer;
相关函数:
private void FixBlock(Tpoint[] point4)
{
for (byte i = 0; i < 4; i++)
{
staticBlock[point4[i].y - 4, point4[i].x] = true;
}
}
private void ClearBlock()
{
for (byte i = 0; i < 20; i++)
{
bool line = true;
for (byte j = 0; j < 10; j++)
if (staticBlock[i, j] == false)
{
line = false;
}
if (line)
{
score += 100;
for(byte k=i;k>0;k--)
{
for (byte j = 0; j < 10; j++)
staticBlock[k, j] = staticBlock[k - 1,j];
}
for (byte j = 0; j < 10; j++)
staticBlock[0, j] = false;
Graphics board = this.CreateGraphics();
Rectangle rectangle = new Rectangle(xOy_x, xOy_y, size * 10, size * 20);
Brush b = new SolidBrush(Color.Black);
board.FillRectangle(b, rectangle);
}
}
}
private bool GameoverCheck(Tpoint[] point4)
{
for (byte i = 0; i < 4; i++)
{
if (point4[i].y < 4)
return false;
}
return true;
}
副一张上课画的草图
好了,以上就是这个项目的全部内容了,我会把源码放在下一篇里,本人是学生,平常还要上课,所以没太多时间和精力写这个,如果还是没看懂的,就去看看我的源码吧,如果有改进方法欢迎在评论区提出来,别忘了关注博主哦!
最后再扯几句完成后的感想吧。完成的时候是上周六晚上,当时真的是乐疯了,拉着室友演示我的游戏,看着屏幕上跳动的方块,旋转,下落,消除,成就感溢于言表。在没做这个项目之前,我只是在b站上看过一点c#教学视频,GDI绘图都不知道是干嘛的。c#是我的第一个面向对象语言,因为我很喜欢游戏,励志做一个游戏开发者,寒假学了unity,了解了unity是用c#写脚本,所以学了c#。很幸运,我面向对象是c#开的蒙,我英语不好(看看我设的变量名就知道啊哈哈哈),而vs的中文本地化做的真的特别好,用起来很舒服。我们学校的计算机专业不教c#,这学期开的课是Java,我一翻Java的书,笑了,这不和c#几乎一样吗(就个人浅薄的知识储备而言,大佬勿喷),所以我c#也没白学,为我之后的Java学习扫清了许多障碍。
这个项目也是我第一个用面向对象思想做的项目,其实还有很多地方还是用的c语言的思想。因为我之前玩过单片机嘛,所以我最开始想做游戏里的固定方块时,想的是做一个16进制数组,然后检验下落用位与运算判断是否有方块(哈哈i2c学到的),后来一想好像用不着这样,这又不是八位机,用不着这么扣扣嗖嗖。
总之,通过这个项目确实锻炼了我的能力,写这几篇博客,也理顺了我的思路,在写博客的同时,我在站内看了几篇同类型的文章,感觉我讲的还是蛮清楚的(我讲的主要是思路,代码写的挺丑的),所以希望看到这的码u能给我一个关注,这样博主会很开心的!
之后,我要开始学32了,我会把一些学习过程中的经验发在这里。怎么说吧,慢慢来吧,我才大一,以后的路还很长,大家一起加油!!!