Advertisement
ada1711

Untitled

Apr 28th, 2023
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 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.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace MemoryGame
  12. {
  13. public partial class Form1 : Form
  14. {
  15. private GameSettings gameSettings;
  16. MemoryCard pierwsza;
  17. MemoryCard druga;
  18.  
  19.  
  20. public Form1()
  21. {
  22. InitializeComponent();
  23. gameSettings = new GameSettings();
  24.  
  25. UstawKontrolki();
  26.  
  27. GenerujKarty();
  28.  
  29. timerCzasPodgladu.Start();
  30. }
  31.  
  32. private void UstawKontrolki()
  33. {
  34. panelKart.Width = gameSettings.bok * gameSettings.kolumny;
  35. panelKart.Height = gameSettings.bok * gameSettings.wiersze;
  36.  
  37. int width = panelKart.Width + 40;
  38. int height = panelKart.Height + 100;
  39. Width = width;
  40. Height = height;
  41.  
  42. lblStartInfo.Text = $"Początek gry za {gameSettings.czasPodgladu}s.";
  43. lblPunktyWartosc.Text = gameSettings.aktualnePunkty.ToString();
  44. lblCzasWartosc.Text = gameSettings.czasGry.ToString();
  45. lblStartInfo.Visible = true;
  46. }
  47.  
  48. private void GenerujKarty()
  49. {
  50. string[] memories = System.IO.Directory.GetFiles(gameSettings.folderObrazki);
  51.  
  52. gameSettings.maxPunkty = memories.Length;
  53.  
  54. List<MemoryCard> buttons = new List<MemoryCard>();
  55.  
  56. foreach(string img in memories)
  57. {
  58. Guid id = Guid.NewGuid();
  59.  
  60. MemoryCard b1 = new MemoryCard(id, gameSettings.plikLogo, img);
  61.  
  62. buttons.Add(b1);
  63.  
  64. MemoryCard b2 = new MemoryCard(id, gameSettings.plikLogo, img);
  65.  
  66. buttons.Add(b2);
  67. }
  68.  
  69. Random random = new Random();
  70.  
  71. panelKart.Controls.Clear();
  72.  
  73. for (int x = 0; x < gameSettings.kolumny; x++)
  74. {
  75. for (int y = 0; y < gameSettings.wiersze; y++)
  76. {
  77. int index = random.Next(0, buttons.Count);
  78.  
  79. MemoryCard b = buttons[index];
  80.  
  81. int margines = 2;
  82.  
  83. b.Location = new Point((x * gameSettings.bok) + (margines * x), (y * gameSettings.bok) + (margines * y));
  84.  
  85. b.Width = gameSettings.bok;
  86. b.Height = gameSettings.bok;
  87. b.Click += BtnClicked;
  88.  
  89. b.Odkryj();
  90.  
  91. panelKart.Controls.Add(b);
  92.  
  93. buttons.Remove(b);
  94. }
  95. }
  96.  
  97.  
  98.  
  99. }
  100.  
  101. private void timerCzasPodgladu_Tick(object sender, EventArgs e)
  102. {
  103. gameSettings.czasPodgladu--;
  104.  
  105. lblStartInfo.Text = $"Początek gry za {gameSettings.czasPodgladu}.";
  106.  
  107. if(gameSettings.czasPodgladu <= 0)
  108. {
  109. lblStartInfo.Visible = false;
  110.  
  111. foreach (Control kontrolka in panelKart.Controls)
  112. {
  113. MemoryCard card = (MemoryCard)kontrolka;
  114. card.Zakryj();
  115. }
  116. timerCzasPodgladu.Stop();
  117. timerCzasGry.Start();
  118. }
  119. }
  120.  
  121. private void BtnClicked(object sender, EventArgs e)
  122. {
  123. MemoryCard btn = (MemoryCard)sender;
  124.  
  125. if (pierwsza == null)
  126. {
  127. pierwsza = btn;
  128. pierwsza.Odkryj();
  129. } else
  130. {
  131. druga = btn;
  132. druga.Odkryj();
  133. panelKart.Enabled = false;
  134.  
  135. if (pierwsza.Id == druga.Id)
  136. {
  137. gameSettings.aktualnePunkty++;
  138. lblPunktyWartosc.Text = gameSettings.aktualnePunkty.ToString();
  139.  
  140. pierwsza = null;
  141. druga = null;
  142.  
  143. panelKart.Enabled = true;
  144. } else
  145. {
  146. timerZakrywacz.Start();
  147. }
  148.  
  149.  
  150. }
  151. }
  152. }
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement