⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 俄罗斯方块游戏
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 俄罗斯方块
{
    public partial class Form1 : Form
    {
        private CurShow cs;
        private Shape s;
        private Record r;


        public Form1()
        {
            InitializeComponent();
            this.lbl_title.Text = "欢迎来到俄罗斯方块世界,按'k'键开始;'w,s,a,d'的方向键控制;以及'j'键变化图形;";//初始化开始的界面
            this.lbl_record.Text = "请开始吧!";



            //创建游戏舞台的对象
            cs = new CurShow(20, 10);


        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar == 'k')         //开始游戏
            {
                //初始化游戏
                this.lbl_title.Visible = false;
                cs.clearShow();//清空数组
                r = new Record(0);
                this.lbl_record.Text = "满行  0  次";
                timer1.Enabled = true;//使计时器生效

                s = createShape();
                timer1_Tick(null, null);
            }
            else if (e.KeyChar == 'l')      //停止游戏
            {
                timer1.Enabled = false;
            }
            else if (e.KeyChar == 'a')
            {
                s.MoveLeft(cs); Draw();
            }

            else if (e.KeyChar == 'd')
            {
                s.MoveRight(cs); Draw();
            }
            else if (e.KeyChar == 's')
            {
                s.Movedown(cs);
                timer1_Tick(null, null);
            }
            else if (e.KeyChar == 'w'||e.KeyChar == 'j')
            {
                s.ShiftShape(cs);
                Draw();
            }



        }





        public Shape createShape()   //随机产生一个形状
        {
            Random tmp = new Random();
            int n = tmp.Next() % 3;

            Shape shape = null;
            switch (n)
            {
                case 0: shape = new ShapeI(); break;
                case 1: shape = new ShapeJ(); break;
                case 2: shape = new ShapeL(); break;
            }
            return shape;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            this.Draw();

            if (!s.Autodown(cs))//若不能下落,说明新的图要进来,旧的图阵先保存好
            {
                if (cs.IsLose())//判定是否图阵顶部有true
                {
                    timer1.Enabled = false;
                    this.lbl_record.Text = "O(∩_∩)O哈哈~你输了!!";

                }
                else
                {
                    cs.saveArray(s);
                    r.AddRecord(cs.EraseFullRow());
                    s = createShape();
                    this.lbl_record.Text = "满行  " + r.SetFullNum + "  次";
                }
            }

        }

        private void Draw()
        {
            cs.DrawBack(CreateGraphics(), new SolidBrush(this.BackColor), new Pen(Color.Red));//画背景
            s.DrawShape(CreateGraphics(), new SolidBrush(Color.Red));//画图形
            cs.DrawShowShape(CreateGraphics(), new SolidBrush(Color.Blue));//画图阵
        }


    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -