Advertisement
alfarovivecrj

Untitled

Jun 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. #pragma once
  2. #include "string.h"
  3. #include <math.h>
  4. namespace Project1 {
  5.  
  6. using namespace System;
  7. using namespace System::ComponentModel;
  8. using namespace System::Collections;
  9. using namespace System::Windows::Forms;
  10. using namespace System::Data;
  11. using namespace System::Drawing;
  12.  
  13. /// <summary>
  14. /// Summary for MyForm
  15. /// </summary>
  16. public ref class MyForm : public System::Windows::Forms::Form
  17. {
  18. public:
  19. MyForm(void)
  20. {
  21. InitializeComponent();
  22. //
  23. //TODO: Add the constructor code here
  24. //
  25. jugador1 = new char[50];
  26. jugador2 = new char[50];
  27. w1=w2=h1=h2=0;
  28. x1=x2=y1=y2=0;
  29. }
  30.  
  31. protected:
  32. /// <summary>
  33. /// Clean up any resources being used.
  34. /// </summary>
  35. ~MyForm()
  36. {
  37. if (components)
  38. {
  39. delete components;
  40. }
  41. }
  42. private: System::ComponentModel::IContainer^ components;
  43. protected:
  44.  
  45. private:
  46. /// <summary>
  47. /// Required designer variable.
  48. /// </summary>
  49.  
  50.  
  51. char *jugador1,*jugador2;
  52. int w1,w2,h1,h2;
  53. int x1,x2,y1,y2;
  54. Bitmap ^img1;
  55. Bitmap ^img2;
  56. int debajo1,arriba2,arriba1,debajo2;
  57. int derecha1, izquierda2;
  58. int izquierda1, derecha2;
  59.  
  60. private: System::Windows::Forms::Label^ label1;
  61. private: System::Windows::Forms::Label^ label2;
  62. private: System::Windows::Forms::Label^ label3;
  63. private: System::Windows::Forms::Label^ label4;
  64. private: System::Windows::Forms::Label^ label5;
  65. private: System::Windows::Forms::Label^ label6;
  66. private: System::Windows::Forms::Timer^ timer1;
  67.  
  68.  
  69.  
  70. #pragma region Windows Form Designer generated code
  71. /// <summary>
  72. /// Required method for Designer support - do not modify
  73. /// the contents of this method with the code editor.
  74. /// </summary>
  75. void InitializeComponent(void)
  76. {
  77. this->components = (gcnew System::ComponentModel::Container());
  78. this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
  79. this->label1 = (gcnew System::Windows::Forms::Label());
  80. this->label2 = (gcnew System::Windows::Forms::Label());
  81. this->label3 = (gcnew System::Windows::Forms::Label());
  82. this->label4 = (gcnew System::Windows::Forms::Label());
  83. this->label5 = (gcnew System::Windows::Forms::Label());
  84. this->label6 = (gcnew System::Windows::Forms::Label());
  85. this->SuspendLayout();
  86. //
  87. // timer1
  88. //
  89. this->timer1->Enabled = true;
  90. this->timer1->Tick += gcnew System::EventHandler(this, &MyForm::timer1_Tick);
  91. //
  92. // label1
  93. //
  94. this->label1->AutoSize = true;
  95. this->label1->Location = System::Drawing::Point(705, 27);
  96. this->label1->Name = L"label1";
  97. this->label1->Size = System::Drawing::Size(35, 13);
  98. this->label1->TabIndex = 0;
  99. this->label1->Text = L"label1";
  100. //
  101. // label2
  102. //
  103. this->label2->AutoSize = true;
  104. this->label2->Location = System::Drawing::Point(516, 26);
  105. this->label2->Name = L"label2";
  106. this->label2->Size = System::Drawing::Size(35, 13);
  107. this->label2->TabIndex = 1;
  108. this->label2->Text = L"label2";
  109. //
  110. // label3
  111. //
  112. this->label3->AutoSize = true;
  113. this->label3->Location = System::Drawing::Point(39, 274);
  114. this->label3->Name = L"label3";
  115. this->label3->Size = System::Drawing::Size(35, 13);
  116. this->label3->TabIndex = 2;
  117. this->label3->Text = L"label3";
  118. //
  119. // label4
  120. //
  121. this->label4->AutoSize = true;
  122. this->label4->Location = System::Drawing::Point(757, 274);
  123. this->label4->Name = L"label4";
  124. this->label4->Size = System::Drawing::Size(35, 13);
  125. this->label4->TabIndex = 3;
  126. this->label4->Text = L"label4";
  127. //
  128. // label5
  129. //
  130. this->label5->AutoSize = true;
  131. this->label5->Location = System::Drawing::Point(39, 301);
  132. this->label5->Name = L"label5";
  133. this->label5->Size = System::Drawing::Size(35, 13);
  134. this->label5->TabIndex = 4;
  135. this->label5->Text = L"label5";
  136. //
  137. // label6
  138. //
  139. this->label6->AutoSize = true;
  140. this->label6->Location = System::Drawing::Point(757, 301);
  141. this->label6->Name = L"label6";
  142. this->label6->Size = System::Drawing::Size(35, 13);
  143. this->label6->TabIndex = 5;
  144. this->label6->Text = L"label6";
  145. //
  146. // MyForm
  147. //
  148. this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  149. this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  150. this->BackColor = System::Drawing::Color::White;
  151. this->ClientSize = System::Drawing::Size(840, 586);
  152. this->Controls->Add(this->label6);
  153. this->Controls->Add(this->label5);
  154. this->Controls->Add(this->label4);
  155. this->Controls->Add(this->label3);
  156. this->Controls->Add(this->label2);
  157. this->Controls->Add(this->label1);
  158. this->Name = L"MyForm";
  159. this->Text = L"Use las teclas direccionales para picachu y las teclas \"A\"W\"S\"D\" para mover el ot"
  160. L"ro personajes";
  161. this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
  162. this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &MyForm::MyForm_Paint);
  163. this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &MyForm::MyForm_KeyDown);
  164. this->ResumeLayout(false);
  165. this->PerformLayout();
  166.  
  167. }
  168. #pragma endregion
  169. private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
  170.  
  171. //copia el nombre de las imagenes de los jugadores
  172. strcpy(jugador1,"picachu1.png");
  173. strcpy(jugador2,"pokemon.png");
  174. //Leemos el archivo imagen
  175. img1 = gcnew Bitmap(gcnew String(jugador1));
  176. img2 = gcnew Bitmap(gcnew String(jugador2));
  177.  
  178.  
  179. w1=img1->Width;
  180. h1=img1->Height;
  181. w2=img2->Width;
  182. h2=img2->Height;
  183.  
  184.  
  185. //posiciones iniciales
  186. x1 = 50;
  187. x2 = 550;
  188. y1 = 150;
  189. y2 = 370;
  190. //radios de las imagenes
  191. //r1=w1/2;
  192. //r2=w2/2;
  193. izquierda1=x1 ;
  194. derecha2 = x2 + w2;
  195. debajo1 = h1+y1;
  196. arriba2 = y2;
  197. derecha1 = x1 + w1;
  198. izquierda2 = x2;
  199. arriba1 = y1;
  200. debajo2 = h2+y2;
  201.  
  202. }
  203. private: System::Void MyForm_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
  204.  
  205. //lienzo
  206. Graphics ^gr = this->CreateGraphics();
  207. //dibuja jugadores
  208.  
  209. gr->DrawImage(img1,x1,y1);
  210. gr->DrawImage(img2,x2,y2);
  211.  
  212. }
  213. private: System::Void MyForm_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) {
  214.  
  215.  
  216. if (e->KeyCode == Keys::W)
  217. y1-=40;
  218. if (e->KeyCode == Keys::A)
  219. x1-=40;
  220. if (e->KeyCode == Keys::S)
  221. y1+=40;
  222. if (e->KeyCode == Keys::D)
  223. x1+=40;
  224.  
  225. if (e->KeyCode == Keys::Right)
  226. x2+=40;
  227. if (e->KeyCode == Keys::Down)
  228. y2+=40;
  229. if(e->KeyCode == Keys::Left)
  230. x2-=40;
  231. if (e->KeyCode == Keys::Up)
  232. y2-=40;
  233.  
  234. }
  235. private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
  236.  
  237. //lienzo
  238. Graphics ^gr = this->CreateGraphics();
  239. //buffer
  240. System::Drawing::BufferedGraphicsContext ^bc= BufferedGraphicsManager::Current;
  241. System::Drawing::BufferedGraphics ^bg= bc->Allocate(gr,this->ClientRectangle);
  242. bg->Graphics->Clear(Color::White);
  243.  
  244.  
  245.  
  246. //dibuja jugadores
  247. bg->Graphics->DrawImage(img1,x1,y1);
  248. bg->Graphics->DrawImage(img2,x2,y2);
  249.  
  250. //verifica colision
  251. //centro1= x1+r1;
  252. //centro2= x2+r2;
  253. izquierda1 = x1;
  254. derecha2 = x2 + w2;
  255. debajo1 = h1 + y1;
  256. arriba2 = y2;
  257. derecha1 = x1 + w1;
  258. izquierda2 = x2;
  259. arriba1 = y1;
  260. debajo2 = h2 + y2;
  261. if(abs(derecha1)<abs(izquierda2)||derecha2<izquierda1||debajo1<arriba2||arriba1>debajo2)
  262. label1->Text="NO Colision";
  263. else
  264. label1->Text="Colision";
  265. // label2->Text = Convert::ToString(centro2-centro1);
  266. label3->Text = Convert::ToString(x1);
  267. label4->Text = Convert::ToString(x2);
  268. label5->Text = Convert::ToString(y1);
  269. label6->Text = Convert::ToString(y2);
  270. bg->Render(gr);
  271. delete bc;
  272. delete bg;
  273. delete gr;
  274.  
  275. }
  276. };
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement