Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.29 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include "Unit15.h"
  6. #include "Unit4.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma resource "*.dfm"
  10. TForm4 *Form4;
  11. int x = 0, y = 0, a[10][10], z1 = 4, z2 = 4, b = 0, c = 0,h=2,e2=0,e3=0;
  12. bool moveup = false, movedown = false, moveright = false, moveleft = false,
  13.     drawred = false, drawgreen = false,drawyellow=false,draw=false;
  14. //---------------------------------------------------------------------------
  15. __fastcall TForm4::TForm4(TComponent* Owner)
  16.     : TForm(Owner)
  17. {
  18. }
  19. //---------------------------------------------------------------------------
  20. void __fastcall TForm4::FormActivate(TObject *Sender)
  21. {
  22.     Form4->SetFocusedControl(Form4);  //Ставим фокус на форму
  23. }
  24.  
  25. //---------------------------------------------------------------------------
  26. void __fastcall TForm4::Timer1Timer(TObject *Sender)
  27. {
  28.     if (moveup == true) {                           //движение вверх
  29.        if   (a[z1][z2]>1){a[z1][z2]=a[z1][z2]-1;}
  30.         z1 = z1 - 1;
  31.  
  32.     }
  33.     if (movedown == true) {                            //движение вниз
  34.         if  (a[z1][z2]>1){a[z1][z2]=a[z1][z2]-1;}
  35.         z1 = z1 + 1;
  36.  
  37.  
  38.     }
  39.     if (moveleft == true) {                            //движение влево
  40.     if  (a[z1][z2]>1){a[z1][z2]=a[z1][z2]-1;}
  41.         z2 = z2 - 1;
  42.  
  43.     }
  44.     if (moveright == true) {                            //движение вправо
  45.     if  (a[z1][z2]>1){a[z1][z2]=a[z1][z2]-1;}
  46.         z2 = z2 + 1;
  47.  
  48.     }
  49.     if (a[z1][z2] == 50) {                             //обрабатываем событие поглощения еды
  50.     for (e2=rand()%9, e3=rand()%9; a[e2][e3] > 0;) {           //проверяем не находится ли еда на теле
  51.       e2=rand()%9;
  52.       e3=rand()%9;
  53.     }
  54.     a[e2][e3]=50;                       //присваиваем рандомной клетке значение еды
  55.         h=h+1;
  56.         if (moveright == true) {        //создаем хвост
  57.           a[z1][z2-1]=h-1;
  58.         }
  59.         if (moveleft == true) {
  60.           a[z1][z2+1]=h-1;
  61.         }
  62.         if (moveup == true) {
  63.           a[z1+1][z2]=h-1;
  64.         }
  65.         if (movedown == true) {
  66.           a[z1-1][z2]=h-1;
  67.         }
  68.     }
  69.     if (z1==-1) {                      //создаем границы
  70.          z1=9;
  71.     }
  72.     if (z1==10) {
  73.           z1=0;
  74.     }
  75.     if (z2==-1) {
  76.          z2=9;
  77.     }
  78.     if (z2==10) {
  79.           z2=0;
  80.     }
  81.     for (x=0; x < 10; x++) {                   //удаляем последний тайл хвоста
  82.        for (y=0; y < 10; y++) {
  83.                  if (a[x][y]>0 and a[x][y]!=50) {
  84.                       a[x][y]=a[x][y]-1;
  85.                  }
  86.        }
  87.     }
  88.     if (h>2 and a[z1][z2]>0 and a[z1][z2]<50) {          //поражение
  89.        Timer1->Enabled=False;
  90.       ShowMessage("Вы съели Вас");
  91.  
  92.  
  93.     }   else
  94.    {    a[z1][z2] = h; }                  //ставим голову
  95.  
  96.             for (x = 0; x < 10; x++) {
  97.              for (y = 0; y < 10; y++) {
  98.             StringGrid1->Cells[x][y] = a[y][x];    //переносим цифру на сетку
  99.        
  100.              
  101.         }
  102.     }
  103.     Label1->Caption=IntToStr(h-2);  //очки
  104.     if (h==49) {
  105.         ShowMessage("Вы победили, Вы крутой");
  106.         Timer1->Enabled=false;
  107.     }
  108. }
  109. //---------------------------------------------------------------------------
  110. void __fastcall TForm4::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
  111.  
  112. {
  113.     if (Key == 39 and moveleft == false) {
  114.         moveright = true;
  115.         moveleft = false;
  116.         movedown = false;
  117.         moveup = false;
  118.     }
  119.     if (Key == 38 and movedown == false) {
  120.         moveup = true;
  121.         moveleft = false;
  122.         movedown = false;
  123.         moveright = false;
  124.     }
  125.     if (Key == 37 and moveright == false) {
  126.         moveleft = true;
  127.         moveright = false;
  128.         movedown = false;
  129.         moveup = false;
  130.     }
  131.     if (Key == 40 and moveup == false) {
  132.         movedown = true;
  133.         moveleft = false;
  134.         moveright = false;
  135.         moveup = false;
  136.     }
  137. }
  138. //---------------------------------------------------------------------------
  139.  
  140. //---------------------------------------------------------------------------
  141.  
  142.  
  143. //---------------------------------------------------------------------------
  144.  
  145. void __fastcall TForm4::FormCreate(TObject *Sender)
  146. {
  147.     for (x = 0; x < 15; x++) {
  148.         for (y = 0; y < 10; y++) {
  149.             a[x][y] = 0;
  150.  
  151.         }
  152.     }
  153.     a[rand() % 9][rand() % 9] = 50;
  154. }
  155. //---------------------------------------------------------------------------
  156. void __fastcall TForm4::StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
  157.           TGridDrawState State)
  158. { if (StringGrid1->Cells[ACol][ARow]==0) {
  159.            StringGrid1->Canvas->Brush->Color=clGreen;
  160.   }
  161.   if (StringGrid1->Cells[ACol][ARow]==6) {
  162.            StringGrid1->Canvas->Brush->Color=clRed;
  163.   }
  164.   if (StringGrid1->Cells[ACol][ARow]==7) {
  165.            StringGrid1->Canvas->Brush->Color=clRed;
  166.   }
  167.   if (StringGrid1->Cells[ACol][ARow]==8) {
  168.            StringGrid1->Canvas->Brush->Color=clRed;
  169.   }
  170.   if (StringGrid1->Cells[ACol][ARow]==9) {
  171.            StringGrid1->Canvas->Brush->Color=clRed;
  172.   }
  173.   if (StringGrid1->Cells[ACol][ARow]==5) {
  174.            StringGrid1->Canvas->Brush->Color=clRed;
  175.   }
  176.   if (StringGrid1->Cells[ACol][ARow]>0 and StringGrid1->Cells[ACol][ARow]<50 ) {
  177.            StringGrid1->Canvas->Brush->Color=clRed;
  178.   }
  179.   if (StringGrid1->Cells[ACol][ARow]==50) {
  180.            StringGrid1->Canvas->Brush->Color=clYellow;
  181.   }
  182.   StringGrid1->Canvas->FillRect(Rect);  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement