Advertisement
PhoenixMee

Untitled

Apr 11th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.73 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "MyGame.h"
  3.  
  4. CMyGame::CMyGame(void) :
  5. startScreen(480, 360, "startScreen.bmp", CColor::Blue(), 0),
  6. underlay(68, 582, "underlay.bmp", CColor::Blue(), 0),
  7. keep_practising(240, 678, "keep_practising.bmp", CColor::Blue(), 0),
  8. reach_the_goal(240, 677, "reach_the_goal.bmp", CColor::Blue(), 0),
  9. cover(362, 680, "cover.bmp", CColor::Blue(), 0),
  10. GUI(69, 582, "GUI.bmp", CColor::Blue(), 0),
  11. GUIclose(69, 703, "GUIclose.bmp", CColor::Blue(), 0),
  12. GUIbutton(120, 703, "GUIbutton2.bmp", CColor::Blue(), 0),
  13. BGMbutton(20, 703, "GUIbutton2.bmp", CColor::Blue(), 0),
  14. Gplus(115, 595, "plus2.bmp", CColor::Blue(), 0),
  15. Gminus(25, 595, "minus2.bmp", CColor::Blue(), 0),
  16. Tplus(115, 550, "plus2.bmp", CColor::Blue(), 0),
  17. Tminus(25, 550, "minus2.bmp", CColor::Blue(), 0),
  18. box_large(-300, -300, "box_large.bmp", CColor::Blue(), 0),
  19. box_small(-300, -300, "box_small.bmp", CColor::Blue(), 0)
  20. {
  21. GUIbutton.LoadImage("GUIbutton2.bmp", "release", 2, 1, 0, 0, CColor::Blue());
  22. GUIbutton.LoadImage("GUIbutton2.bmp", "press", 2, 1, 1, 0, CColor::Blue());
  23. BGMbutton.LoadImage("GUIbutton2.bmp", "release", 2, 1, 0, 0, CColor::Blue());
  24. BGMbutton.LoadImage("GUIbutton2.bmp", "press", 2, 1, 1, 0, CColor::Blue());
  25. Gplus.LoadImage("plus2.bmp", "release", 2, 1, 0, 0, CColor::Blue());
  26. Gplus.LoadImage("plus2.bmp", "press", 2, 1, 1, 0, CColor::Blue());
  27. Gminus.LoadImage("minus2.bmp", "release", 2, 1, 0, 0, CColor::Blue());
  28. Gminus.LoadImage("minus2.bmp", "press", 2, 1, 1, 0, CColor::Blue());
  29. Tplus.LoadImage("plus2.bmp", "release", 2, 1, 0, 0, CColor::Blue());
  30. Tplus.LoadImage("plus2.bmp", "press", 2, 1, 1, 0, CColor::Blue());
  31. Tminus.LoadImage("minus2.bmp", "release", 2, 1, 0, 0, CColor::Blue());
  32. Tminus.LoadImage("minus2.bmp", "press", 2, 1, 1, 0, CColor::Blue());
  33. }
  34.  
  35. CMyGame::~CMyGame(void)
  36. {
  37. }
  38.  
  39. void CMyGame::StarsControl(Uint16 x, Uint16 y) // on-click function - star spawn, attributes, aiming, trajectory
  40. {
  41. color_coordinator = CColor(rand() % 155 + 100, rand() % 155 + 100, rand() % 155 + 100);
  42. CSprite *pStar = new CSpriteOval(CVector(x, y), 3, color_coordinator, GetTime());
  43. stars.push_back(pStar);
  44. pStar->SetMass(100);
  45. pStar->SetStatus(stars.size());
  46. initial_aim = CVector(x, y);
  47. if (pStar->HitTest(x, y))
  48. {
  49. CSprite *pStarclick = new CSpriteRect(x, y, 7, 7, CColor::DarkRed(), GetTime()); // small sprite to show player where they initially clicked
  50. starclick.push_back(pStarclick);
  51. s_Aiming = true;
  52. }
  53. for (CSprite *pTrajectory : trajectory) delete pTrajectory; // deleted previous star's trajectory : memory efficient
  54. trajectory.clear();
  55. }
  56.  
  57. void CMyGame::LevelControl(int a) // game rules to be called on specific levels
  58. {
  59. if (a == 2)
  60. {
  61. if (stars.size() == 15 && s_Aiming == false)
  62. {
  63. SetLevel(3);
  64. levelup.Play("levelup.wav");
  65. }
  66. }
  67. else if (a == 3)
  68. {
  69. for (CSprite* pStar : stars) if (pStar->GetStatus() == 17 && pStar->IsDeleted())
  70. {
  71. SetLevel(12);
  72. gameover.Play("gameover.wav");
  73. BGM.Stop();
  74. }
  75. }
  76. else if (a == 4)
  77. {
  78. for (CSprite* pStar : stars)
  79. {
  80. if (pStar->GetStatus() == 16 && pStar->IsDeleted())
  81. {
  82. SetLevel(12);
  83. gameover.Play("gameover.wav");
  84. BGM.Stop();
  85. }
  86. if (pStar->GetMass() > 99999 && pStar->GetX() < 100) pStar->SetVelocity(100, 0);
  87. if (pStar->GetMass() > 99999 && pStar->GetX() > 860) pStar->SetVelocity(-100, 0);
  88. }
  89. }
  90. else if (a == 5)
  91. {
  92. for (CSprite* pStar : stars)
  93. {
  94. if (pStar->GetStatus() == 16 && pStar->IsDeleted())
  95. {
  96. SetLevel(12);
  97. gameover.Play("gameover.wav");
  98. BGM.Stop();
  99. }
  100. if (pStar->GetMass() > 99999 && pStar->GetX() < 100) pStar->SetVelocity(100, 0);
  101. if (pStar->GetMass() > 99999 && pStar->GetX() > 860) pStar->SetVelocity(-100, 0);
  102. }
  103. for (CSprite *pGoal : goal)
  104. {
  105. if (pGoal->GetX() < 100) pGoal->SetVelocity(100, 0);
  106. if (pGoal->GetX() > 860) pGoal->SetVelocity(-100, 0);
  107. }
  108. }
  109. else if (a == 6)
  110. {
  111. for (CSprite* pStar : stars) if (pStar->GetStatus() == 15 && pStar->IsDeleted())
  112. {
  113. SetLevel(10);
  114. gameover.Play("gameover.wav");
  115. BGM.Stop();
  116. }
  117. if (box_small.GetX() > 860) box_small.SetVelocity(0, 100);
  118. if (box_small.GetY() > 620) box_small.SetVelocity(-100, 0);
  119. if (box_small.GetX() < 100) box_small.SetVelocity(0, -100);
  120. if (box_small.GetY() < 100) box_small.SetVelocity(100, 0);
  121. if (box_small.GetX() > 1060)
  122. {
  123. SetLevel(12);
  124. gameover.Play("gameover.wav");
  125. BGM.Stop();
  126. }
  127. }
  128. else if (a == 7)
  129. {
  130. for (CSprite* pStar : stars)
  131. {
  132. if (pStar->GetStatus() == 18 && pStar->IsDeleted())
  133. {
  134. SetLevel(12);
  135. gameover.Play("gameover.wav");
  136. BGM.Stop();
  137. }
  138. if (pStar->GetMass() > 99999 && pStar->GetY() < 100) pStar->SetVelocity(0, 100);
  139. if (pStar->GetMass() > 99999 && pStar->GetY() > 620) pStar->SetVelocity(0, -100);
  140. }
  141. }
  142. else if (a == 8)
  143. {
  144. for (CSprite* pStar : stars)
  145. {
  146. if (pStar->GetStatus() == 16 && pStar->IsDeleted())
  147. {
  148. SetLevel(12);
  149. gameover.Play("gameover.wav");
  150. BGM.Stop();
  151. }
  152. if (pStar->GetMass() > 99999 && pStar->GetX() > 620)
  153. {
  154. pStar->SetX(620);
  155. pStar->SetVelocity(0, -100);
  156. }
  157. else if (pStar->GetMass() > 99999 && pStar->GetY() > 520)
  158. {
  159. pStar->SetY(520);
  160. pStar->SetVelocity(100, 0);
  161. }
  162. else if (pStar->GetMass() > 99999 && pStar->GetX() < 300)
  163. {
  164. pStar->SetX(300);
  165. pStar->SetVelocity(0, 100);
  166. }
  167. else if (pStar->GetMass() > 99999 && pStar->GetY() < 200)
  168. {
  169. pStar->SetY(200);
  170. pStar->SetVelocity(-100, 0);
  171. }
  172. }
  173. for (CSprite *pGoal : goal)
  174. {
  175. if (pGoal->GetX() < 100)
  176. {
  177. pGoal->SetX(100);
  178. pGoal->SetVelocity(0, -100);
  179. }
  180. else if (pGoal->GetX() > 860)
  181. {
  182. pGoal->SetX(860);
  183. pGoal->SetVelocity(0, 100);
  184. }
  185. else if (pGoal->GetY() > 620)
  186. {
  187. pGoal->SetY(620);
  188. pGoal->SetVelocity(-100, 0);
  189. }
  190. else if (pGoal->GetY() < 100)
  191. {
  192. pGoal->SetY(100);
  193. pGoal->SetVelocity(100, 0);
  194. }
  195. }
  196. }
  197. else if (a == 9)
  198. {
  199. for (CSprite* pStar : stars) if (pStar->GetStatus() == 17 && pStar->IsDeleted())
  200. {
  201. SetLevel(12);
  202. gameover.Play("gameover.wav");
  203. BGM.Stop();
  204. }
  205. }
  206. else if (a == 10)
  207. {
  208. for (CSprite* pStar : stars) if (pStar->GetStatus() == 16 && pStar->IsDeleted())
  209. {
  210. SetLevel(12);
  211. gameover.Play("gameover.wav");
  212. BGM.Stop();
  213. }
  214. }
  215. }
  216.  
  217. void CMyGame::GameRules() // general game rules, collision detection, GUI control applied every level
  218. {
  219. for (CSprite *pStar : stars)
  220. {
  221. if (pStar->GetX() > 960 || pStar->GetX() < 0)
  222. {
  223. pStar->SetMass(0);
  224. pStar->Delete();
  225. }
  226. if (pStar->GetY() > 720 || pStar->GetY() < 0)
  227. {
  228. pStar->SetMass(0);
  229. pStar->Delete();
  230. }
  231. if (s_Trajectory)
  232. {
  233. if (pStar->GetStatus() == stars.size() && pStar->GetMass() < 100000)
  234. {
  235. if (!pStar->IsDeleted())
  236. {
  237. CSprite* pTrajectory = new CSpriteOval(pStar->GetPosition(), 1, color_coordinator, GetTime());
  238. trajectory.push_back(pTrajectory);
  239. }
  240. }
  241. }
  242. else if (s_Trajectory == false)
  243. {
  244. for (CSprite *pTrajectory : trajectory) delete pTrajectory;
  245. trajectory.clear();
  246. }
  247. for (CSprite* pGoal : goal) if (pStar->HitTest(pGoal))
  248. {
  249. NewLevel();
  250. if (GetLevel() != 10) levelup.Play("levelup.wav");
  251. else
  252. {
  253. BGM.Stop();
  254. win.Play("win.wav");
  255. }
  256. }
  257. }
  258.  
  259. for (CSprite *pExplode : explode)
  260. {
  261. pExplode->SetStatus(pExplode->GetStatus() - 1);
  262. if (pExplode->GetStatus() <= 0) pExplode->Delete();
  263. }
  264.  
  265. if (starclick.size() == 2 && s_Aiming == false) starclick.pop_front();
  266. if (starclickup.size() == 2 && s_Aiming == false) starclickup.pop_front();
  267. if (stars.size() == 19) stars.clear();
  268.  
  269.  
  270. if (GetLevel() != 6) box_small.SetSpeed(0);
  271. if (keep_practising.GetX() < -150) keep_practising.SetPosition(240, 678);
  272. if (reach_the_goal.GetX() < -150) reach_the_goal.SetPosition(240, 677);
  273. reach_the_goal.SetXVelocity(-30);
  274. keep_practising.SetXVelocity(-30);
  275. }
  276.  
  277. void CMyGame::CalculateDistance() // get the numerical value of distance from one star to all others, and store in array
  278. {
  279. for each (CSprite *pStar in stars) each_star_pos[pStar->GetStatus()] = pStar->GetPos();
  280. for (CSprite *pStar : stars)
  281. {
  282. if (pStar->GetStatus() != stars.size() || pStar->GetStatus() == stars.size() && s_Aiming == false)
  283. {
  284. for (int i = 0; i < stars.size() + 1; i++)
  285. if (i != pStar->GetStatus())
  286. each_star_dist[pStar->GetStatus()][i] = Distance(pStar->GetPos(), each_star_pos[i]);
  287. }
  288. }
  289. }
  290.  
  291. void CMyGame::CalculateForce() // with known distance and mass calculate force applied to other stars. Newton's law of universal gravity: F = G (Mm / (r * r))
  292. {
  293. for each (CSprite *pStar in stars) each_star_mass[pStar->GetStatus()] = pStar->GetMass();
  294. for (CSprite *pStar : stars)
  295. {
  296. if (pStar->GetStatus() != stars.size() || pStar->GetStatus() == stars.size() && s_Aiming == false)
  297. {
  298. for (int i = 0; i < stars.size() + 1; i++)
  299. if (i != pStar->GetStatus())
  300. each_star_G[pStar->GetStatus()][i] = G * (pStar->GetMass() * each_star_mass[i]) / (each_star_dist[pStar->GetStatus()][i] * each_star_dist[pStar->GetStatus()][i]);
  301. }
  302. }
  303. }
  304.  
  305. void CMyGame::ApplyForces() // accelerate stars by normal displacement of other stars for direction and apply Newton's second law: A = F / M
  306. {
  307. for (CSprite *pStar : stars)
  308. {
  309. if (pStar->GetStatus() != stars.size() || pStar->GetStatus() == stars.size() && s_Aiming == false)
  310. {
  311. for (int i = 0; i < stars.size() + 1; i++)
  312. {
  313. if (pStar->GetMass() < 99999) // UPDATE: large yellow stars are static, only stars with less mass will accelerate, for playability
  314. if (i != pStar->GetStatus() && each_star_dist[pStar->GetStatus()][i] >= 10)
  315. pStar->Accelerate((Normalize(each_star_pos[i] - pStar->GetPos())) * each_star_G[i][pStar->GetStatus()] / pStar->GetMass());
  316. if (i == pStar->GetStatus()) continue;
  317. if (each_star_dist[pStar->GetStatus()][i] <= 10 && pStar->GetMass() == 100)
  318. {
  319. CSprite *pExplode = new CSpriteOval(pStar->GetPosition(), 7, CColor::Red(), GetTime());
  320. explode.push_back(pExplode);
  321. pExplode->SetStatus(15);
  322. // --- IMPORTANT: resetting mass and pos of deleted star to keep increasing size of star list, which is necessary for physics calculations and game logic
  323. pStar->SetPosition(rand() % 100000 +1000, rand() % 100000 + 1000); // BUG FIX: less chance of stars' distance being <= 10 thus causing all stars to be deleted (previously: pStar->SetPosition(0, 0) when star collision)
  324. pStar->SetMass(0);
  325. pStar->Delete();
  326. explodes.Play("explode.wav");
  327. }
  328. }
  329. }
  330. }
  331. }
  332.  
  333. inline void CMyGame::SmallBoxFunc(Uint16 x, Uint16 y)
  334. {
  335. if (s_Aiming && !box_small.HitTest(x, y))
  336. {
  337. s_Aiming = false;
  338. CSprite *pStarclickup = new CSpriteRect(x, y, 7, 7, CColor::White(), GetTime());
  339. starclickup.push_back(pStarclickup);
  340. }
  341. }
  342.  
  343. inline void CMyGame::LargeBoxFunc(Uint16 x, Uint16 y)
  344. {
  345. if (s_Aiming && !box_large.HitTest(x, y))
  346. {
  347. s_Aiming = false;
  348. CSprite *pStarclickup = new CSpriteRect(x, y, 7, 7, CColor::White(), GetTime());
  349. starclickup.push_back(pStarclickup);
  350. }
  351. }
  352.  
  353. void CMyGame::OnUpdate()
  354. {
  355. // physics
  356. CalculateDistance();
  357. CalculateForce();
  358. ApplyForces();
  359.  
  360. // general
  361. GameRules();
  362.  
  363. // level control
  364. if (GetLevel() == 2) LevelControl(2);
  365. else if (GetLevel() == 3) LevelControl(3);
  366. else if (GetLevel() == 4) LevelControl(4);
  367. else if (GetLevel() == 5) LevelControl(5);
  368. else if (GetLevel() == 6) LevelControl(6);
  369. else if (GetLevel() == 7) LevelControl(7);
  370. else if (GetLevel() == 8) LevelControl(8);
  371. else if (GetLevel() == 9) LevelControl(9);
  372. else if (GetLevel() == 10) LevelControl(10);
  373.  
  374. // misc
  375. if (GetLevel() == 12 || GetLevel() == 11) GUIbool = false;
  376.  
  377. long t = GetTime();
  378. for (CSprite *pStar : stars) pStar->Update(t);
  379. for (CSprite *pStarclick : starclick) pStarclick->Update(t);
  380. for (CSprite *pStarclickup : starclickup) pStarclickup->Update(t);
  381. for (CSprite *pExplode : explode) pExplode->Update(t);
  382. if (s_Trajectory) for (CSprite *pTrajectory : trajectory) pTrajectory->Update(t);
  383. for (CSprite *pGoal : goal) pGoal->Update(t);
  384. keep_practising.Update(t);
  385. reach_the_goal.Update(t);
  386. box_small.Update(t);
  387.  
  388. trajectory.remove_if(deleted);
  389. explode.remove_if(deleted);
  390. }
  391.  
  392. void CMyGame::OnDraw(CGraphics* g)
  393. {
  394. if (GetLevel() != 12 && GetLevel() != 11 && GetLevel() != 13)
  395. {
  396. if (GUIbool)
  397. {
  398. underlay.Draw(g);
  399. if (GetLevel() == 2) keep_practising.Draw(g);
  400. else if (GetLevel() != 2) reach_the_goal.Draw(g);
  401. cover.Draw(g);
  402. }
  403. if (GetLevel() == 3) box_large.Draw(g);
  404. if (GetLevel() == 4 || GetLevel() == 5 || GetLevel() == 6 || GetLevel() == 7 || GetLevel() == 8 || GetLevel() == 9 || GetLevel() == 10) box_small.Draw(g);
  405. if (s_Trajectory) for (CSprite *pTrajectory : trajectory) pTrajectory->Draw(g);
  406. for (CSprite *pStar : stars) pStar->Draw(g);
  407. for (CSprite *pStarclick : starclick) pStarclick->Draw(g);
  408. for (CSprite *pStarclickup : starclickup) pStarclickup->Draw(g);
  409. for (CSprite *pExplode : explode) pExplode->Draw(g);
  410. for (CSprite *pGoal : goal) pGoal->Draw(g);
  411. if (GUIbool)
  412. {
  413. GUI.Draw(g);
  414. Gplus.Draw(g);
  415. Gminus.Draw(g);
  416. Tplus.Draw(g);
  417. Tminus.Draw(g);
  418. }
  419. else GUIclose.Draw(g);
  420. GUIbutton.Draw(g);
  421. BGMbutton.Draw(g);
  422. }
  423.  
  424. // text drawing
  425.  
  426. if (GUIbool)
  427. {
  428. *g << font(16) << color(CColor::Black()) << xy(55, 587) << G;
  429. if (s_Trajectory) *g << font(16) << color(CColor::Green()) << xy(57, 542) << "ON";
  430. else *g << font(16) << color(CColor::Red()) << xy(57, 542) << "OFF";
  431. *g << font(16) << color(CColor::Black()) << xy(40, 500) << each_star_G[1][stars.size()];
  432. *g << font(16) << color(CColor::Black()) << xy(40, 458) << each_star_dist[1][stars.size()];
  433. }
  434.  
  435. if (GetLevel() == 1) // BUG FIX: level 1 is 'menu mode', necessary because the choice of 'practise' or 'begin' on title screen would not allow SetLevel(). Fixed by not using MenuMode().
  436. {
  437. startScreen.Draw(g);
  438. return;
  439. }
  440. if (GetLevel() == 2)
  441. {
  442. if (GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 15 - stars.size();
  443. if (stars.size() == 0)
  444. *g << font(30) << color(CColor::White()) << top << center << endl << endl << endl << "right click to add a large star";
  445. if (stars.size() == 1)
  446. *g << font(30) << color(CColor::White()) << top << center << endl << endl << endl << "left click and drag to push a smaller star";
  447. if (stars.size() > 2 && stars.size() <= 5)
  448. *g << font(22) << color(CColor::White()) << top << center << endl << endl << endl << endl << "minimise and maximise GUI with the top right button" << endl << "on the GUI, pause and resume BGM on the top left";
  449. if (stars.size() > 6 && stars.size() <= 9)
  450. *g << font(22) << color(CColor::White()) << top << center << endl << endl << endl << endl << "stars are deleted if they go outside the game space"
  451. << endl << "or if they collide";
  452. if (stars.size() > 10 && stars.size() <= 12)
  453. *g << font(30) << color(CColor::White()) << top << center << endl << endl << endl << endl << "there are 8 levels to complete";
  454. if (stars.size() == 13 && s_Aiming == false)
  455. *g << font(30) << color(CColor::White()) << top << center << endl << endl << endl << "level 1 will begin shortly";
  456. }
  457. else if (GetLevel() == 3)
  458. {
  459. if (GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 17 - stars.size();
  460. if (stars.size() == 2)
  461. *g << font(22) << color(CColor::White()) << top << center << endl << endl << endl << "you can only push stars within the dotted box"
  462. << endl << "if you go outside of the box while aiming, the star will be dropped" << endl <<
  463. "the grey circle is your goal";
  464. }
  465. else if (GetLevel() == 4 && GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 16 - stars.size();
  466. else if (GetLevel() == 5 && GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 16 - stars.size();
  467. else if (GetLevel() == 6 && GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 15 - stars.size();
  468. else if (GetLevel() == 7 && GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 18 - stars.size();
  469. else if (GetLevel() == 8 && GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 16 - stars.size();
  470. else if (GetLevel() == 9 && GUIbool) *g << font(20) << color(CColor::White()) << xy(58, 628) << 17 - stars.size();
  471. else if (GetLevel() == 10 && GUIbool) *g << font(18) << color(CColor::White()) << xy(58, 628) << 16 - stars.size();
  472. else if (GetLevel() == 11) *g << font(80) << color(CColor::White()) << vcenter << center << "YOU WIN!";
  473. else if (GetLevel() == 12)
  474. {
  475. *g << font(80) << color(CColor::White()) << vcenter << center << "GAME OVER";
  476. *g << font(30) << color(CColor::White()) << vcenter << center << endl << endl << "F2 to restart";
  477. }
  478. else if (GetLevel() == 13)
  479. {
  480. *g << font(80) << color(CColor::White()) << top << center << endl << endl << endl << "CAN YOU\nCOMPLETE THEM ALL?";
  481. GUIbool = false;
  482. }
  483. }
  484.  
  485.  
  486. void CMyGame::OnInitialize()
  487. {
  488. }
  489.  
  490. void CMyGame::OnDisplayMenu()
  491. {
  492. StartGame();
  493. }
  494.  
  495. void CMyGame::OnStartGame()
  496. {
  497. BGM.Play("BGM.wav", -1, 3000);
  498. BGMbool = true;
  499. GUIbool = true;
  500. s_Aiming = false;
  501. s_Trajectory = true;
  502. BGMbutton.SetImage("release");
  503. GUIbutton.SetImage("release");
  504. Gplus.SetImage("release");
  505. Gminus.SetImage("release");
  506. Tplus.SetImage("release");
  507. Tminus.SetImage("release");
  508. }
  509.  
  510. void CMyGame::OnStartLevel(Sint16 nLevel)
  511. {
  512. for (CSprite *pTrajectory : trajectory) delete pTrajectory;
  513. trajectory.clear();
  514. for (CSprite *pStar : stars) delete pStar;
  515. stars.clear();
  516. for (CSprite *pStarclick : starclick) delete pStarclick;
  517. starclick.clear();
  518. for (CSprite *pStarclickup : starclickup) delete pStarclickup;
  519. starclickup.clear();
  520. for (CSprite *pExplode : explode) delete pExplode;
  521. explode.clear();
  522. for (CSprite *pGoal : goal) delete pGoal;
  523. goal.clear();
  524.  
  525. switch (nLevel)
  526. {
  527. case 3:
  528. goal.push_back(new CSpriteOval(CVector(860, 620), 40, 40, CColor::LightGray(), GetTime()));
  529. box_large.SetPosition(100, 100);
  530. {
  531. CSprite *pStar = new CSpriteOval(CVector(740, 500), 10, CColor::Yellow(), GetTime());
  532. stars.push_back(pStar);
  533. pStar->SetMass(100000);
  534. pStar->SetStatus(stars.size());
  535. }
  536. {
  537. CSprite *pStar = new CSpriteOval(CVector(400, 350), 10, CColor::Yellow(), GetTime());
  538. stars.push_back(pStar);
  539. pStar->SetMass(100000);
  540. pStar->SetStatus(stars.size());
  541. }
  542. break;
  543. case 4:
  544. goal.push_back(new CSpriteOval(CVector(480, 50), 40, 40, CColor::LightGray(), GetTime()));
  545. box_small.SetPosition(480, 625);
  546. {
  547. CSprite *pStar = new CSpriteOval(CVector(480, 360), 10, CColor::Yellow(), GetTime());
  548. stars.push_back(pStar);
  549. pStar->SetMass(100000);
  550. pStar->SetVelocity(100, 0);
  551. pStar->SetStatus(stars.size());
  552. }
  553. break;
  554. case 5:
  555. box_small.SetPosition(480, 625);
  556. {
  557. CSprite *pGoal = new CSpriteOval(CVector(480, 50), 40, 40, CColor::LightGray(), GetTime());
  558. pGoal->SetVelocity(-100, 0);
  559. goal.push_back(pGoal);
  560. }
  561. {
  562. CSprite *pStar = new CSpriteOval(CVector(480, 360), 10, CColor::Yellow(), GetTime());
  563. stars.push_back(pStar);
  564. pStar->SetMass(100000);
  565. pStar->SetVelocity(100, 0);
  566. pStar->SetStatus(stars.size());
  567. }
  568. break;
  569. case 6:
  570. box_small.SetPosition(860, 100);
  571. box_small.SetSpeed(100);
  572. goal.push_back(new CSpriteOval(CVector(480, 360), 40, 40, CColor::LightGray(), GetTime()));
  573. break;
  574. case 7:
  575. box_small.SetPosition(860, 360);
  576. goal.push_back(new CSpriteOval(CVector(180, 360), 40, 40, CColor::LightGray(), GetTime()));
  577. {
  578. CSprite *pStar = new CSpriteOval(CVector(480, 360), 10, CColor::Yellow(), GetTime());
  579. stars.push_back(pStar);
  580. pStar->SetMass(100000);
  581. pStar->SetVelocity(0, 100);
  582. pStar->SetStatus(stars.size());
  583. }
  584. {
  585. CSprite *pStar = new CSpriteOval(CVector(380, 360), 10, CColor::Yellow(), GetTime());
  586. stars.push_back(pStar);
  587. pStar->SetMass(100000);
  588. pStar->SetVelocity(0, -100);
  589. pStar->SetStatus(stars.size());
  590. }
  591. {
  592. CSprite *pStar = new CSpriteOval(CVector(580, 360), 10, CColor::Yellow(), GetTime());
  593. stars.push_back(pStar);
  594. pStar->SetMass(100000);
  595. pStar->SetVelocity(0, -100);
  596. pStar->SetStatus(stars.size());
  597. }
  598. break;
  599. case 8:
  600. box_small.SetPosition(460, 360);
  601. {
  602. CSprite *pGoal = new CSpriteOval(CVector(100, 100), 40, 40, CColor::LightGray(), GetTime());
  603. pGoal->SetVelocity(100, 0);
  604. goal.push_back(pGoal);
  605. }
  606. {
  607. CSprite *pStar = new CSpriteOval(CVector(620, 200), 10, CColor::Yellow(), GetTime());
  608. stars.push_back(pStar);
  609. pStar->SetMass(100000);
  610. pStar->SetVelocity(-100, 0);
  611. pStar->SetStatus(stars.size());
  612. }
  613. break;
  614. case 9:
  615. box_small.SetPosition(860, 100);
  616. goal.push_back(new CSpriteOval(CVector(100, 100), 40, 40, CColor::LightGray(), GetTime()));
  617. {
  618. CSprite *pStar = new CSpriteOval(CVector(460, 200), 10, CColor::Yellow(), GetTime());
  619. stars.push_back(pStar);
  620. pStar->SetMass(100000);
  621. pStar->SetStatus(stars.size());
  622. }
  623. {
  624. CSprite *pStar = new CSpriteOval(CVector(460, 520), 10, CColor::Yellow(), GetTime());
  625. stars.push_back(pStar);
  626. pStar->SetMass(100000);
  627. pStar->SetStatus(stars.size());
  628. }
  629. break;
  630. case 10:
  631. box_small.SetPosition(460, 645);
  632. goal.push_back(new CSpriteOval(CVector(460, 50), 20, 20, CColor::LightGray(), GetTime()));
  633. {
  634. CSprite *pStar = new CSpriteOval(CVector(460, 360), 10, CColor::Yellow(), GetTime());
  635. stars.push_back(pStar);
  636. pStar->SetMass(100000);
  637. pStar->SetStatus(stars.size());
  638. }
  639. break;
  640. }
  641. }
  642.  
  643. void CMyGame::OnGameOver()
  644. {
  645. }
  646.  
  647. void CMyGame::OnTerminate()
  648. {
  649. }
  650.  
  651. // Keyboard Event Handlers
  652. void CMyGame::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode)
  653. {
  654. if (sym == SDLK_F4 && (mod & (KMOD_LALT | KMOD_RALT)))
  655. StopGame();
  656. if (sym == SDLK_SPACE)
  657. PauseGame();
  658. if (sym == SDLK_F2)
  659. NewGame();
  660. if (sym == SDLK_BACKSPACE) for (CSprite* pStar : stars) if (pStar->GetMass() == 100000)
  661. {
  662. pStar->Delete();
  663. pStar->SetMass(0);
  664. }
  665. }
  666.  
  667. void CMyGame::OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode)
  668. {
  669. }
  670.  
  671. // Mouse Events Handlers
  672. void CMyGame::OnMouseMove(Uint16 x,Uint16 y,Sint16 relx,Sint16 rely,bool bLeft,bool bRight,bool bMiddle)
  673. {
  674. for (CSprite *pStar : stars)
  675. {
  676. if (pStar->GetStatus() == stars.size())
  677. if (s_Aiming)
  678. pStar->SetPosition(x, y);
  679.  
  680. if (GetLevel() == 3)
  681. LargeBoxFunc(x, y);
  682. else if (GetLevel() != 2) SmallBoxFunc(x, y);
  683. }
  684. }
  685.  
  686. void CMyGame::OnLButtonDown(Uint16 x,Uint16 y)
  687. {
  688. if (GetLevel() == 1 && x > 340 && x < 620 && y < 355 && y > 300)
  689. {
  690. SetLevel(2);
  691. menuchoice.Play("menuchoice.wav");
  692. }
  693. else if (GetLevel() == 1 && x > 340 && x < 620 && y < 270 && y > 215)
  694. {
  695. SetLevel(3);
  696. menuchoice.Play("menuchoice.wav");
  697. }
  698.  
  699. if (GetLevel() == 2 && !GUI.HitTest(x, y)) StarsControl(x, y);
  700. else if (GetLevel() == 3 && box_large.HitTest(x, y) && stars.size() <= 16) StarsControl(x, y);
  701. else if (GetLevel() == 4 && box_small.HitTest(x, y) && stars.size() <= 15) StarsControl(x, y);
  702. else if (GetLevel() == 5 && box_small.HitTest(x, y) && stars.size() <= 15) StarsControl(x, y);
  703. else if (GetLevel() == 6 && box_small.HitTest(x, y) && stars.size() <= 14) StarsControl(x, y);
  704. else if (GetLevel() == 7 && box_small.HitTest(x, y) && stars.size() <= 17) StarsControl(x, y);
  705. else if (GetLevel() == 8 && box_small.HitTest(x, y) && stars.size() <= 15) StarsControl(x, y);
  706. else if (GetLevel() == 9 && box_small.HitTest(x, y) && stars.size() <= 16) StarsControl(x, y);
  707. else if (GetLevel() == 10 && box_small.HitTest(x, y) && stars.size() <= 15) StarsControl(x, y);
  708. if (Gplus.HitTest(x, y))
  709. {
  710. Gplus.SetImage("press");
  711. menuclick.Play("open.wav");
  712. G += 0.5f;
  713. if (G == 0)
  714. G = 0.5;
  715. }
  716. if (Gminus.HitTest(x, y))
  717. {
  718. Gminus.SetImage("press");
  719. menuclick.Play("open.wav");
  720. G -= 0.5f;
  721. if (G == 0)
  722. G = -0.5; // so that G constant never reaches 0 (making the game too easy)
  723. }
  724. if (Tplus.HitTest(x, y))
  725. {
  726. Tplus.SetImage("press");
  727. menuclick.Play("open.wav");
  728. s_Trajectory = true;
  729. }
  730. if (Tminus.HitTest(x, y))
  731. {
  732. Tminus.SetImage("press");
  733. menuclick.Play("open.wav");
  734. s_Trajectory = false;
  735. }
  736. if (GUIbutton.HitTest(x, y) && GUIbool)
  737. {
  738. GUIbutton.SetImage("press");
  739. GUImin.Play("GUImin.wav");
  740. GUIbool = false;
  741. }
  742. else if (GUIbutton.HitTest(x, y) && GUIbool == false)
  743. {
  744. GUIbutton.SetImage("press");
  745. GUImax.Play("GUImax.wav");
  746. GUIbool = true;
  747. }
  748. if (BGMbutton.HitTest(x, y) && BGMbool)
  749. {
  750. BGMbutton.SetImage("press");
  751. menuclick.Play("open.wav");
  752. BGM.Pause();
  753. BGMbool = false;
  754. }
  755. else if (BGMbutton.HitTest(x, y) && BGMbool == false)
  756. {
  757. BGMbutton.SetImage("press");
  758. menuclick.Play("open.wav");
  759. BGM.Resume();
  760. BGMbool = true;
  761. }
  762. }
  763.  
  764. void CMyGame::OnLButtonUp(Uint16 x,Uint16 y)
  765. {
  766. if (IsGameMode())
  767. {
  768. for (CSprite *pStar : stars)
  769. {
  770. if (pStar->GetStatus() == stars.size())
  771. {
  772. if (s_Aiming)
  773. {
  774. pStar->SetVelocity(Normalize(initial_aim - pStar->GetPosition()) * (Distance(pStar->GetPosition(), initial_aim)));
  775. CSprite *pStarclickup = new CSpriteRect(x, y, 7, 7, CColor::White(), GetTime());
  776. starclickup.push_back(pStarclickup);
  777. s_Aiming = false;
  778. }
  779. }
  780. }
  781. BGMbutton.SetImage("release");
  782. GUIbutton.SetImage("release");
  783. Gplus.SetImage("release");
  784. Gminus.SetImage("release");
  785. Tplus.SetImage("release");
  786. Tminus.SetImage("release");
  787. }
  788. }
  789.  
  790. void CMyGame::OnRButtonDown(Uint16 x,Uint16 y)
  791. {
  792. if (IsGameMode())
  793. {
  794. if (GetLevel() == 2)
  795. {
  796. CSprite *pStar = new CSpriteOval(CVector(x, y), 10, CColor::Yellow(), GetTime());
  797. stars.push_back(pStar);
  798. pStar->SetMass(100000);
  799. pStar->SetStatus(stars.size());
  800. for (CSprite *pTrajectory : trajectory) pTrajectory->Delete();
  801. trajectory.clear();
  802. }
  803. }
  804. }
  805.  
  806. void CMyGame::OnRButtonUp(Uint16 x,Uint16 y)
  807. {
  808. }
  809.  
  810. void CMyGame::OnMButtonDown(Uint16 x,Uint16 y)
  811. {
  812. }
  813.  
  814. void CMyGame::OnMButtonUp(Uint16 x,Uint16 y)
  815. {
  816. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement