Вход/Регистрация
Справочник Жаркова по проектированию и программированию искусственного интеллекта. Том 7: Программирование на Visual C# искусственного интеллекта. Издание 2
вернуться

Жарков Валерий

Шрифт:

messageBrush = new SolidBrush(Color.Black);

//We turn on the timer:

timer1.Enabled = true;

} //End of the method Form1_Load.

И наконец, вместо приведённого выше метода updatePositions записываем следующий метод, дополненный новым кодом для изменения координат, обнаружения столкновений объектов, уничтожения помидоров и подсчёта очков.

Листинг 5.11. Метод для изменения координат и обнаружения столкновения объектов.

private void updatePositions

{

if (goingRight)

{

cx += xSpeed;

}

else

{

cx -= xSpeed;

}

if ((cx + cheeseImage.Width) >= this.Width)

{

goingRight = false;

//At the time of collision, the Beep signal is given:

Microsoft.VisualBasic.Interaction.Beep;

}

if (cx <= 0)

{

goingRight = true;

//At the time of collision, the Beep signal is given:

Microsoft.VisualBasic.Interaction.Beep;

}

if (goingDown)

{

cy += ySpeed;

}

else

{

cy -= ySpeed;

}

//That cheese did not come for the button3.Location.Y:

if ((cy + cheeseImage.Height) >= button3.Location.Y)

{

goingDown = false;

//At the time of collision, the Beep signal is given:

Microsoft.VisualBasic.Interaction.Beep;

}

if (cy <= 0)

{

goingDown = true;

//At the time of collision, the Beep signal is given:

Microsoft.VisualBasic.Interaction.Beep;

}

//We set to rectangles of coordinate of objects:

cheeseRectangle.X = cx;

cheeseRectangle.Y = cy;

breadRectangle.X = bx;

breadRectangle.Y = by;

// check for collisions.

if (goingDown)

{

// only bounce if the cheese is going down

if (cheeseRectangle.IntersectsWith(breadRectangle))

{

//At the time of collision,

//the Beep signal is given:

Microsoft.VisualBasic.Interaction.Beep;

// we have a collision

bool rightIn = breadRectangle.Contains(

cheeseRectangle.Right,

cheeseRectangle.Bottom);

bool leftIn = breadRectangle.Contains(

cheeseRectangle.Left,

cheeseRectangle.Bottom);

// now deal with the bounce

if (rightIn & leftIn)

{

// bounce up

goingDown = false;

}

else

{

// bounce up

goingDown = false;

// now sort out horizontal bounce

if (rightIn)

{

goingRight = false;

}

if (leftIn)

{

goingRight = true;

}

}

}

}

else

{

// only destroy tomatoes of the cheese is going up

for (int i = 0; i < tomatoes.Length; i++)

{

if (!tomatoes[i].visible)

{

continue;

}

if (cheeseRectangle.IntersectsWith(

tomatoes[i].rectangle))

{

//At the time of collision,

//the Beep signal is given:

Microsoft.VisualBasic.Interaction.Beep;

// hide the tomato

tomatoes[i].visible = false;

// bounce down

goingDown = true;

// update the score

scoreValue = scoreValue + 10;

messageString = "Points : " + scoreValue;

// only destroy one at a time

Конец ознакомительного фрагмента.

  • 1
  • ...
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

Ебукер (ebooker) – онлайн-библиотека на русском языке. Книги доступны онлайн, без утомительной регистрации. Огромный выбор и удобный дизайн, позволяющий читать без проблем. Добавляйте сайт в закладки! Все произведения загружаются пользователями: если считаете, что ваши авторские права нарушены – используйте форму обратной связи.

Полезные ссылки

  • Моя полка

Контакты

  • chitat.ebooker@gmail.com

Подпишитесь на рассылку: