C#制作进度条

时间:2014-02-21 10:13:00

标签: c# math 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整数的位置。

2 个答案:

答案 0 :(得分:4)

您可以使用Winform名称空间中构建的进度条。

因此,在您的实例中,您可以设置每个级别的进度条的值

所以步骤是

  • 拖动新进度条//设置宽度

New Form

然后什么时候你想升级或增加经验

        private void LevelUp()
        {
            progressBar1.Maximum = 100; //new experience needed
        }

        private void ShowExperience(int xp)
        {
            progressBar1.Value += xp; // Add the xp
        }

这就是你的经验吧,以及经验的增加

我还在下面提供了一个工作示例的链接

http://tinyurl.com/pflyvhu

链接到样式化进度条

http://www.dotnetperls.com/progressbar

一些样式代码

            progressBar1.BackColor = Color.Aqua;
            progressBar1.ForeColor = Color.White;

            // you can also use images

            progressBar1.BackgroundImage = new Bitmap("");

答案 1 :(得分:1)

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);

更新: 在更新的问题中,您将PlayerXPNeededXP定义为int。划分它们后,只要0小于PlayerXP,您就会获得NeededXP。将它们转换为两倍以获得正确的结果。