2016-12-15 19 views
0

我目前正在使用c#only(沒有Unity)閒置遊戲(A.K.A點擊者遊戲),用戶通過點擊按鈕收集金錢並升級金礦,這裏是代碼;c#空閒遊戲;添加金錢問題

namespace Business_Tycoon 
{ 
public partial class Form1 : Form 
{  
    public decimal level; 
    public decimal money; 
    public decimal revenue; 
    public decimal multiplier; 
    public decimal price; 
    public Form1() 
    { 
     InitializeComponent(); 
     start(); 
    } 
    void start() 
    { 
     money = 10;   
    } 

    void update() 
    { 
     money = money + revenue; 
    } 


    void button1_Click(object sender, EventArgs e) 
    { 
     price = ((price/100) * 150m); 
     level = (level + 1); 
     multiplier = 1.10m;     
     money = (money - price); 
     revenue = (level * multiplier);    
     button1.Text = "Price: " + price;     
     textBox1.Text = "Item Bought"; 
     label4.Text = "Money: " + Convert.ToString(money); 
     label1.Text = "Level: " + Convert.ToString(level); 
     label3.Text = "Revenue: " + Convert.ToString(revenue); 
    } 


    private void button2_Click(object sender, EventArgs e) 
    { 
     update(); 
    } 
} 

}

  • Button1的是水平按鈕,玩家點擊此,減去 錢,它的水平了一次金礦的一個級別。
  • Button2是收集按鈕,玩家點擊此按鈕,添加從金礦創建的收入 。

但是,要從Button2收集玩家必須先點擊Button1來調平它,我不知道如何解決這個問題。玩家應該能夠點擊收集按鈕並添加金礦的收入。

請coud有人幫忙,謝謝 - 鮃

回答

0

請嘗試以下,它應該是有道理的。您需要初始化變量並使用線程進行更新。我使用了一個計時器,因爲它是最簡單的可用資源。

public partial class Form1 : Form { 

    public decimal level { get; set; } 
    public decimal money { get; set; } 
    public decimal revenue { get; set; } 
    public decimal multiplier { get; set; } 
    public decimal price { get; set; } 

    private Timer Updater { get; set; } 

    public Form1() { 
     InitializeComponent(); 
     Updater = new Timer(); 
     Start(); 
    } 
    void Start() { 
     money = 10; 
     multiplier = 1.10m; 
     price = 5; 

     label1.Text = "Level: " + money.ToString(); 
     label3.Text = "Revenue: " + revenue.ToString(); 
     button1.Text = "Price: " + price.ToString(); 
     label4.Text = "Money: " + money.ToString(); 

     Updater.Interval = 1000; //interval in milliseconds || this will tick every second 
     Updater.Tick += Updater_Tick; 
     Updater.Start(); 
    } 

    private void Updater_Tick(object sender, EventArgs e) { 
      money += revenue; 

     label4.Text = "Money: " + money.ToString(); 


    } 

    void button1_Click(object sender, EventArgs e) { 
     if (money >= price) { 
      money -= price; 
      price = ((price/100)*150m); 
      button1.Text = "Price: " + price.ToString(); 
      level++; 
      label1.Text = "Level: " + money.ToString(); 
      label3.Text = "Revenue: " + revenue.ToString(); 
      revenue = level*multiplier; 


      textBox1.Text = "Item Bought"; 

     } 
    } 
} 
+0

感謝您的幫助,這有助於很多:) – Maximus

0

我真的不明白你的問題。如果用戶點擊了Button2,那麼會發生什麼?如果要強制用戶先單擊Button1,則可以將Button2設置爲默認禁用,並在Button1單擊時將其翻轉爲啓用。讓我知道我該怎麼幫忙。

+0

問題是,button1必須先按下才能生成按鈕2之前的收入值,而按鈕2可以被按下,但情況並非如此。 Button2應該可以在遊戲過程中隨時按下以收取收入 – Maximus

+0

@Maximus然後... *不要*禁用按鈕? – Abion47