this.ClientSize.Height);
}
//We create a object of the Graphics class from the buffer:
using (Graphics g = Graphics.FromImage(backBuffer))
{
//We clear the form:
g.Clear(Color.White);
//We draw the image 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 draw the image on the Form1:
e.Graphics.DrawImage(backBuffer, 0, 0);
} //End of the method Form1_Paint.
Добавление новых объектов в игру соответственно усложняет код. В панели Properties (для Form1) на вкладке Events дважды щёлкаем по имени события Load. Появившийся шаблон метода Form1_Load после записи нашего кода принимает следующий вид.
Листинг 5.6. Метод для рисования изображения.
private void Form1_Load(object sender, EventArgs e)
{
//We load into objects of class System.Drawing.Image
//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 tomato:
tomatoImage =
new Bitmap(myAssembly.GetManifestResourceStream(
myName_of_project + "." + "tomato.gif"));
//We initialize an array of tomatoes and rectangles:
initialiseTomatoes();
//We place the tomatoes in an upper part of the screen:
placeTomatoes();
//We turn on the timer: