dimanche 26 avril 2015

Checking if a label I click, is a part of a pattern of labels

I am making a game about remembering a pattern of labels, that show up by them changing colors. The user then has to click the labels showed, in the correct order.

After the pattern has been randomly chosen, it goes into a list called the 'pattern'. My problem here is; how do I make it so that my label_Click event handler can be used to check if the correct label has been clicked for the entire list, in the correct order. Sorry that my code is clumsy and without comments, I have just started programming.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Spil
{
    public partial class Form1 : Form
    {
        Random rnd = new Random();
        Label[] labelArray;
        int turn = 1;
        int lives = 3;
        List<Label> pattern = new List<Label>();

        public Form1()
        {
            InitializeComponent();

            labelArray = new Label[] { label1, label2, label3, label4, label5, label6, label7, label8, label9 };
        }

        private void DisplayOrder()
        {
            for (int i = 0; i < labelArray.Length; i++)
            {
                labelArray[i].BackColor = Color.Blue;
                labelArray[i].Click += label_Click;
            }

            for (int i = -2; i < turn; i++)
            {
                int chosenNumber = rnd.Next(0, 9);
                labelArray[chosenNumber].BackColor = Color.Green;
                Thread.Sleep(1000);
                labelArray[chosenNumber].BackColor = Color.Blue;
                pattern.Add(labelArray[chosenNumber]);
            }
        }

        private void label_Click(object sender, EventArgs e)
        {
            Label clickedLabel = sender as Label;
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            System.Timers.Timer t = new System.Timers.Timer(100);

            t.Elapsed += t_Elapsed;

            t.Start();
        }

        void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ((System.Timers.Timer)sender).Stop();

            DisplayOrder();
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire