简体   繁体   中英

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.

So Player has 20xp and he needs 40xp, I want the progress bar to show, 50% full.

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?

Hope i gave enough info i am making a label expand to a curtain width (260%) to be my progress bar.

EDIT:

Using PictureBox to get 10xp every click to test the bar.

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.

What you can do is use the progress bar that is built within the Winform namespace.

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

Link to styling the Progress Bar

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 . When you divide them, you'll get 0 as long as PlayerXP is less than NeededXP . Cast / change them to double to get correct results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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