练习1
尝试利用定时器控制例题10-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 WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
///设置两个progressBar的初始值
InitializeComponent();
progressBar1.Maximum = 100;
progressBar1.Minimum = 0;
progressBar2.Maximum = 100;
progressBar2.Minimum = 0;
progressBar1.Step = 5;
progressBar2.Step = 8;
timer1.Enabled = true;
}
/// <summary>
/// 定时器启动之后的操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
progressbar2();
progressbar1();
}
/// <summary>
/// 第一根进度条的操作
/// </summary>
private void progressbar1()
{
progressBar1.PerformStep();
if(progressBar1.Value>=100)
{
progressBar2.Value = 100;
timer1.Enabled = false;
//将定时器关掉
MessageBox.Show("进度完成!");
}
}
/// <summary>
/// 第二根进度条的操作
/// </summary>
private void progressbar2()
{
progressBar2.PerformStep();
if(progressBar2.Value>=100)
{
progressBar2.Value = 0;
}
}
}
}
-
结果
练习2
直接抄例题
练习3
直接抄例题
练习4
本来想用线程池写,但是出错了,不能使用static关键字,然后用timer写了一点点,进度条不能小于0和大于100的问题解决不了,当血条大于100或者小于0的时候就会退出程序,发生异常。先把这部分代码贴出来。
- 代码
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;
/// <summary>
/// 使用多线程打怪物小游戏,怪物和人类分别拥有血量属性,人类拥有姓名属性,物理攻击
/// 和法术攻击的方法,在主程序中调用两种方法,使人类用物理攻击和法术攻击击打怪物
/// 并实时输出怪物血量,直至血量为0
/// </summary>
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
int delta = 10;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//HumanBlood.Maximum = 100;
HumanBlood.Value = 0;
//MonsterBlood.Maximum = 100;
MonsterBlood.Value = 0;
pictureMonster.Image = Image.FromFile(@"C:\Users\Hab_L\Desktop\怪物.jpg");
}
private void radioButton1_Click(object sender, EventArgs e)
{
pictureWoman.Image = Image.FromFile(@"C:\Users\Hab_L\Desktop\male.jpg");
}
private void radioButton2_Click(object sender, EventArgs e)
{
pictureWoman.Image = Image.FromFile(@"C:\Users\Hab_L\Desktop\female.png");
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
//怪物攻击是定时的
MonsterMagic.Image = Image.FromFile(@"C:\Users\Hab_L\Desktop\monstermagic(已去底).jpg");
int OrX = MonsterMagic.Location.X;
int OrY = MonsterMagic.Location.Y;
int i = MonsterMagic.Location.X + delta;
MonsterMagic.Location = new Point(i, MonsterMagic.Location.Y);
if (i >= ((pictureWoman.Location.X)- pictureWoman.Width))
{
delta = -i+ pictureMonster.Width;
HumanBlood.Value = HumanBlood.Value + 10;
}
else if(i<=pictureMonster.Location.X+ pictureMonster.Width)
{
delta = 10;
}
}
private void firstAgression_Click(object sender, EventArgs e)
{
timer2.Enabled = true;
}
private void timer2_Tick(object sender, EventArgs e)
{
picturePhysics.Image = Image.FromFile(@"C:\Users\Hab_L\Desktop\箭.jpg");
int OrX = picturePhysics.Location.X;
int OrY = picturePhysics.Location.Y;
int i = picturePhysics.Location.X - delta;
picturePhysics.Location = new Point(i, MonsterMagic.Location.Y);
if (i <= ((pictureMonster.Location.X)+ pictureMonster.Width))//+ pictureMonster.Width)
{
delta = -i;
MonsterBlood.Value = MonsterBlood.Value + 10;
if (MonsterBlood.Value >= 100)
{
//MonsterBlood.Value = 10;
MessageBox.Show("恭喜!你赢了!");
timer2.Enabled = false;
}
}
else if (i >= ((pictureWoman.Location.X) - pictureWoman.Width))
{
delta = 10;
}
}
private void picturePhysics_Click(object sender, EventArgs e)
{
}
private void timer3_Tick(object sender, EventArgs e)
{
if (MonsterBlood.Value <= 0)
{
MonsterBlood.Value = 0;
MessageBox.Show("恭喜!你赢了!");
timer2.Enabled = false;
}
}
}
}