Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. // Timer's ID, used to run the animations at 30FPS
  6. UINT_PTR IDT_TIMER1;
  7.  
  8. // Resolution.
  9. // Change it if we want to scale up the resolution (or even make adjustable-resolution settings)
  10. short unsigned int ResolutionWidth = 640;
  11. short unsigned int ResolutionHeight = 480;
  12.  
  13. int ObstacleSpawnPos; // Must be off-screen on the right
  14. short unsigned int ObstaclesDelay; // Number of meters [units] between obstacles.
  15. bool playing = true; // is set to false when player dies
  16. bool dying = false;
  17.  
  18. unsigned int nScore = 0; // How many obstacles has player passed?
  19.  
  20. HWND hScore, hWnd; // Window handlers
  21.  
  22. /****************************** GRAPHIC DISPLAY *******************/
  23. // what obstacles should look like - currently unused because I don't know how to display these images properly ;/
  24. // (displaying them as separate window (using CreateWindow()) techinically works, but leads to a lot of random screen flickering)
  25. HBITMAP bmpObstacle = (HBITMAP) LoadImage(NULL, L"tube.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  26. // for obstacle's appearance, use this instead
  27. COLORREF rgbObstacle = RGB(102, 204, 102);
  28. COLORREF rgbPlayer = RGB(225, 0, 0);
  29.  
  30. void SetColor(HDC handler, COLORREF color) {
  31. SelectObject(handler, GetStockObject(DC_PEN));
  32. SetDCPenColor(handler, color);
  33. SelectObject(handler, GetStockObject(DC_BRUSH));
  34. SetDCBrushColor(handler, color);
  35. }
  36.  
  37. /****************************** UPDATING THE SCORE WINDOW *******************/
  38. HWND UpdateScore() {
  39.  
  40. // Convert nScore int to LPCTSTR so it can be displayed as a static window
  41. wchar_t lScore[3];
  42. _itow_s (nScore, lScore, 10);
  43.  
  44. return CreateWindow(L"STATIC", lScore, WS_VISIBLE | WS_CHILD,
  45. ResolutionWidth/2-25, 25, 50, 50, hWnd, NULL, NULL, NULL);
  46. }
  47.  
  48.  
  49. /* Player class. There's only one player so using a class isn't a MUST
  50. but I'd rather have a whole thing self-contained rather than use
  51. bunch of different variables all over the place*/
  52. class Player {
  53. private:
  54. unsigned short int SpawnWidth; // Spawn point (from screen's left side)
  55. unsigned short int FramesInTheAir; // starts increasing after jumping. Player doesn't immediatelly start
  56. // falling down, instead he's steady in the air for couple of seconds before going down
  57.  
  58. void Jump() {
  59. if(CurHeight > Destination) CurHeight-=2;
  60. else {
  61. FramesInTheAir++;
  62. if(FramesInTheAir > 5) { FramesInTheAir = 0; Jumping = false; }
  63. }
  64. }
  65.  
  66.  
  67. public:
  68. int CurHeight; // How far up player jumped (or fell)
  69. int JumptHeight; // How far CAN player jump
  70. int FallSpeed; // How fast player
  71. int Size; // how high & wide the player is
  72. bool Jumping;
  73. int Destination; // Set when player jumps
  74. int DefPosition; // Same as SpawnWidth? Investigate WTF was I thinking
  75.  
  76. Player() {SpawnWidth = 100; Size = 20; Jumping = false; CurHeight = ResolutionHeight/2-Size; JumptHeight=50; DefPosition=50;}
  77.  
  78. void Create(HDC hDC) {
  79.  
  80. // Player is an elipse
  81. SetColor(hDC, rgbPlayer);
  82. Ellipse(hDC, DefPosition, CurHeight, DefPosition+Size, CurHeight+Size);
  83.  
  84. // player fell to the bottom
  85. if(CurHeight+Size*2 == ResolutionHeight) playing = false;
  86.  
  87. if(!Jumping || dying) CurHeight+=2;
  88. else Jump();
  89. }
  90. };
  91.  
  92. // used in game
  93. Player PC;
  94.  
  95.  
  96. // obstacles
  97. class Obstacle {
  98. private:
  99. int Gap;
  100. bool PointGiven; // Has the point for passing this obstacle been awarded yet?
  101.  
  102. // used in Draw()
  103. void Display(int StartHeight, int FullHeight, HDC hDC) {
  104. MoveToEx(hDC, Position, StartHeight, NULL);
  105.  
  106. // Color change
  107. SetColor(hDC, rgbObstacle);
  108.  
  109. // I wrote so much code to draw lines and fill them with
  110. // green, but it turns out you can just use Rectangle() function
  111. // Now, I'm all for showing off complicated code, but sometimes less is more...
  112. // performance seems to agree with me.
  113. Rectangle(hDC, Position, StartHeight, Position+Width, FullHeight);
  114.  
  115. // Check player's position relative to this obstacle
  116. if(((Position >= PC.DefPosition && Position <= PC.DefPosition+PC.Size) ||
  117. (Position+Width >= PC.DefPosition && Position+Width <= PC.DefPosition+PC.Size))
  118. && ((PC.CurHeight < 0) || (PC.CurHeight > StartHeight && PC.CurHeight < FullHeight))) dying = true;
  119. }
  120.  
  121. // triggers when player passes through an obstacle
  122. void Passed() {
  123. nScore++;
  124. hScore = UpdateScore();
  125. PointGiven = true;
  126. }
  127.  
  128. public:
  129. int Position; // position on the world map, going from the right side of the screen to the left
  130. int Height; // Randomized height the object appears at. (10- (ResolutionHeight-150))
  131. // Some obstales should be very low and others very high, but they must be always visible on screen
  132. int nID; // To check how many obstacles there are on screen
  133. int Width; // how wide the obstacle is
  134. bool Real;
  135. bool SpawnedNext;
  136.  
  137.  
  138. Obstacle() { // Constructor
  139. Position = ResolutionWidth + 50;
  140. Width = 30;
  141. Gap = PC.Size*4;
  142. PointGiven = false;
  143. Real = true;
  144. SpawnedNext = false;
  145.  
  146. srand (time(NULL));
  147. Height = rand() % 32 + 10;
  148. Height*=10;
  149. }
  150.  
  151. Obstacle(bool real) {
  152. Position = ResolutionWidth + 200;
  153. Real = false; // yeah, make it always false, because if we wanted a real one, we wouldnt put a bool parameter in there in the first place. Not-Real basically means that it won't appear.
  154. }
  155.  
  156. // Creates an obstacle visible in game
  157. void Draw(HDC hDC) {
  158. Display(0, Height, hDC);
  159. Display(Height+Gap, ResolutionHeight, hDC);
  160. if(!PointGiven && Position+Width/2 < PC.DefPosition) Passed();
  161. }
  162.  
  163.  
  164. };
  165.  
  166. /* Should actually be Screen.height/(Obstacle.width + SpawnDelay),
  167. but until I include support for more resolutions, "5" is a perfectly
  168. adequate solution too*/
  169. Obstacle aObstacles[5];
  170.  
  171.  
  172.  
  173. /****************************** ALL THE CONTROLS FOR THE OBSTACLES *******************/
  174.  
  175. void SpawnNextObstacle() {
  176. for(int i=0; i<sizeof(aObstacles)/sizeof(*aObstacles); i++) {
  177. if(!aObstacles[i].Real) {
  178. aObstacles[i] = Obstacle();
  179. break;
  180. }
  181. }
  182. }
  183.  
  184. void MovingObstacles() {
  185. if(dying) return;
  186.  
  187. for(int i=0; i<sizeof(aObstacles)/sizeof(*aObstacles); i++) {
  188. if(aObstacles[i].Real) {
  189. // Move an obstacle to the right
  190. aObstacles[i].Position--;
  191. if(aObstacles[i].Position + aObstacles[i].Width < 0) aObstacles[i] = Obstacle(false);
  192.  
  193. // Spawn the next obstacle if this one's been on screen long enough
  194. if(aObstacles[i].Position < ResolutionWidth-ObstaclesDelay && !aObstacles[i].SpawnedNext) {
  195. aObstacles[i].SpawnedNext = true;
  196. SpawnNextObstacle();
  197. break;
  198. }
  199. }
  200. }
  201. }
  202.  
  203. void DrawObstacles(HDC hDC) {
  204. for(int i=0; i<sizeof(aObstacles)/sizeof(*aObstacles); i++) {
  205. if(aObstacles[i].Real) aObstacles[i].Draw(hDC);
  206. }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement