我是C#的新手,我正在尝试制作一些点答器游戏。我想这样做,以便如果cslave大于0,则变量PP每隔一秒递增一次。我尝试了一些其他代码中看到的不同事情,但我无法使其工作。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Clicker_Game
{
public partial class Form1 : Form
{
private Timer gameLoop = new Timer();
public Form1()
{
InitializeComponent();
gameLoop.Interval = 1000;
gameLoop.Tick += Update;
button2.Text = "Click here to buy Cookieze slave Cost 10 PP";
}
// Makes it so everytime you click the button it will add 1 to PP
private double pp = 0;
private int cslave = 0;
private double cSlaveC = 10;
private void Button1_Click(object sender, EventArgs e)
{
pp++;
string current = "Your current PP is ";
label1.Text = current + pp.ToString();
}
private void Button2_Click(object sender, EventArgs e)
{
if (pp >= cSlaveC)
{
gameLoop.Start();
pp -= cSlaveC;
string current = "Your current PP is ";
label1.Text = current + pp.ToString();
cslave++;
cSlaveC = cSlaveC * 2;
string cslaveM = "You currently have ";
string cslaveW = " Cookieze slaves";
label2.Text = cslaveM + cslave + cslaveW;
button2.Text = "Click here to buy Cookieze slave Cost " + cSlaveC + " PP";
}
}
private void Update(object sender, EventArgs e)
{
gameLoop.Start();
pp++;
}
}
}
答案 0 :(得分:0)
您可以执行以下操作。
var timer = new Timer();
timer.Interval = 1000;
timer.Tick += (s,e) =>
{
if(cslave > 0)
pp++;
};
timer.Start();
或使用您的代码。
var timer = new Timer();
timer.Interval = 1000;
timer.Tick += Update;
在您的按钮单击事件中
timer.Start();
最后在您的更新方法中
if(cslave > 0)
pp++;