Nerviie

Jogo Memória

Dec 3rd, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. // Jogo da memória – resolução possível
  2. using System;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Threading;
  6. namespace memoria{
  7.  public partial class inicio : Form {
  8.  int[] nums = new int[20];
  9.  int[] aux = new int[20];
  10.  // Image[] imagens = new Image[100];
  11.  int f = 0;
  12.  int tag;
  13.  PictureBox pic1, pic;
  14.  public inicio() {
  15.  InitializeComponent();
  16.  }
  17.  private void button1_Click(object sender, EventArgs e) {
  18.  geraNumeros();
  19.  CreatePic();
  20.  Application.DoEvents();
  21.  pausa();
  22.  TapaPic(this.Controls);
  23.  }
  24.  private void geraNumeros(){
  25.  int p, i = 1;
  26.  Random rnd = new Random();
  27.  while (i <= 8){
  28.  p = rnd.Next(1, 9);
  29.  if (nums[p] == 0){
  30.  nums[p] = p;
  31.  aux[i] = p;
  32.  i++;
  33.  }
  34.  }
  35.  //for (i = 1; i <= 8; i++)
  36.  // imagens[i] = Properties.Resources.ResourceManager.GetObject("_" + aux[i]) as
  37. Image;
  38.  }
  39.  private void CreatePic(){
  40.  //tenho 8 imagens, mas a imagem 1 é igual à imagem 5
  41.  // a imagem 2 igual à imagem 6, etc.
  42.  int k = 1;
  43.  int posx, posy;
  44.  posy = 45;
  45.  for (int i = 1; i <= 2; i++) {
  46.  posx = 30;
  47.  for (int j = 1; j <= 4; j++) {
  48.  string name = "picture dinamica_" + k;
  49.  PictureBox pic = new PictureBox();
  50.  pic.Name = name;
  51.  pic.Tag = aux[k];
  52.  pic.Font = new Font("Georgia", 12);
  53.  pic.Size = new System.Drawing.Size(60, 50);
  54.  pic.Location = new System.Drawing.Point(posx, posy);
  55.  pic.Image = Properties.Resources.ResourceManager.GetObject("_" + aux[k]) as
  56. Image;
  57.  pic.BackColor = Color.Black;
  58.  pic.SizeMode = PictureBoxSizeMode.StretchImage;
  59.  pic.Click += new EventHandler(this.picture_Click);
  60.  Controls.Add(pic);
  61.  k++;
  62.  posx += 65;
  63.  }
  64.  posy += 65;
  65.  }
  66.  }
  67.  protected void picture_Click(object sender, EventArgs e) {
  68.  pic1 = (sender as PictureBox);
  69.  if (f == 0){
  70.  pic = pic1;
  71.  tag = (int)(pic.Tag);
  72.  pic.Image = Properties.Resources.ResourceManager.GetObject("_" + tag) as Image;
  73.  f = 1;
  74.  }
  75.  else{
  76.  pic1 = (sender as PictureBox);
  77.  int t = (int)(pic1.Tag);
  78.  pic1.Image = Properties.Resources.ResourceManager.GetObject("_" + t) as Image;
  79.  Application.DoEvents();
  80.  pausa();
  81.  f = 0;
  82.  if (t == tag + 4 || t == tag - 4){
  83.  pic.Enabled = false;
  84.  pic1.Enabled = false;
  85.  }
  86.  else{
  87.  pic1.Image = Properties.Resources.ff2018;
  88.  pic.Image = Properties.Resources.ff2018;
  89.  }
  90.  }
  91.  }
  92.  private void pausa() {
  93.  Thread.Sleep(1000);
  94.  // TimeSpan ts4 = new TimeSpan(4000);
  95.  }
  96.  private void TapaPic(Control.ControlCollection controles)
  97.  {
  98.  foreach (Control ctrl in controles) {
  99.  if (ctrl is PictureBox) {
  100.  ((PictureBox)(ctrl)).Image = Properties.Resources.ff2018;
  101.  }
  102.  }
  103.  }
  104.  }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment