This works when I used a rectangle as the food but now I'm trying to use my own image for the food but can't get it to work correctly. I keep getting the error of "Cannot convert Image to Rectangle" when using IntersectsWith
so I'm stuck there and not sure what to do.
My error is on this part of the code:if (snake.SnakeRec[i].IntersectsWith(food.foodRec))
for (int i = 0; i < snake.SnakeRec.Length; i++)
{
if (snake.SnakeRec[i].IntersectsWith(food.foodRec))
{
score += 10;
snake.growSnake();
food.foodLocation(randFood);
}
}
collision();
this.Invalidate();
In the Food.cs class
public void drawFood(Graphics paper)
{
Image foodRec = Image.FromFile(@"C:\food.bmp");
Rectangle rectangleAreaToDrawImage = new Rectangle(x, y, width, height);
paper.DrawImage(foodRec, rectangleAreaToDrawImage);
}
If you need more code let me know.
Edit: Solved
I had to change:
if (snake.SnakeRec[i].IntersectsWith(food.foodRec))
Rectangle rectangleAreaToDrawImage = new Rectangle(x, y, width, height);
to:
if (snake.SnakeRec[i].IntersectsWith(food.rectangleAreaToDrawImage))
rectangleAreaToDrawImage = new Rectangle(x, y, width, height);