G
G
goldolov_na2019-05-26 23:50:06
C++ / C#
goldolov_na, 2019-05-26 23:50:06

How to check if all elements have been clicked?

there is a picturebox and selected objects are located on it by coordinates, how to check that all elements have been clicked?
here is part of the code:

public Form4()
        {
 
            InitializeComponent();
            objects = new List<MapObject2>();
            objects.Add(new MapObject2()
            {
                Name = "Мумия",
                X = 258,
                Y = 136,
                Width = 24,
                Height = 43
            });
 
            objects.Add(new MapObject2()
            {
                Name = "Банка",
                X = 286,
                Y = 178,
                Width = 17,
                Height = 17
            });
 
            objects.Add(new MapObject2()
            {
                Name = "Змеи",
                X = 370,
                Y = 303,
                Width = 23,
                Height = 19
            });
 
            objects.Add(new MapObject2()
            {
                Name = "Царь",
                X = 73,
                Y = 132,
                Width = 31,
                Height = 42
            });
 
            objects.Add(new MapObject2()
            {
                Name = "Колесница",
                X = 352,
                Y = 103,
                Width = 65,
                Height = 41
            });
 
            objects.Add(new MapObject2()
            {
                Name = "Конь",
                X = 5,
                Y = 163,
                Width = 15,
                Height = 38
            });
 
        }
        List<MapObject2> objects;
private void PictureBox2_MouseClick(object sender, MouseEventArgs e)
        {
            int currentX = e.X;
            int currentY = e.Y;
            for (int i = 0; i < objects.Count; i++)
            {
                if (currentX > (objects[i].X - objects[i].Width / 2) & currentX < (objects[i].X + objects[i].Width / 2)
                & currentY > (objects[i].Y - objects[i].Height / 2) & currentY < (objects[i].Y + objects[i].Height / 2))
                {
                    MessageBox.Show("Ты нашел слово!");
 
                }
 
            }
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
goldolov_na, 2019-05-27
@goldolov_na

Code for the MapObject2 class

public   class MapObject2
{
    public string Name;
    public int X;

    public int Y;
    public int Width;
    public int Height;

    public bool IsClicked = false;
}

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {
        int currentX = e.X;
        int currentY = e.Y;
        for (int i = 0; i < objects.Count; i++)
        {
            if (currentX > (objects[i].X - objects[i].Width / 2) & currentX < (objects[i].X + objects[i].Width / 2)
            & currentY > (objects[i].Y - objects[i].Height / 2) & currentY < (objects[i].Y + objects[i].Height / 2))
            {
                MessageBox.Show("Ты нашел слово: " + objects[i].Name);
                //Отмечаем что данный объект прокликан
                objects[i].IsClicked = true;
            }
        }

        //Проверяем все ли объекты прокликаны
        if (IsAllClicked()) MessageBox.Show("Все объекты прокликаны");
    }

    //Проверка коллекции объектов - все ли прокликаны
    private bool IsAllClicked()
    {
        bool AllObjectsClicked = true;
        foreach (MapObject2 item in objects)
        {
            if (!item.IsClicked)
            {
                AllObjectsClicked = false; break;
            }
        }
        return AllObjectsClicked;
    }

F
Foggy Finder, 2019-05-27
@FoggyFinder

Treat "whether there was a press" as a state.
Create a new, separate class that will contain all additional states, object properties for more convenient work with it:

public class SomeObject
{
    public bool WasClicked { get; set; }
    public MapObject2 Object { get; }

    public SomeObject(MapObject2 mobj)
    {
        Object = new MapObject2(mobj);
    }
}

Warning - do not use the name SomeObject . Names should reflect the meaning, purpose of the object. It’s hard for me to understand the purpose of the project, so I can’t give a meaningful name. This also applies to the MapObject2 class .
There are other options, but if I were you, I would choose this approach.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question