Добавить в цитаты Настройки чтения

Страница 18 из 18

int scoreValue = 0;

Приведённый выше код в теле метода Form1_Paint заменяем на тот, который дан на следующем листинге.

Листинг 5.9. Метод для рисования изображения.

private void Form1_Paint(object sender, PaintEventArgs e)

{

//If the buffer empty, we create the new buffer:

if (backBuffer == null)

{

backBuffer = new Bitmap(this.ClientSize.Width,

this.ClientSize.Height);

}

//We create a object of class Graphics from the buffer:

using (Graphics g = Graphics.FromImage(backBuffer))

{

//We clear the form:

g.Clear(Color.White);

//We draw the images of objects in the backBuffer:

g.DrawImage(cheeseImage, cx, cy);

g.DrawImage(breadImage, bx, by);

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

{

if (tomatoes[i].visible)

{

g.DrawImage(tomatoImage,

tomatoes[i].rectangle.X,

tomatoes[i].rectangle.Y);

}

}

//We write the player's points:

g.DrawString(messageString, messageFont, messageBrush,

messageRectangle);

}

//We draw the image on the Form1:

e.Graphics.DrawImage(backBuffer, 0, 0);

} //End of the method Form1_Paint.

Приведённый выше код в теле метода Form1_Load (для загрузки файлов изображений игровых объектов) заменяем на тот, который дан на следующем листинге.

Листинг 5.10. Метод для загрузки файлов изображений.

private void Form1_Load(object sender, EventArgs e)

{

//We load into objects of the System.Drawing.Image class

//the image files of the set format, added to the project,

//by means of ResourceStream:

cheeseImage =

new Bitmap(myAssembly.GetManifestResourceStream(

myName_of_project + "." + "cheese.JPG"));

breadImage =

new Bitmap(myAssembly.GetManifestResourceStream(

myName_of_project + "." + "bread.JPG"));

//We initialize the rectangles, described around objects:

cheeseRectangle = new Rectangle(cx, cy,

cheeseImage.Width, cheeseImage.Height);

breadRectangle = new Rectangle(bx, by,

breadImage.Width, breadImage.Height);

//We load the image file of a new object:

tomatoImage =

new Bitmap(myAssembly.GetManifestResourceStream(

myName_of_project + "." + "tomato.gif"));

//We initialize an array of new objects and rectangles,

//described around these objects:

initialiseTomatoes();

//We place new objects in an upper part of the screen:

placeTomatoes();

//We create and initialize a font for record of points:

messageFont = new Font(FontFamily.GenericSansSerif, 10,

FontStyle.Regular);

//We reserve a rectangle on the screen

//for record of points:

messageRectangle = new Rectangle(0, 0,

this.ClientSize.Width, scoreHeight);

//We set the color of a brush for record of points:

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

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

Текст предоставлен ООО «ЛитРес».

Прочитайте эту книгу целиком, купив полную легальную версию на ЛитРес.

Безопасно оплатить книгу можно банковской картой Visa, MasterCard, Maestro, со счета мобильного телефона, с платежного терминала, в салоне МТС или Связной, через PayPal, WebMoney, Яндекс.Деньги, QIWI Кошелек, бонусными картами или другим удобным Вам способом.