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 别踩白块er
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Button kaiguan = new Button();
        private void Form1_Load(object sender, EventArgs e)
        {
            //游戏区域
            panel1.Size = new Size(400,600);
            panel1.BackColor = Color.White;
            panel1.Location = new Point(0,0);
            this.Size = new Size(500,600);
            this.Controls.Add(panel1);

            kaiguan.Text = "开始游戏";
            kaiguan.Location = new Point(500,200);
            this.Controls.Add(kaiguan);
            kaiguan.Click += Kaiguan_Click;

        }

        private void Kaiguan_Click(object sender, EventArgs e)
        {
            if (kaiguan.Text=="开始游戏")
            {
                timer1.Start();
                timer2.Start();
                kaiguan.Text = "游戏暂停";
            }
            else if (kaiguan.Text=="游戏暂停")
            {
                timer1.Stop();
                timer2.Stop();
                kaiguan.Text = "开始游戏";
            }
        }

        Random ran = new Random();
        
        private void timer1_Tick(object sender, EventArgs e)
        {
            //黑键
            Label hei = new Label();
            hei.Size = new Size(100,100);
            hei.BackColor = Color.Black;
            //黑left
            int p = ran.Next(0,4) * 100;
            hei.BorderStyle = BorderStyle.FixedSingle;
            hei.Location = new Point(p,-100);
            panel1.Controls.Add(hei);
            hei.MouseClick += Hei_MouseClick;
            Label lab1 = kuai();
            Label lab2 = kuai();
            Label lab3= kuai();

            switch (p)
            {
                case 0:
                    lab1.Location = new Point(1*100,-100);
                    lab2.Location = new Point(2 * 100, -100);
                    lab3.Location = new Point(3 * 100, -100);
                    break;
                case 100:
                    lab1.Location = new Point(0,-100);
                    lab2.Location = new Point(2*100,-100);
                    lab3.Location = new Point(3*100,-100);
                    break;
                case 200:
                    lab1.Location = new Point(0, -100);
                    lab2.Location = new Point(1 * 100, -100);
                    lab3.Location = new Point(3 * 100, -100);
                    break;
                case 300:
                    lab1.Location = new Point(0, -100);
                    lab2.Location = new Point(1 * 100, -100);
                    lab3.Location = new Point(2 * 100, -100);
                    break;
            }
        }
        //黑块单机事件
        private void Hei_MouseClick(object sender, MouseEventArgs e)
        {
            Label lab = sender as Label;
            lab.BackColor = Color.White;
        }
       private   Label kuai()
        {
            //白键
            Label lab = new Label();
            lab.Size = new Size(100,100);
            lab.BorderStyle = BorderStyle.FixedSingle;
            lab.BackColor = Color.White;
            panel1.Controls.Add(lab);
            lab.MouseClick += Lab_MouseClick;
            return lab;

        }
        //白块单机事件
        private void Lab_MouseClick(object sender, MouseEventArgs e)
        {
            Label lab = sender as Label;
            lab.BackColor = Color.Red;
            timer2.Stop();
            timer1.Stop();
            MessageBox.Show("Game over");
        
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            foreach (Control  item in panel1.Controls)
            {
                item.Top += 2;
                if (item.Top >= panel1.Height)
                {
                   
                    if (item.BackColor==Color.Black)
                    {
                    timer1.Stop();
                    timer2.Stop();
                    MessageBox.Show("Game over");

                    }
                    else
                    {
                        item.Dispose();
                    }
                }
             

            }
        }
    }
}

 

 

 


 

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值