Sharing C# development about a freshman Tetris project, with source code attached (4)

Overall process

It’s the last section!

Since the code when I explain is incomplete, I will put the source code in the appendix (new bloggers are so serious, please pay attention!)

First, turn off the timer and draw the chessboard

 timer1.Enabled = false;
 DrawBoard();

Generate object

 if(tstart)//生成随机枚举类型控制游戏对象类型
            {
                Random rd = new Random();
                random=rd.Next(0,7);
                angle = 1;
                tstart = false;
            }

 GO gO = (GO)random;

Initialization of temporary dropped objects

            byte tempAngle;
            bool tangle = false;
            bool tfall = false;
            tempAngle = angle;
            Tpoint tempcenturePoint = new Tpoint();
            tempcenturePoint.x = centureX;
            tempcenturePoint.y = centureY;

Key detection (knock on the blackboard!!)

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;
             
            }

Drop detection

 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;

Drawing after detection

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;

related functions:

 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;
        }

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbWFtYXdoZXM=,size_20,color_FFFFFF,t_70,g_se,x_16

 

A copy of a sketch drawn in class

Okay, that’s all the content of this project. I will put the source code in the next article. I am a student and I usually have to go to class, so I don’t have much time and energy to write this. If you still don’t understand, go to Take a look at my source code. If you have any improvement methods, please put them forward in the comment area. Don’t forget to follow the blogger!

 

Finally, let me share some thoughts after finishing it. It was completed last Saturday night. I was really crazy about it. I asked my roommate to demonstrate my game and watched the blocks jumping on the screen, rotating, falling, and eliminating. I felt a sense of accomplishment beyond words. Before doing this project, I only watched some c# teaching videos on site b, and I didn’t even know what GDI drawing was for. C# is my first object-oriented language. Because I like games very much and am inspired to be a game developer, I learned Unity during the winter vacation. I learned that Unity uses C# to write scripts, so I learned C#. Fortunately, I am familiar with object-oriented programming in C#, and my English is not good (you will know just by looking at the variable names I set, hahaha), and the Chinese localization of VS is really good, and it is very comfortable to use. The computer major in our school does not teach C#. The course offered this semester is Java. I flipped through the Java book and laughed. Isn’t this almost the same as C# (in terms of my shallow knowledge reserve, please don’t criticize me). So I didn’t learn C# in vain, and it cleared many obstacles for my future Java learning.

This project is also my first project using object-oriented thinking. In fact, there are still many places where C language thinking is still used. Because I have played with microcontrollers before, when I first wanted to make fixed blocks in the game, I wanted to make a hexadecimal array, and then check the whereabouts and use bitwise AND operations to determine whether there are blocks (haha, learned from i2c). Later, when I thought about it, it seemed that there was no need to do this. This is not an eight-bit machine, so there was no need to click and swish like this.

In short, this project has really exercised my abilities. Writing these blogs has also streamlined my thinking. While writing blogs, I read several articles of the same type on the site, and I feel that what I said is quite good. It's clear (I'm mainly talking about ideas, the code is quite ugly), so I hope that seeing this code can give me some attention, so that bloggers will be very happy!

After that, I will start learning 32, and I will post some experiences in the learning process here. Let me put it this way, take your time. I am only a freshman and there is still a long way to go. Let’s work hard together! ! !

 

 

 

Guess you like

Origin blog.csdn.net/qq_38830492/article/details/123654826