Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Jogo da memória – resolução possível
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Threading;
- namespace memoria{
- public partial class inicio : Form {
- int[] nums = new int[20];
- int[] aux = new int[20];
- // Image[] imagens = new Image[100];
- int f = 0;
- int tag;
- PictureBox pic1, pic;
- public inicio() {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e) {
- geraNumeros();
- CreatePic();
- Application.DoEvents();
- pausa();
- TapaPic(this.Controls);
- }
- private void geraNumeros(){
- int p, i = 1;
- Random rnd = new Random();
- while (i <= 8){
- p = rnd.Next(1, 9);
- if (nums[p] == 0){
- nums[p] = p;
- aux[i] = p;
- i++;
- }
- }
- //for (i = 1; i <= 8; i++)
- // imagens[i] = Properties.Resources.ResourceManager.GetObject("_" + aux[i]) as
- Image;
- }
- private void CreatePic(){
- //tenho 8 imagens, mas a imagem 1 é igual à imagem 5
- // a imagem 2 igual à imagem 6, etc.
- int k = 1;
- int posx, posy;
- posy = 45;
- for (int i = 1; i <= 2; i++) {
- posx = 30;
- for (int j = 1; j <= 4; j++) {
- string name = "picture dinamica_" + k;
- PictureBox pic = new PictureBox();
- pic.Name = name;
- pic.Tag = aux[k];
- pic.Font = new Font("Georgia", 12);
- pic.Size = new System.Drawing.Size(60, 50);
- pic.Location = new System.Drawing.Point(posx, posy);
- pic.Image = Properties.Resources.ResourceManager.GetObject("_" + aux[k]) as
- Image;
- pic.BackColor = Color.Black;
- pic.SizeMode = PictureBoxSizeMode.StretchImage;
- pic.Click += new EventHandler(this.picture_Click);
- Controls.Add(pic);
- k++;
- posx += 65;
- }
- posy += 65;
- }
- }
- protected void picture_Click(object sender, EventArgs e) {
- pic1 = (sender as PictureBox);
- if (f == 0){
- pic = pic1;
- tag = (int)(pic.Tag);
- pic.Image = Properties.Resources.ResourceManager.GetObject("_" + tag) as Image;
- f = 1;
- }
- else{
- pic1 = (sender as PictureBox);
- int t = (int)(pic1.Tag);
- pic1.Image = Properties.Resources.ResourceManager.GetObject("_" + t) as Image;
- Application.DoEvents();
- pausa();
- f = 0;
- if (t == tag + 4 || t == tag - 4){
- pic.Enabled = false;
- pic1.Enabled = false;
- }
- else{
- pic1.Image = Properties.Resources.ff2018;
- pic.Image = Properties.Resources.ff2018;
- }
- }
- }
- private void pausa() {
- Thread.Sleep(1000);
- // TimeSpan ts4 = new TimeSpan(4000);
- }
- private void TapaPic(Control.ControlCollection controles)
- {
- foreach (Control ctrl in controles) {
- if (ctrl is PictureBox) {
- ((PictureBox)(ctrl)).Image = Properties.Resources.ff2018;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment