簡體   English   中英

C#制作進度條

[英]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://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("");
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 除以它們時,只要PlayerXP小於NeededXP ,您將得到0 投射/將其更改為兩倍以獲取正確的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM