igogiko

C# Incremental Game

May 29th, 2014
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5.  
  6. namespace Incremental
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         private int score = 0;
  11.         private int clickFactor = 1;
  12.         private double priceIncrement = 1.5;
  13.  
  14.         private Dictionary<string, Dictionary<string, int>> items = new Dictionary<string, Dictionary<string, int>>
  15.         {
  16.             {"Joke", new Dictionary<string, int> {{"price", 10}, {"score", 1}, {"owned", 0}}},
  17.             {"Feather", new Dictionary<string, int> {{"price", 50}, {"score", 3}, {"owned", 0}}},
  18.             {"Laughing Gas", new Dictionary<string, int> {{"price", 200}, {"score", 5}, {"owned", 0}}}
  19.         };
  20.  
  21.         private Dictionary<string, Dictionary<string, Control>> controls = new Dictionary<string, Dictionary<string, Control>>();
  22.  
  23.         public Form1()
  24.         {
  25.             InitializeComponent();
  26.             createButtons();
  27.         }
  28.  
  29.         private void createButtons()
  30.         {
  31.             int count = 0;
  32.  
  33.             foreach (KeyValuePair<string, Dictionary<string, int>> item in items)
  34.             {
  35.                 Button btn = new Button();
  36.                 btn.Text = item.Key;
  37.                 btn.Size = new Size(137, 23);
  38.                 btn.Location = new Point(329, 32 + 28 * count);
  39.                 btn.Click += btnItem_Click;
  40.                
  41.                 Label price = new Label();
  42.                 price.Text = items[item.Key]["price"].ToString();
  43.                 price.Location = new Point(265, 37 + 28 * count);
  44.  
  45.                 Label owned = new Label();
  46.                 owned.Text = items[item.Key]["owned"].ToString();
  47.                 owned.Location = new Point(472, 37 + 28 * count);
  48.                
  49.                 this.Controls.Add(btn);
  50.                 this.Controls.Add(price);
  51.                 this.Controls.Add(owned);
  52.                
  53.                 controls.Add(item.Key, new Dictionary<string, Control> {
  54.                     {"button", btn},
  55.                     {"price", price},
  56.                     {"owned", owned}
  57.                 });
  58.  
  59.                 count++;
  60.             }
  61.         }
  62.  
  63.         private void scoreUpdater_Tick(object sender, EventArgs e)
  64.         {
  65.             int addScore = 0;
  66.             foreach (KeyValuePair<string, Dictionary<string, int>> item in items)
  67.             {
  68.                 addScore += items[item.Key]["owned"] * items[item.Key]["score"];
  69.             }
  70.  
  71.             lblTicklesPerSec.Text = addScore.ToString();
  72.             score += addScore;
  73.         }
  74.  
  75.         private void gameLoop_Tick(object sender, EventArgs e)
  76.         {
  77.             foreach (KeyValuePair<string, Dictionary<string, Control>> control in controls)
  78.             {
  79.                 controls[control.Key]["button"].Enabled = score >= items[control.Key]["price"];
  80.                 controls[control.Key]["price"].Text = items[control.Key]["price"].ToString();
  81.                 controls[control.Key]["owned"].Text = items[control.Key]["owned"].ToString();
  82.             }
  83.  
  84.             lblTickles.Text = score.ToString();
  85.         }
  86.  
  87.         private void btnClick_MouseDown(object sender, MouseEventArgs e)
  88.         {
  89.             btnClick.Text = "^o^";
  90.         }
  91.  
  92.         private void btnClick_MouseUp(object sender, MouseEventArgs e)
  93.         {
  94.             btnClick.Text = "o_o";
  95.             score += clickFactor;
  96.         }
  97.  
  98.         private void buyItem(string item)
  99.         {
  100.             score -= items[item]["price"];
  101.             items[item]["price"] = (int)(items[item]["price"] * priceIncrement);
  102.             items[item]["owned"] += 1;
  103.         }
  104.  
  105.         private void btnItem_Click(object sender, EventArgs e)
  106.         {
  107.             buyItem(((Button)sender).Text);
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment