Sybatron

My version

Oct 21st, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace Hangman_Sybatron
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         List<string> words = new List<string> ();
  16.         List<Label> labels = new List<Label> ();
  17.         string word;
  18.         int wrongCounter = 0;
  19.  
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.         private void Form1_Shown(object sender, EventArgs e)
  25.         {
  26.             DrawHangmanPost();
  27.             ReadFromFile();
  28.             CreateLabels();
  29.            
  30.         }
  31.         enum BodyParts
  32.         {
  33.             Head,
  34.             Body,
  35.             Right_Arm,
  36.             Left_Arm,
  37.             Left_Leg,
  38.             Right_Leg,
  39.             Left_Eye,
  40.             Right_Eye,
  41.             Mouth
  42.         }
  43.         void DrawHangmanPost()
  44.         {
  45.             Graphics g = panel1.CreateGraphics();
  46.             Pen p = new Pen(Color.Brown, 10);
  47.             g.DrawLine(p,new Point(180,10), new Point(180,190));
  48.             g.DrawLine(p, new Point(180, 15), new Point(90, 15));
  49.             g.DrawLine(p, new Point(90, 10), new Point(90, 40));
  50.  
  51.             /*
  52.             //Draw the human
  53.             DrawBodyPart(BodyParts.Head);
  54.             DrawBodyPart(BodyParts.Right_Eye);
  55.             DrawBodyPart(BodyParts.Left_Eye);
  56.             DrawBodyPart(BodyParts.Mouth);
  57.             DrawBodyPart(BodyParts.Body);
  58.             DrawBodyPart(BodyParts.Left_Arm);
  59.             DrawBodyPart(BodyParts.Right_Arm);
  60.             DrawBodyPart(BodyParts.Left_Leg);
  61.             DrawBodyPart(BodyParts.Right_Leg);
  62.             */
  63.         }
  64.         void DrawBodyPart(BodyParts bp)
  65.         {
  66.             Graphics g = panel1.CreateGraphics();
  67.             Pen p = new Pen(Color.Plum, 2);
  68.             SolidBrush s = new SolidBrush(Color.MediumAquamarine);
  69.             switch(bp)
  70.             {
  71.                 case BodyParts.Head:
  72.                     g.DrawEllipse(p, 73, 39, 35, 45);
  73.                     break;
  74.                 case BodyParts.Left_Eye:
  75.                     g.FillEllipse(s, 80, 50, 5, 5);
  76.                     break;
  77.                 case BodyParts.Right_Eye:
  78.                     g.FillEllipse(s, 95, 50, 5, 5);
  79.                     break;
  80.                 case BodyParts.Mouth:
  81.                     g.DrawArc(new Pen(Color.Tomato, 2), new Rectangle(83, 70, 15, 10), 0, -180);
  82.                     break;
  83.  
  84.                 case BodyParts.Body:
  85.                     g.DrawLine(p,new Point(90,85),new Point(90,130));
  86.                     break;
  87.  
  88.                 case BodyParts.Left_Arm:
  89.                     g.DrawLine(p, new Point(90,95), new Point(75, 115));
  90.                     break;
  91.                 case BodyParts.Right_Arm:
  92.                     g.DrawLine(p, new Point(90, 95), new Point(105, 115));
  93.                     break;
  94.  
  95.                 case BodyParts.Left_Leg:
  96.                     g.DrawLine(p, new Point(90, 129), new Point(80, 170));
  97.                     break;
  98.                 case BodyParts.Right_Leg:
  99.                     g.DrawLine(p, new Point(90, 129), new Point(100, 170));
  100.                     break;
  101.             }
  102.         }
  103.  
  104.         string GetRandomWord()
  105.         {
  106.             int length = words.Count;
  107.             if (length <= 0)
  108.                 return "";
  109.             Random random = new Random ();
  110.             int index = random.Next (0, length-1);
  111.             return words[index];
  112.         }
  113.  
  114.         void ReadFromFile()
  115.         {
  116.             if (!File.Exists(".\\words.txt"))
  117.                 return;
  118.  
  119.             StreamReader streamReader = new StreamReader(".\\words.txt");
  120.  
  121.             if (streamReader == null)
  122.                 return;
  123.  
  124.             while (!streamReader.EndOfStream)
  125.             {
  126.                 words.Add(streamReader.ReadLine());
  127.             }
  128.  
  129.             streamReader.Close();
  130.         }
  131.         void CreateLabels()
  132.         {
  133.             word = GetRandomWord();
  134.             char[] chars = word.ToCharArray();
  135.             int between = 334 / chars.Length - 1;
  136.             for (int i = 0; i <= chars.Length - 1; i++)
  137.             {
  138.                 labels.Add(new Label());
  139.                 labels[i].Location = new Point((i * between) + 10, 50);
  140.                 labels[i].Text = "_";
  141.                 labels[i].Font = new Font("Arial", 12);
  142.                 labels[i].BackColor=Color.Transparent;
  143.                 labels[i].Parent = WordsGroupBox;
  144.                 labels[i].BringToFront();
  145.                 labels[i].CreateControl();
  146.             }
  147.             int length = chars.Length;
  148.             WordLenghtLabel.Text = WordLenghtLabel.Text + " " + length.ToString();
  149.         }
  150.  
  151.  
  152.         private void NewGameMenu_Click(object sender, EventArgs e)
  153.         {
  154.             Application.Restart();
  155.         }
  156.  
  157.         private void ExitGameMenu_Click(object sender, EventArgs e)
  158.         {
  159.             Application.Exit();
  160.         }
  161.  
  162.         private void AboutMenu_Click(object sender, EventArgs e)
  163.         {
  164.             MessageBox.Show("От Sybatron\nPower Supersport", "Отностно \"Бесеница\"");
  165.         }
  166.  
  167.         private void SubmitLetterButton_Click(object sender, EventArgs e)
  168.         {
  169.             char letter = LetterTextBox.Text.ToLower().ToCharArray()[0];
  170.             LetterTextBox.Text = "";
  171.             if (!char.IsLetter(letter) || (letter>='\u0061' && letter<='\u007a'))
  172.             {
  173.                 MessageBox.Show("Можете да въвеждате само букви на български", "Грешка!",
  174.                     MessageBoxButtons.OK, MessageBoxIcon.Error);
  175.                 return;
  176.             }
  177.             if (word.Contains(letter))
  178.             {
  179.                 char[] letters = word.ToCharArray();
  180.                 for (int i = 0; i < letters.Length; i++)
  181.                 {
  182.                     if (letters[i] == letter)
  183.                     {
  184.                         labels[i].Text = letter.ToString();
  185.                     }
  186.                 }
  187.             }
  188.             else
  189.             {
  190.                 MessageBox.Show("Буквата, която въведохте, я няма в думата", "Извинете!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  191.                 DrawBodyPart((BodyParts)wrongCounter);
  192.                 missedLetters.Text = missedLetters.Text + letter.ToString() + ",";
  193.                 EndOfGame(wrongCounter);
  194.                 wrongCounter++;
  195.             }
  196.            
  197.  
  198.         }
  199.  
  200.         private void SubmitWordButton_Click(object sender, EventArgs e)
  201.         {
  202.             if (WordTextBox.Text == word)
  203.             {
  204.                 EndOfGame(10);
  205.             }
  206.             else
  207.             {
  208.                 MessageBox.Show("Думата, която въведохте не е исканата", "Извинете!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  209.                 DrawBodyPart((BodyParts)wrongCounter);
  210.                 EndOfGame(wrongCounter);
  211.                 wrongCounter++;
  212.                 WordTextBox.Text = "";
  213.             }
  214.         }
  215.  
  216.         void EndOfGame(int counter)
  217.         {
  218.             if (counter == 8)
  219.             {
  220.                 MessageBox.Show("Вие загубихте!\nДумата е: "+word.ToString(), "Край на играта!",
  221.                     MessageBoxButtons.OK, MessageBoxIcon.Warning);
  222.                 SubmitWordsGroupBox.Enabled = false;
  223.             }
  224.             else if (counter == 10)
  225.             {
  226.                 MessageBox.Show("Вие спечелихте!\nПознахте думата: " + word.ToString(), "Поздравления!");
  227.                 SubmitWordsGroupBox.Enabled = false;
  228.             }
  229.         }
  230.  
  231.         private void TestButton_Click(object sender, EventArgs e)
  232.         {
  233.             EndOfGame(9);
  234.         }
  235.  
  236.         /*
  237.         string GetRandomWord()
  238.         {
  239.             int length = words.Count;
  240.             if (length <= 0)
  241.                 return "";
  242.             Random random = new Random ();
  243.             int index = random.Next (0, length);
  244.             return words[index];
  245.         }
  246.  
  247.         void ReadFromFile()
  248.         {
  249.             if (!File.Exists(".\words.txt"))
  250.                 return;
  251.  
  252.             StreamReader streamReader = new StreamReader(".\words.txt");
  253.  
  254.             if (streamReader == null)
  255.                 return;
  256.  
  257.             while (!streamReader.EndOfStream)
  258.             {
  259.                 words.Add(streamReader.ReadLine());
  260.             }
  261.  
  262.             streamReader.Close();
  263.         }*/
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment