       .  7:   Visual C#  .  2
  


                   ()  -     ,   ,           .    Visual C#   IX ,      7  8.   7 . I.   Visual C#. II.        . III.      -. IV.          . V.              . VI.  .      VC#    ,      .              VC#   .





 

       .  7:   Visual C#  .  2


      

  (  )








                                  , ,       ,        Visual C#         Microsoft ().

  ()     ,    ,   ,         .     , ,      ,    ,      -    .  ,         .           .      , ,     ,   ,      ,        .            .

  (, , ) Visual Studio (  VS)   -               ,             .  ,    VS      ,       ,       (    )            Visual C#, Visual Basic  Visual C++.      (   )    ,     ,              .

       ,      .           Visual C#        VC#.     ,        (        ZharkovPress.ru     )    ,         .           ,            VC#,      ;       ,           Visual C#     .    ,        ,    ,   (   ,     ,       ),      ,     .         ,           .     .           (        ,    )      ( : ,  )           VS.               (      )        .                  (    Help  Visual Studio,  ,      ),       , ,     (   )   , , , , ,      (  )     .               VS  (  ),            ,        .

     .

.  I.   Visual C#.  1.   .  2.         .  3.              .  II.         .  4.      .  5.   ,       .  6.    .  7.       .  8.         .  9.       ,         .  10.      .  III.        "-"       .  11.        "-"   3x3.  12.        -   6x7.  IV.                   .  13.               .  14.               .  V.                       .  15.                    .  VI. ,          .  16.     . . .

        ,    Microsoft,      Microsof.                   ,        .     ,        (         )  ,                ,              ,            () .

                       ,    (  )    Visual C#     , ,   ,                           .      ,   ,      , , , ,    .

    (     )         .

,          email   ZharkovPress.ru.




 I.   Visual C#





 1.   





1.1.   


  (computer game)            ,   .

   ,       (game program),           ,      .

       ,            ,        .  ,  ,  ,     .

                    .

          Microsoft Windows.

             Microsoft Visual C#    Microsoft Visual Studio  .

      (  )  .                DirectX,     DirectX  XNA  ,   Microsoft.

,    ,    .

  ,       (   ,    puzzle  )   ,        (, ,   ..).      ,   ,          .           ,     .

 (arcade game)      ,  ,  ,  ,     ,   ,      ,  ,     .     ,   ,        .           ,   .        ,  (shooter)   (action).

   .              ,         .       , ,    ,      ,       .      ,     ,   .

   (adventure game)  ,          , ,    .

   (extensive game)  ,     , ,     .

-    , , ,     .

         (,   ..).

         ,   , ,       ,     .

 (interactive game)  ,           .

 (network game)  ,      ,            LAN  Local Area Network.

 (training game)  ,     .

          .   (RPG Role Playing Game)   ,         (     ) .

  ,    (no-solution game),   .

     ,    ,    ,   ,    ,      ,      .

                   .    ,     ,     (,  ),    .

         ,        .




1.2.   


     -     ,      Visual Studio,     (  )      .

1.   (   , ,   ..) Visual Studio   VS  Microsoft.         . ,     VS, , Express-  -,      Microsoft.        VS    []       ZharkovPress.ru.

VS    CLR (Common Languege Runtime),      .       (   ) ,     (  CLR). ,         ,      MSIL (Microsoft Intermediate Language)       .             .

             (  )  Visual C#,       ,       (Visual Basic, C#, ++, J#)    (   []). ,     Visual Studio        Visual Studio.

     Windows,        Visual Studio,  ,   Visual C#       GDI+ (Graphics Device Interface)   GDI+            .

  ,     ZharkovPress.ru         DirectX     .

2.    DirectX / XNA,     Microsof       DirectX     DirectX Software Development Kit (SDK)  XNA.         DX  XNA.    DX  XNA     Microsoft. ,     Microsoft   DirectX        Windows     Technologies. , DirectX (  DX)   ,      (  ).

,                  DirectX  XNA,        ,     DirectX  XNA.




 2.         





2.1.  - 


             .            ,        ,    ,     .         :     ;     ;     ;      ().

    (   )  :

1)  ,     (,   ,          Visual Studio),    ,       ,            (,     )  -  ()     Visual C#;

2)  ,             ( ,      )    ,    ,      (   , , Windows)   ( )     .




2.2.  


    -   (  )      .    ,   ,         ,               (  ).

    ( )   Visual C#   Visual Studio          .

1.   ( :  , , ,   ..) Visual Studio    New Project ( File, New, Project).

2.   New Project (. 2.1)   Project types    Visual C#, Windows  ,    Templates   ( ,  )  Templates, Visual C#, Windows Classic Desktop, Windows Forms App (.NET Framework);  Name   , , Calculator.

    Location    (      ),      .

       Location (  Browse)            ,         Create New Folder.

3.     (Solution)     ,            (   ),    (  )  Create directory for solution (. 2.1)     .








. 2.1.   Project Types    Visual C#, Windows.

4.   New Projects (. 2.1)  OK.

Visual C#       Form1    (,   )   Form1.cs [Design],          (. 2.2).

5.   ,          ,        Visual Studio.      Visual Studio   :

    (Solution)   ,    Build  Build Solution        ,    Ctrl+Shift+B; ,         Tools, Customize,      Alt,     (   - Microsoft Word);

    (Solution)    (   ),    Build, Build Selection        ;  Build    Build Selection   Selection    ,    Calculator.

      Output   Output (. 2.2)   C#       :

Compile complete  0 errors, 0 warnings

Calculator -> D:\MyDocs\DesktopProjects\DesktopProjects2\Calculator\bin\Debug\Calculator.exe

========== Build: 1succeeded or up-to-date, 0 failed, 0 skipped ==========















. 2.2.  Form1    (,   ).

6.   ,     :  Debug  Start Without Debugging (   Ctrl+F5)  Start ( F5),        .

  Form1       Form1   ,          .      Close ()          .

   Visual Studio ,        (   ).




2.3.   


  (        Toolbox, . 2.3)         .

1.  Properties ()    Form1 (. 2.4)    (    )    View, Properties Window          ,         .

   Properties   Form1,     ,            Properties.   Properties        Properties (   , ,   Events).   (  )              ;           Enter.

    ( )   ,       Form1.cs [Design]    Solution Explorer (- )    Form1.cs.

2.   Properties   Form1 (. 2.4)  ()  Font      .    Font (. 2.4).

3.   Font () , ,  Times New Roman   (Size) 14          ,         Toolbox.   Font  OK.

4.    ,   Properties   Text   Form1  (    :  , Paste), , Calculator;    Text (   Enter), . 2.4.

5.    BackColor (. 2.4)    (     Control)       Form1.         []. ,             .

6.    BackgroundImage (), . 2.4,           ( (.bmp), (.jpg), (.gif)  .)    ,              ;          (, TextBox  Button)   (, Timer),       [].

7. ,     Toolbox (. 2.3)    Windows Forms   .                  GroupBox,        .      Toolbox (. 2.3)   GroupBox ( ).    .    groupBox1,   Properties   Text  groupBox1  (    :  , Paste), ,   (Addition of numbers);     Text (   Enter)    Properties      .

8.      TextBox (   );  Properties    textBox1;   ( )  textBox1,   Properties   Text   (       Enter).

9.           .

10.    ,    ,      Label. ,   label1       ,    Properties   label1   Text  label1  (    :  , Paste), ,  1 (number 1).

11.        ,          +.

12.           =,        :   Toolbox  Button;   ;  button1       ,    Properties   Text  button1  (    :  , Paste), ,   =;     Text (   Enter).

  Form1   Class View    .      (. 2.6).

,         Visual Studio      ,              .

 ,   ,     ,         () Visual Studio.









. 2.3.   Toolbox. . 2.4.  Solution Explorer  Properties.








. 2.5.   Font  .








. 2.6.    () .




2.4.  


   Form1.cs            .          ,                Events (    )   Properties (. 2.4). ,       = (. 2.6)        Click   Events  Properties.         ,                   .  .   Form1.cs     :

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace Calculator

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

}

}

    Form1.cs             :

private void button1_Click(object sender, EventArgs e)

{



}

          (using System;),   System (   )        .

            .

     (   , , TextBox) ,   (  )      .

     : 

 2.1.      .

double a, b, c;

a = Convert.ToDouble(textBox1.Text);

b = Convert.ToDouble(textBox2.Text);

c = a + b;

textBox3.Text = c.ToString();

     :



double a, b;

a = Convert.ToDouble(textBox1.Text);

b = Convert.ToDouble(textBox2.Text);

textBox3.Text = (a + b).ToString();

     :

double a;

a = Convert.ToDouble(textBox1.Text);

textBox3.Text = (a +

Convert.ToDouble(textBox2.Text)).ToString();

     :



textBox3.Text = (Convert.ToDouble(textBox1.Text) +

Convert.ToDouble(textBox2.Text)).ToString();

          .     (  , ,     )     [].

   ( Build, Build Selection      )      Output   C#    ,    (         )          .   ,   ,     ,         .

  ,    Debug  Start Without Debugging (      ).

        (. 2.6)      (. 2.7),            .




2.5.  


  : 1,1 + 2,2 = 3,3     . 2.7.










. 2.7.    .

 . 2.7 ,            ,         . ,      2.1   ( ) ,      .          .

    :   ,   .  ,                       Visual C# (  ).




2.6.   


          .        , , ,   (  2). ,        (   ,       ),          .     .      , ,   ( 3).    =    . 2.8.








. 2.8.     .

     (, ), , 5,555555555555557E+19 (. 2.8)    E  ,   E  . ,       ,    ,  (   6  . 2.8     ).      =          ,  ,    ( )          . ,          (31).

           .

1.          .

2.        (15).

3.       (31).

4.    x   |x|

1*10E-031 <= |x| <= 9.99999999999999*10E+031.

5.    ():

 

1 <= |x| <= 999999999999999

;



 

1*10E-031 <= |x| < 1



999999999999999 < |x| <= 9.99999999999999*10E+031

.

     ,     -     Windows-,   - .

     ,        ,     ,  (    )          -          ,         ( )        .

    ,          Form1.




2.7.    


                , ,   ,             (  ).          .

       Timer  :

1.    Toolbox   Timer (. 2.3).

2. ٸ  .

 Form1     timer1 (. 2.9),         .

,                   .

3.   Properties   timer1:

  Enabled  False  True (. 2.10);

  Interval     100  , ,  500  (,  1000   1 ).

,         Properties,    , ,    .

 2.2.        .

private void InitializeTimer()

{

//We turn on the timer:

timer1.Enabled = true;

//We generate the Tick event through each Interval of time:

timer1.Interval = 500;

}















. 2.9.   Timer. . 2.10.  Properties.

         500  ( 0,5 )      Tick      timer1_Tick (.   2.3),  ,       .

,             .      .

, ,  :

1)    ,    ,    (, ,   ..)      Interval;

2)    ,    ,          Interval,       N_Interval ;

3)    ,      ,           .

   ,         Interval.      timer1 (. 2.8)    (   Properties   Events       Tick).

  Form1.cs     ,     ,      .

 2.3.    .  1.

//We declare the Boolean myText variable and appropriate it

//false (by default too "false"):

bool myText = false;

private void timer1_Tick(object sender, EventArgs e)

{

//We set the alternation of " Calculator " and

//" Calculator with animation ":

if (myText == false)

{

this.Text = " Calculator ";

//We change the myText value to opposite:

myText = true; //or so: myText =! myText;

}

else

{

this.Text = " Calculator with animation ";

//We change the myText value to opposite:

myText = false; //or so: myText =! myText;

}

}

           .

   ,     ,    ,          Interval,       N_Interval.     Form1.cs    (    )      ,     ,     .

 2.4.    .  2.

//We declare the counter of number of intervals

//and set its initial value:

private int counter = 0; //by default too it is equal to 0.

//We declare and set the number N_Interval of intervals,

//after which one text is replaced by another:

private int N_Interval = 3;

private void timer1_Tick(object sender, EventArgs e)

{

//We check, value of the counter

//equally or yet is not present to number N_Interval of

//intervals,

//after which one text is replaced by another:

if (counter >= N_Interval)

{

//If value of the counter collected and is equal

//N_Interval, we output other text:

this.Text = "Calculator";

//We nullify the value of the counter again:

counter = 0;

}

else

{

//If value of the counter did not collect yet

//and N_Interval is not equal,

//that we output the first text:

this.Text = "Calculator with animation";

//We increase value of the counter on 1:

counter = counter + 1;

}

}

 ,   ,     ,     N_Interval_Stop  ,           .     Form1.cs    (    )        ,       ,     .

 2.5.    .  3.

//We declare the counter of number of intervals

//and set its initial value:

private int counter = 0;

//We declare and set the number N_Interval of intervals,

//after which one text is replaced by another:

private int N_Interval = 3;

//We declare and nullify the i_Interval_Stop counter,

//which calculates the number of intervals

//to an animation stop:

private int i_Interval_Stop = 0;

//We declare and set the number N_Interval_Stop of intervals,

//on reaching which animation stops:

private int N_Interval_Stop = 10;

private void timer1_Tick(object sender, EventArgs e)

{

//Value of the i_Interval_Stop counter,

//which calculates the number of intervals

//to an animation stop, we increase on 1:

i_Interval_Stop = i_Interval_Stop + 1;

//We check the number i_Interval_Stop of intervals,

//on reaching which animation stops:

if (i_Interval_Stop >= N_Interval_Stop)

timer1.Enabled = false;

//We check, value of the counter

//equally or yet is not present to number N_Interval of

//intervals,

//after which one text is replaced by another:

if (counter >= N_Interval)

{

//If value of the counter collected and is equal

//N_Interval, we output other text:

this.Text = "Calculator";

//We nullify value of the counter again:

counter = 0;

}

else

{

//If value of the counter did not collect yet

//and N_Interval is not equal,

//that we output the first text:

this.Text = "Calculator with animation";

//We increase value of the counter on 1:

counter = counter + 1;

}

}

       timer1_Tick,      ,    .

   

private void timer1_Tick(object sender, EventArgs e)

   ,   timer1_Tick  (Handles)  Tick,  (      Interval)   () timer1.   (bool myText = false;)      myText   timer1_Tick.    myText         timer1_Tick,      (  )   timer1_Tick   myText   ,     .

 -   , ,           : Build, Build Selection; Debug, Start Without Debugging.

  Visual C#           .         500  ( 0,5 )   (Calculator)      (Calculator with animation) (. 2.10),      .








. 2.10.    .

   2.3  2.5   " (Calculator)"   "" (..   " (Calculator)"),          "   (Calculator with animation)",       (  )   .          (. 2.10).

      ( 2.4)    ( 2.5);     .        .      ,  ,  ,   .

,             . ,        ,        2.3  2.5    this    Name  -   (label1, button1  ..),           .






2.8.     


           (    - ), ,    ,     ,       , ,   ..           .       ,      -         true  Enabled     false, ,       (      ).

 2.6.  ,  .

timer1.Enabled = false;

         ,          .

  ,         :

 2.7.  ,  .

timer1.Enabled = true;

                  , , .      Form1  (  )    Button,   Text  &Stop/Start Animation       (   Properties      Events      Click).   Form1.cs     ,     ,     ,     :

 2.8.      .

//We declare the Boolean OffOn variable and set it "false":

bool OffOn = false;

private void button2_Click(object sender, EventArgs e)

{

//We set alternation of a stop and resuming of animation

//after each click of the button2 button:

if (OffOn == false)

{

//We stop the animation:

timer1.Enabled = false;

//We change the OffOn value to opposite:

OffOn = true; //or so: OffOn =! OffOn;

}

else

{

//We resume the animation:

timer1.Enabled = true;

//We change OffOn value to opposite:

OffOn = false; //or so: OffOn =! OffOn;

}

}

       ( 2.3)  , , : Ctrl+F5.   Visual C#           .         500  ( 0,5 )   (Calculator)      (Calculator with animation) (. 2.11),      .








. 2.11.        Stop/Start Animation.



         (. 2.11).     Text   &Stop/Start Animation   &,    S  , ,       ,     Alt+s.

  ,           ,    Properties      Events      Click       ,     2.8.

     ,         ,      , , []    ZharkovPress.ru.




2.9.            


       (  ),        ,    ,       (, , )          ,   .       ()    , ,   Visual Basic,      .

  , ,  Visual Basic   .

1.   Project  Add Reference (   Solution Explorer             Add Reference).

   Add Reference (. 2.12).

2.   Add Reference   (.NET)     (dynamic link library), , Microsoft.VisualBasic    OK.

        Solution Explorer (. 2.13).

      Visual Basic         (  )   ,     .















. 2.12.  Microsoft.VisualBasic. . 2.13.  Solution Explorer.




2.10.           


,         ,        .

        ,      Visual Basic   ,       -         Beep().    Visual Basic   Beep()      Tick ,          Tick     .

   Beep()    , ,  Tick    Visual C#,     ,   Visual C#   .

           ()    ,    Visual Basic.      , ,  Tick   Visual C#   (   Microsoft)     (.),       ,        (. 2.14).








. 2.14.  ,      VC#.

    Visual Basic,        (Microsoft.VisualBasic).

    Microsoft.VisualBasic      (.),         VisualBasic,        (. 2.15). ,             (dynamic link library) Microsoft.VisualBasic,         .      Interaction,       :

Microsoft.VisualBasic.Interaction








. 2.15.  ,     .

    Microsoft.VisualBasic.Interaction    (.),        (  ) Visual Basic   Interaction,        (. 2.16).      Beep(),        (Microsoft.VisualBasic.Interaction.Beep()).

 ,        , ,  Tick   Visual C#      Beep():

Microsoft.VisualBasic.Interaction.Beep();

   Beep       Tick      Interval.








. 2.16.  ,     .

        N_Beep,        (, 10)   N_Beep:

//We declare the counter of number of intervals

//and set its initial value:

int i = 0;

//We declare and set the number N_Beepof of intervals,

//after which a giving of a sound signal will stop:

int N_Beep = 10;

        i:

i = i + 1;

if (i <= N_Beep)

Microsoft.VisualBasic.Interaction.Beep();

           , ,          (   ).

  ,      []          ,              .

    ,    ()                (  )     Visual C#    ,    (  )   ( ) ,            .

         -   ,    ( )               .




 3.              





3.1.      


   . ,                     ,       (      )         ,       .                        .     (,  )      ,      , ,   .     :

  :

     ;

     ;

     ;

    (   ):

  OK,         .

 ()            (          ,   );   ,      .

,    (       ),      -.

1.  VS   New Project (  File, New, Project).

2.   New Projects ( )   Project Types  Visual C#;  Templates ,   ( )  Templates, Visual C#, Windows Classic Desktop, Windows Forms App (.NET Framework);  Name   , , Calculator2_2 (  2    ,    2    ). ٸ OK.

  Visual C#        Form1 (    ).

        (. 3.1)     ( , , , )    (  Timer:  Enabled   True;  Interval, ,   ,  100 ).




3.2.   


        Project  Add Windows Form (   Solution Explorer         Add, Add Windows Form).    Add New Item (. 3.2).

  Add New Item        Add (   VS   Open).   Visual C#    VS   Form2,  ,  Form1 (. 3.3)     Solution Explorer   Form2.cs (. 3.4).   Form2  ,    Solution Explorer     Form2.cs (. 3.4).

,  ,    Form2 (. 3.3).

        ,      .








. 3.1.     .










. 3.2.   Add New Item       Add.















. 3.3. Form2  .  . 3.4.  Solution Explorer.




3.3.  


    Form1   .   (   )    .

 3.1.  Button1_Click      .

private void button1_Click(object sender, EventArgs e)

{

double A, B;

A = Convert.ToDouble(textBox1.Text);

B = Convert.ToDouble(textBox2.Text);

Form2 myForm2 = new Form2();

myForm2.C = A;

myForm2.D = B;

myForm2.Show();

}

,   Visual C#,    Visual Basic,      .  Show()   ,      ShowDialog().

    Form2.         C  D,       ,       .

 3.2.    Button1_Click     Form2.

public double C, D;

private void button1_Click(object sender, EventArgs e)

{

double F, G;

F = C;

G = D;

textBox1.Text = F.ToString();

textBox2.Text = G.ToString();

textBox3.Text = (F * G).ToString();

}

  ,  A  B, , ,   C  D,   ,    ,       TextBox,         .  F, G    (    )     C, D. ,              ,         ( 3.1  3.2),   .

          ZharkovPress.ru.




3.4.       


                     ,    ,    -  .         ,  :

    ;

      .

         :

   ,     (  )      Properties   Interval;

      ,    ()   .

        timer1      .   Form1.cs    timer1_Tick    Tick,  (  )   () timer1.     ,       ,     .

 3.3.       .

//We declare and nullify the global variable:

int i = 0;

private void timer1_Tick(object sender, EventArgs e)

{

//We write the text of heading in the myString variable:

string myString = "Calculator2_2 ";

//On the right  to the left appear the i-letter of

//a heading of form:

this.Text = myString.Substring(0, i);

//We organize a cycle of output of

//the following i-letter:

i = i + 1;

if (i == myString.Length)

i = 1;

}

             .

          ( ):

   ;

   ,     (  )      Properties   Interval;

             ()   .

   timer1      .

  Form2.cs  ;      ,      ,     .

 3.4.       .

//We declare the global variable "myString"

//and write in it the text of heading:

public static string myString = "Calculator2_2. Form2 ";

//We declare the global variable i

//and equate its value  to number of signs of heading:

int i = myString.Length;

private void timer1_Tick(object sender, EventArgs e)

{

//At the left  to the right leaves one i-letter

//of a heading:

this.Text = myString.Substring(0, i);

//We organize a removal cycle of

//the following i-letter of heading:

i = i  1;

if (i == -1)

i = myString.Length;

}

      -   (   ),    3.3  3.4  

this.Text = myString.Substring(0, i);

   this       ( Name), , (button1.)  .




3.5.  


      ()   -, ,     :

1.  : Build, Build Selection; Debug, Start Without Debugging.

  Visual C#                   (. 3.5).         .

2.      .

3. ٸ   ,       =.

   (. 3.6)      .         .

4.      OK.

  Visual C#     (. 3.6):

      ;

      ;

       .

     x (Close).   Visual C#   ,     .                .








. 3.5.  .








. 3.6.  .

                .

1. ٸ     ( ),   ( )   (,   )    =.

         .

2. ٸ OK          .

          .      (     )  .

      x (Close)   ,     ( ) .

 ,           .

     ( )      -        ,     ,     ,       .

      ,            Windows,   -   :          (    ,    );       ,     ;      ,        ,     ;    (       ) ,      .  ,     ,              .

    (  )      (, )                 ,     .       ,   ,        ,         Visual Studio   , ,   .




 II.         





 4.      





4.1.     


         ,         ,     microsoft.com: Rob Miles. Games Programming with Cheese: Part One.           ,   ,     Visual Studio,             ,   ,     Visual Studio.

          .        (  )             (arcade).

  ,   , , , -  , ,    (cheese).          ,        , , 25 x 32 .       ,    -  , , Paint,       Windows.

   , ,       ,    ,      .

      : VS   New Project   Project types    Visual C#, Windows,   Templates   Templates, Visual C#, Windows Classic Desktop, Windows Forms App (.NET Framework),   Name  (   )     OK.  , ,  ,      ,          ,    Name    .  ,   Form1 (. 4.1)   .  (   ) ,        . ,          ,    Properties ( Form1)   BackColor   Window.






















. 4.1. . . 4.2.    SE ()   Properties ().

   (      )    cheese.jpg   ,  :  Project  Add Existing Item,      Files of type  All Files,            Add (     ).

  Solution Explorer     (. 4.2).

    cheese.jpg          ,  :  Solution Explorer     ,    Properties (  )   Build Action (  )      Content ()  None   Embedded Resource ( ).

  ,     Form1.cs    System.Reflection    Assembly:

using System.Reflection; //For the Assembly class.

  Properties ( Form1)   Events      Paint.    Form1_Paint       .

   , ,    PictureBox     -     .

 4.1.    .

//We declare the object of class System.Drawing.Image

//for a product:

Image cheeseImage;

//We load into the project the image files according

//to such scheme:

//We create an object myAssembly of the Assembly class

//and appropriate to it

//the link to the executed assembly of our application:

static Assembly myAssembly = Assembly.GetExecutingAssembly();

//We create the myAssemblyName object of

//the System.Reflection.AssemblyName class and appropriate to

//it the assembly name, which consists of a project name,

//Version, Culture, PublicKeyToken:

static AssemblyName myAssemblyName = myAssembly.GetName();

//From the assembly name by means of the Name property

//we select a string project name:

static string myName_of_project = myAssemblyName.Name;

private void Form1_Paint(object sender, PaintEventArgs e)

