Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Take and Run.cpp : Defines the entry point for the application.
- //
- #include "stdafx.h"
- #include "Take and Run.h"
- #define MAX_LOADSTRING 100
- // Global Variables:
- HINSTANCE hInst; // current instance
- TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
- TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
- WCHAR str[255] = L"Score: 0"; // score containor
- WCHAR level[255] = L"Level: 1"; // level containor
- const UINT_PTR BALL1_ID = 0x1;
- const UINT_PTR BALL2_ID = 0x3;
- const UINT_PTR BALL3_ID = 0x4;
- const UINT_PTR BALL4_ID = 0x5;
- const UINT_PTR BALL5_ID = 0x6;
- const UINT_PTR BALL6_ID = 0x7;
- const UINT_PTR BALL7_ID = 0x8;
- const UINT_PTR BALL8_ID = 0x9;
- const UINT_PTR BALL9_ID = 0x10;
- const UINT_PTR BALL10_ID = 0x11;
- const UINT_PTR SCORECOUNTER_ID = 0x12;
- int life = 0; // life
- int scorecounter = 0; // score counter
- RECT *rect;
- POINT pt;
- // Forward declarations of functions included in this code module:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- struct balls {
- int Ex;
- int Ey;
- };
- balls ball1 = {-26, 25};
- balls ball2 = {300, 50};
- balls ball3 = {-26, 200};
- balls ball4 = {-26, 250};
- balls ball5 = {-26, 100};
- balls ball6 = {300, 70};
- balls ball7 = {-26, 30};
- balls ball8 = {-26, 200};
- balls ball9 = {300, 250};
- balls ball10 = {300, 100};
- VOID Level1(HWND hWnd)
- {
- int i=0;
- SetTimer(hWnd, BALL1_ID, 1, NULL);
- SetTimer(hWnd, BALL2_ID, 2, NULL);
- SetTimer(hWnd, BALL3_ID, 3, NULL);
- SetTimer(hWnd, BALL4_ID, 4, NULL);
- SetTimer(hWnd, BALL5_ID, 5, NULL);
- SetTimer(hWnd, BALL6_ID, 6, NULL);
- SetTimer(hWnd, BALL7_ID, 7, NULL);
- SetTimer(hWnd, BALL8_ID, 8, NULL);
- SetTimer(hWnd, BALL9_ID, 9, NULL);
- SetTimer(hWnd, BALL10_ID, 10, NULL);
- SetTimer(hWnd, SCORECOUNTER_ID, 1000, NULL);
- }
- VOID OnPaint(HDC hdc,HWND hWnd)
- {
- FontFamily fontfamily(L"Times New Roman");
- SolidBrush red(Color(255, 0, 0));
- SolidBrush green(Color(0, 255, 0));
- SolidBrush blue(Color(0, 0, 255));
- if(life==0)
- {
- hdc=GetDC(hWnd);
- HDC memDC=CreateCompatibleDC(hdc);
- HBITMAP hMemMap=CreateCompatibleBitmap(hdc, 300, 300);
- HBITMAP hOldMap=(HBITMAP)SelectObject(memDC, hMemMap);
- // Drawing
- Graphics draw(memDC);
- draw.FillRectangle(&green, 0, 0, 300, 25);
- Font font(&fontfamily, 14, 0, UnitPixel);
- PointF p1(180.0f, 4.0f);
- PointF p2(30.0f, 4.0f);
- draw.DrawString(level, -1, &font, p2, &blue);
- draw.DrawString(str, -1, &font, p1, &blue);
- draw.FillEllipse(&red, ball1.Ex, ball1.Ey, 25, 25);
- draw.FillEllipse(&red, ball2.Ex, ball2.Ey, 25, 25);
- draw.FillEllipse(&red, ball3.Ex, ball3.Ey, 25, 25);
- draw.FillEllipse(&red, ball4.Ex, ball4.Ey, 25, 25);
- draw.FillEllipse(&red, ball5.Ex, ball5.Ey, 25, 25);
- draw.FillEllipse(&red, ball6.Ex, ball6.Ey, 25, 25);
- draw.FillEllipse(&red, ball7.Ex, ball7.Ey, 25, 25);
- draw.FillEllipse(&red, ball8.Ex, ball8.Ey, 25, 25);
- draw.FillEllipse(&red, ball9.Ex, ball9.Ey, 25, 25);
- draw.FillEllipse(&red, ball10.Ex, ball10.Ey, 25, 25);
- BitBlt(hdc, 0, 0, 300, 300, memDC, 0, 0, SRCCOPY);
- SelectObject(memDC, hOldMap);
- DeleteObject(hMemMap);
- DeleteDC(memDC);
- }
- else
- {
- hdc=GetDC(hWnd);
- HDC memDC=CreateCompatibleDC(hdc);
- HBITMAP hMemMap=CreateCompatibleBitmap(hdc, 300, 300);
- HBITMAP hMemOld=(HBITMAP)SelectObject(memDC, hMemMap);
- // Drawing
- Graphics draw(memDC);
- Font font(&fontfamily, 44, 0, UnitPixel);
- PointF PointF(100.0f, 100.0f);
- draw.DrawString(L"You Lost!", -1, &font, PointF, &blue);
- BitBlt(hdc, 0, 0, 300, 300, memDC, 0, 0, SRCCOPY);
- SelectObject(memDC, hMemOld);
- DeleteObject(hMemMap);
- DeleteDC(memDC);
- }
- }
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
- GdiplusStartupInput gdiplusStartupInput;
- ULONG_PTR gdiplusToken;
- // TODO: Place code here.
- MSG msg;
- HACCEL hAccelTable;
- // Initialize GDI+.
- GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
- // Initialize global strings
- LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadString(hInstance, IDC_TAKEANDRUN, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
- // Perform application initialization:
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
- hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TAKEANDRUN));
- // Main message loop:
- while (GetMessage(&msg, NULL, 0, 0))
- {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int) msg.wParam;
- }
- //
- // FUNCTION: MyRegisterClass()
- //
- // PURPOSE: Registers the window class.
- //
- // COMMENTS:
- //
- // This function and its usage are only necessary if you want this code
- // to be compatible with Win32 systems prior to the 'RegisterClassEx'
- // function that was added to Windows 95. It is important to call this function
- // so that the application will get 'well formed' small icons associated
- // with it.
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TAKEANDRUN));
- wcex.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(110));
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TAKEANDRUN);
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
- return RegisterClassEx(&wcex);
- }
- //
- // FUNCTION: InitInstance(HINSTANCE, int)
- //
- // PURPOSE: Saves instance handle and creates main window
- //
- // COMMENTS:
- //
- // In this function, we save the instance handle in a global variable and
- // create and display the main program window.
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
- hInst = hInstance; // Store instance handle in our global variable
- hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, 300, 300, NULL, NULL, hInstance, NULL);
- if (!hWnd)
- {
- return FALSE;
- }
- // set timer
- Level1(hWnd);
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- //
- // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
- //
- // PURPOSE: Processes messages for the main window.
- //
- // WM_COMMAND - process the application menu
- // WM_PAINT - Paint the main window
- // WM_DESTROY - post a quit message and return
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int wmId, wmEvent;
- PAINTSTRUCT ps;
- HDC hdc;
- GetCursorPos(&pt);
- ScreenToClient(hWnd,&pt);
- switch (message)
- {
- case WM_COMMAND:
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
- // Parse the menu selections:
- switch (wmId)
- {
- case IDM_ABOUT:
- DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
- break;
- case IDM_EXIT:
- DestroyWindow(hWnd);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- OnPaint(hdc, hWnd);
- EndPaint(hWnd, &ps);
- break;
- case WM_MOUSEMOVE:
- if(pt.x>=ball1.Ex&&pt.x<=(ball1.Ex+25)&&pt.y<=ball1.Ey&&pt.y>=(ball1.Ey+25))
- {
- life=1;
- InvalidateRect(hWnd, rect, false);
- }
- else if(pt.x>=ball2.Ex&&pt.x<=(ball2.Ex+25)&&pt.y>=ball2.Ey&&pt.y<=(ball2.Ey+25))
- {
- life=1;
- InvalidateRect(hWnd, rect, false);
- }
- else if(pt.x>=ball3.Ex&&pt.x<=(ball3.Ex+25)&&pt.y>=ball3.Ey&&pt.y<=(ball3.Ey+25))
- {
- life=1;
- InvalidateRect(hWnd, rect, false);
- }
- else if(pt.x>=ball4.Ex&&pt.x<=(ball4.Ex+25)&&pt.y>=ball4.Ey&&pt.y<=(ball4.Ey+25))
- {
- life=1;
- InvalidateRect(hWnd, rect, false);
- }
- else if(pt.x>=ball5.Ex&&pt.x<=(ball5.Ex+25)&&pt.y>=ball5.Ey&&pt.y<=(ball5.Ey+25))
- {
- life=1;
- InvalidateRect(hWnd, rect, false);
- }
- else if(pt.x>=ball6.Ex&&pt.x<=(ball6.Ex+25)&&pt.y>=ball6.Ey&&pt.y<=(ball6.Ey+25))
- {
- life=1;
- InvalidateRect(hWnd, rect, false);
- }
- else if(pt.x>=ball7.Ex&&pt.x<=(ball7.Ex+25)&&pt.y>=ball7.Ey&&pt.y<=(ball7.Ey+25))
- {
- life=1;
- InvalidateRect(hWnd, rect, false);
- }
- else if(pt.x>=ball8.Ex&&pt.x<=(ball8.Ex+25)&&pt.y>=ball8.Ey&&pt.y<=(ball8.Ey+25))
- {
- life=1;
- InvalidateRect(hWnd, rect, false);
- }
- else if(pt.x>=ball9.Ex&&pt.x<=(ball9.Ex+25)&&pt.y>=ball9.Ey&&pt.y<=(ball9.Ey+25))
- {
- life=1;
- InvalidateRect(hWnd, rect, false);
- }
- else if(pt.x>=ball10.Ex&&pt.x<=(ball10.Ex+25)&&pt.y>=ball10.Ey&&pt.y<=(ball10.Ey+25))
- {
- life=1;
- InvalidateRect(hWnd, rect, false);
- }
- break;
- case WM_TIMER:
- switch(wParam)
- {
- case BALL1_ID:
- if(ball1.Ex<300)
- {
- ball1.Ex++;
- }
- else
- {
- KillTimer(hWnd, BALL1_ID);
- }
- InvalidateRect(hWnd, rect, false);
- break;
- case BALL2_ID:
- if(ball2.Ex>-26)
- {
- ball2.Ex--;
- }
- else
- {
- KillTimer(hWnd, BALL3_ID);
- }
- InvalidateRect(hWnd, rect, false);
- break;
- case BALL3_ID:
- if(ball3.Ex<300)
- {
- ball3.Ex++;
- }
- else
- {
- KillTimer(hWnd, BALL3_ID);
- }
- InvalidateRect(hWnd, rect, false);
- break;
- case BALL4_ID:
- if(ball3.Ex<300)
- {
- ball4.Ex++;
- }
- else
- {
- KillTimer(hWnd, BALL4_ID);
- }
- InvalidateRect(hWnd, rect, false);
- break;
- case BALL5_ID:
- if(ball5.Ex<300)
- {
- ball5.Ex++;
- }
- else
- {
- KillTimer(hWnd, BALL5_ID);
- }
- InvalidateRect(hWnd, rect, false);
- break;
- case BALL6_ID:
- if(ball6.Ex>-26)
- {
- ball6.Ex--;
- }
- else
- {
- KillTimer(hWnd, BALL6_ID);
- }
- InvalidateRect(hWnd, rect, false);
- break;
- case BALL7_ID:
- if(ball7.Ex<300)
- {
- ball7.Ex++;
- }
- else
- {
- KillTimer(hWnd, BALL7_ID);
- }
- InvalidateRect(hWnd, rect, false);
- break;
- case BALL8_ID:
- if(ball8.Ex<300)
- {
- ball8.Ex++;
- }
- else
- {
- KillTimer(hWnd, BALL8_ID);
- }
- InvalidateRect(hWnd, rect, false);
- break;
- case BALL9_ID:
- if(ball9.Ex>-26)
- {
- ball9.Ex--;
- }
- else
- {
- KillTimer(hWnd, BALL9_ID);
- }
- InvalidateRect(hWnd, rect, false);
- break;
- case BALL10_ID:
- if(ball10.Ex>-26)
- {
- ball10.Ex--;
- }
- else
- {
- KillTimer(hWnd, BALL10_ID);
- }
- InvalidateRect(hWnd, rect, false);
- break;
- case SCORECOUNTER_ID:
- if(life==0)
- {
- if(scorecounter<1000)
- {
- scorecounter+=100;
- _swprintf(str, _T("Score: %d"), scorecounter);
- InvalidateRect(hWnd, rect, false);
- }
- else
- {
- _swprintf(level, _T("Level: %d"), 2);
- InvalidateRect(hWnd, rect, false);
- }
- }
- break;
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- // Message handler for about box.
- INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- UNREFERENCED_PARAMETER(lParam);
- switch (message)
- {
- case WM_INITDIALOG:
- return (INT_PTR)TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return (INT_PTR)TRUE;
- }
- break;
- }
- return (INT_PTR)FALSE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement