Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.41 KB | None | 0 0
  1. #include "wx/wx.h"
  2. #include <wx/timer.h>
  3.  
  4. #include "ex7.h"
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <time.h>
  8. using namespace std;
  9.  
  10. #define UP 1
  11. #define UPRIGHT 2
  12. #define RIGHT 3
  13. #define DOWNRIGHT 4
  14. #define DOWN 5
  15. #define DOWNLEFT 6
  16. #define LEFT 7
  17. #define UPLEFT 8
  18.  
  19. IMPLEMENT_APP(myApp)
  20.  
  21. // ========================================================================================================
  22. // wxApp
  23. // ========================================================================================================
  24. bool myApp::OnInit()
  25. {
  26. srand(time(NULL));// add for got real randomal
  27. MyFrame *frame = new MyFrame();
  28. frame->Show(true);
  29. wxImage image;
  30. wxImage::AddHandler(new wxPNGHandler);
  31.  
  32. bool isloadOK = image.LoadFile("shape01.png", wxBITMAP_TYPE_PNG);
  33. if (isloadOK)
  34. {
  35. DragShape *newShape = new DragShape(wxBitmap(image));
  36. newShape->SetPosition(wxPoint(50, 50));
  37. frame->GetCanvas()->SetShape(newShape);
  38. }
  39. int numOfEnemy = 3;
  40. while (numOfEnemy > 0)
  41. {
  42. bool isloadOK = image.LoadFile("shape01.png", wxBITMAP_TYPE_PNG);
  43. if (isloadOK)
  44. {
  45. DragShape *newShape = new DragShape(wxBitmap(image));
  46. newShape->SetPosition(wxPoint((rand() % 470) + 1, (rand() % 360) + 1));
  47. frame->GetCanvas()->SetShapesQueue(newShape);
  48. }
  49. numOfEnemy--;
  50. }
  51. frame->Show(true);
  52. SetTopWindow(frame);
  53. return true;
  54. }
  55.  
  56. // ========================================================================================================
  57. // MyFrame
  58. // ========================================================================================================
  59. MyFrame::MyFrame()
  60. : wxFrame((wxFrame *)NULL, wxID_ANY, "EX 7",
  61. wxPoint(20, 20), wxSize(470, 360))
  62. {
  63. wxSize sz = GetClientSize();
  64. m_canvas = new MyCanvas(this, wxID_ANY, wxPoint(0, 0), wxSize(sz.GetX(), sz.GetY()));
  65. }
  66.  
  67. // ========================================================================================================
  68. // MyCanvas
  69. // ========================================================================================================
  70. wxBEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
  71. EVT_PAINT(MyCanvas::OnPaint)
  72. EVT_TIMER(wxID_FILE9, MyCanvas::OnTimer)
  73. wxEND_EVENT_TABLE()
  74.  
  75. // ----------------------------------------------------------------------------
  76. MyCanvas::MyCanvas(wxWindow *parent, wxWindowID id,
  77. const wxPoint &pos, const wxSize &size)
  78. : wxScrolledWindow(parent, id, pos, size, wxSUNKEN_BORDER), m_timer(this, wxID_FILE9)
  79. {
  80. m_timer.Start(50);
  81. Bind(wxEVT_CHAR_HOOK, &MyCanvas::OnChar, this);
  82. SetBackgroundColour(*wxWHITE);
  83. SetCursor(wxCursor(wxCURSOR_ARROW));
  84. }
  85.  
  86. // ----------------------------------------------------------------------------
  87. void MyCanvas::OnTimer(wxTimerEvent &event)
  88. {
  89. wxSize sz = this->GetClientSize();
  90. DragShape *temp;
  91. DragShape *temp2;
  92. bool flag = false;
  93. for (int i = 0; i < shapesQueue.size(); i++)
  94. {
  95. temp = shapesQueue.front();
  96. shapesQueue.pop();
  97. if (shapesQueue.empty())
  98. {
  99. wxMessageBox(wxT("You WIN (:"));
  100. exit(1);
  101. }
  102. if (temp->GetRect().Intersects(_shape->GetRect()))
  103. {
  104. wxMessageBox(wxT("you LOSE :("));
  105. exit(1);
  106. }
  107. for (int j = 0; j < shapesQueue.size(); j++)
  108. {
  109. temp2 = shapesQueue.front();
  110. shapesQueue.pop();
  111.  
  112. if (temp->GetRect().Intersects(temp2->GetRect()))
  113. {
  114. DragShape *result = new PairShape(temp, temp2);
  115. shapesQueue.push(result);
  116. flag = true;
  117. }
  118. else
  119. {
  120. shapesQueue.push(temp2);
  121. }
  122. }
  123. if (flag == false)
  124. {
  125. shapesQueue.push(temp);
  126. }
  127. else
  128. {
  129. flag = false;
  130. }
  131. }
  132. for (int i = 0; i < shapesQueue.size(); i++)
  133. {
  134. temp = shapesQueue.front();
  135. shapesQueue.pop();
  136. temp->updateDirection();
  137. temp->hitWall(sz);
  138. temp->updateSteps();
  139. shapesQueue.push(temp);
  140. }
  141. Refresh(true);
  142. Update();
  143. }
  144. // ----------------------------------------------------------------------------
  145. void MyCanvas::OnChar(wxKeyEvent &event)
  146. {
  147.  
  148. int x = 0, y = 0;
  149. if (event.GetUnicodeKey() != WXK_NONE)
  150. return;
  151. // It's a special key, deal with all the known ones:
  152. switch (event.GetKeyCode())
  153. {
  154. case WXK_NUMPAD_LEFT:
  155. case WXK_LEFT:
  156. x--;
  157. break;
  158. case WXK_NUMPAD_RIGHT:
  159. case WXK_RIGHT:
  160. x++;
  161. break;
  162. case WXK_NUMPAD_UP:
  163. case WXK_UP:
  164. y--;
  165. break;
  166. case WXK_NUMPAD_DOWN:
  167. case WXK_DOWN:
  168. y++;
  169. break;
  170. }
  171.  
  172. _shape->SetPosition(wxPoint(_shape->GetPosition().x + x, _shape->GetPosition().y + y));
  173. Refresh(true);
  174. Update();
  175. }
  176.  
  177. // ----------------------------------------------------------------------------
  178. void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event))
  179. {
  180. wxPaintDC dc(this);
  181. PrepareDC(dc);
  182. DrawShapes(dc);
  183. }
  184.  
  185. // ----------------------------------------------------------------------------
  186. void MyCanvas::DrawShapes(wxDC &dc)
  187. {
  188. _shape->Draw(dc);
  189. DragShape *temp;
  190. for (int i = 0; i < shapesQueue.size(); i++)
  191. {
  192. temp = shapesQueue.front();
  193. temp->Draw(dc);
  194. shapesQueue.pop();
  195. shapesQueue.push(temp);
  196. }
  197. }
  198. // ----------------------------------------------------------------------------
  199. MyCanvas::~MyCanvas()
  200. {
  201. }
  202.  
  203. // ========================================================================================================
  204. // DragShape
  205. // ========================================================================================================
  206. DragShape::DragShape(const wxBitmap &bitmap)
  207. {
  208. m_bitmap = bitmap;
  209. m_pos.x = 0;
  210. m_pos.y = 0;
  211. this->direction = rand() % 8 + 1;
  212. this->counterSteps = 0;
  213. }
  214.  
  215. bool DragShape::Draw(wxDC &dc)
  216. {
  217. if (m_bitmap.IsOk())
  218. {
  219. wxMemoryDC memDC;
  220. memDC.SelectObject(m_bitmap);
  221.  
  222. dc.Blit(m_pos.x, m_pos.y, m_bitmap.GetWidth(), m_bitmap.GetHeight(),
  223. &memDC, 0, 0, wxCOPY, true);
  224.  
  225. return true;
  226. }
  227. else
  228. return false;
  229. }
  230.  
  231. void DragShape::updateDirection()
  232. {
  233. if (this->counterSteps == 17)
  234. {
  235. this->direction = ((rand() % 8) + 1);
  236. this->counterSteps = 0;
  237. }
  238. }
  239.  
  240. void DragShape ::updateSteps()
  241. {
  242.  
  243. switch (this->GetDirection())
  244. {
  245. case UP:
  246. this->SetPosition(wxPoint(this->GetPosition().x, this->GetPosition().y - 1));
  247. break;
  248. case UPRIGHT:
  249. this->SetPosition(wxPoint(this->GetPosition().x + 1, this->GetPosition().y - 1));
  250. break;
  251. case RIGHT:
  252. this->SetPosition(wxPoint(this->GetPosition().x + 1, this->GetPosition().y));
  253. break;
  254. case DOWNRIGHT:
  255. this->SetPosition(wxPoint(this->GetPosition().x + 1, this->GetPosition().y + 1));
  256. break;
  257. case DOWN:
  258. this->SetPosition(wxPoint(this->GetPosition().x, this->GetPosition().y + 1));
  259. break;
  260. case DOWNLEFT:
  261. this->SetPosition(wxPoint(this->GetPosition().x - 1, this->GetPosition().y + 1));
  262. break;
  263. case LEFT:
  264. this->SetPosition(wxPoint(this->GetPosition().x - 1, this->GetPosition().y));
  265. break;
  266. case UPLEFT:
  267. this->SetPosition(wxPoint(this->GetPosition().x - 1, this->GetPosition().y - 1));
  268. break;
  269. }
  270. this->SetCounterSteps(this->GetCounterSteps() + 1);
  271. }
  272.  
  273. void DragShape::hitWall(wxSize sz)
  274. {
  275. wxRect r = this->GetRect();
  276. int posX = r.GetX();
  277. int posY = r.GetY();
  278. int height = r.GetHeight();
  279. int width = r.GetWidth();
  280. if (posX <= 0)
  281. {
  282. if (this->GetDirection() == LEFT)
  283. {
  284. this->SetDirection(RIGHT);
  285. }
  286. else if (this->GetDirection() == UPLEFT)
  287. {
  288. this->SetDirection(RIGHT);
  289. }
  290. else if (this->GetDirection() == DOWNLEFT)
  291. {
  292. this->SetDirection(DOWNRIGHT);
  293. }
  294. }
  295. else if ((posX + width) >= sz.GetX())
  296. {
  297. if (this->GetDirection() == RIGHT)
  298. {
  299. this->SetDirection(LEFT);
  300. }
  301. else if (this->GetDirection() == UPRIGHT)
  302. {
  303. this->SetDirection(UPLEFT);
  304. }
  305. else if (this->GetDirection() == DOWNRIGHT)
  306. {
  307. this->SetDirection(DOWNLEFT);
  308. }
  309. }
  310. else if ((posY + height) >= sz.GetY())
  311. {
  312. if (this->GetDirection() == DOWN)
  313. {
  314. this->SetDirection(UP);
  315. }
  316. else if (this->GetDirection() == DOWNLEFT)
  317. {
  318. this->SetDirection(UPLEFT);
  319. }
  320. else if (this->GetDirection() == DOWNRIGHT)
  321. {
  322. this->SetDirection(UPRIGHT);
  323. }
  324. }
  325. else if (posY <= 0)
  326. {
  327. if (this->GetDirection() == UP)
  328. {
  329. this->SetDirection(DOWN);
  330. }
  331. else if (this->GetDirection() == UPRIGHT)
  332. {
  333. this->SetDirection(DOWNRIGHT);
  334. }
  335. else if (this->GetDirection() == UPLEFT)
  336. {
  337. this->SetDirection(DOWNLEFT);
  338. }
  339. }
  340. }
  341.  
  342. // ========================================================================================================
  343. // PairShape
  344. // ========================================================================================================
  345.  
  346. PairShape::PairShape(DragShape *enemy1, DragShape *enemy2)
  347. {
  348. this->enemyOne = enemy1;
  349. this->enemyTwo = enemy2;
  350. this->SetPosition(wxPoint(enemyOne->GetPosition().x / 2 + enemyTwo->GetPosition().x / 2, enemyOne->GetPosition().y / 2 + enemyTwo->GetPosition().y / 2));
  351. this->counterSteps = 0;
  352. this->enemyOne->SetCounterSteps(0);
  353. this->enemyTwo->SetCounterSteps(0);
  354. this->direction = this->enemyOne->GetDirection();
  355. this->enemyTwo->SetDirection(this->direction);
  356. }
  357. bool PairShape::Draw(wxDC &dc)
  358. {
  359. this->enemyOne->Draw(dc);
  360. this->enemyTwo->Draw(dc);
  361. }
  362. void PairShape::updateDirection()
  363. {
  364. if (this->counterSteps == 17)
  365. {
  366. this->SetDirection(((rand() % 8) + 1));
  367. this->counterSteps = 0;
  368. this->enemyOne->SetDirection(this->direction);
  369. this->enemyTwo->SetDirection(this->direction);
  370. this->enemyOne->SetCounterSteps(0);
  371. this->enemyTwo->SetCounterSteps(0);
  372. }
  373. }
  374.  
  375. void PairShape::updateSteps()
  376. {
  377. this->enemyOne->updateSteps();
  378. this->enemyTwo->updateSteps();
  379. this->SetCounterSteps(this->GetCounterSteps() + 1);
  380. switch (this->GetDirection())
  381. {
  382. case UP:
  383. this->SetPosition(wxPoint(this->GetPosition().x, this->GetPosition().y - 1));
  384. break;
  385. case UPRIGHT:
  386. this->SetPosition(wxPoint(this->GetPosition().x + 1, this->GetPosition().y - 1));
  387. break;
  388. case RIGHT:
  389. this->SetPosition(wxPoint(this->GetPosition().x + 1, this->GetPosition().y));
  390. break;
  391. case DOWNRIGHT:
  392. this->SetPosition(wxPoint(this->GetPosition().x + 1, this->GetPosition().y + 1));
  393. break;
  394. case DOWN:
  395. this->SetPosition(wxPoint(this->GetPosition().x, this->GetPosition().y + 1));
  396. break;
  397. case DOWNLEFT:
  398. this->SetPosition(wxPoint(this->GetPosition().x - 1, this->GetPosition().y + 1));
  399. break;
  400. case LEFT:
  401. this->SetPosition(wxPoint(this->GetPosition().x - 1, this->GetPosition().y));
  402. break;
  403. case UPLEFT:
  404. this->SetPosition(wxPoint(this->GetPosition().x - 1, this->GetPosition().y - 1));
  405. break;
  406. }
  407. }
  408. void PairShape ::hitWall(wxSize sz)
  409. {
  410. wxRect r = this->GetRect();
  411. int posX = r.GetX();
  412. int posY = r.GetY();
  413. int height = r.GetHeight();
  414. int width = r.GetWidth();
  415. if (posX <= 0)
  416. {
  417. if (this->GetDirection() == LEFT)
  418. {
  419. this->SetDirection(RIGHT);
  420. }
  421. else if (this->GetDirection() == UPLEFT)
  422. {
  423. this->SetDirection(RIGHT);
  424. }
  425. else if (this->GetDirection() == DOWNLEFT)
  426. {
  427. this->SetDirection(DOWNRIGHT);
  428. }
  429. }
  430. else if ((posX + width) >= sz.GetX())
  431. {
  432. if (this->GetDirection() == RIGHT)
  433. {
  434. this->SetDirection(LEFT);
  435. }
  436. else if (this->GetDirection() == UPRIGHT)
  437. {
  438. this->SetDirection(UPLEFT);
  439. }
  440. else if (this->GetDirection() == DOWNRIGHT)
  441. {
  442. this->SetDirection(DOWNLEFT);
  443. }
  444. }
  445. else if ((posY + height) >= sz.GetY())
  446. {
  447. if (this->GetDirection() == DOWN)
  448. {
  449. this->SetDirection(UP);
  450. }
  451. else if (this->GetDirection() == DOWNLEFT)
  452. {
  453. this->SetDirection(UPLEFT);
  454. }
  455. else if (this->GetDirection() == DOWNRIGHT)
  456. {
  457. this->SetDirection(UPRIGHT);
  458. }
  459. }
  460. else if (posY <= 0)
  461. {
  462. if (this->GetDirection() == UP)
  463. {
  464. this->SetDirection(DOWN);
  465. }
  466. else if (this->GetDirection() == UPRIGHT)
  467. {
  468. this->SetDirection(DOWNRIGHT);
  469. }
  470. else if (this->GetDirection() == UPLEFT)
  471. {
  472. this->SetDirection(DOWNLEFT);
  473. }
  474. }
  475. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement