K
K
Kirill912 20072019-09-17 23:17:59
C++ / C#
Kirill912 2007, 2019-09-17 23:17:59

If the object is in contact with black, then output to the console?

Hello!
Program: visual studio (winforms).
Please tell me how to insert this code:

if (obj.intersects(pictureBox2) && pictureBox1.color == Color.black) {
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CTDk { 
public partial class Form1 : Form { 
public Form1() { 
InitializeComponent(); 
//сюда
Console.WriteLine("Hello World!"); 
}
}
}

Tried like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CTDk { 
public partial class Form1 : Form { 
public Form1() { 
InitializeComponent(); 
if (obj.intersects(pictureBox2) && pictureBox1.color == Color.black) {
Console.WriteLine("Hello World!"); 
} 
}
}
}

But it doesn't work(
Gives an error.
Thank you in advance :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Foggy Finder, 2019-09-18
@FoggyFinder

First, a small lyrical digression.
If your question is related to any of the previous ones, then be sure to provide a link to it so that you can understand the context.
Now to the question.
Your object ( obj ) is a PictureBox , a control that, not surprisingly, does not have its own intersects method .
But you can use the Bounds property to get the location and dimensions of your objects:

if(pictureBox1.Bounds.IntersectsWith(pictureBox2.Bounds))
{
// put your code here
}

Regarding the second part - checking that the object is of a certain color. To be sure that the picture is completely the same color, you will have to check each pixel.
But, I think you can get things done in a much simpler way.
For example, if your object can change colors, and you are interested in whether it is black, then at the time of the color change, store this state ( isBlack ) in the Tag property of your pictureBox.
If you are writing some simple game like a snake, then you are not stuck yet, I advise you to take a little time to divide the code into semantic parts - define classes that describe your objects, interaction logic and separately display them on the form.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question