Advertisement
angryatti

MemoryCardGame C#

Feb 7th, 2023 (edited)
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.67 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.Drawing.Drawing2D;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. // second version MemoryCardGame C# assets_pic any pictures that contain unique picture
  12. namespace WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         Button[,] tb;
  17.         //int[] egesztomb;
  18.         Random r;
  19.         int lepes;
  20.         int talalat;
  21.         Button b1, b2;
  22.        
  23.         public Form1()
  24.         {
  25.             InitializeComponent();
  26.             r = new Random();
  27.             tb = new Button[4, 4];
  28.             b1 = b2 = null;
  29.  
  30.         }
  31.        
  32.  
  33.  
  34.  
  35.      
  36.  
  37.         private void Form1_Load(object sender, EventArgs e)
  38.         {
  39.             images = new Image[8];
  40.  
  41.           images[0] = resizeImage(Image.FromFile("assets_pic/1.png"), new Size(150, 150)); ;
  42.  
  43.             images[1] = resizeImage(Image.FromFile("assets_pic/2.png"), new Size(150, 150)); ;
  44.             images[2] = resizeImage(Image.FromFile("assets_pic/3.png"), new Size(150, 150)); ;
  45.             images[3] = resizeImage(Image.FromFile("assets_pic/4.png"), new Size(150, 150)); ;
  46.             images[4] = resizeImage(Image.FromFile("assets_pic/5.png"), new Size(150, 150)); ;
  47.             images[5] = resizeImage(Image.FromFile("assets_pic/6.png"), new Size(150, 150)); ;
  48.             images[6] = resizeImage(Image.FromFile("assets_pic/7.png"), new Size(150, 150)); ;
  49.             images[7] = resizeImage(Image.FromFile("assets_pic/8.png"), new Size(150, 150)); ;
  50.         }
  51.  
  52.         private void bt2CloseTheGame_Click(object sender, EventArgs e)
  53.         {
  54.             this.Close();
  55.         }
  56.         private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
  57.         {
  58.             //Get the image current width  
  59.             int sourceWidth = imgToResize.Width;
  60.             //Get the image current height  
  61.             int sourceHeight = imgToResize.Height;
  62.             float nPercent = 0;
  63.             float nPercentW = 0;
  64.             float nPercentH = 0;
  65.             //Calulate  width with new desired size  
  66.             nPercentW = ((float)size.Width / (float)sourceWidth);
  67.             //Calculate height with new desired size  
  68.             nPercentH = ((float)size.Height / (float)sourceHeight);
  69.             if (nPercentH < nPercentW)
  70.                 nPercent = nPercentH;
  71.             else
  72.                 nPercent = nPercentW;
  73.             //New Width  
  74.             int destWidth = (int)(sourceWidth * nPercent);
  75.             //New Height  
  76.             int destHeight = (int)(sourceHeight * nPercent);
  77.             Bitmap b = new Bitmap(destWidth, destHeight);
  78.             Graphics g = Graphics.FromImage((System.Drawing.Image)b);
  79.             g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  80.             // Draw image with new width and height  
  81.             g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
  82.             g.Dispose();
  83.             return (System.Drawing.Image)b;
  84.         }
  85.  
  86.         private void bt1NewGame_Click(object sender, EventArgs e)
  87.         {
  88.             if (tb[0, 0] != null)
  89.             {
  90.                 for (int i = 0; i < 4; i++)
  91.                 {
  92.                     for (int j = 0; j < 4; j++)
  93.                     {
  94.                         tb[i, j].Dispose();
  95.                     }
  96.                 }
  97.             }
  98.             lepes = 0;
  99.             label2.Text = lepes.ToString();
  100.             talalat = 0;
  101.             label4.Text = talalat.ToString();
  102.             for (int i = 0; i < 4; i++)
  103.             {
  104.                 for (int j = 0; j < 4; j++)
  105.                 {
  106.                     tb[i, j] = new Button();
  107.                     tb[i, j].Parent = pmain;
  108.                     tb[i, j].Width = 150;
  109.                     tb[i, j].Height = 150;
  110.                     tb[i, j].Left = j * 150;
  111.                     tb[i, j].Top = i * 150;
  112.                     tb[i, j].Tag = 0;
  113.                     tb[i, j].BackColor = Color.DarkGray;
  114.                     tb[i, j].FlatStyle = FlatStyle.Flat;
  115.                     tb[i, j].Click += Klikk;
  116.                 }
  117.             }
  118.             for (int i = 1; i <= 2; i++)
  119.             {
  120.                 for (int j = 1; j <= 8; j++)
  121.                 {
  122.                     int x, y;
  123.                     do
  124.                     {
  125.                         x = r.Next(0, 4);
  126.                         y = r.Next(0, 4);
  127.                     } while ((int)tb[x, y].Tag != 0);
  128.                     tb[x, y].Tag = j;
  129.                 }
  130.             }
  131.         }
  132.         Image[] images = null;
  133.         private async void Klikk(object sender, EventArgs e)
  134.         {
  135.             Button b = sender as Button;
  136.             b.Text = "";
  137.             b.Enabled = false;
  138.            
  139.  
  140.  
  141.  
  142.             b.Text = ((int)b.Tag-1).ToString();
  143.            
  144.            
  145.             label2.Text = (++lepes).ToString();
  146.             if (b1 == null)
  147.             {
  148.                 b1 = b;
  149.                 b1.BackgroundImage = images[(int)b.Tag - 1];
  150.             }
  151.             else if (b2 == null)
  152.             {
  153.                 b2 = b;
  154.                 b2.BackgroundImage = images[(int)b.Tag - 1];
  155.  
  156.                 if (b1.Text == b2.Text)
  157.                 {
  158.                     label4.Text = (++talalat).ToString();
  159.                     //   b1.BackColor = Color.LightBlue;
  160.                     //  b2.BackColor = Color.LightBlue;
  161.  
  162.                     b1.BackgroundImage = images[(int)b.Tag - 1];
  163.                     b2.BackgroundImage = images[(int)b.Tag - 1];
  164.                     if (talalat == 8)
  165.                     {
  166.                         MessageBox.Show("Gratulálok! " + lepes.ToString() + " lépésből megoldotta!");
  167.                     }
  168.                 }
  169.                 else
  170.                 {
  171.                     //  Application.DoEvents();
  172.                     await Task.Delay(500);
  173.                     b1.Text = "";
  174.                     b2.Text = "";
  175.                     b1.Enabled = true;
  176.                     b2.Enabled = true;
  177.                     b = null;
  178.                     b1.BackColor = Color.DarkGray;
  179.                     b2.BackColor = Color.DarkGray;
  180.                     b1.BackgroundImage = null;
  181.                     b2.BackgroundImage = null;
  182.                 }
  183.                 b1 = b2 = null;
  184.  
  185.                 foreach (Button ReInit in tb)
  186.                 {
  187.  
  188.                     ReInit.Text = "";
  189.                     if (ReInit.BackgroundImage == null)
  190.                     {
  191.                         ReInit.Enabled = true;
  192.                     }
  193.                 }
  194.  
  195.             }
  196.         }
  197.  
  198.     }
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement