fork download
  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #pragma hdrstop
  5.  
  6. #include "Unit1.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma resource "*.dfm"
  10.  
  11. #define canvas_w 600
  12. #define canvas_h 600
  13. #define grid_w 10
  14. #define grid_h 10
  15.  
  16. TForm1 *Form1;
  17.  
  18. using namespace std;
  19.  
  20. struct snake{
  21. int head;
  22. int length;
  23. int eat;
  24. };
  25. snake snake;
  26.  
  27. const width = canvas_w/grid_w;
  28. const height = canvas_h/grid_h;
  29.  
  30. TColor color[3] = {clWhite, clBlack, clRed};
  31. Graphics::TBitmap *bitmap[3] = {NULL};
  32. //---------------------------------------------------------------------------
  33. __fastcall TForm1::TForm1(TComponent* Owner)
  34. : TForm(Owner)
  35. {
  36. Panel1->ClientWidth = canvas_w;
  37. Panel1->ClientHeight = canvas_h;
  38. Image1->Align = alClient;
  39. Image1->Stretch = true;
  40. snake.head = 6;
  41. snake.length = 20;
  42. snake.eat = 0;
  43. }
  44. //---------------------------------------------------------------------------
  45. void __fastcall TForm1::FormCreate(TObject *Sender)
  46. {
  47. for(int i =0; i<3; i++)
  48. {
  49. bitmap[i] = new Graphics::TBitmap();
  50. bitmap[i]->Width = grid_w;
  51. bitmap[i]->Height = grid_h;
  52. bitmap[i]->Canvas->Brush->Color = color[i];
  53. bitmap[i]->Canvas->FillRect(Rect(0, 0, width, height));
  54. }
  55. initial();
  56.  
  57. }
  58. //---------------------------------------------------------------------------
  59. void __fastcall TForm1::Button1Click(TObject *Sender)
  60. {
  61. delete [] bitmap; //release
  62. Close();
  63. }
  64. //---------------------------------------------------------------------------
  65. void __fastcall TForm1::initial()
  66. {
  67.  
  68. Image1->Canvas->Brush->Color = color[0]; //clear
  69. Image1->Canvas->FillRect(Rect(0, 0, canvas_w, canvas_h));
  70.  
  71. Image1->Canvas->Draw(canvas_w/2, canvas_h/2, bitmap[2]); //food
  72. for(int i= 0; i< snake.length; i++) //snake
  73. Image1->Canvas->Draw(i*grid_w, canvas_h-2*grid_h, bitmap[1]);
  74.  
  75. Label2->Caption = 0; //score
  76.  
  77. }
  78.  
  79. void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
  80. TShiftState Shift)
  81. {
  82. if(Key == VK_UP)
  83. Label2->Caption = 100;
  84.  
  85.  
  86. }
  87.  
  88.  
Success #stdin #stdout 0.02s 2236KB
stdin
Standard input is empty
stdout
Standard output is empty