关于一个大一学生的俄罗斯方块项目分享C#开发,附源码(附录)

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 Mamawhes_Tetris
{
    public   enum  GO {I=0,L,O,Z,T,tL,tZ };
    public enum Keypress { NULL,A,S,D,J,K};
    public partial class Form1 : Form
    {

        private const byte size = 20;//方块大小
        private const int xOy_x = 200, xOy_y = 60;//坐标原点
        private byte centureX=5;
        private byte centureY=0;
        private GO gO;
        private int random;
        private bool tstart=true;
        private byte angle = 1;
        private Keypress Keypress=Keypress.NULL;
        private int response = 0;
        public int score = 0;
        private bool[,] staticBlock;
        private bool timer=true;


        public byte labelColor = 0;


        public Form1()
        {
            InitializeComponent();
            staticBlock = new bool[20, 10];
            for (byte i=0; i < 20; i++)
                for (byte j=0; j < 10; j++)
                    staticBlock[i, j] = false;
            //staticBlock[4, 2] = true ;
            timer1.Interval = 100;
            timer1.Enabled = true;

        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }


        private void timer1_Tick(object sender, EventArgs e)
        {

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

        
        public void StaticBlock(bool[,] array)     
        {
            for (byte i = 0; i < 20; i++)
                for (byte j = 0; j < 10; j++)
                    if (array[i, j])
                        DrawOneExtenBlock(j, i);
        }
        public void DrawOneExtenBlock(byte x,byte y)
        {
            Graphics centureBlock = this.CreateGraphics();
            Pen blockPen = new Pen(Brushes.Lime);
            Point p1 = new Point(xOy_x + x * size + 1, xOy_y + y * size + 1);
            Point p2 = new Point(xOy_x + (x + 1) * size - 1, xOy_y + y * size + 1);
            Point p3 = new Point(xOy_x + x * size + 1, xOy_y + (y + 1) * size - 1);
            Point p4 = new Point(xOy_x + (x + 1) * size - 1, xOy_y + (y + 1) * size - 1);
            centureBlock.DrawLine(blockPen, p1, p4);
            centureBlock.DrawLine(blockPen, p2, p3);
            Rectangle rectangle = new Rectangle(xOy_x + x * size + 1, xOy_y + y * size + 1, size - 2, size - 2);
            centureBlock.DrawRectangle(blockPen, rectangle);

        }
        public void MoveBlock(GO type,Tpoint[] point4)
        {
            for (byte i=0;i<4;i++)
           DrawOneMoveBlock(type,(byte)point4[i].x, (byte)point4[i].y); 
        }
        public void DrawBoard()
        {
                Graphics board = this.CreateGraphics();
                Pen pen = new Pen(Brushes.Gray);
            //画实心黑色矩形,清屏
            //Rectangle rectangle = new Rectangle(xOy_x, xOy_y, size*10, size*20);
            //Brush b = new SolidBrush(Color.Black);
            //board.FillRectangle(b, rectangle);
            //画10x20棋盘
            for (byte i = 0; i <= 20; i++)
                {
                    Point p00 = new Point(xOy_x, xOy_y + size * i);
                    Point p01 = new Point(xOy_x + size * 10, xOy_y + size * i);
                    board.DrawLine(pen, p00, p01);
                }
                for (byte i = 0; i <= 10; i++)
                {
                    Point p00 = new Point(xOy_x + size * i, xOy_y);
                    Point p01 = new Point(xOy_x + size * i, xOy_y + size * 20);
                    board.DrawLine(pen, p00, p01);
                }
        }
        public void DrawOneMoveBlock(GO gameObject, byte x, byte y)
        {
            if (y >= 4)
            {
             y -= 4;
            Graphics centureBlock = this.CreateGraphics();
            Brush brush;
            switch(gameObject)
            {
                case GO.I:brush = Brushes.Cyan;break;
                case GO.L:brush = Brushes.Magenta;break;
                case GO.O:brush = Brushes.Yellow;break;
                case GO.Z:brush = Brushes.DeepSkyBlue;break;
                case GO.T:brush = Brushes.DarkOrchid;break;
                case GO.tL: brush = Brushes.OrangeRed;break;
                case GO.tZ: brush = Brushes.SpringGreen;break;
                default:brush = Brushes.Black;break;
            }
            Pen blockPen = new Pen(brush);
            Rectangle rectangle = new Rectangle(xOy_x + x * size , xOy_y + y * size , size , size );
            centureBlock.DrawRectangle(blockPen, rectangle);
            }
            
            
        }

        public bool CrachCheck(Tpoint[] point4)
        {
            
            for(byte i=0;i<4;i++)
            {
               
                if(point4[i].x<0|| point4[i].x>9 || point4[i].y == 24)
                {

                    return false;
                }
                if(point4[i].y>4&& point4[i].y<23)
                {
                    if (staticBlock[point4[i].y - 4, point4[i].x])
                        return false;
                }
            }
            return true;
        }
        private bool FallCheck(Tpoint[] point4)
        {
            for (byte i = 0; i < 4; i++)
            {

                
                if (point4[i].x < 0 || point4[i].x > 9)
                {

                    break;
                }
                if(point4[i].y == 24)
                {
                    return false;
                }
                if (point4[i].y > 4 )
                {
                    if (staticBlock[point4[i].y - 4, point4[i].x])
                        return false;
                }
            }
            return true;
        }
        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;
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.J)
            {
                Keypress = Keypress.J;
            }
            if (e.KeyCode == Keys.A)
            {
                Keypress = Keypress.A;
            }
            if (e.KeyCode == Keys.D)
            {
                Keypress = Keypress.D;
            }
            if (e.KeyCode == Keys.S)
            {
                Keypress = Keypress.S;
            }
        }

        private void label3_Click(object sender, EventArgs e)//彩蛋
        {
            labelColor++;
            switch (labelColor)
            {
                case 1: this.label3.ForeColor = Color.White; break;
                case 2: this.label3.ForeColor = Color.LightBlue; break;
                case 3: this.label3.ForeColor = Color.LawnGreen; break;
                case 4: this.label3.ForeColor = Color.LightPink; break;
                case 5: this.label3.ForeColor = Color.LightYellow; break;
                case 6: this.label3.ForeColor = Color.Brown; break;
                case 7: this.label3.ForeColor = Color.Red; break;
                case 8: this.label3.ForeColor = Color.Gray; break;
                case 9: this.label3.ForeColor = Color.Magenta; break;
                default: labelColor = 1; break;
            }

        }


    }

}

Form1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mamawhes_Tetris
{
   public class Tpoint
    {
       public int x;
       public int y;
    }
}

Tpoint.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mamawhes_Tetris
{
    class Objectgame 
    {
        private GO type;
        private Tpoint centurePoint;
        private Tpoint[] point4;
        private byte angle;
        private int[,] vectorX;
        private int[,] vectorY;
       public Objectgame()
        {
            point4 = new Tpoint[4];
            //centurePoint = new Tpoint();
            vectorX = new int[7, 3];
            vectorY = new int[7, 3];
            int n;//这是预先导入的矢量
            n = 0;
            vectorX[n, 0] = 0; vectorX[n, 1] = 0; vectorX[n, 2] = 0;
            vectorY[n, 0] = -1; vectorY[n, 1] = 1; vectorY[n, 2] = 2;
            n = 1;
            vectorX[n, 0] = 0; vectorX[n, 1] = 0; vectorX[n, 2] = 1;
            vectorY[n, 0] = -1; vectorY[n, 1] = 1; vectorY[n, 2] = 1;
            n = 2;
            vectorX[n, 0] = 0; vectorX[n, 1] = 1; vectorX[n, 2] = 1;
            vectorY[n, 0] = -1; vectorY[n, 1] =-1; vectorY[n, 2] = 0;
            n = 3;
            vectorX[n, 0] = 0; vectorX[n, 1] = 1; vectorX[n, 2] = 1;
            vectorY[n, 0] = -1; vectorY[n, 1] = 0; vectorY[n, 2] = 1;
            n = 4;
            vectorX[n, 0] = -1; vectorX[n, 1] = 1; vectorX[n, 2] = 0;
            vectorY[n, 0] = 0; vectorY[n, 1] = 0; vectorY[n, 2] = 1;
            n = 5;
            vectorX[n, 0] = 0; vectorX[n, 1] = 0; vectorX[n, 2] = -1;
            vectorY[n, 0] = -1; vectorY[n, 1] = 1; vectorY[n, 2] = 1;
            n = 6;
            vectorX[n, 0] = 0; vectorX[n, 1] = -1; vectorX[n, 2] = -1;
            vectorY[n, 0] = -1; vectorY[n, 1] = 0; vectorY[n, 2] = 1;
            
        }
        public Tpoint[] GetPoint(GO t,Tpoint p,byte a)//旋转角度=90*(a-1)a为1—4
        {
            type = t;
             centurePoint = p;
            angle = a;
            point4[0] = new Tpoint();
            point4[1] = new Tpoint();
            point4[2] = new Tpoint();
            point4[3] = new Tpoint();

            point4[0].x= centurePoint.x;
            point4[0].y = centurePoint.y;
            for (byte i=1;i<4;i++)
            {
                int dx, dy,temp;
                dx = vectorX[(int)type, i-1];
                dy = vectorY[(int)type, i-1];
                for (byte j=0;j<angle-1;j++)
                {
                    temp = dx;dx = dy;dy = -temp;

                }
                point4[i].x = centurePoint.x+dx;
                point4[i].y = centurePoint.y+dy;
                
            }
                return point4;//返回一个坐标数组,包含下落对象的四个坐标
        }
       
        
    }

}

Objectgame.cs

猜你喜欢

转载自blog.csdn.net/qq_38830492/article/details/123657457
今日推荐