decna12255

review of code

Feb 22nd, 2012
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1. below lies the main function for my metroid version of pong.
  2.  
  3. MAIN FUNCTION<><><><><><><><><>
  4.  
  5. ///SDL_App.cpp : Defines the entry point for our Project
  6. //
  7. //#include "stdafx.h"
  8. #include <Windows.h>
  9. #include "GL_Functions.h"
  10. #include "pctimer.h"
  11. #include <string>
  12. #include <cmath>
  13. #include <stdio.h>
  14. #include <crtdbg.h>
  15.  
  16.  
  17. int main(int argc, char* argv[])
  18. {
  19.  
  20. //Lets open the window and initialise opengl
  21. InitGL(1024,768);
  22. int background = LoadTexture ("./images/background.png");
  23. int mball = LoadTexture ("./images/mball.png");
  24. int leftpaddle_2 = LoadTexture ("./images/paddle_2.png");
  25. int rightpaddle_1 = LoadTexture ("./images/paddle_1.png");
  26. //int menu = LoadTexture ("./images/menu.png");
  27.  
  28.  
  29. //variables
  30. // 'M' stand for metroid....
  31. float backgroundx = 0;
  32. float backgroundy = 0;
  33. float mballx = 492;
  34. float mbally = 384;
  35. float ballxspeed = 4;
  36. float ballyspeed = 2;
  37. float rightpaddle_x = 990;
  38. float rightpaddle_y = 20;
  39. float leftpaddle_x = 15;
  40. float leftpaddle_y = 450;
  41. float ballrside = mballx + 40;
  42. float position = 0;
  43. float paddle_width = 5;
  44. float paddle_length = 15;
  45. float ball_width = 2.5;
  46. float ball_length = 5;
  47. //float menu = 0;
  48.  
  49.  
  50. do
  51. {
  52. //Clear the screen, so previous frames don't build up
  53. ClearScreen();
  54.  
  55. mballx = mballx + ballxspeed;
  56. mbally = mbally + ballyspeed;
  57.  
  58.  
  59. //////////draws images to screen////////////////////////////////////////////////////
  60. DrawSprite(background, backgroundx, backgroundy, 1024, 768); //////////////
  61. DrawLine(512,0,512,768); //////////////
  62. DrawSprite(mball, mballx, mbally, 40, 40); //////////////
  63. DrawSprite(leftpaddle_2, leftpaddle_x, leftpaddle_y, 20, 100); ////////////
  64. DrawSprite(rightpaddle_1, rightpaddle_x, rightpaddle_y, 20, 100); //////////
  65. ////////////////////////////////////////////////////////////////////////////////////
  66.  
  67.  
  68. //highscores
  69. //{
  70. // int iHighscore[6}
  71. // int iScore = 0
  72. // char scorestring[] = "score"
  73. // while (iscore < 6)
  74. // printf("Highscore: %i %i/n"), (iScore+1), iHighscore[iScore]);
  75. // ++iScore;
  76. // for{
  77. // (int iHighscore = 0;, iHighscore < 10, iHighscore++)
  78. // printf(
  79. // }
  80. //}
  81.  
  82.  
  83. // ball speed/control
  84.  
  85. if (mballx > 984)
  86. {
  87. mballx = 492;
  88. mbally = 384;
  89. ballxspeed *= -1;
  90. }
  91.  
  92. if ( mballx < 0)
  93. {
  94. mballx = 492;
  95. mbally = 384;
  96. ballxspeed *= -1;
  97. }
  98.  
  99. if (mbally > 1024 || mbally < 0)
  100. ballyspeed *= -.5;
  101.  
  102. // controls for players one and two
  103. // player one's controls ( 'w' and 's')
  104. // player two's controls ( 'up' and 'down')
  105.  
  106.  
  107. if (IsKeyDown(KEY_UP))
  108. {
  109. rightpaddle_y = rightpaddle_y -5;
  110. if( rightpaddle_y < 0 )
  111. {
  112. rightpaddle_y = 0;
  113. }
  114. }
  115.  
  116. if (IsKeyDown(KEY_DOWN) && rightpaddle_y < 668)
  117. rightpaddle_y = rightpaddle_y +5;
  118.  
  119. if (IsKeyDown('w') && leftpaddle_x > 0)
  120. leftpaddle_y = leftpaddle_y -5;
  121.  
  122. if (IsKeyDown('s') && leftpaddle_x < 668)
  123. leftpaddle_y = leftpaddle_y +5;
  124.  
  125. // closes program down atfer user presses escape!
  126. if (IsKeyDown(KEY_ESCAPE))
  127. CloseDown();
  128.  
  129.  
  130. //limit paddles so that stay with in boundaries placed
  131. //well they would if i could program
  132. {
  133. if(leftpaddle_y < 0)
  134. leftpaddle_y = 0;
  135.  
  136. if(leftpaddle_y + paddle_width > 668)
  137. leftpaddle_y = 668 - paddle_width;
  138.  
  139. if(rightpaddle_y + paddle_width > 668)
  140. rightpaddle_y = 668 - paddle_width;
  141. }
  142. //ball impact values below
  143. //makes ball go bouncy bouncy
  144. //collision is ment to be here...
  145. {
  146. if(rightpaddle_x >= mballx || rightpaddle_x <= (mballx + ball_width))
  147.  
  148. if(ball_length + ball_width > paddle_width + paddle_length)
  149. mballx,mbally = 0;
  150. }
  151.  
  152. {
  153. if(rightpaddle_x >= mballx && rightpaddle_x <= (mballx + ball_width) ||
  154. (rightpaddle_x + paddle_width ) >= mballx && (rightpaddle_x + paddle_width ) <= (mballx + ball_width))
  155.  
  156. if(rightpaddle_y >= mbally && rightpaddle_y <= ( mbally + ball_length) ||
  157. (rightpaddle_y + paddle_length) >= mbally && (rightpaddle_y + paddle_length) <= ( mbally +ball_length))
  158. //The ball and paddle appear to overlap.
  159. return true;
  160. //The ball and the paddle appear to NOT overlap:
  161. return false;
  162. }
  163.  
  164. //these do stuff
  165. //without them game crashes....no idea why
  166. //fiddling around with collision and these were results
  167. {
  168. while(mballx > rightpaddle_x + rightpaddle_y)
  169.  
  170. if(mballx = leftpaddle_x - position && leftpaddle_y > ball_width)
  171.  
  172. if(mbally = rightpaddle_x + position && rightpaddle_x > ball_width)
  173. return(0);
  174. break;
  175. }
  176. //Stop it from running too fast! Sleep ZZzzz
  177. Sleep(5);
  178.  
  179. {
  180. while (FrameworkUpdate()); //Do some secret stuff,
  181.  
  182. FreeTexture(leftpaddle_2);
  183. FreeTexture(rightpaddle_1);
  184. FreeTexture(background);
  185. FreeTexture(mball);
  186. //FreeTexture(menu);
  187. }
  188.  
  189.  
  190. {
  191. //Close down
  192. CloseDown();
  193. _CrtDumpMemoryLeaks();
  194. //Quit!
  195. return(0);
  196. }
  197.  
  198.  
  199.  
  200. }
  201.  
  202.  
  203.  
  204.  
  205.  
  206. menu functionsss::::
  207. and here is the menu function which doesn't work yet but thats for another day.
  208.  
  209. // SDL_App.cpp : Defines the entry point for our Project
  210. //
  211. //#include "stdafx.h"
  212. #include <Windows.h>
  213. #include "GL_Functions.h"
  214. #include "pctimer.h"
  215. #include <string>
  216. #include <cmath>
  217. #include <stdio.h>
  218. #include <crtdbg.h>
  219.  
  220. enum STATE
  221. {
  222. MAINMENU, // will take user to main menu at start of game or during
  223. SINGLEPLAYER, // one player mode
  224. MULTIPLAYER, // player vs player mode
  225. HIGHSCORES, //lists high score
  226. INSTRUCTIONS, // opens the options menu
  227. QUIT // when user presses escape key program closes
  228. };
  229.  
  230.  
  231. STATE currentstate;
  232.  
  233. void main(int argc, char* argv[])
  234. {
  235. //Opens the window
  236. InitGL(1024, 768);
  237.  
  238. //The selectors texture load and X,Y values
  239. int Arrow = LoadTexture("./images/arrow.png");
  240. int ArrowY = 250;
  241. int Arrowx = 150;
  242. float Rot = 0;
  243.  
  244.  
  245. const int MAINMENU = 1;
  246. const int SINGLEPLAYER = 2;
  247.  
  248.  
  249.  
  250. ////////////////////////////////
  251. //MAINMENU SWITCHING
  252. {
  253. currentstate = MAINMENU;
  254. bool bcontinueGame = true;
  255.  
  256. while (bcontinueGame)
  257. {
  258. switch(currentstate)
  259. {
  260. case MAINMENU:
  261. bcontinueGame = MAINMENU;
  262. case SINGLEPLAYER:
  263. bcontinueGame = SINGLEPLAYER;
  264. case QUIT:
  265. break;
  266. default:
  267. break;
  268. };
  269. }
  270. }
  271.  
  272.  
  273. do
  274.  
  275. {
  276. //Clear Screen
  277. ClearScreen();
  278.  
  279. //Headings and menu items///////////////////////////////////////////////////
  280. DrawString("METROID PONG", 64, 64, 5.0f); ////////////////////////////////
  281. DrawString("1. Singleplayer", 64, 256, 3.0f); /////////////////////////////
  282. DrawString("2. Multiplayer", 64, 384, 3.0f); //////////////////////////////
  283. DrawString("3. Instructions", 64, 400, 3.0f); ////////////////////////////
  284. DrawString("4. High Scores", 64, 512, 3.0f); /////////////////////////////
  285. DrawString("5. Exit", 64, 640, 3.0f); /////////////////////////////
  286. ////////////////////////////////////////////////////////////////////////////
  287.  
  288.  
  289. //The arrow
  290. DrawSpriteRotated(Arrow, 32, ArrowY, 32, 32, Rot += 200.0f);
  291.  
  292. //Moving the arrow
  293. if (IsKeyDown(KEY_DOWN))
  294. {
  295. ArrowY = ArrowY + 100;
  296. }
  297.  
  298. if (ArrowY > 700)
  299. {
  300. ArrowY = 250;
  301. }
  302.  
  303. if (IsKeyDown(KEY_UP))
  304. {
  305. ArrowY = ArrowY - 100;
  306. }
  307.  
  308. if (ArrowY < 255)
  309. {
  310. ArrowY = 250;
  311. }
  312.  
  313.  
  314.  
  315.  
  316. //Slows the game
  317. Sleep(60);
  318. }
  319.  
  320. //Do some secret stuff
  321. while (FrameworkUpdate());
  322.  
  323. //Clean up after yourself
  324. FreeTexture(Arrow);
  325.  
  326. //Close down
  327. CloseDown();
  328.  
  329. _CrtDumpMemoryLeaks();
  330. }
Advertisement
Add Comment
Please, Sign In to add comment