{

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

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

//by means of ResourceStream:

cheeseImage =

new Bitmap(myAssembly.GetManifestResourceStream(

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

//We draw the image on the Form1:

e.Graphics.DrawImage(cheeseImage, 10, 20);

}

       :

Build, Build Selection; Debug, Start Without Debugging.

  Form1        cheese.jpg (. 4.1).

           (   )           (e.Graphics.DrawImage(myBitmap, 10, 20);).




4.2.   


      .     ,         .      ,    ().

     updatePositions,   .            (   x  y).  ,    ( )   .

 4.2.   .

//Current abscissa of an object:

int cx = 50;

//Current ordinate of an object:

int cy = 100;



private void updatePositions()

{

cx++; //or cx = cx + 1;

cy++; //or cy = cy + 1;

}

,     cx  cy,    .         ,    ,       .

  ,    updatePositions    ,   .    Toolbox     Timer ().   Properties (   Timer)   Enabled    False,   Interval   40 (,   25       ; 1000   1 ).

 ,      Timer () ,      ,      ,      .     Properties (   Timer)   Enabled     False.

 ,     ,     .      Form1_Paint    :

//We turn on the timer:

timer1.Enabled = true;

,     Form1_Paint    .

 4.3.    .

private void Form1_Paint(object sender, PaintEventArgs e)

{

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

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

//by means of ResourceStream:

cheeseImage =

new Bitmap(myAssembly.GetManifestResourceStream(

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

//We draw the image on the Form1:

e.Graphics.DrawImage(cheeseImage, cx, cy);

//We turn on the timer:

timer1.Enabled = true;

}

  ,    Form1_Paint,         cx  cy.

      Timer (    ).    timer1_Tick,      updatePositions    Invalidate ( Refresh)        .

 4.4.         .

private void timer1_Tick(object sender, EventArgs e)

{

//We call the method:

updatePositions();

//We redraw the screen:

Invalidate();

}

       :

Build, Build Selection; Debug, Start Without Debugging.

  Visual C#   Form1   ,         cheese.jpg          (   )   (. 4.3).

















. 4.3.      . . 4.4.  .

  ,        .

 ,    ,         .




4.3.      


            , ,   .        , :

 ,    ,        - ;        ,     ;

,    , ,  ,            ,     .

              .

     ,      :

  , ..         x, y;       cheese.jpg,       ;

   x, y         ;        ;

     x, y     (),                   ,        ;

  ,        ()   ;

    ,      , ,               ;             45 ;

      ,       Timer ();

      Interval  Timer;            Interval;

   (    )  ;         .

         (  ) ,  ,               ,        .

 ,       updatePositions   .

 4.5.    .

//Movement on an axis "x" to the right:

bool goingRight = true;

//Movement on an axis of "y" to the down:

bool goingDown = true;

private void updatePositions()

{

if (goingRight)

{

cx++;

}

else

{

cx;

}

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

{

goingRight = false;

}

if (cx <= 0)

{

goingRight = true;

}

if (goingDown)

{

cy++;

}

else

{

cy;

}

if ((cy + cheeseImage.Height) >= this.ClientSize.Height)

{

goingDown = false;

}

if (cy <= 0)

{

goingDown = true;

}

}

   ,    x, y    +1,        x, y (  ),     -1,        x, y (  ).

       (cheeseImage.Width  cheeseImage.Height)   this.ClientSize.Width  this.ClientSize.Height).            .

   (Build, Build Selection; Debug, Start Without Debugging)  ,    Form1       cheese.jpg      ,     (. 4.4).

        .




4.4.         


       .   , , 100 ,    25             4 .        ,      1    Interval   ,     xSpeed,       Interval   ,     :

if (goingRight)

{

cx += xSpeed;

}

else

{

cx -= xSpeed;

}

  xSpeed,       (  x)  .

     y      :

if (goingDown)

{

cy += ySpeed;

}

else

{

cy -= ySpeed;

}

          change   :

private void changeSpeed(int change)

{

xSpeed += change;

ySpeed += change;

}

     change      changeSpeed.    change      Interval    ,  ,  ,   .

         , ,        (   ),    .          ()    ,         , ,  Visual Basic.     Project   Add Reference,   Add Reference   (.NET)   Microsoft.VisualBasic    OK.    , , updatePositions  :

Microsoft.VisualBasic.Interaction.Beep();

  ,     .  ,       updatePositions   .

 4.6.    .

//The current increment of movement on an axis "x":

int xSpeed = 1;

//The current increment of movement on an axis "y":

int ySpeed = 1;

//The method for increase in traverse speed:

private void changeSpeed(int change)

{

xSpeed += change;

ySpeed += change;

}

//The method for change of coordinates of an object:

private void updatePositions()

{

if (goingRight)

{

cx += xSpeed;

}

else

{

cx -= xSpeed;

}

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

{

goingRight = false;



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

Microsoft.VisualBasic.Interaction.Beep();

}

if (cx <= 0)

{

goingRight = true;

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

Microsoft.VisualBasic.Interaction.Beep();

}

if (goingDown)

{

cy += ySpeed;

}

else

{

cy -= ySpeed;

}

if ((cy + cheeseImage.Height) >= this.ClientSize.Height)

{

goingDown = false;



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

Microsoft.VisualBasic.Interaction.Beep();

}

if (cy <= 0)

{

goingDown = true;



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

Microsoft.VisualBasic.Interaction.Beep();

}

}

      -    , ,    Button ().    Toolbox      Button    Properties   Text     ,      . ,       Button (   )         .

        .

       (changeSpeed(1);)   .

 4.7.     .

private void button1_Click(object sender, EventArgs e)

{

changeSpeed(1);

}

      .        (changeSpeed(-1);)   .

 4.8.     .

private void button2_Click(object sender, EventArgs e)

{

changeSpeed(-1);

}

   (Build, Build Selection; Debug, Start Without Debugging)  ,    Form1       cheese.jpg     (. 4.5  4.6),    ,              .

,            Beep.















. 4.5.  .  . 4.6.  .




4.5.      


,       cheese.jpg  ,    , ,      ,     .            (    ).

   (      )     bread.jpg   ,  :  Project  Add Existing Item,      Files of type  All Files,            Add (     ).   Solution Explorer    .

    bread.jpg          ,  :  Solution Explorer     ,    Properties (  )   Build Action (  )      Content ()  None   Embedded Resource ( ).

    breadImage ( Image)           bx  by    ,   ,          .        Form1_Paint   ,     .

 4.9.    .

//We declare the object of class System.Drawing.Image

//for the subject:

Image breadImage; // = null by default.

//Current abscissa of a subject:

int bx = 0;

//Current ordinate of a subject:

int by = 0;

private void Form1_Paint(object sender, PaintEventArgs e)

{

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

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

//by means of the ResourceStream:

cheeseImage =

new Bitmap(myAssembly.GetManifestResourceStream(

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

breadImage =

new Bitmap(myAssembly.GetManifestResourceStream(

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

//We draw the images on the Form1:

e.Graphics.DrawImage(cheeseImage, cx, cy);

e.Graphics.DrawImage(breadImage, bx, by);

//We turn on the timer:

timer1.Enabled = true;

}

   (Build, Build Selection; Debug, Start Without Debugging)  ,    Form1     cheese.jpg    bread.jpg (    ), . 4.7.

















. 4.7.     . . 4.8.   .

   ,   ,       (  ).




4.6.        


         ,       ,     ,       ,        ,     .            ,           ,    .

       ,       Form1_Paint   ,      (  ).

 4.10.    .

//Buffer in the view of object of class Bitmap:

Bitmap backBuffer = null;

private void Form1_Paint(object sender, PaintEventArgs e)

{

//We load into object of class System.Drawing.Image

//the image file 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"));

//If it is necessary, we create the new buffer:

if (backBuffer == null)

{

backBuffer = new Bitmap(this.ClientSize.Width,

this.ClientSize.Height);

}

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

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

{

//We clear the form:

g.Clear(Color.White);

//We draw the image in the backBuffer buffer:

g.DrawImage(breadImage, bx, by);

g.DrawImage(cheeseImage, cx, cy);

}

//We draw the image on the Form1:

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



//We turn on the timer:

timer1.Enabled = true;

} //End of the method Form1_Paint.

      ,  ,   ,    .   ,     Form1_Paint   Windows      (background color),      (white),           .    ,    Windows   .          ,       OnPaintBackground,        ,     .

 4.11.  OnPaintBackground.



protected override void OnPaintBackground(

System.Windows.Forms.PaintEventArgs e)

{

//We prohibit to redraw a background.

}



  OnPaintBackground      Form1_Paint, ,    Form1.

    (Build, Build Selection; Debug, Start Without Debugging)        ,     .

        (. 4.8),         ,     ,               .         .




4.7.           


       ,      ,      .     (Up),  (Down),  (Left)   (Right)            Toolbox, , ,    .  ,      Button      Text    , ,    (. 4.9).   ,   Form1   Properties    Size, ,  384; 473.















. 4.9.      . . 4.10.   .

  ,     ,   ,   ,           .

        ,    Properties   Events      MouseMove.              .

 4.12.    .

private void button3_Click(object sender, EventArgs e)

{

//We move an object up:

by -= ySpeed;

}

private void button4_Click(object sender, EventArgs e)

{

//We move an object down:

by += ySpeed;

}

private void button5_Click(object sender, EventArgs e)

{

//We move an object to the left:

bx -= xSpeed;

}

private void button6_Click(object sender, EventArgs e)

{

//We move an object to the right:

bx += xSpeed;

}

private void Form1_MouseMove(object sender, MouseEventArgs e)

{

//We determine the coordinates of mouse pointer on form:

int mouseX = e.X;

int mouseY = e.Y;

//We set the coordinates of bread, equal to coordinates

//of mouse:

bx = mouseX;

by = mouseY;

}

 ,       (, int bx = 150; int by = 200;)

   (Build, Build Selection; Debug, Start Without Debugging)   (  )   ,      .

      , ,                (. 4.9).

 . 4.9 ,         Button (     (Focus) ),          ,    Enter,         .   ,      , ,   Button,   - , ,   Form1_Paint     (button2.Focus();).       , ,     Tab     ,        Enter.

  ,      ,   ,   ,            .

,       (   )    , ,  Button  .

,    MouseMove,      -   ,     Properties   Events   Form1 (. 4.11).








. 4.11.    .

,       Button (   )           .

        (. 4.10),    , ,     ,     ,               .          (  ).

 ,        :

   ;

 ;

      , , ;

   ;

       , ,      ;

     (      );

      ;

      .

        .




 5.   ,       





5.1.  ,   


        ,         ,      microsoft.com:

Rob Miles. Games Programming with Cheese: Part Two.

          .

              .

         ,    . ,    , ..     ,   .       ,         .

,      bread.jpg,   . 5.1.








. 5.1. ,   .

             ,              .      x, y       ,      (bx, by)     (bx + batWidth, by + batHeight)      .

   .NET Framework (  )   Rectangle (   System.Drawing),   - Rectangle Constructor   .    - Rectangle Constructor (      )   (Int32, Int32, Int32, Int32)  Rectangle   (  )    . 5.1.

 5.1.

- Rectangle Constructor (Int32, Int32, Int32, Int32)  Rectangle.

Visual Basic (Declaration)

Public Sub New ( _

x As Integer, _

y As Integer, _

width As Integer, _

height As Integer _

Visual Basic (Usage))

Dim x As Integer

Dim y As Integer

Dim width As Integer

Dim height As Integer

Dim instance As New Rectangle(x, y, width, height)

C#

public Rectangle (

int x,

int y,

int width,

int height

)

C++

public:

Rectangle (

int x,

int y,

int width,

int height

)

J#

public Rectangle (

int x,

int y,

int width,

int height

)

JScript

public function Rectangle (

x : int,

y : int,

width : int,

height : int

)

   - Rectangle Constructor   :

x   x    ;

y   y    ;

width   (  x) ;

height   (  y) .

       ,   ,   , , :

//The rectangle, described around the first object:

Rectangle cheeseRectangle;

//The rectangle, described around the second object:

Rectangle breadRectangle;

   -   (    new)    -, , :

cheeseRectangle = new Rectangle(cx, cy,

cheeseImage.Width, cheeseImage.Height);

breadRectangle = new Rectangle(bx, by,

breadImage.Width, breadImage.Height);




5.2.   ,    


   Rectangle (   System.Drawing)  ,       .   ,        ,   ,           ,     ,    .

       - .  -   Rectangle,        Intersect  IntersectsWith (. 5.2)     .






. 5.2.    Intersect  IntersectsWith.

     IntersectsWith (      )   (Rectangle rect)  Rectangle   (  )    . 5.2.

 5.2.

  Rectangle.IntersectsWith  Rectangle.

Visual Basic (Declaration)

Public Function IntersectsWith ( _

rect As Rectangle _

) As Boolean

Visual Basic (Usage)

Dim instance As Rectangle

Dim rect As Rectangle

Dim returnValue As Boolean

returnValue = instance.IntersectsWith(rect)

C#

public bool IntersectsWith (

Rectangle rect

)

C++

public:

bool IntersectsWith (

Rectangle rect

)

J#

public boolean IntersectsWith (

Rectangle rect

)

JScript

public function IntersectsWith (

rect : Rectangle

) : Boolean

  IntersectsWith         ,     (Rectangle rect).

  ,           ,      False.

   ,           ,   IntersectsWith    True,         -    (    ), ,   :

//We check the collision of objects:

if (cheeseRectangle.IntersectsWith(breadRectangle))

{

//We change the direction of the movement to opposite:

goingDown = !goingDown;

//At the time of collision, we give a sound signal Beep:

Microsoft.VisualBasic.Interaction.Beep();

}




5.3.    


  ,        (    )   ,        Form1_Paint   ,      (  ).

 5.1.    .

//The rectangle, described around the first object:

Rectangle cheeseRectangle;

//The rectangle, described around the second object:

Rectangle breadRectangle;

private void Form1_Paint(object sender, PaintEventArgs 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);

//If it is necessary, we create the new buffer:

if (backBuffer == null)

{

backBuffer = new Bitmap(this.ClientSize.Width,

this.ClientSize.Height);

}

//We createobject 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 backBuffer:

g.DrawImage(cheeseImage, cx, cy);

g.DrawImage(breadImage, bx, by);

}

//We draw the image on Form1:

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

//We turn on the timer:

timer1.Enabled = true;

} //End of the method Form1_Paint.

     updatePositions      ,      .

 5.2.        .

private void updatePositions()

{

if (goingRight)

{

cx += xSpeed;

}

else

{

cx -= xSpeed;

}

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

{

goingRight = false;

//At the time of collision,

//the sound signal Beep is given:

Microsoft.VisualBasic.Interaction.Beep();

}

if (cx <= 0)

{

goingRight = true;

//At the time of collision,

//the sound signal Beep 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 sound signal Beep is given:

Microsoft.VisualBasic.Interaction.Beep();

}

if (cy <= 0)

{

goingDown = true;

//At the time of collision,

//the sound signal Beep is given:

Microsoft.VisualBasic.Interaction.Beep();

}

//We set to rectangles of coordinates of objects:

cheeseRectangle.X = cx;

cheeseRectangle.Y = cy;

breadRectangle.X = bx;

breadRectangle.Y = by;

//We check the collision of objects:

if (cheeseRectangle.IntersectsWith(breadRectangle))

{

//We change the direction of the movement to opposite:

goingDown = !goingDown;

//At the time of collision,

//the sound signal Beep is given:

Microsoft.VisualBasic.Interaction.Beep();

}

} //End of the updatePositions method.

   (Build, Build Selection; Debug, Start Without Debugging)            ,  ,    ,   (. 5.3). , ,         45 ,        (   )   45 .




5.4.      


        ,             .        (  ),       .    ,     .

    ,   ,      ,   . 5.4.   1  3         45            .   2         90      .

     ,   ,      ,   . 5.5.















. 5.3.    . . 5.4.   .




. 5.5.   .

  1  4          45            .   2  3         45 ,      ,    ,   .

     ,   . 5.5,        updatePositions      ,       .

 5.3.        .

private void updatePositions()

{

if (goingRight)

{

cx += xSpeed;

}

else

{

cx -= xSpeed;

}

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

{

goingRight = false;

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

Microsoft.VisualBasic.Interaction.Beep();

}

if (cx <= 0)

{

goingRight = true;

//At the time of collision, the signal Beep 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 signal Beep is given:

Microsoft.VisualBasic.Interaction.Beep();

}

if (cy <= 0)

{

goingDown = true;

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

Microsoft.VisualBasic.Interaction.Beep();

}

//We check the collision of objects:

if (goingDown)

{

//If cheese moves down and there is the collision:

if (cheeseRectangle.IntersectsWith(breadRectangle))

{

//At the time of collision, the signal Beep

//is given:

Microsoft.VisualBasic.Interaction.Beep();

//We have the collision:

bool rightIn = breadRectangle.Contains(

cheeseRectangle.Right,

cheeseRectangle.Bottom);

bool leftIn = breadRectangle.Contains(

cheeseRectangle.Left,

cheeseRectangle.Bottom);

//types of collisions:

if (rightIn & leftIn)

{

//bounce up:

goingDown = false;

}

else

{

//bounce up:

goingDown = false;

//the bounces in horizontal direction:

if (rightIn)

{

goingRight = false;

}

if (leftIn)

{

goingRight = true;

}

}

}

}

} //End of the method updatePositions.

   (Build, Build Selection; Debug, Start Without Debugging)    Button         ,  ,         (  ),     ,       .    ,      .




5.5.   


             , ,  (tomatoes)    tomato.gif, . 5.6.








. 5.6.

.

    i-     tomatoes[i]          (. 5.7),          (. 5.8).

         IntersectWith.

      visible,     false ( : tomatoes[i].visible = false;).

    Button     ,       ,         ,    .

    (      )    tomato.gif   ,  :  Project  Add Existing Item,      Files of type  All Files,            Add (     ).   Solution Explorer    .

    tomato.gif          ,  :  Solution Explorer     ,    Properties (  )   Build Action (  )       Embedded Resource ( ).















. 5.7.   . . 5.8.       .

           ,   Form1     .

 5.4.      (tomatoes).

//We declare the object of the System.Drawing.Image class

//for product:

Image tomatoImage;

//Position and state of tomato

struct tomato

{

public Rectangle rectangle;

public bool visible;

}

// Spacing between tomatoes. Set once for the game

int tomatoSpacing = 4;

// Height, at which the tomatoes are drawn. Will change

// as the game progresses. Starts at the top.

int tomatoDrawHeight = 4;

// The number of tomatoes on the screen. Set at the start

// of the game by initialiseTomatoes.

int noOfTomatoes;

// Positions of the tomato targets.

tomato[] tomatoes;

// called once to set up all the tomatoes.

void initialiseTomatoes()

{

noOfTomatoes = (this.ClientSize.Width  tomatoSpacing) /

(tomatoImage.Width + tomatoSpacing);

// create an array to hold the tomato positions

tomatoes = new tomato[noOfTomatoes];

// x coordinate of each potato

int tomatoX = tomatoSpacing / 2;

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

{

tomatoes[i].rectangle =

new Rectangle(tomatoX, tomatoDrawHeight,

tomatoImage.Width, tomatoImage.Height);

tomatoX = tomatoX + tomatoImage.Width + tomatoSpacing;

}

}

// Called to place a row of tomatoes.

private void placeTomatoes()

{

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

{

tomatoes[i].rectangle.Y = tomatoDrawHeight;

tomatoes[i].visible = true;

}

}

      Form1_Paint   ,     .

 5.5.    .

private void Form1_Paint(object sender, PaintEventArgs e)

{

//If it is necessary, we create the new buffer:

if (backBuffer == null)

{

backBuffer = new Bitmap(this.ClientSize.Width,

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:

timer1.Enabled = true;

}

 ,     updatePositions   ,      ,      .

 5.7.        .

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 the 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;

//We check the collision of objects

//taking into account the tomatoes:

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;

// only destroy one at a time

break;

}

}

}

} //End of the method updatePositions.

   (Build, Build Selection; Debug, Start Without Debugging)  i-          (. 5.7),         (. 5.8).

    Button     ,       ,         ,    .

         .




5.6.     


      ,         ,    ,    .             ( )   ,       , ,    -  .

   (score)        (scorer)       (,  DrawString)  :

g.DrawString(messageString, messageFont, messageBrush,

messageRectangle);

,     DrawString        messageFont,  messageBrush       messageRectangle,         .  . 5.9   20   2  ,   5.10  50   5  .

          , , 10   :

scoreValue = scoreValue + 10;

      ,  .















. 5.9.  20   2  . . 5.10.  50 .

             .

      ,       ,   4  , , , 20:

int tomatoDrawHeight = 20;

    Form1      .

 5.8.  .

// Font for score messages.

Font messageFont = null;

// Rectangle for score display.

Rectangle messageRectangle;

// Height of the score panel.

int scoreHeight = 20;

// Brush used to draw the messages.

SolidBrush messageBrush;

// The string, which is drawn as the user message.

string messageString = "Score : 0";

// Score in a game.

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




  .


   .

   ,     (https://www.litres.ru/pages/biblio_book/?art=68905044)  .

      Visa, MasterCard, Maestro,    ,   ,     ,  PayPal, WebMoney, ., QIWI ,       .


