当前位置: 代码迷 >> 综合 >> 别踩白块c#小案例
  详细解决方案

别踩白块c#小案例

热度:16   发布时间:2023-09-05 19:20:21.0

别踩白块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 别踩白块
{public partial class Form1 : Form{public Form1(){InitializeComponent();}Panel BG = new Panel();//游戏区域Timer diao = new Timer();//掉落的timerTimer hei = new Timer();//生成黑块的timerButton kg = new Button();//开关按钮Label jf = new Label();//计分板private void Form1_Load(object sender, EventArgs e){this.WindowState = FormWindowState.Maximized;BG.Size = new Size(400,1000);BG.BackColor = Color.Pink;BG.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width/2-BG.Width/2,Screen.PrimaryScreen.WorkingArea.Height/2-BG.Height/2);this.Controls.Add(BG);//控制按钮kg.Text = "开始游戏";kg.AutoSize = true;kg.Location = new Point(BG.Left+BG.Width+50,50);kg.Click += Kg_Click;this.Controls.Add(kg);//掉落diao.Interval = 20;diao.Tick += Diao_Tick;//生成块块hei.Interval = 1000;hei.Tick += Hei_Tick;//计分板jf.Text = "得分:" + 0 + "分";jf.Location = new Point(BG.Left+BG.Width+50,150);this.Controls.Add(jf);}//游戏区域宽度固定  随机黑块的出现  因为一行就只有一个黑块(暂时)Random r = new Random();//生成一个随机数private void Hei_Tick(object sender, EventArgs e){   //黑块Label x = new Label();x.BorderStyle = BorderStyle.Fixed3D;x.BackColor = Color.Black;x.Size = new Size(100,200);int j = r.Next(0, 4) * 100;//一共宽就800平均随机就四种  left:  0  200  400  600x.Location = new Point(j,-200);//随机黑块的位置x.Click += X_Click;BG.Controls.Add(x);//白块Label y = new Label();y.Click += Y_Click;y.BorderStyle = BorderStyle.Fixed3D;y.Size = new Size(100, 200);y.BackColor = Color.White;BG.Controls.Add(y);Label z = new Label();z.Click += Z_Click;z.BorderStyle = BorderStyle.Fixed3D;z.Size = new Size(100, 200);z.BackColor = Color.White;BG.Controls.Add(z);Label c = new Label();c.Click += C_Click;c.BorderStyle = BorderStyle.Fixed3D;c.Size = new Size(100, 200);c.BackColor = Color.White;BG.Controls.Add(c);switch (j){case 0:y.Location = new Point(100, -200);z.Location = new Point(200, -200);c.Location = new Point(300, -200);break;case 100:y.Location = new Point(0, -200);z.Location = new Point(200, -200);c.Location = new Point(300, -200);break;case 200:y.Location = new Point(0, -200);z.Location = new Point(100, -200);c.Location = new Point(300, -200);break;case 300:y.Location = new Point(0, -200);z.Location = new Point(100, -200);c.Location = new Point(200, -200);break;default:break;}}private void C_Click(object sender, EventArgs e){hei.Stop();diao.Stop();MessageBox.Show("请重新开始");}private void Z_Click(object sender, EventArgs e){hei.Stop();diao.Stop();MessageBox.Show("请重新开始");}private void Y_Click(object sender, EventArgs e){hei.Stop();diao.Stop();MessageBox.Show("请重新开始");}//黑块的点击事件int f = 0;//定义计分变量private void X_Click(object sender, EventArgs e){Label s = sender as Label;s.BackColor = Color.White;f++;jf.Text = "得分:" + f + "分";}//掉落的timerprivate void Diao_Tick(object sender, EventArgs e){foreach (Control it in BG.Controls){it.Top += 5;if (it.BackColor==Color.White&&it.Top>=BG.Height){it.Dispose();}else if (it.BackColor == Color.Black && it.Top >= BG.Height){ hei.Stop();diao.Stop();MessageBox.Show("请重新开始");}}}//开关按钮private void Kg_Click(object sender, EventArgs e){if (kg.Text == "开始游戏"){hei.Start();diao.Start();kg.Text = "暂停游戏";}else if (kg.Text=="暂停游戏"){hei.Stop();diao.Stop();kg.Text = "开始游戏";}}}
}

 

  相关解决方案