简体   繁体   English

C#制作进度条

[英]C# Making progress bar

Ok im in a bit of a pickle, I am making a game, and i have experience in it. 好吧,我有点泡菜,我在做游戏,而且我有经验。

Now for the player to level up it needs to have a curtain xp. 现在,为了使玩家升级,它需要安装xp窗帘。

So Player has 20xp and he needs 40xp, I want the progress bar to show, 50% full. 所以播放器有20xp,而他需要40xp,我希望进度条显示为50%已满。

Cant figure that out, and also the progress bar is 260px wide and i need it to when its 50% full, have the width of the progress bar to be 130px long, and at 100% complete i need it to be 260px wide? 不能弄清楚,进度条的宽度也是260px,我需要50%的进度条时,进度条的宽度是否必须是130px长,而在100%完成时,我的宽度是260px?

Hope i gave enough info i am making a label expand to a curtain width (260%) to be my progress bar. 希望我提供了足够的信息,我正在将标签扩展到窗帘宽度(260%)作为进度条。

EDIT: 编辑:

Using PictureBox to get 10xp every click to test the bar. 使用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 and Label4 where to test the PlayerXP and the NeededXP ints. Label1和Label4在何处测试PlayerXP和NeededXP int。

What you can do is use the progress bar that is built within the Winform namespace. 您可以使用Winform命名空间中内置的进度栏。

So in your instance you would set the Value of the progress bar each level up 因此,在您的实例中,您将在每个级别上设置进度条的值

So the steps are 所以步骤是

  • Drag New progress bar //Set width 拖动新进度条//设置宽度

新表格

then when ever you want to level up or add experience 然后,只要您想升级或增加经验

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

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

So that is your experience bar, and the increase in experience as well 这就是您的体验吧,以及体验的增加

I have also included a link to a working example below 我还在下面提供了一个工作示例的链接

http://tinyurl.com/pflyvhu http://tinyurl.com/pflyvhu

Link to styling the Progress Bar 链接到进度栏样式

http://www.dotnetperls.com/progressbar http://www.dotnetperls.com/progressbar

Some Styling Code 一些样式代码

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

Update: In your updated question, you define both PlayerXP and NeededXP as int . 更新:在更新的问题中,将PlayerXPNeededXP都定义为int When you divide them, you'll get 0 as long as PlayerXP is less than NeededXP . 除以它们时,只要PlayerXP小于NeededXP ,您将得到0 Cast / change them to double to get correct results. 投射/将其更改为两倍以获取正确的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM