c#空闲游戏;增加资金问题

时间:2016-12-15 21:49:37

标签: c#

我目前正在开发一款仅限c#(无Unity)闲置游戏(A.K.A点击游戏),用户通过点击按钮收集资金并升级金矿,这里是代码;

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进行升级,我不知道如何解决这个问题。玩家应该只需点击收集按钮并添加金矿的收入。

请给别人帮忙,谢谢 - Maximus

2 个答案:

答案 0 :(得分:0)

我真的不明白你的问题。如果用户点击了Button2那么会发生什么?如果要强制用户先单击Button1,可以在默认情况下禁用Button2,并在单击Button1时将其翻转为启用。让我知道我该如何帮助。

答案 1 :(得分: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";

        }
    }
}