Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- // second version MemoryCardGame C# assets_pic any pictures that contain unique picture
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- Button[,] tb;
- //int[] egesztomb;
- Random r;
- int lepes;
- int talalat;
- Button b1, b2;
- public Form1()
- {
- InitializeComponent();
- r = new Random();
- tb = new Button[4, 4];
- b1 = b2 = null;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- images = new Image[8];
- images[0] = resizeImage(Image.FromFile("assets_pic/1.png"), new Size(150, 150)); ;
- images[1] = resizeImage(Image.FromFile("assets_pic/2.png"), new Size(150, 150)); ;
- images[2] = resizeImage(Image.FromFile("assets_pic/3.png"), new Size(150, 150)); ;
- images[3] = resizeImage(Image.FromFile("assets_pic/4.png"), new Size(150, 150)); ;
- images[4] = resizeImage(Image.FromFile("assets_pic/5.png"), new Size(150, 150)); ;
- images[5] = resizeImage(Image.FromFile("assets_pic/6.png"), new Size(150, 150)); ;
- images[6] = resizeImage(Image.FromFile("assets_pic/7.png"), new Size(150, 150)); ;
- images[7] = resizeImage(Image.FromFile("assets_pic/8.png"), new Size(150, 150)); ;
- }
- private void bt2CloseTheGame_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
- {
- //Get the image current width
- int sourceWidth = imgToResize.Width;
- //Get the image current height
- int sourceHeight = imgToResize.Height;
- float nPercent = 0;
- float nPercentW = 0;
- float nPercentH = 0;
- //Calulate width with new desired size
- nPercentW = ((float)size.Width / (float)sourceWidth);
- //Calculate height with new desired size
- nPercentH = ((float)size.Height / (float)sourceHeight);
- if (nPercentH < nPercentW)
- nPercent = nPercentH;
- else
- nPercent = nPercentW;
- //New Width
- int destWidth = (int)(sourceWidth * nPercent);
- //New Height
- int destHeight = (int)(sourceHeight * nPercent);
- Bitmap b = new Bitmap(destWidth, destHeight);
- Graphics g = Graphics.FromImage((System.Drawing.Image)b);
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- // Draw image with new width and height
- g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
- g.Dispose();
- return (System.Drawing.Image)b;
- }
- private void bt1NewGame_Click(object sender, EventArgs e)
- {
- if (tb[0, 0] != null)
- {
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- tb[i, j].Dispose();
- }
- }
- }
- lepes = 0;
- label2.Text = lepes.ToString();
- talalat = 0;
- label4.Text = talalat.ToString();
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- tb[i, j] = new Button();
- tb[i, j].Parent = pmain;
- tb[i, j].Width = 150;
- tb[i, j].Height = 150;
- tb[i, j].Left = j * 150;
- tb[i, j].Top = i * 150;
- tb[i, j].Tag = 0;
- tb[i, j].BackColor = Color.DarkGray;
- tb[i, j].FlatStyle = FlatStyle.Flat;
- tb[i, j].Click += Klikk;
- }
- }
- for (int i = 1; i <= 2; i++)
- {
- for (int j = 1; j <= 8; j++)
- {
- int x, y;
- do
- {
- x = r.Next(0, 4);
- y = r.Next(0, 4);
- } while ((int)tb[x, y].Tag != 0);
- tb[x, y].Tag = j;
- }
- }
- }
- Image[] images = null;
- private async void Klikk(object sender, EventArgs e)
- {
- Button b = sender as Button;
- b.Text = "";
- b.Enabled = false;
- b.Text = ((int)b.Tag-1).ToString();
- label2.Text = (++lepes).ToString();
- if (b1 == null)
- {
- b1 = b;
- b1.BackgroundImage = images[(int)b.Tag - 1];
- }
- else if (b2 == null)
- {
- b2 = b;
- b2.BackgroundImage = images[(int)b.Tag - 1];
- if (b1.Text == b2.Text)
- {
- label4.Text = (++talalat).ToString();
- // b1.BackColor = Color.LightBlue;
- // b2.BackColor = Color.LightBlue;
- b1.BackgroundImage = images[(int)b.Tag - 1];
- b2.BackgroundImage = images[(int)b.Tag - 1];
- if (talalat == 8)
- {
- MessageBox.Show("Gratulálok! " + lepes.ToString() + " lépésből megoldotta!");
- }
- }
- else
- {
- // Application.DoEvents();
- await Task.Delay(500);
- b1.Text = "";
- b2.Text = "";
- b1.Enabled = true;
- b2.Enabled = true;
- b = null;
- b1.BackColor = Color.DarkGray;
- b2.BackColor = Color.DarkGray;
- b1.BackgroundImage = null;
- b2.BackgroundImage = null;
- }
- b1 = b2 = null;
- foreach (Button ReInit in tb)
- {
- ReInit.Text = "";
- if (ReInit.BackgroundImage == null)
- {
- ReInit.Enabled = true;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement