刚学了一点C#多线程的知识,写了个简单的打字游戏,
设了失误的次数,如允许失误次数为0时,游戏就结束显示一个LABEL提示。
一开始我先把LABEL隐藏了,游戏结束后再把LABEL显示出来,但现在无法把LABEL显示出来,
麻烦大大们能指点一下。
代码如下:public partial class Form1 : Form
{
Random ran = new Random(); //用作产生随机数
public Form1()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
} private void timer1_Tick(object sender, EventArgs e)
{
int x = 0; //用作X坐标
char c = (char)ran.Next(97, 122); //产生字符 x = ran.Next(10, this.Width); //X坐标用随机数生成
Label lbl = new Label(); lbl.BackColor = System.Drawing.Color.Transparent;//set the backcolor
lbl.Font = new System.Drawing.Font("黑体", 14F);
lbl.ForeColor = Color.Black;
lbl.Location = new System.Drawing.Point(x, 10);
lbl.Size = new System.Drawing.Size(20, 20);//label's size
lbl.Text = c.ToString(); this.Controls.Add(lbl);//加到控件上 //生成移动类
Motion move = new Motion(ref lbl, this); }
}
public class Motion
{
private Label _lbl;
private Form1 _myForm; //接收窗体对象
public static char input;
public static int sum; //记录得分
public static int verdict;//判断是否需要恢复线程
public static int LifeCount = 10; //生命计算器
public static int SleepTime = 500;//线程睡眠时间
public static int TimeCount; //提速的依据
public Thread thread;
public Motion(ref Label lbl, Form1 myForm)
{
_lbl = lbl;
_myForm = myForm;
//对每个LABEL都用一个线程控制
thread = new Thread(new ThreadStart(Move));
thread.Start();
} public void Move()
{
while (true)
{
if (LifeCount <= 0)//生命值小于等于0则中止线程
{
//把窗体上隐藏了的LABEL显示
_myForm.label1.Visible = true;
_myForm.label1.Text = "oh no!";
//禁用TIMER控件停止生成新的LABEL
_myForm.timer1.Enabled = false; break;
}
lock (this)
{ //if press key equal label's text so destroy it
if (input.ToString() == _lbl.Text)//判断字符是否相等,相等则消除
{
_lbl.Dispose();//destroy the label
input = ' ';
sum += 5;
_myForm.lblScore.Text = sum.ToString();
thread.Abort(); //字符消除后再中断线程
}
Thread.Sleep(SleepTime);//sleep 0.5 second that going to work again
_lbl.Top += 10; if (_lbl.Top > _myForm.Height - 100)
{
//LABEL的TOP属性大于窗体高度-100就中断该线程
_lbl.Dispose();//destroy the label --LifeCount; //到底部则减生命
_myForm.lblLife.Text = LifeCount.ToString();//先赋值再中断线程
thread.Abort();//abort thread
break;
} }//lock
}//while
}
}
设了失误的次数,如允许失误次数为0时,游戏就结束显示一个LABEL提示。
一开始我先把LABEL隐藏了,游戏结束后再把LABEL显示出来,但现在无法把LABEL显示出来,
麻烦大大们能指点一下。
代码如下:public partial class Form1 : Form
{
Random ran = new Random(); //用作产生随机数
public Form1()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
} private void timer1_Tick(object sender, EventArgs e)
{
int x = 0; //用作X坐标
char c = (char)ran.Next(97, 122); //产生字符 x = ran.Next(10, this.Width); //X坐标用随机数生成
Label lbl = new Label(); lbl.BackColor = System.Drawing.Color.Transparent;//set the backcolor
lbl.Font = new System.Drawing.Font("黑体", 14F);
lbl.ForeColor = Color.Black;
lbl.Location = new System.Drawing.Point(x, 10);
lbl.Size = new System.Drawing.Size(20, 20);//label's size
lbl.Text = c.ToString(); this.Controls.Add(lbl);//加到控件上 //生成移动类
Motion move = new Motion(ref lbl, this); }
}
public class Motion
{
private Label _lbl;
private Form1 _myForm; //接收窗体对象
public static char input;
public static int sum; //记录得分
public static int verdict;//判断是否需要恢复线程
public static int LifeCount = 10; //生命计算器
public static int SleepTime = 500;//线程睡眠时间
public static int TimeCount; //提速的依据
public Thread thread;
public Motion(ref Label lbl, Form1 myForm)
{
_lbl = lbl;
_myForm = myForm;
//对每个LABEL都用一个线程控制
thread = new Thread(new ThreadStart(Move));
thread.Start();
} public void Move()
{
while (true)
{
if (LifeCount <= 0)//生命值小于等于0则中止线程
{
//把窗体上隐藏了的LABEL显示
_myForm.label1.Visible = true;
_myForm.label1.Text = "oh no!";
//禁用TIMER控件停止生成新的LABEL
_myForm.timer1.Enabled = false; break;
}
lock (this)
{ //if press key equal label's text so destroy it
if (input.ToString() == _lbl.Text)//判断字符是否相等,相等则消除
{
_lbl.Dispose();//destroy the label
input = ' ';
sum += 5;
_myForm.lblScore.Text = sum.ToString();
thread.Abort(); //字符消除后再中断线程
}
Thread.Sleep(SleepTime);//sleep 0.5 second that going to work again
_lbl.Top += 10; if (_lbl.Top > _myForm.Height - 100)
{
//LABEL的TOP属性大于窗体高度-100就中断该线程
_lbl.Dispose();//destroy the label --LifeCount; //到底部则减生命
_myForm.lblLife.Text = LifeCount.ToString();//先赋值再中断线程
thread.Abort();//abort thread
break;
} }//lock
}//while
}
}
我在这里判断,游戏结束
while (true)
{
if (LifeCount <= 0)//生命值小于等于0则中止线程
{
//把窗体上隐藏了的LABEL显示
_myForm.label1.Visible = true;
_myForm.label1.Text = "oh no!";
//禁用TIMER控件停止生成新的LABEL
_myForm.timer1.Enabled = false; break;
}
但无法把LABEL显示出来。
{
//把窗体上隐藏了的LABEL显示
_myForm.label1.Visible = isShow;
_myForm.label1.Text = "oh no!";
//禁用TIMER控件停止生成新的LABEL
_myForm.timer1.Enabled = !isShow;
}
delegate void InvokeShow(bool isShow);while (true)
{
if (LifeCount <= 0)//生命值小于等于0则中止线程
{ Invoke(InvokeShow(LabelShow), true); break;
}
“错误 1 “WindowsApplication1.Motion.InvokeShow”是“类型”,但此处被当做“变量”来使用 ”