Advertisement
Guest User

Untitled

a guest
May 29th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.50 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. private: bool isWait;
  30.  
  31. public:
  32. MainForm(void)
  33. {
  34. InitializeComponent();
  35. this->cageBox->Image = cage->getImage();
  36. this->cageBox->Size = this->cageBox->Image->Size;
  37. this->cageBox->BackColor = Color::Transparent;
  38. this->parrotBox->BackColor = Color::Transparent;
  39. this->label1->BackColor = Color::Transparent;
  40. this->label1->ForeColor = Color::Red;
  41. this->cageBtn->Click += gcnew EventHandler(this, &MainForm::moveClick);
  42. this->healthBar->Maximum = 1000;
  43. this->healthBar->Minimum = 0;
  44. this->healthBar->Value = 500;
  45. this->timer1->Tick += gcnew EventHandler(this, &MainForm::barInc);
  46. this->foodBtn->Click += gcnew EventHandler(this, &MainForm::onBtnFeed);
  47. this->timer2->Tick += gcnew EventHandler(this, &MainForm::flyTimer);
  48. this->BackgroundImage = Image::FromFile("images/back.jpg");
  49. this->BackgroundImageLayout = ImageLayout::Stretch;
  50. }
  51.  
  52. protected:
  53. ~MainForm()
  54. {
  55. if (components)
  56. {
  57. delete components;
  58. }
  59. }
  60. private: System::ComponentModel::IContainer^ components;
  61. protected:
  62.  
  63. private:
  64. void barInc(System::Object^ sender, System::EventArgs^ e)
  65. {
  66. if (parrot->getEat())
  67. {
  68. healthBar->Value = healthBar->Maximum;
  69. parrot->setEat(false);
  70. timer1->Interval = 5000;
  71. }
  72. else if ((healthBar->Value - 5 <= 0))
  73. {
  74. healthBar->Value = 0;
  75. timer1->Enabled = false;
  76. timer2->Enabled = false;
  77. MessageBox::Show("Your parrot died.", "The End :(");
  78. this->~MainForm();
  79. this->Close();
  80. }
  81. else
  82. {
  83. timer1->Interval = 100;
  84. healthBar->Increment(-1);
  85. if (cage->isOpen() && !parrot->getFly())
  86. {
  87. timer2->Enabled = true;
  88. timer2->Interval = 5000;
  89. isWait = true;
  90. }
  91. }
  92. label1->Text = (healthBar->Value / 10).ToString() + "%";
  93. }
  94. private:
  95. void onBtnFeed(System::Object^ sender, System::EventArgs^ e)
  96. {
  97. if (!parrot->getFly())
  98. {
  99. parrot->setEat(true);
  100. }
  101. }
  102. private:
  103. void moveClick(Object^ sender, EventArgs^ e)
  104. {
  105. if (cage->isOpen())
  106. {
  107. cage->setType(1);
  108. parrot->setFly(false);
  109. parrot->setDraw(false);
  110. this->cageBtn->Text = "Open";
  111. cageBtn->Enabled = true;
  112. foodBtn->Enabled = true;
  113. timer2->Enabled = false;
  114. setImages();
  115. }
  116. else
  117. {
  118. cage->setType(-2);
  119. parrot->setFly(true);
  120. parrot->setDraw(true);
  121. this->cageBtn->Text = "Close";
  122. cageBtn->Enabled = false;
  123. foodBtn->Enabled = false;
  124. timer2->Interval = 50;
  125. timer2->Enabled = true;
  126. setImages();
  127. }
  128.  
  129. }
  130. private:
  131. void flyTimer(System::Object^ sender, System::EventArgs^ e)
  132. {
  133. Point^ point = parrotBox->Location;
  134. if (isWait)
  135. {
  136. cage->setType(-2);
  137. parrot->setFly(true);
  138. parrot->setDraw(true);
  139. this->cageBtn->Text = "Close";
  140. cageBtn->Enabled = false;
  141. foodBtn->Enabled = false;
  142. setImages();
  143. isWait = false;
  144. timer2->Interval = 10;
  145. }
  146. else
  147. {
  148. if (parrot->getDirection())
  149. {
  150. if ((point->X > 10) && (point->Y > 50))
  151. {
  152. if (point->X == 450)
  153. {
  154. this->parrotBox->Image->RotateFlip(RotateFlipType::RotateNoneFlipX);
  155. }
  156. parrotBox->Location = Point(point->X - 2, point->Y - 2);
  157. }
  158. else if (point->X < 600)
  159. {
  160. if (point->X <= 150)
  161. {
  162. Image^ img = parrotBox->Image;
  163. img->RotateFlip(RotateFlipType::RotateNoneFlipX);
  164. parrotBox->Image = img;
  165. }
  166. parrotBox->Location = Point(point->X + 4, point->Y);
  167. }
  168. if (point->X == 598)
  169. {
  170. Image^ img = parrotBox->Image;
  171. img->RotateFlip(RotateFlipType::RotateNoneFlipX);
  172. parrotBox->Image = img;
  173. parrot->setDirection(false);
  174. }
  175. }
  176. else
  177. {
  178. if (point->X > 150 && point->Y == 50)
  179. {
  180. parrotBox->Location = Point(point->X - 4, point->Y);
  181. }
  182. else if ((point->X < 450) && (point->Y < 350))
  183. {
  184. if (point->Y == 50)
  185. {
  186. Image^ img = parrotBox->Image;
  187. img->RotateFlip(RotateFlipType::Rotate180FlipY);
  188. parrotBox->Image = img;
  189. }
  190. parrotBox->Location = Point(point->X + 2, point->Y + 2);
  191. }
  192. if (point->Y == 350)
  193. {
  194. parrot->setDirection(true);
  195. cage->setType(2);
  196. parrot->setFly(false);
  197. parrot->setDraw(false);
  198. cageBtn->Enabled = true;
  199. foodBtn->Enabled = true;
  200. timer2->Enabled = false;
  201. setImages();
  202. }
  203. }
  204. delete point;
  205. }
  206. }
  207.  
  208. private: void setImages()
  209. {
  210. this->cageBox->Image = cage->getImage();
  211. this->cageBox->Size = this->cageBox->Image->Size;
  212.  
  213. if (parrot->getDraw())
  214. {
  215. this->parrotBox->Image = parrot->getImage();
  216. this->parrotBox->Size = this->parrotBox->Image->Size;
  217. this->parrotBox->BackColor = Color::Transparent;
  218. parrotBox->Visible = true;
  219. }
  220. else
  221. {
  222. delete this->parrotBox->Image;
  223. parrotBox->Visible = false;
  224. }
  225.  
  226. }
  227.  
  228. private:
  229. #pragma region Windows Form Designer generated code
  230. void InitializeComponent(void)
  231. {
  232. this->components = (gcnew System::ComponentModel::Container());
  233. System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(MainForm::typeid));
  234. this->cageBtn = (gcnew System::Windows::Forms::Button());
  235. this->foodBtn = (gcnew System::Windows::Forms::Button());
  236. this->healthBar = (gcnew System::Windows::Forms::ProgressBar());
  237. this->cageBox = (gcnew System::Windows::Forms::PictureBox());
  238. this->parrotBox = (gcnew System::Windows::Forms::PictureBox());
  239. this->label1 = (gcnew System::Windows::Forms::Label());
  240. this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
  241. this->timer2 = (gcnew System::Windows::Forms::Timer(this->components));
  242. (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->cageBox))->BeginInit();
  243. (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->parrotBox))->BeginInit();
  244. this->SuspendLayout();
  245. //
  246. // cageBtn
  247. //
  248. this->cageBtn->Location = System::Drawing::Point(5, 335);
  249. this->cageBtn->Name = L"cageBtn";
  250. this->cageBtn->Size = System::Drawing::Size(100, 40);
  251. this->cageBtn->TabIndex = 0;
  252. this->cageBtn->Text = L"Open";
  253. this->cageBtn->UseVisualStyleBackColor = true;
  254. //
  255. // foodBtn
  256. //
  257. this->foodBtn->Location = System::Drawing::Point(5, 398);
  258. this->foodBtn->Name = L"foodBtn";
  259. this->foodBtn->Size = System::Drawing::Size(100, 40);
  260. this->foodBtn->TabIndex = 1;
  261. this->foodBtn->Text = L"Feed";
  262. this->foodBtn->UseVisualStyleBackColor = true;
  263. //
  264. // healthBar
  265. //
  266. this->healthBar->BackColor = System::Drawing::SystemColors::Control;
  267. this->healthBar->Location = System::Drawing::Point(12, 13);
  268. this->healthBar->Name = L"healthBar";
  269. this->healthBar->Size = System::Drawing::Size(681, 23);
  270. this->healthBar->TabIndex = 2;
  271. //
  272. // cageBox
  273. //
  274. this->cageBox->Location = System::Drawing::Point(601, 237);
  275. this->cageBox->Name = L"cageBox";
  276. this->cageBox->Size = System::Drawing::Size(147, 221);
  277. this->cageBox->TabIndex = 3;
  278. this->cageBox->TabStop = false;
  279. //
  280. // parrotBox
  281. //
  282. this->parrotBox->Location = System::Drawing::Point(450, 350);
  283. this->parrotBox->Name = L"parrotBox";
  284. this->parrotBox->Size = System::Drawing::Size(138, 137);
  285. this->parrotBox->TabIndex = 4;
  286. this->parrotBox->TabStop = false;
  287. //
  288. // label1
  289. //
  290. this->label1->AutoSize = true;
  291. this->label1->Location = System::Drawing::Point(712, 23);
  292. this->label1->Name = L"label1";
  293. this->label1->Size = System::Drawing::Size(27, 13);
  294. this->label1->TabIndex = 5;
  295. this->label1->Text = L"50%";
  296. //
  297. // timer1
  298. //
  299. this->timer1->Enabled = true;
  300. //
  301. // timer2
  302. //
  303. this->timer2->Interval = 10;
  304. //
  305. // MainForm
  306. //
  307. this->ClientSize = System::Drawing::Size(760, 450);
  308. this->Controls->Add(this->label1);
  309. this->Controls->Add(this->parrotBox);
  310. this->Controls->Add(this->cageBox);
  311. this->Controls->Add(this->healthBar);
  312. this->Controls->Add(this->foodBtn);
  313. this->Controls->Add(this->cageBtn);
  314. this->Icon = (cli::safe_cast<System::Drawing::Icon^>(resources->GetObject(L"$this.Icon")));
  315. this->MaximizeBox = false;
  316. this->MinimizeBox = false;
  317. this->Name = L"MainForm";
  318. this->Text = L"ParrotCage";
  319. (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->cageBox))->EndInit();
  320. (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->parrotBox))->EndInit();
  321. this->ResumeLayout(false);
  322. this->PerformLayout();
  323. this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
  324.  
  325. }
  326. #pragma endregion
  327. };
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement