Advertisement
Tician

textGame

Jun 26th, 2017
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 21.31 KB | None | 0 0
  1.     public partial class Form1 : Form
  2.     {
  3.         private Player _player = new Player(20, 20, 0, 0, 10, 1, 2);
  4.         private Monster _currentMonster;
  5.         private Fight _currentFight;
  6.        
  7.  
  8.         public Form1()
  9.         {
  10.             try
  11.             {
  12.                 InitializeComponent();
  13.                 InitializeStats();
  14.                 InitializeEvents();
  15.                 _player.MoveTo(World.LocationById(World.location_id_home));
  16.                 InitializeButtons();
  17.             }
  18.             catch(Exception ex)
  19.             {
  20.                 //MessageBox.Show(ex.Message);
  21.                 MessageBox.Show(Convert.ToString(ex));
  22.             }                  
  23.         }
  24.  
  25.         private void Form1_Load(object sender, EventArgs e)
  26.         {
  27.         }
  28.  
  29.         private void InitializeStats() //Fill those Labels!
  30.         {          
  31.             lbCurHP.Text = _player.curHP.ToString();
  32.             lbMaxHP.Text = _player.maxHP.ToString();
  33.             lbGold.Text = _player.gold.ToString();
  34.             lbAttack.Text = _player.attack.ToString();
  35.             lbAttack.Text = _player.attack.ToString();
  36.             lbCurExp.Text = _player.curExp.ToString();
  37.             lbMaxExp.Text = _player.maxExp.ToString();
  38.             lbLevel.Text = _player.level.ToString();
  39.  
  40.             hpBar.Maximum = _player.maxHP;
  41.             hpBar.Value = _player.curHP;
  42.             expBar.Maximum = _player.maxExp;
  43.             expBar.Value = _player.curExp;
  44.  
  45.             lbTime.Text = World.GetNextTime(1).ToString();
  46.             lbDay.Text = World.GetNextDay(_player).ToString();
  47.         }
  48.  
  49.         private void UpdateStats()
  50.         {
  51.             lbCurHP.Text = _player.curHP.ToString();
  52.             lbMaxHP.Text = _player.maxHP.ToString();
  53.             lbGold.Text = _player.gold.ToString();
  54.             lbAttack.Text = _player.attack.ToString();
  55.             lbAttack.Text = _player.attack.ToString();
  56.             lbCurExp.Text = _player.curExp.ToString();
  57.             lbMaxExp.Text = _player.maxExp.ToString();
  58.             lbLevel.Text = _player.level.ToString();
  59.  
  60.             hpBar.Maximum = _player.maxHP;
  61.             hpBar.Value = _player.curHP;
  62.             expBar.Maximum = _player.maxExp;
  63.             expBar.Value = _player.curExp;
  64.         }
  65.  
  66.  
  67.         //*****************************//
  68.         //***********EVENTS************//
  69.         //*****************************//
  70.  
  71.         private void InitializeEvents()
  72.         {
  73.             _player.HpChanged += UpdateHpLabel;
  74.             _player.LocationChanged += UpdateControls;
  75.             _currentFight.MonsterAppear += MonsterAppearOutput;
  76.             _currentMonster.HpChanged += MonsterTakesDamageOutput;
  77.             _player.Victory += MonsterDeadOutput;
  78.             _currentMonster.MonsterAttack += MonsterAttackOutput;
  79.         }
  80.  
  81.         public void UpdateHpLabel(object sender, PropertyChangedEventArgs e)
  82.         {
  83.             lbCurHP.Text = _player.curHP.ToString();
  84.             hpBar.Value = _player.curHP;
  85.         }
  86.  
  87.         public void UpdateControls(object sender, PropertyChangedEventArgs e)
  88.         {            
  89.             this.Controls.Add(_player.currentLocation.control);
  90.             richTextBox1.Text = _player.currentLocation.description;
  91.         }  
  92.  
  93.         public void MonsterAppearOutput(object sender, EventArgs e)
  94.         {
  95.             richTextBox1.Text = "A wild " + _currentMonster.name + " level " + _currentMonster.level + " appears!\nIt has "+_currentMonster.curHP+" HP.";
  96.         }
  97.  
  98.         public void MonsterTakesDamageOutput(object sender, PropertyChangedEventArgs e)
  99.         {
  100.             richTextBox1.AppendText("\nYou hit the "+_currentMonster.name+" and dealt "+_player.attack+ " damage.");
  101.             richTextBox1.AppendText("\nThe " + _currentMonster.name + " has " + _currentMonster.curHP + " life remaining.");
  102.         }
  103.  
  104.         public void MonsterDeadOutput(object sender, EventArgs e)
  105.         {
  106.             richTextBox1.AppendText("\nYou willed the poor " + _currentMonster.name);
  107.             richTextBox1.AppendText("\nYou gained " + _currentMonster.exp + " experience.");
  108.             richTextBox1.AppendText("\nYou picked up " + _currentMonster.gold + " gold.");
  109.             richTextBox1.ScrollToCaret();
  110.             UpdateStats();
  111.         }
  112.  
  113.         public void MonsterAttackOutput(object sender, EventArgs e)
  114.         {
  115.             richTextBox1.AppendText("\nThe "+_currentMonster.name+ " attacks back and hits you with "+_currentMonster.attack+ " damage.");
  116.             richTextBox1.ScrollToCaret();
  117.         }
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.         //*****************************//
  125.         //**********BUTTONS************//
  126.         //*****************************//
  127.  
  128.  
  129.         private void InitializeButtons()
  130.         {
  131.             World.LocationById(World.location_id_home).control.Controls[0].Click += homeExplore;
  132.             World.LocationById(World.location_id_home).control.Controls[1].Click += homeWait;
  133.             World.LocationById(World.location_id_home).control.Controls[2].Click += homeLocations;
  134.             World.LocationById(World.location_id_home).control.Controls[3].Click += homeSleep;
  135.  
  136.             World.LocationById(World.location_id_overview).control.Controls[0].Click += overviewBack;
  137.             World.LocationById(World.location_id_overview).control.Controls[1].Click += overviewForest;
  138.  
  139.             World.LocationById(World.location_id_fight).control.Controls[0].Click += attack;
  140.             World.LocationById(World.location_id_fight).control.Controls[1].Click += run;
  141.         }
  142.  
  143.         //Home
  144.         private void homeExplore(object sender, EventArgs e)
  145.         {
  146.             if (_player.discoveredForest == false)
  147.             {
  148.                 _player.DiscoverForest();
  149.                 richTextBox1.Text = "You walk around and found a forest!";
  150.             }
  151.             else
  152.             {
  153.                 richTextBox1.Text = "You walk around but there is nothing more to find here...";
  154.             }
  155.             lbTime.Text = World.GetNextTime(1).ToString();
  156.         }
  157.  
  158.         private void homeWait(object sender, EventArgs e)
  159.         {
  160.             richTextBox1.Text = "You sit around, staring holes into the clouds";
  161.             lbTime.Text = World.GetNextTime(1).ToString();
  162.             _player.Heal(1);
  163.         }
  164.  
  165.         private void homeLocations(object sender, EventArgs e)
  166.         {
  167.             _player.MoveTo(World.LocationById(World.location_id_overview));
  168.         }
  169.  
  170.         private void homeSleep(object sender, EventArgs e)
  171.         {
  172.             lbDay.Text = World.GetNextDay(_player).ToString();
  173.             lbTime.Text = World.GetNextTime(1).ToString();
  174.             richTextBox1.Text = "You just slept... Good morning sunshine! A new day is waiting for you!";
  175.             _player.Heal(20);
  176.         }
  177.  
  178.         //Overview
  179.         private void overviewBack(object sender, EventArgs e)
  180.         {
  181.             _player.MoveTo(World.LocationById(World.location_id_home));
  182.         }
  183.  
  184.         private void overviewForest(object sender, EventArgs e)
  185.         {
  186.             Random random = new Random();
  187.  
  188.             int number = random.Next(1, 4);
  189.            
  190.  
  191.             switch (number)
  192.             {
  193.                 case 1:
  194.                     richTextBox1.Text = "You enjoy a peaceful walk in the forest, you feel healthier!";
  195.                     _player.Heal(2);
  196.                     break;
  197.                 case 2:
  198.                     richTextBox1.Text = "You trip over a root and scrape your knee.";
  199.                     _player.TakeDamage(5);
  200.                     break;
  201.                 case 3:
  202.                     Monster standardMonster = World.MonsterById(World.monster_id_wolf);
  203.                     _currentMonster = new Monster(standardMonster.id, standardMonster.name, standardMonster.maxHP, standardMonster.curHP, standardMonster.attack, standardMonster.exp, standardMonster.gold, standardMonster.level);
  204.                     _currentFight = new Fight(_player, _currentMonster);
  205.                     break;
  206.                 case 4:
  207.                     richTextBox1.Text = "nothing yet";
  208.                     break;
  209.             }
  210.             lbTime.Text = World.GetNextTime(1).ToString();
  211.         }
  212.  
  213.         //Fight
  214.         private void attack(object sender, EventArgs e)
  215.         {
  216.             _currentFight.FightRound();
  217.         }
  218.  
  219.         private void run(object sender, EventArgs e)
  220.         {
  221.             Random random = new Random();
  222.             int number = random.Next(1, 4);
  223.             if(number == 1)
  224.             {                
  225.                 _player.MoveTo(World.LocationById(World.location_id_overview));
  226.                 richTextBox1.AppendText("\nBack here again...");
  227.             }
  228.             else
  229.             {
  230.                 richTextBox1.AppendText("\nYou couldn't get away!");
  231.                 _currentMonster.Attack(_player);
  232.             }
  233.         }
  234.     }
  235.  
  236.     public class Controls : Panel
  237.     {  
  238.         public int id { get; set; }
  239.        
  240.         public Controls(int id, string name)
  241.         {
  242.             this.id = id;
  243.             this.Name = name;
  244.  
  245.             Location = new Point(185, 279);
  246.             Size = new Size(377, 75);
  247.         }
  248.        
  249.         public void AddButton(Button button, string text, bool enabled, bool visible)
  250.         {
  251.             button.Size = new Size(75, 23);
  252.             button.Text = text;
  253.             button.UseVisualStyleBackColor = true;
  254.             button.Enabled = enabled;
  255.             button.Visible = visible;
  256.  
  257.             int ccount = Controls.Count; //numer of buttons
  258.             int x = ccount * 81 + 3;
  259.             button.Location = new Point(x, 3);
  260.  
  261.             Controls.Add(button);
  262.         }                
  263.     }
  264.  
  265.     public class Location
  266.     {
  267.         public int id { get; set; }
  268.         public string name { get; set; }
  269.         public string description { get; set; }
  270.         public Controls control { get; set; }
  271.  
  272.  
  273.         public Location(int id, string name, string description, Controls control)
  274.         {
  275.             this.id = id;
  276.             this.name = name;
  277.             this.description = description;
  278.             this.control = control;
  279.         }
  280.     }
  281.  
  282.     public class Monster
  283.     {
  284.         public int id { get; set; }
  285.         public string name { get; set; }
  286.         public int maxHP { get; set; }
  287.         public int curHP { get; set; }
  288.         public int attack { get; set; }
  289.         public int exp { get; set; }
  290.         public int gold { get; set; }
  291.         public int level { get; set; }
  292.  
  293.         //Events
  294.         public event PropertyChangedEventHandler HpChanged;
  295.         public event EventHandler MonsterAttack;
  296.  
  297.         public Monster(int id, string name, int maxHP, int curHP, int attack, int exp, int gold, int level)
  298.         {
  299.             this.id = id;
  300.             this.name = name;
  301.             this.maxHP = maxHP;
  302.             this.curHP = curHP;
  303.             this.attack = attack;
  304.             this.exp = exp;
  305.             this.gold = gold;
  306.             this.level = level;
  307.         }
  308.  
  309.         public void TakeDamage(int damage)
  310.         {
  311.             if (curHP > damage)
  312.             {
  313.                 curHP = curHP - damage;
  314.                 HpChanged(this, new PropertyChangedEventArgs("curHP"));
  315.             }
  316.             else
  317.             {
  318.                 curHP = 0;
  319.             }
  320.         }
  321.  
  322.         public void Attack(Player player)
  323.         {
  324.             player.TakeDamage(attack);
  325.             MonsterAttack(this, new EventArgs());
  326.         }
  327.     }
  328.  
  329.     public class Places
  330.     {
  331.         public int id { get; set; }
  332.         public string name { get; set; }
  333.  
  334.         public Places(int id, string name)
  335.         {
  336.             this.id = id;
  337.             this.name = name;
  338.         }
  339.     }
  340.  
  341.     public class Player
  342.     {
  343.         public int curHP { get; set; }
  344.         public int maxHP { get; set; }
  345.         public int gold { get; set; }
  346.         public int curExp { get; set; }
  347.         public int maxExp { get; set; }
  348.         public int level { get; set; }
  349.         public int attack { get; set; }
  350.  
  351.         public Location currentLocation { get; set; }
  352.  
  353.         //Events
  354.         public event PropertyChangedEventHandler HpChanged;
  355.         public event PropertyChangedEventHandler LocationChanged;
  356.         public event EventHandler Victory;
  357.  
  358.         //other player-stuff
  359.         public bool discoveredForest { get; set; }
  360.  
  361.         //only here
  362.  
  363.         public Player(int curHP, int maxHP, int gold, int curExp, int maxExp, int level, int attack)
  364.         {
  365.             this.curHP = curHP;
  366.             this.maxHP = maxHP;
  367.             this.gold = gold;
  368.             this.curExp = curExp;
  369.             this.maxExp = maxExp;
  370.             this.level = level;
  371.             this.attack = attack;
  372.         }
  373.  
  374.         public void DiscoverForest()
  375.         {
  376.             discoveredForest = true;
  377.             World.LocationById(World.location_id_home).control.Controls[2].Enabled = true;
  378.             World.LocationById(World.location_id_overview).control.Controls[1].Visible = true;
  379.         }
  380.  
  381.         public void TakeDamage(int damage)
  382.         {
  383.             if (curHP > damage)
  384.             {
  385.                 curHP = curHP - damage;
  386.             }
  387.             else
  388.             {
  389.                 curHP = 1;
  390.                 Die();
  391.             }
  392.             HpChanged(this, new PropertyChangedEventArgs("curHP"));
  393.         }
  394.  
  395.         public void Heal(int restore)
  396.         {
  397.             if (curHP < maxHP - restore)
  398.             {
  399.                 curHP = curHP + restore;
  400.             }
  401.             else
  402.             {
  403.                 curHP = maxHP;
  404.             }
  405.             HpChanged(this, new PropertyChangedEventArgs("curHP"));
  406.         }
  407.  
  408.         public void MoveTo(Location location)
  409.         {
  410.             if (currentLocation != null)
  411.             {
  412.                 currentLocation.control.Visible = false;
  413.             }
  414.             currentLocation = location;
  415.             currentLocation.control.Visible = true;            
  416.             LocationChanged(this, new PropertyChangedEventArgs("currentLocation"));
  417.         }
  418.  
  419.         public void GetRewards(int exp, int gold)
  420.         {
  421.             this.gold += gold;
  422.             if (curExp + exp < maxExp)
  423.             {
  424.                 curExp += exp;
  425.             }
  426.             else
  427.             {
  428.                 curExp = (curExp + exp) - maxExp;
  429.                 LevelUp();
  430.             }
  431.             Victory(this, new EventArgs());
  432.         }
  433.  
  434.         public void LevelUp()
  435.         {
  436.             level += 1;
  437.             attack += 2;
  438.             maxHP += 5;
  439.             curHP += 5;
  440.         }
  441.  
  442.         public void Die()
  443.         {
  444.             MoveTo(World.LocationById(World.location_id_home));
  445.         }
  446.     }
  447.  
  448.     public static class World
  449.     {
  450.         //Lists
  451.         public static readonly List<Monster> monstersList = new List<Monster>();
  452.         public static readonly List<Location> locationsList = new List<Location>();
  453.         public static readonly List<Controls> controlsList = new List<Controls>();
  454.         public static readonly List<Places> placesList = new List<Places>();
  455.  
  456.         static int time = 6;
  457.         static int day = 0;
  458.  
  459.         //Monster
  460.         public const int monster_id_wolf = 1;
  461.         public const int monster_id_bee = 2;
  462.  
  463.         //Locations
  464.         public const int location_id_home = 1;
  465.         public const int location_id_overview = 2;
  466.         public const int location_id_fight = 3;
  467.  
  468.         //Controls
  469.         public const int control_id_home = 1;
  470.         public const int control_id_overview = 2;
  471.         public const int control_id_fight = 3;
  472.  
  473.         //Places
  474.         public const int place_id_forest = 1;
  475.  
  476.         static World()
  477.         {
  478.             PopulateMonsters();
  479.             PopulatePlaces();
  480.             CreateControls();
  481.             PopulateLocations();
  482.         }
  483.  
  484.         public static int GetNextTime(int hours)
  485.         {
  486.             time += hours;
  487.  
  488.             if(time > 21)
  489.             {
  490.                 LocationById(location_id_home).control.Controls[0].Enabled = false;
  491.                 LocationById(location_id_home).control.Controls[1].Enabled = false;
  492.                 LocationById(location_id_home).control.Controls[2].Enabled = false;
  493.                 LocationById(location_id_home).control.Controls[3].Enabled = true;
  494.  
  495.                 LocationById(location_id_overview).control.Controls[1].Enabled = false;
  496.             }
  497.  
  498.             return time;
  499.         }
  500.  
  501.         public static int GetNextDay(Player _player)
  502.         {
  503.             day += 1;
  504.             Player player = _player;
  505.             LocationById(location_id_home).control.Controls[0].Enabled = true;
  506.             LocationById(location_id_home).control.Controls[1].Enabled = true;
  507.             LocationById(location_id_home).control.Controls[3].Enabled = false;
  508.  
  509.             if (_player.discoveredForest != false)
  510.             {
  511.                 LocationById(location_id_home).control.Controls[2].Enabled = true;
  512.             }
  513.  
  514.             LocationById(location_id_overview).control.Controls[1].Enabled = true;
  515.  
  516.             time = 6;
  517.  
  518.             return day;
  519.         }
  520.  
  521.         private static void PopulateMonsters()
  522.         {
  523.             Monster wolf = new Monster(monster_id_wolf, "Wolf", 10, 10, 1, 10, 2, 1);
  524.             Monster bee = new Monster(monster_id_bee, "Bee", 1, 1, 1, 1, 1, 1);
  525.  
  526.             monstersList.Add(wolf);
  527.             monstersList.Add(bee);
  528.         }
  529.  
  530.         private static void PopulateLocations()
  531.         {
  532.             Location home = new Location(location_id_home, "Home", "This is your home", ControlById(control_id_home));
  533.             Location overview = new Location(location_id_overview, "Overview", "The places you discovered", ControlById(control_id_overview));
  534.             Location fight = new Location(location_id_fight, "Fight", "A wild monster appeared: ", ControlById(control_id_fight));
  535.  
  536.             locationsList.Add(home);
  537.             locationsList.Add(overview);
  538.             locationsList.Add(fight);
  539.         }
  540.  
  541.         private static void PopulatePlaces()
  542.         {
  543.             Places forest = new Places(place_id_forest, "Forest");
  544.  
  545.             placesList.Add(forest);
  546.         }
  547.  
  548.         private static void CreateControls()
  549.         {
  550.             Controls home = new Controls(control_id_home,"home");
  551.             Button explore = new Button();
  552.             Button wait = new Button();
  553.             Button locations = new Button();
  554.             Button sleep = new Button();
  555.             home.AddButton(explore, "Explore", true, true);
  556.             home.AddButton(wait, "Wait", true, true);
  557.             home.AddButton(locations, "Locations", false, true);
  558.             home.AddButton(sleep, "Sleep", false, true);
  559.  
  560.             Controls overview = new Controls(control_id_overview, "overview");
  561.             Button back = new Button();
  562.             overview.AddButton(back, "Back", true, true);
  563.             foreach(Places place in placesList)
  564.             {
  565.                 string name = place.name;
  566.                 Button button = new Button();
  567.                 overview.AddButton(button, name, true, false);
  568.             }
  569.  
  570.             Controls fight = new Controls(control_id_fight, "Fight");
  571.             Button attack = new Button();
  572.             Button run = new Button();
  573.             fight.AddButton(attack, "Attack", true, true);
  574.             fight.AddButton(run, "Run", true, true);
  575.  
  576.  
  577.             controlsList.Add(home);
  578.             controlsList.Add(overview);
  579.             controlsList.Add(fight);
  580.         }
  581.  
  582.         public static Controls ControlById(int id)
  583.         {
  584.             foreach(Controls control in controlsList)
  585.             {
  586.                 if(control.id == id)
  587.                 {
  588.                     return control;
  589.                 }
  590.             }
  591.             return null;
  592.         }
  593.  
  594.         public static Location LocationById(int id)
  595.         {
  596.             foreach(Location location in locationsList)
  597.             {
  598.                 if(location.id == id)
  599.                 {
  600.                     return location;
  601.                 }
  602.             }
  603.             return null;
  604.         }
  605.  
  606.         public static Monster MonsterById(int id)
  607.         {
  608.             foreach(Monster monster in monstersList)
  609.             {
  610.                 if(monster.id == id)
  611.                 {
  612.                     return monster;
  613.                 }
  614.             }
  615.             return null;
  616.         }
  617.  
  618.         public static Places PlaceById(int id)
  619.         {
  620.             foreach(Places place in placesList)
  621.             {
  622.                 if(place.id == id)
  623.                 {
  624.                     return place;
  625.                 }
  626.             }
  627.             return null;
  628.         }
  629.     }
  630.  
  631.     public class Fight
  632.     {
  633.         Player player;
  634.         Monster monster;
  635.  
  636.         //Events
  637.         public event EventHandler MonsterAppear;
  638.  
  639.         public Fight(Player player, Monster monster)
  640.         {
  641.             this.player = player;
  642.             this.monster = monster;
  643.             player.MoveTo(World.LocationById(World.location_id_fight));
  644.             MonsterAppear(this, new EventArgs());
  645.         }
  646.  
  647.         public void FightRound()
  648.         {
  649.             monster.TakeDamage(player.attack);
  650.             if (monster.curHP != 0)
  651.             {
  652.                 monster.Attack(player);
  653.             }
  654.             else
  655.             {
  656.                 player.GetRewards(monster.exp, monster.gold);
  657.             }
  658.         }
  659.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement