c#.net之窗体应用程序实现简单版别踩白块儿(加分+点击白块死亡+黑块落下死亡)

设计界面如下:(如有不足,欢迎批评)c#.net之窗体应用程序实现简单版别踩白块儿(加分+点击白块死亡+黑块落下死亡)_第1张图片
运行效果如下:c#.net之窗体应用程序实现简单版别踩白块儿(加分+点击白块死亡+黑块落下死亡)_第2张图片

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 别踩白块
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /*需求分析:
             1.创建游戏区
             2.游戏区每行4个,
             3.实现每行随机白块与黑块
             4.点击黑块得分+1
             5.点击白块游戏结束
             */
        Random r = new Random();
        //创建游戏区
        Panel bg = new Panel();
        //创建白块与黑块
        Timer greatkuai = new Timer();
        //控制块下落
        Timer xl = new Timer();
        //记录分数
        int count = 0;
        Label defen = new Label();
        private void Form1_Load(object sender, EventArgs e)
        {
            //this.FormBorderStyle = FormBorderStyle.None;
            this.Size = new Size(500,700);
            this.Location = new Point(Screen.PrimaryScreen.Bounds.Width/2-this.Width/2,Screen.PrimaryScreen.Bounds.Height/2-this.Height/2);

            //创建游戏区
            bg.Size = new Size(400,700);
            bg.Location = new Point(0,0);
            bg.BackColor = Color.White;
            this.Controls.Add(bg);

            //创建白块与黑块
            greatkuai.Interval = 1000;
            greatkuai.Tick += Greatkuai_Tick;
            greatkuai.Start();

            //控制块下落
            xl.Interval =20;
            xl.Tick += Xl_Tick;
            xl.Start();

           //得分
            defen.Text = 0 + "";
            defen.Location = new Point(410,10);
            defen.BorderStyle = BorderStyle.FixedSingle;
            defen.Font = new Font("",20);
           // defen.BackColor = Color.Red;
            defen.AutoSize = true;
            this.Controls.Add(defen);

        }
        //创建白块与黑块
        private void Greatkuai_Tick(object sender, EventArgs e)
        {
            Label hei = new Label();
            hei.Size = new Size(100,170);
            hei.BackColor = Color.Black;
            int p = r.Next(0,4) * 100;
            hei.Location = new Point(p,-170);
            hei.BorderStyle = BorderStyle.FixedSingle;
            bg.Controls.Add(hei);
            hei.MouseClick += Hei_MouseClick;
            Label bai1 = bai();
            Label bai2 = bai();
            Label bai3 = bai();

            switch (p)
            {
                case 0:
                    bai1.Location = new Point(100,-170);
                    bai2.Location = new Point(2*100, -170);
                    bai3.Location = new Point(3*100, -170);
                    break;
                case 100:
                    bai1.Location = new Point(0, -170);
                    bai2.Location = new Point(2 * 100, -170);
                    bai3.Location = new Point(3 * 100, -170);
                    break;
                case 200:
                    bai1.Location = new Point(0, -170);
                    bai2.Location = new Point(100, -170);
                    bai3.Location = new Point(3 * 100, -170);
                    break;
                case 300:
                    bai1.Location = new Point(0, -170);
                    bai2.Location = new Point(1 * 100, -170);
                    bai3.Location = new Point(2 * 100, -170);
                    break;
            }
        }
        //创建白块
        private Label bai()
        {
            Label bai = new Label();
            bai.Size = new Size(100,170);
            bai.BorderStyle = BorderStyle.FixedSingle;
            bai.BackColor = Color.White;
            bg.Controls.Add(bai);
            bai.MouseClick += Bai_MouseClick;
            return bai;
        }
        //黑块点击事件
        private void Hei_MouseClick(object sender, MouseEventArgs e)
        {
            Label lab = sender as Label;
            lab.BackColor = Color.White;
            count += 1;
            defen.Text = count + "分";
        }
        //白块点击事件
        private void Bai_MouseClick(object sender, MouseEventArgs e)
        {
            Label lab1 = sender as Label;
            greatkuai.Stop();
            xl.Stop();
            MessageBox.Show("游戏结束");
        }

        //控制块下落
        private void Xl_Tick(object sender, EventArgs e)
        {
            foreach (Control  item in bg.Controls)
            {
                item.Top += 5;
                if (item.Top>=bg.Height)
                {
                    if (item.BackColor==Color.Black)
                    {
                        greatkuai.Stop();
                        xl.Stop();
                        MessageBox.Show("游戏结束");
                    }
                    else
                    {
                        item.Dispose();
                    }
                }
            }
        }
        //右键关闭
        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

你可能感兴趣的:(c#.net之窗体应用程序实现简单版别踩白块儿(加分+点击白块死亡+黑块落下死亡))