I've made a simple clicking game where you spam a button in order to get coins which you can use to buy upgrades to get even more coins.
I've only 2 classes one that is responsible for the game logic - MainGame
and one for the items available in the shop - Item
.
MainGame
public partial class MainGame : Form
{
private readonly Item[] purchasableItems = new Item[20];
private long _currentMoney = 0;
private long currentMoney
{
get { return _currentMoney; }
set
{
_currentMoney = value;
lbCurrentMoney.Text = $@"{currentMoney} $";
}
}
private long _moneyPerClick = 1;
private long moneyPerClick
{
get { return _moneyPerClick; }
set
{
_moneyPerClick = value;
bGetMoney.Text = $@"{moneyPerClick} $";
}
}
private Thread countThread = null;
private bool stop = false;
public MainGame()
{
InitializeComponent();
Initialize();
}
private void Initialize()
{
lbCurrentMoney.Text = $@"{currentMoney} $";
bGetMoney.Text = $@"{moneyPerClick} $";
for (int i = 0; i < purchasableItems.Length; i++)
{
purchasableItems[i] = new Item((long) Math.Pow(2, i + 1), (long) (Math.Pow(2, i + 2)*10), flpShop,
OnShopItemClickableMouseDown, OnShopItemMouseUp);
}
UpdateItemAccessibility();
}
private void OnShopItemMouseUp(object s, EventArgs e, Item item)
{
stop = true;
UpdateItemAccessibility();
}
private void OnShopItemClickableMouseDown(object s, EventArgs e, Item item)
{
stop = false;
countThread = new Thread(() =>
{
while (!stop && currentMoney >= item.RequiredMoney)
{
if (lbCurrentMoney.InvokeRequired && bGetMoney.InvokeRequired)
{
Invoke(new MethodInvoker(() =>
{
currentMoney -= item.RequiredMoney;
moneyPerClick += item.DollarsPerClick;
}));
}
Thread.Sleep(100);
}
});
countThread.Start();
}
private void bGetMoney_Click(object sender, EventArgs e)
{
currentMoney += moneyPerClick;
UpdateItemAccessibility();
}
private void UpdateItemAccessibility()
{
foreach (Item item in purchasableItems)
{
item.UpdateAccessibility(currentMoney);
}
}
}
Item
public class Item
{
public delegate void ShopItemEvent(object sender, EventArgs e, Item item);
public long DollarsPerClick { get; }
public long RequiredMoney { get; }
private Button buttonActivator;
public Item(long dollarsPerClick, long requiredMoney, Control parent, ShopItemEvent onMouseDown, ShopItemEvent onMouseUp)
{
DollarsPerClick = dollarsPerClick;
RequiredMoney = requiredMoney;
CreateBuyButton(parent, onMouseDown, onMouseUp);
}
private void CreateBuyButton(Control parent, ShopItemEvent onMouseDown, ShopItemEvent onMouseUp)
{
buttonActivator = new Button
{
Text = $@"{DollarsPerClick} $ per click." + Environment.NewLine +
$@"Cost : {RequiredMoney}",
Size = new Size(parent.Width - 10, 40),
Enabled = false
};
buttonActivator.MouseDown += (sender, EventArgs) => onMouseDown(sender, EventArgs, this);
buttonActivator.MouseUp += (sender, EventArgs) => onMouseUp(sender, EventArgs, this);
parent.Controls.Add(buttonActivator);
}
public void UpdateAccessibility(long currentMoney)
{
buttonActivator.Enabled = currentMoney >= RequiredMoney;
}
}
The code is quite short but I think that there are plenty of stuff to improve.