Reputation: 385
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.
Upvotes: 0
Views: 749
Reputation: 4658
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.
Upvotes: 1
Reputation: 414
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
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
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("");
Upvotes: 4