[英]C# Making progress bar
好吧,我有点泡菜,我在做游戏,而且我有经验。
现在,为了使玩家升级,它需要安装xp窗帘。
所以播放器有20xp,而他需要40xp,我希望进度条显示为50%已满。
不能弄清楚,进度条的宽度也是260px,我需要50%的进度条时,进度条的宽度是否必须是130px长,而在100%完成时,我的宽度是260px?
希望我提供了足够的信息,我正在将标签扩展到窗帘宽度(260%)作为进度条。
编辑:
使用PictureBox每次点击即可获得10xp的测试条。
private void pictureBox2_Click(object sender, EventArgs e)
{
GetXP();
}
private void GetXP()
{
int XpBarWidth = 260;
int PlayerXP = Convert.ToInt32(Exp.Text);
int NeededXP = xp_needed;
label1.Text = PlayerXP.ToString();
label4.Text = NeededXP.ToString();
int Ratio = (PlayerXP / NeededXP);
XpBar.Width = (int)(XpBarWidth * Ratio);
XpBar.Refresh();
}
Label1和Label4在何处测试PlayerXP和NeededXP int。
您可以使用Winform命名空间中内置的进度栏。
因此,在您的实例中,您将在每个级别上设置进度条的值
所以步骤是
然后,只要您想升级或增加经验
private void LevelUp()
{
progressBar1.Maximum = 100; //new experience needed
}
private void ShowExperience(int xp)
{
progressBar1.Value += xp; // Add the xp
}
这就是您的体验吧,以及体验的增加
我还在下面提供了一个工作示例的链接
链接到进度栏样式
http://www.dotnetperls.com/progressbar
一些样式代码
progressBar1.BackColor = Color.Aqua;
progressBar1.ForeColor = Color.White;
// you can also use images
progressBar1.BackgroundImage = new Bitmap("");
double XpBarWidth = 260; // maximum width of your xp bar
double Player.XP = 20; // current xp of your player
double NeededXP = 40; // xp needed for next lvl
double ratio = (Player.XP / NeededXP);
Label XpLabel = ... // the label where you want to display the xp
XpLabel.Width = (int)(XpBarWidth * ratio);
更新:在更新的问题中,将PlayerXP
和NeededXP
都定义为int
。 除以它们时,只要PlayerXP
小于NeededXP
,您将得到0
。 投射/将其更改为两倍以获取正确的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.