これは、食べ物として長方形を使用した場合に機能しますが、現在、食べ物に自分の画像を使用しようとしていますが、正しく機能させることができません。使用時に「画像を長方形に変換できません」というエラーが表示され続けるIntersectsWith
ので、そこで行き詰まり、どうしたらよいかわかりません。
私のエラーはコードのこの部分にあります: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();
Food.csクラスで
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 (snake.SnakeRec[i].IntersectsWith(food.foodRec))
Rectangle rectangleAreaToDrawImage = new Rectangle(x, y, width, height);
に:
if (snake.SnakeRec[i].IntersectsWith(food.rectangleAreaToDrawImage))
rectangleAreaToDrawImage = new Rectangle(x, y, width, height);