ravagetalon

Untitled

Jun 16th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. //*************************************************
  2. // Michael Ametrano
  3. // IT102
  4. //*************************************************
  5.  
  6. #include <windows.h>
  7. #include <string>
  8. #include <stdio.h>
  9. using namespace std;
  10.  
  11. const char g_szClassName[] = "myWindowClass";
  12. const int ID_TIMER = 1;
  13. const int BALL_MOVE_DELTA = 6; //ball speed
  14. const int PADDLE_MOVE_DELTA = 3;
  15.  
  16.  
  17. typedef struct _BALLINFO
  18. {
  19. int width;
  20. int height;
  21. int x;
  22. int y;
  23.  
  24. int dx;
  25. int dy;
  26. }BALLINFO;
  27.  
  28. BALLINFO g_ballInfo;
  29.  
  30. typedef struct _PADDLEINFO
  31. {
  32. int width;
  33. int height;
  34. int x;
  35. int y;
  36.  
  37. int dx;
  38. int dy;
  39. }PADDLEINFO;
  40.  
  41. PADDLEINFO g_leftPaddleInfo, g_rightPaddleInfo;
  42.  
  43. TCHAR buff[10];
  44. int sOne = 0;
  45. int sTwo = 0;
  46.  
  47. string message = " - ";
  48.  
  49. /* Score tracking */
  50. //string scoreOne = sOne;
  51. //string scoreTwo = sTwo;
  52.  
  53. /* crazy hack attempt to fix score displaying */
  54. /*char scoreOne[1];
  55. sprintf_s(scoreOne, sizeof(scoreOne) "%d", sOne);*/
  56.  
  57. void DrawBall(HDC hdc, RECT* prc)
  58. {
  59.  
  60. HBRUSH blueBrush=CreateSolidBrush(RGB(0,0,255));
  61. HGDIOBJ old=SelectObject(hdc,blueBrush);
  62.  
  63. Ellipse(hdc, g_ballInfo.x, g_ballInfo.y, g_ballInfo.x+g_ballInfo.width, g_ballInfo.y+g_ballInfo.height);
  64.  
  65. DeleteObject(blueBrush);
  66. SelectObject(hdc,old);
  67. }
  68.  
  69. void DrawLeftPaddle(HDC hdc, RECT* prc)
  70. {
  71. HBRUSH redBrush=CreateSolidBrush(RGB(255,0,0));
  72. HGDIOBJ old=SelectObject(hdc,redBrush);
  73.  
  74. Rectangle(hdc, g_leftPaddleInfo.x, g_leftPaddleInfo.y, g_leftPaddleInfo.x + g_leftPaddleInfo.width, g_leftPaddleInfo.y+g_leftPaddleInfo.height);
  75.  
  76. DeleteObject(redBrush);
  77. SelectObject(hdc,old);
  78. }
  79. void DrawRightPaddle(HDC hdc, RECT* prc)
  80. {
  81. HBRUSH redBrush=CreateSolidBrush(RGB(255,0,0));
  82. HGDIOBJ old=SelectObject(hdc,redBrush);
  83.  
  84. Rectangle(hdc, g_rightPaddleInfo.x, g_rightPaddleInfo.y, g_rightPaddleInfo.x + g_rightPaddleInfo.width, g_rightPaddleInfo.y+g_rightPaddleInfo.height);
  85.  
  86. DeleteObject(redBrush);
  87. SelectObject(hdc,old);
  88. }
  89.  
  90. void UpdateBall(RECT* prc)
  91. {
  92. g_ballInfo.x += g_ballInfo.dx;
  93. g_ballInfo.y += g_ballInfo.dy;
  94.  
  95. if((g_ballInfo.x < 30) && (g_ballInfo.y > g_leftPaddleInfo.y && g_ballInfo.y < g_leftPaddleInfo.y+60))
  96. {
  97. g_ballInfo.x = 30;
  98. g_ballInfo.dx = BALL_MOVE_DELTA;
  99. }
  100.  
  101. if((g_ballInfo.x > 580) && (g_ballInfo.y > g_rightPaddleInfo.y && g_ballInfo.y < g_rightPaddleInfo.y+60))
  102. {
  103. g_ballInfo.x = 580;
  104. g_ballInfo.dx = -BALL_MOVE_DELTA;
  105. }
  106.  
  107.  
  108. if(g_ballInfo.x < 0)
  109. {
  110. g_ballInfo.x = 0;
  111. g_ballInfo.dx = BALL_MOVE_DELTA;
  112. }
  113. else if(g_ballInfo.x + g_ballInfo.width > prc->right)
  114. {
  115. g_ballInfo.x = prc->right - g_ballInfo.width;
  116. g_ballInfo.dx = -BALL_MOVE_DELTA;
  117. }
  118.  
  119. if(g_ballInfo.y < 0)
  120. {
  121. g_ballInfo.y = 0;
  122. g_ballInfo.dy = BALL_MOVE_DELTA;
  123. }
  124. else if(g_ballInfo.y + g_ballInfo.height > prc->bottom)
  125. {
  126. g_ballInfo.y = prc->bottom - g_ballInfo.height;
  127. g_ballInfo.dy = -BALL_MOVE_DELTA;
  128. }
  129. }
  130.  
  131. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  132. {
  133. switch(msg)
  134. {
  135. case WM_CREATE:
  136. {
  137. UINT_PTR ret; //unsigned int to hold result of timer creation
  138.  
  139. g_leftPaddleInfo.width = 10;
  140. g_leftPaddleInfo.height = 60;
  141.  
  142. g_leftPaddleInfo.x = 20;
  143. g_leftPaddleInfo.y = 100;
  144.  
  145. g_rightPaddleInfo.width = 10;
  146. g_rightPaddleInfo.height = 60;
  147.  
  148. g_rightPaddleInfo.x = 590;
  149. g_rightPaddleInfo.y = 100;
  150.  
  151. g_ballInfo.width = 10;
  152. g_ballInfo.height = 10;
  153.  
  154. g_ballInfo.x = 300;
  155.  
  156. g_ballInfo.dx = BALL_MOVE_DELTA;
  157. g_ballInfo.dy = BALL_MOVE_DELTA;
  158.  
  159. ret = SetTimer(hwnd, ID_TIMER, 20, NULL); // 20 milliseconds
  160. if(ret == 0)
  161. MessageBox(hwnd, "Could not SetTimer()!", "Error", MB_OK | MB_ICONEXCLAMATION);
  162.  
  163. }
  164. break;
  165. case WM_CLOSE:
  166. DestroyWindow(hwnd);
  167. break;
  168. case WM_PAINT:
  169. {
  170. RECT rcClient; //struct to hold rectangle coordinates
  171. PAINTSTRUCT ps; //struct representing client area of window
  172. HDC hdc = BeginPaint(hwnd, &ps);
  173.  
  174. GetClientRect(hwnd, &rcClient); //get coordinates of client area of window
  175.  
  176. DrawBall(hdc, &rcClient);
  177.  
  178. EndPaint(hwnd, &ps);
  179. }
  180. break;
  181. case WM_TIMER:
  182. {
  183. if(g_leftPaddleInfo.y < 1)
  184. {
  185.  
  186. }
  187. else if (GetAsyncKeyState(VK_UP) < 0)
  188. {
  189. g_leftPaddleInfo.y-=6;
  190. }
  191. if(g_leftPaddleInfo.y > 380)
  192. {
  193.  
  194. }
  195. else if(GetAsyncKeyState(VK_DOWN) < 0)
  196. {
  197. g_leftPaddleInfo.y+=6;
  198. }
  199.  
  200. /* Computer Paddle Control (Perfect. Imperfect not implemented) */
  201. g_rightPaddleInfo.y = g_ballInfo.y-10;
  202.  
  203. /* score updating */
  204. /*if (g_ballInfo.x < 20)
  205. {
  206. sTwo++;
  207. }
  208. if (g_ballInfo.x > 590)
  209. {
  210. sOne++;
  211. }*/
  212.  
  213. /* trivial Score detection */
  214. if (g_ballInfo.x == 0)
  215. {
  216. sTwo++;
  217. }
  218.  
  219. if (g_ballInfo.x == 640)
  220. {
  221. sOne++;
  222. }
  223.  
  224.  
  225. RECT rcClient; //struct to hold rectangle coordinates
  226. HDC hdc = GetDC(hwnd); //grab a handle to the window (Device Context)
  227.  
  228. GetClientRect(hwnd, &rcClient); //get coordinates of client area of window
  229. // and store them in rcClient
  230. UpdateBall(&rcClient); //calculate next ball position
  231.  
  232. FillRect(hdc, &rcClient, (HBRUSH)GetStockObject(WHITE_BRUSH));
  233.  
  234. TextOut(hdc,20, 20, buff, wsprintf(buff, "%d", sOne));
  235. TextOut(hdc,500, 20, buff, wsprintf(buff, "%d", sTwo));
  236.  
  237.  
  238. DrawBall(hdc, &rcClient); //clear screan and draw ball in new position
  239. DrawLeftPaddle(hdc, &rcClient);
  240. DrawRightPaddle(hdc, &rcClient);
  241.  
  242. ReleaseDC(hwnd, hdc); //release the handle to the window (Device Context)
  243. }
  244. break;
  245.  
  246.  
  247.  
  248. case WM_KEYDOWN:
  249. /* if(wParam == VK_DOWN) // try this way instead - See the problem?
  250. y+=6;
  251. if(wParam == VK_UP)
  252. y-=6;
  253. */
  254. break;
  255. case WM_KEYUP:
  256.  
  257. break;
  258. case WM_DESTROY:
  259. KillTimer(hwnd, ID_TIMER);
  260. PostQuitMessage(0);
  261. break;
  262. default:
  263. return DefWindowProc(hwnd, msg, wParam, lParam);
  264. }
  265. return 0;
  266. }
  267.  
  268. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  269. LPSTR lpCmdLine, int nCmdShow)
  270. {
  271. WNDCLASSEX wc;
  272. HWND hwnd;
  273. MSG Msg;
  274.  
  275. wc.cbSize = sizeof(WNDCLASSEX);
  276. wc.style = 0;
  277. wc.lpfnWndProc = WndProc;
  278. wc.cbClsExtra = 0;
  279. wc.cbWndExtra = 0;
  280. wc.hInstance = hInstance;
  281. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  282. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  283. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  284. wc.lpszMenuName = NULL;
  285. wc.lpszClassName = g_szClassName;
  286. wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  287.  
  288. if(!RegisterClassEx(&wc))
  289. {
  290. MessageBox(NULL, "Window Registration Failed!", "Error!",
  291. MB_ICONEXCLAMATION | MB_OK);
  292. return 0;
  293. }
  294.  
  295. hwnd = CreateWindowEx(
  296. WS_EX_CLIENTEDGE,
  297. g_szClassName,
  298. "PONG!",
  299. WS_OVERLAPPEDWINDOW,
  300. CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
  301. NULL, NULL, hInstance, NULL);
  302.  
  303. if(hwnd == NULL)
  304. {
  305. MessageBox(NULL, "Window Creation Failed!", "Error!",
  306. MB_ICONEXCLAMATION | MB_OK);
  307. return 0;
  308. }
  309.  
  310. ShowWindow(hwnd, nCmdShow);
  311. UpdateWindow(hwnd);
  312.  
  313. while(GetMessage(&Msg, NULL, 0, 0) > 0)
  314. {
  315. TranslateMessage(&Msg);
  316. DispatchMessage(&Msg);
  317. }
  318. return Msg.wParam;
  319. }
Advertisement
Add Comment
Please, Sign In to add comment