Advertisement
Guest User

Untitled

a guest
May 28th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.94 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "TImage.h"
  4. #include "Cage.h"
  5. #include "Parrot.h"
  6.  
  7. namespace ParrotInCage {
  8.  
  9. using namespace System;
  10. using namespace System::ComponentModel;
  11. using namespace System::Collections;
  12. using namespace System::Windows::Forms;
  13. using namespace System::Data;
  14. using namespace System::Drawing;
  15.  
  16. public ref class MainForm : public Form
  17. {
  18. private:
  19. private: System::Windows::Forms::Button^ cageBtn;
  20. private: System::Windows::Forms::Button^ foodBtn;
  21. private: System::Windows::Forms::PictureBox^ cageBox;
  22. private: System::Windows::Forms::PictureBox^ parrotBox;
  23. Parrot^ parrot = gcnew Parrot();
  24. private: System::Windows::Forms::ProgressBar^ healthBar;
  25. private: System::Windows::Forms::Label^ label1;
  26. private: System::Windows::Forms::Timer^ timer1;
  27. private: System::Windows::Forms::Timer^ timer2;
  28. private: Cage^ cage = gcnew Cage();
  29.  
  30. public:
  31. MainForm(void)
  32. {
  33.  
  34. InitializeComponent();
  35. this->cageBox->Image = cage->getImage();
  36. this->cageBox->Size = this->cageBox->Image->Size;
  37. this->parrotBox->Image = parrot->getImage();
  38. this->parrotBox->Size = this->parrotBox->Image->Size;
  39. this->cageBtn->Click += gcnew EventHandler(this, &MainForm::moveClick);
  40. this->healthBar->Maximum = 1000;
  41. this->healthBar->Minimum = 0;
  42. this->healthBar->Value = 500;
  43. this->timer1->Tick += gcnew EventHandler(this, &MainForm::barInc);
  44. this->foodBtn->Click += gcnew EventHandler(this, &MainForm::onBtnFeed);
  45. this->timer2->Tick += gcnew EventHandler(this, &MainForm::flyTimer);
  46. }
  47.  
  48. protected:
  49. ~MainForm()
  50. {
  51. if (components)
  52. {
  53. delete components;
  54. }
  55. }
  56. private: System::ComponentModel::IContainer^ components;
  57. protected:
  58.  
  59. private:
  60. void barInc(System::Object^ sender, System::EventArgs^ e)
  61. {
  62. if (parrot->getEat())
  63. {
  64. healthBar->Value = healthBar->Maximum;
  65. parrot->setEat(false);
  66. timer1->Interval = 5000;
  67. }
  68. else if ( (healthBar->Value - 5 <= 0) )
  69. {
  70. healthBar->Value = 0;
  71. timer1->Enabled = false;
  72. }
  73. else
  74. {
  75. timer1->Interval = 100;
  76. healthBar->Increment(-1);
  77. }
  78. label1->Text = (healthBar->Value / 10).ToString() + "%";
  79. }
  80. private:
  81. void onBtnFeed(System::Object^ sender, System::EventArgs^ e)
  82. {
  83. if (!parrot->getFly() )
  84. {
  85. parrot->setEat(true);
  86. }
  87. }
  88. private:
  89. void moveClick(Object^ sender, EventArgs^ e)
  90. {
  91. if (this->cageBtn->Text == "Close")
  92. {
  93. this->cageBtn->Text = "Open";
  94. cage->setBool(false);
  95. cageBtn->Enabled = true;
  96. toFreeParrot(true);
  97. }
  98. else
  99. {
  100. this->cageBtn->Text = "Close";
  101. cage->setBool(true);
  102. cageBtn->Enabled = false;
  103. toFreeParrot(false);
  104. }
  105.  
  106. }
  107. private:
  108. void flyTimer(System::Object^ sender, System::EventArgs^ e)
  109. {
  110. Point^ point = parrotBox->Location;
  111. if (parrot->getDirection())
  112. {
  113. if ((point->X > 10) && (point->Y > 50))
  114. {
  115. if (point->X == 450)
  116. {
  117. this->parrotBox->Image->RotateFlip(RotateFlipType::RotateNoneFlipX);
  118. }
  119. parrotBox->Location = Point(point->X - 1, point->Y - 1);
  120. }
  121. else if (point->X < 600)
  122. {
  123. if ( point->X <= 150)
  124. {
  125. Image^ img = parrotBox->Image;
  126. img->RotateFlip(RotateFlipType::Rotate180FlipXY);
  127. parrotBox->Image = img;
  128. }
  129. parrotBox->Location = Point(point->X + 2, point->Y);
  130. }
  131. if (point->X == 600)
  132. {
  133. Image^ img = parrotBox->Image;
  134. img->RotateFlip(RotateFlipType::RotateNoneFlipX);
  135. parrotBox->Image = img;
  136. parrot->setDirection(false);
  137. }
  138. }
  139. else
  140. {
  141. if (point->X > 150 && point->Y == 50)
  142. {
  143. parrotBox->Location = Point(point->X - 2, point->Y);
  144. }
  145. else if ((point->X < 450) && (point->Y < 350))
  146. {
  147. if (point->Y == 50)
  148. {
  149. Image^ img = parrotBox->Image;
  150. img->RotateFlip(RotateFlipType::Rotate180FlipY);
  151. parrotBox->Image = img;
  152. }
  153. parrotBox->Location = Point(point->X + 1, point->Y + 1);
  154. }
  155. if (point->Y == 350)
  156. {
  157. parrot->setDirection(true);
  158. cage->setBool(true);
  159. cageBtn->Enabled = true;
  160. toFreeParrot(true);
  161. }
  162.  
  163. }
  164. delete point;
  165.  
  166.  
  167. }
  168.  
  169. private: void toFreeParrot(bool isOpenAlready)
  170. {
  171. parrot->setFly(!isOpenAlready);
  172. timer2->Enabled = !isOpenAlready;
  173. foodBtn->Enabled = isOpenAlready;
  174. this->cageBox->Image = cage->getImage();
  175. this->cageBox->Size = this->cageBox->Image->Size;
  176. this->parrotBox->Image = parrot->getImage();
  177. this->parrotBox->Size = this->parrotBox->Image->Size;
  178. this->parrotBox->Image->RotateFlip(RotateFlipType::RotateNoneFlipX);
  179. }
  180.  
  181. private:
  182. #pragma region Windows Form Designer generated code
  183. void InitializeComponent(void)
  184. {
  185. this->components = (gcnew System::ComponentModel::Container());
  186. this->cageBtn = (gcnew System::Windows::Forms::Button());
  187. this->foodBtn = (gcnew System::Windows::Forms::Button());
  188. this->healthBar = (gcnew System::Windows::Forms::ProgressBar());
  189. this->cageBox = (gcnew System::Windows::Forms::PictureBox());
  190. this->parrotBox = (gcnew System::Windows::Forms::PictureBox());
  191. this->label1 = (gcnew System::Windows::Forms::Label());
  192. this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
  193. this->timer2 = (gcnew System::Windows::Forms::Timer(this->components));
  194. (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->cageBox))->BeginInit();
  195. (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->parrotBox))->BeginInit();
  196. this->SuspendLayout();
  197. //
  198. // cageBtn
  199. //
  200. this->cageBtn->Location = System::Drawing::Point(5, 335);
  201. this->cageBtn->Name = L"cageBtn";
  202. this->cageBtn->Size = System::Drawing::Size(100, 40);
  203. this->cageBtn->TabIndex = 0;
  204. this->cageBtn->Text = L"Open";
  205. this->cageBtn->UseVisualStyleBackColor = true;
  206. //
  207. // foodBtn
  208. //
  209. this->foodBtn->Location = System::Drawing::Point(5, 398);
  210. this->foodBtn->Name = L"foodBtn";
  211. this->foodBtn->Size = System::Drawing::Size(100, 40);
  212. this->foodBtn->TabIndex = 1;
  213. this->foodBtn->Text = L"Feed";
  214. this->foodBtn->UseVisualStyleBackColor = true;
  215. //
  216. // healthBar
  217. //
  218. this->healthBar->BackColor = System::Drawing::SystemColors::Control;
  219. this->healthBar->Location = System::Drawing::Point(12, 13);
  220. this->healthBar->Name = L"healthBar";
  221. this->healthBar->Size = System::Drawing::Size(681, 23);
  222. this->healthBar->TabIndex = 2;
  223. //
  224. // cageBox
  225. //
  226. this->cageBox->Location = System::Drawing::Point(600, 218);
  227. this->cageBox->Name = L"cageBox";
  228. this->cageBox->Size = System::Drawing::Size(147, 221);
  229. this->cageBox->TabIndex = 3;
  230. this->cageBox->TabStop = false;
  231. //
  232. // parrotBox
  233. //
  234. this->parrotBox->Location = System::Drawing::Point(450, 350);
  235. this->parrotBox->Name = L"parrotBox";
  236. this->parrotBox->Size = System::Drawing::Size(138, 137);
  237. this->parrotBox->TabIndex = 4;
  238. this->parrotBox->TabStop = false;
  239. //
  240. // label1
  241. //
  242. this->label1->AutoSize = true;
  243. this->label1->Location = System::Drawing::Point(712, 23);
  244. this->label1->Name = L"label1";
  245. this->label1->Size = System::Drawing::Size(27, 13);
  246. this->label1->TabIndex = 5;
  247. this->label1->Text = L"50%";
  248. //
  249. // timer1
  250. //
  251. this->timer1->Enabled = true;
  252. //
  253. // timer2
  254. //
  255. this->timer2->Interval = 10;
  256. //
  257. // MainForm
  258. //
  259. this->ClientSize = System::Drawing::Size(760, 450);
  260. this->Controls->Add(this->label1);
  261. this->Controls->Add(this->parrotBox);
  262. this->Controls->Add(this->cageBox);
  263. this->Controls->Add(this->healthBar);
  264. this->Controls->Add(this->foodBtn);
  265. this->Controls->Add(this->cageBtn);
  266. this->Name = L"MainForm";
  267. (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->cageBox))->EndInit();
  268. (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->parrotBox))->EndInit();
  269. this->ResumeLayout(false);
  270. this->PerformLayout();
  271.  
  272. }
  273. #pragma endregion
  274. };
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement