C#简易别踩白块

在这里插入图片描述

分析:
1.实现黑块位置的随机
2.通过黑块的位置判断白块的可能性
3.实现基本的功能等;
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();
        }
        bool s = true;
        Timer tim = new Timer();
        Timer tim1 = new Timer();
        private void Form1_Load(object sender, EventArgs e)
        {
            panel1.Width = 360;
            tim.Interval = 1000;
            label2.Text = "分数:" + i;
            tim.Tick += Tim_Tick;
            tim1.Interval = 50;
            tim1.Tick += Tim1_Tick;
            label1.Click += Label1_Click;

        }
        int i = 0;
        private void Label1_Click(object sender, EventArgs e)
        {
            if (s == true)
            {
                tim.Start();
                tim1.Start();
                label1.Text = "暂停游戏";
                label1.BackColor = Color.Pink;
                s = false;
            }
            else
            {
                tim.Stop();
                tim1.Stop();
                label1.Text = "开始游戏";
                label1.BackColor = Color.Black;
                s = true;
            }
        }

       
           
        private void Tim1_Tick(object sender, EventArgs e)
        {
            foreach (Control item in panel1.Controls)
            {
                item.Top += 5;
                if (item.BackColor==Color.Black)
                {
                     if (item.Top >= panel1.Height-item.Height)
                     {
                         tim.Stop();
                         tim1.Stop();
                         MessageBox.Show("Game Over");
                     }
                }
            }
        }

        Random r = new Random();
        private void Tim_Tick(object sender, EventArgs e)
        {
        //创建一个黑块随机它的left
            Label lb = new Label();
            lb.Size = new Size(90,90);
            lb.BorderStyle = BorderStyle.FixedSingle;
            int p = (r.Next(0, 4))*90;
            lb.BackColor = Color.Black;
            lb.MouseClick += Lb_MouseClick;
            lb.Location = new Point(p,-97);
            panel1.Controls.Add(lb);
            Label k1 = Lb();
            Label k2 = Lb();
            Label k3 = Lb();
            //通过给块出现的可能性写出白块的位置
            switch (p)
            {
                case 0:
                    k1.Location = new Point(90, -97);
                    k2.Location = new Point(2 * 90, -97);
                    k3.Location = new Point(3 * 90, -97);
                    break;
                case 90:
                    k1.Location = new Point(0 * 90, -97);
                    k2.Location = new Point(2 * 90, -97);
                    k3.Location = new Point(3 * 90, -97);
                    break;
                case 180:
                    k1.Location = new Point(0 * 90, -97);
                    k2.Location = new Point(1 * 90, -97);
                    k3.Location = new Point(3 * 90, -97);
                    break;
                case 270:
                    k1.Location = new Point(0 * 90, -97);
                    k2.Location = new Point(1 * 90, -97);
                    k3.Location = new Point(2 * 90, -97);
                    break;
            }

        }
//黑块的点击事件
        private void Lb_MouseClick(object sender, MouseEventArgs e)
        {
            Label lb = sender as Label;
            lb.BackColor = Color.White;
            i++;
            label2.Text = "分数:" + i;
          
        }
       
//创建Label
        Label Lb()
        {
            Label lb1 = new Label();
            //lb1.AutoSize = false;
            lb1.Size = new Size(90, 90);
            lb1.BorderStyle = BorderStyle.FixedSingle;
            lb1.BackColor = Color.White;
            lb1.Click += Lb1_Click;
            panel1.Controls.Add(lb1);
            return lb1;
        }

//白块的点击事件
        private void Lb1_Click(object sender, EventArgs e)
        {
            foreach (Control item in panel1.Controls)
            {
                if (item.BackColor == Color.White)
                {
                    tim.Stop();
                    tim1.Stop();
                    MessageBox.Show("Game Over");
                    item.Dispose();
                }
            }
        }
       
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值