Advertisement
Guest User

Untitled

a guest
Aug 17th, 2010
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.59 KB | None | 0 0
  1. // Take and Run.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Take and Run.h"
  6.  
  7. #define MAX_LOADSTRING 100
  8.  
  9. // Global Variables:
  10. HINSTANCE hInst; // current instance
  11. TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  12. TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
  13. WCHAR str[255] = L"Score: 0"; // score containor
  14. WCHAR level[255] = L"Level: 1"; // level containor
  15. const UINT_PTR BALL1_ID = 0x1;
  16. const UINT_PTR BALL2_ID = 0x3;
  17. const UINT_PTR BALL3_ID = 0x4;
  18. const UINT_PTR BALL4_ID = 0x5;
  19. const UINT_PTR BALL5_ID = 0x6;
  20. const UINT_PTR BALL6_ID = 0x7;
  21. const UINT_PTR BALL7_ID = 0x8;
  22. const UINT_PTR BALL8_ID = 0x9;
  23. const UINT_PTR BALL9_ID = 0x10;
  24. const UINT_PTR BALL10_ID = 0x11;
  25. const UINT_PTR SCORECOUNTER_ID = 0x12;
  26. int life = 0; // life
  27. int scorecounter = 0; // score counter
  28. RECT *rect;
  29. POINT pt;
  30.  
  31. // Forward declarations of functions included in this code module:
  32. ATOM MyRegisterClass(HINSTANCE hInstance);
  33. BOOL InitInstance(HINSTANCE, int);
  34. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  35. INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  36.  
  37. struct balls {
  38. int Ex;
  39. int Ey;
  40. };
  41. balls ball1 = {-26, 25};
  42. balls ball2 = {300, 50};
  43. balls ball3 = {-26, 200};
  44. balls ball4 = {-26, 250};
  45. balls ball5 = {-26, 100};
  46. balls ball6 = {300, 70};
  47. balls ball7 = {-26, 30};
  48. balls ball8 = {-26, 200};
  49. balls ball9 = {300, 250};
  50. balls ball10 = {300, 100};
  51. VOID Level1(HWND hWnd)
  52. {
  53. int i=0;
  54. SetTimer(hWnd, BALL1_ID, 1, NULL);
  55. SetTimer(hWnd, BALL2_ID, 2, NULL);
  56. SetTimer(hWnd, BALL3_ID, 3, NULL);
  57. SetTimer(hWnd, BALL4_ID, 4, NULL);
  58. SetTimer(hWnd, BALL5_ID, 5, NULL);
  59. SetTimer(hWnd, BALL6_ID, 6, NULL);
  60. SetTimer(hWnd, BALL7_ID, 7, NULL);
  61. SetTimer(hWnd, BALL8_ID, 8, NULL);
  62. SetTimer(hWnd, BALL9_ID, 9, NULL);
  63. SetTimer(hWnd, BALL10_ID, 10, NULL);
  64. SetTimer(hWnd, SCORECOUNTER_ID, 1000, NULL);
  65. }
  66. VOID OnPaint(HDC hdc,HWND hWnd)
  67. {
  68. FontFamily fontfamily(L"Times New Roman");
  69. SolidBrush red(Color(255, 0, 0));
  70. SolidBrush green(Color(0, 255, 0));
  71. SolidBrush blue(Color(0, 0, 255));
  72. if(life==0)
  73. {
  74. hdc=GetDC(hWnd);
  75. HDC memDC=CreateCompatibleDC(hdc);
  76. HBITMAP hMemMap=CreateCompatibleBitmap(hdc, 300, 300);
  77. HBITMAP hOldMap=(HBITMAP)SelectObject(memDC, hMemMap);
  78.  
  79. // Drawing
  80. Graphics draw(memDC);
  81. draw.FillRectangle(&green, 0, 0, 300, 25);
  82. Font font(&fontfamily, 14, 0, UnitPixel);
  83. PointF p1(180.0f, 4.0f);
  84. PointF p2(30.0f, 4.0f);
  85. draw.DrawString(level, -1, &font, p2, &blue);
  86. draw.DrawString(str, -1, &font, p1, &blue);
  87. draw.FillEllipse(&red, ball1.Ex, ball1.Ey, 25, 25);
  88. draw.FillEllipse(&red, ball2.Ex, ball2.Ey, 25, 25);
  89. draw.FillEllipse(&red, ball3.Ex, ball3.Ey, 25, 25);
  90. draw.FillEllipse(&red, ball4.Ex, ball4.Ey, 25, 25);
  91. draw.FillEllipse(&red, ball5.Ex, ball5.Ey, 25, 25);
  92. draw.FillEllipse(&red, ball6.Ex, ball6.Ey, 25, 25);
  93. draw.FillEllipse(&red, ball7.Ex, ball7.Ey, 25, 25);
  94. draw.FillEllipse(&red, ball8.Ex, ball8.Ey, 25, 25);
  95. draw.FillEllipse(&red, ball9.Ex, ball9.Ey, 25, 25);
  96. draw.FillEllipse(&red, ball10.Ex, ball10.Ey, 25, 25);
  97. BitBlt(hdc, 0, 0, 300, 300, memDC, 0, 0, SRCCOPY);
  98. SelectObject(memDC, hOldMap);
  99. DeleteObject(hMemMap);
  100. DeleteDC(memDC);
  101. }
  102. else
  103. {
  104. hdc=GetDC(hWnd);
  105. HDC memDC=CreateCompatibleDC(hdc);
  106. HBITMAP hMemMap=CreateCompatibleBitmap(hdc, 300, 300);
  107. HBITMAP hMemOld=(HBITMAP)SelectObject(memDC, hMemMap);
  108.  
  109. // Drawing
  110. Graphics draw(memDC);
  111. Font font(&fontfamily, 44, 0, UnitPixel);
  112. PointF PointF(100.0f, 100.0f);
  113. draw.DrawString(L"You Lost!", -1, &font, PointF, &blue);
  114.  
  115. BitBlt(hdc, 0, 0, 300, 300, memDC, 0, 0, SRCCOPY);
  116. SelectObject(memDC, hMemOld);
  117. DeleteObject(hMemMap);
  118. DeleteDC(memDC);
  119. }
  120. }
  121. int APIENTRY _tWinMain(HINSTANCE hInstance,
  122. HINSTANCE hPrevInstance,
  123. LPTSTR lpCmdLine,
  124. int nCmdShow)
  125. {
  126. UNREFERENCED_PARAMETER(hPrevInstance);
  127. UNREFERENCED_PARAMETER(lpCmdLine);
  128. GdiplusStartupInput gdiplusStartupInput;
  129. ULONG_PTR gdiplusToken;
  130.  
  131. // TODO: Place code here.
  132. MSG msg;
  133. HACCEL hAccelTable;
  134.  
  135. // Initialize GDI+.
  136. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  137.  
  138. // Initialize global strings
  139. LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  140. LoadString(hInstance, IDC_TAKEANDRUN, szWindowClass, MAX_LOADSTRING);
  141. MyRegisterClass(hInstance);
  142.  
  143. // Perform application initialization:
  144. if (!InitInstance (hInstance, nCmdShow))
  145. {
  146. return FALSE;
  147. }
  148.  
  149. hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TAKEANDRUN));
  150.  
  151. // Main message loop:
  152. while (GetMessage(&msg, NULL, 0, 0))
  153. {
  154. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  155. {
  156. TranslateMessage(&msg);
  157. DispatchMessage(&msg);
  158. }
  159. }
  160.  
  161. return (int) msg.wParam;
  162. }
  163.  
  164.  
  165.  
  166. //
  167. // FUNCTION: MyRegisterClass()
  168. //
  169. // PURPOSE: Registers the window class.
  170. //
  171. // COMMENTS:
  172. //
  173. // This function and its usage are only necessary if you want this code
  174. // to be compatible with Win32 systems prior to the 'RegisterClassEx'
  175. // function that was added to Windows 95. It is important to call this function
  176. // so that the application will get 'well formed' small icons associated
  177. // with it.
  178. //
  179. ATOM MyRegisterClass(HINSTANCE hInstance)
  180. {
  181. WNDCLASSEX wcex;
  182.  
  183. wcex.cbSize = sizeof(WNDCLASSEX);
  184.  
  185. wcex.style = CS_HREDRAW | CS_VREDRAW;
  186. wcex.lpfnWndProc = WndProc;
  187. wcex.cbClsExtra = 0;
  188. wcex.cbWndExtra = 0;
  189. wcex.hInstance = hInstance;
  190. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TAKEANDRUN));
  191. wcex.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(110));
  192. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  193. wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TAKEANDRUN);
  194. wcex.lpszClassName = szWindowClass;
  195. wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  196.  
  197. return RegisterClassEx(&wcex);
  198. }
  199.  
  200. //
  201. // FUNCTION: InitInstance(HINSTANCE, int)
  202. //
  203. // PURPOSE: Saves instance handle and creates main window
  204. //
  205. // COMMENTS:
  206. //
  207. // In this function, we save the instance handle in a global variable and
  208. // create and display the main program window.
  209. //
  210. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  211. {
  212. HWND hWnd;
  213.  
  214. hInst = hInstance; // Store instance handle in our global variable
  215.  
  216. hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  217. CW_USEDEFAULT, 0, 300, 300, NULL, NULL, hInstance, NULL);
  218.  
  219. if (!hWnd)
  220. {
  221. return FALSE;
  222. }
  223. // set timer
  224. Level1(hWnd);
  225.  
  226. ShowWindow(hWnd, nCmdShow);
  227. UpdateWindow(hWnd);
  228.  
  229. return TRUE;
  230. }
  231.  
  232. //
  233. // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  234. //
  235. // PURPOSE: Processes messages for the main window.
  236. //
  237. // WM_COMMAND - process the application menu
  238. // WM_PAINT - Paint the main window
  239. // WM_DESTROY - post a quit message and return
  240. //
  241. //
  242. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  243. {
  244. int wmId, wmEvent;
  245. PAINTSTRUCT ps;
  246. HDC hdc;
  247. GetCursorPos(&pt);
  248. ScreenToClient(hWnd,&pt);
  249.  
  250. switch (message)
  251. {
  252. case WM_COMMAND:
  253. wmId = LOWORD(wParam);
  254. wmEvent = HIWORD(wParam);
  255. // Parse the menu selections:
  256. switch (wmId)
  257. {
  258. case IDM_ABOUT:
  259. DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  260. break;
  261. case IDM_EXIT:
  262. DestroyWindow(hWnd);
  263. break;
  264. default:
  265. return DefWindowProc(hWnd, message, wParam, lParam);
  266. }
  267. break;
  268. case WM_PAINT:
  269. hdc = BeginPaint(hWnd, &ps);
  270. OnPaint(hdc, hWnd);
  271. EndPaint(hWnd, &ps);
  272. break;
  273. case WM_MOUSEMOVE:
  274. if(pt.x>=ball1.Ex&&pt.x<=(ball1.Ex+25)&&pt.y<=ball1.Ey&&pt.y>=(ball1.Ey+25))
  275. {
  276. life=1;
  277. InvalidateRect(hWnd, rect, false);
  278. }
  279. else if(pt.x>=ball2.Ex&&pt.x<=(ball2.Ex+25)&&pt.y>=ball2.Ey&&pt.y<=(ball2.Ey+25))
  280. {
  281. life=1;
  282. InvalidateRect(hWnd, rect, false);
  283. }
  284. else if(pt.x>=ball3.Ex&&pt.x<=(ball3.Ex+25)&&pt.y>=ball3.Ey&&pt.y<=(ball3.Ey+25))
  285. {
  286. life=1;
  287. InvalidateRect(hWnd, rect, false);
  288. }
  289. else if(pt.x>=ball4.Ex&&pt.x<=(ball4.Ex+25)&&pt.y>=ball4.Ey&&pt.y<=(ball4.Ey+25))
  290. {
  291. life=1;
  292. InvalidateRect(hWnd, rect, false);
  293. }
  294. else if(pt.x>=ball5.Ex&&pt.x<=(ball5.Ex+25)&&pt.y>=ball5.Ey&&pt.y<=(ball5.Ey+25))
  295. {
  296. life=1;
  297. InvalidateRect(hWnd, rect, false);
  298. }
  299. else if(pt.x>=ball6.Ex&&pt.x<=(ball6.Ex+25)&&pt.y>=ball6.Ey&&pt.y<=(ball6.Ey+25))
  300. {
  301. life=1;
  302. InvalidateRect(hWnd, rect, false);
  303. }
  304. else if(pt.x>=ball7.Ex&&pt.x<=(ball7.Ex+25)&&pt.y>=ball7.Ey&&pt.y<=(ball7.Ey+25))
  305. {
  306. life=1;
  307. InvalidateRect(hWnd, rect, false);
  308. }
  309. else if(pt.x>=ball8.Ex&&pt.x<=(ball8.Ex+25)&&pt.y>=ball8.Ey&&pt.y<=(ball8.Ey+25))
  310. {
  311. life=1;
  312. InvalidateRect(hWnd, rect, false);
  313. }
  314. else if(pt.x>=ball9.Ex&&pt.x<=(ball9.Ex+25)&&pt.y>=ball9.Ey&&pt.y<=(ball9.Ey+25))
  315. {
  316. life=1;
  317. InvalidateRect(hWnd, rect, false);
  318. }
  319. else if(pt.x>=ball10.Ex&&pt.x<=(ball10.Ex+25)&&pt.y>=ball10.Ey&&pt.y<=(ball10.Ey+25))
  320. {
  321. life=1;
  322. InvalidateRect(hWnd, rect, false);
  323. }
  324. break;
  325. case WM_TIMER:
  326. switch(wParam)
  327. {
  328. case BALL1_ID:
  329. if(ball1.Ex<300)
  330. {
  331. ball1.Ex++;
  332. }
  333. else
  334. {
  335. KillTimer(hWnd, BALL1_ID);
  336. }
  337. InvalidateRect(hWnd, rect, false);
  338. break;
  339. case BALL2_ID:
  340. if(ball2.Ex>-26)
  341. {
  342. ball2.Ex--;
  343. }
  344. else
  345. {
  346. KillTimer(hWnd, BALL3_ID);
  347. }
  348. InvalidateRect(hWnd, rect, false);
  349. break;
  350. case BALL3_ID:
  351. if(ball3.Ex<300)
  352. {
  353. ball3.Ex++;
  354. }
  355. else
  356. {
  357. KillTimer(hWnd, BALL3_ID);
  358. }
  359. InvalidateRect(hWnd, rect, false);
  360. break;
  361. case BALL4_ID:
  362. if(ball3.Ex<300)
  363. {
  364. ball4.Ex++;
  365. }
  366. else
  367. {
  368. KillTimer(hWnd, BALL4_ID);
  369. }
  370. InvalidateRect(hWnd, rect, false);
  371. break;
  372. case BALL5_ID:
  373. if(ball5.Ex<300)
  374. {
  375. ball5.Ex++;
  376. }
  377. else
  378. {
  379. KillTimer(hWnd, BALL5_ID);
  380. }
  381. InvalidateRect(hWnd, rect, false);
  382. break;
  383. case BALL6_ID:
  384. if(ball6.Ex>-26)
  385. {
  386. ball6.Ex--;
  387. }
  388. else
  389. {
  390. KillTimer(hWnd, BALL6_ID);
  391. }
  392. InvalidateRect(hWnd, rect, false);
  393. break;
  394. case BALL7_ID:
  395. if(ball7.Ex<300)
  396. {
  397. ball7.Ex++;
  398. }
  399. else
  400. {
  401. KillTimer(hWnd, BALL7_ID);
  402. }
  403. InvalidateRect(hWnd, rect, false);
  404. break;
  405. case BALL8_ID:
  406. if(ball8.Ex<300)
  407. {
  408. ball8.Ex++;
  409. }
  410. else
  411. {
  412. KillTimer(hWnd, BALL8_ID);
  413. }
  414. InvalidateRect(hWnd, rect, false);
  415. break;
  416. case BALL9_ID:
  417. if(ball9.Ex>-26)
  418. {
  419. ball9.Ex--;
  420. }
  421. else
  422. {
  423. KillTimer(hWnd, BALL9_ID);
  424. }
  425. InvalidateRect(hWnd, rect, false);
  426. break;
  427. case BALL10_ID:
  428. if(ball10.Ex>-26)
  429. {
  430. ball10.Ex--;
  431. }
  432. else
  433. {
  434. KillTimer(hWnd, BALL10_ID);
  435. }
  436. InvalidateRect(hWnd, rect, false);
  437. break;
  438. case SCORECOUNTER_ID:
  439. if(life==0)
  440. {
  441. if(scorecounter<1000)
  442. {
  443. scorecounter+=100;
  444. _swprintf(str, _T("Score: %d"), scorecounter);
  445. InvalidateRect(hWnd, rect, false);
  446. }
  447. else
  448. {
  449. _swprintf(level, _T("Level: %d"), 2);
  450. InvalidateRect(hWnd, rect, false);
  451. }
  452. }
  453. break;
  454. }
  455. break;
  456. case WM_DESTROY:
  457. PostQuitMessage(0);
  458. break;
  459. default:
  460. return DefWindowProc(hWnd, message, wParam, lParam);
  461. }
  462. return 0;
  463. }
  464.  
  465. // Message handler for about box.
  466. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  467. {
  468. UNREFERENCED_PARAMETER(lParam);
  469. switch (message)
  470. {
  471. case WM_INITDIALOG:
  472. return (INT_PTR)TRUE;
  473.  
  474. case WM_COMMAND:
  475. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  476. {
  477. EndDialog(hDlg, LOWORD(wParam));
  478. return (INT_PTR)TRUE;
  479. }
  480. break;
  481. }
  482. return (INT_PTR)FALSE;
  483. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement