Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*************************************************
- // Michael Ametrano
- // IT102
- //*************************************************
- #include <windows.h>
- #include <string>
- #include <stdio.h>
- using namespace std;
- const char g_szClassName[] = "myWindowClass";
- const int ID_TIMER = 1;
- const int BALL_MOVE_DELTA = 6; //ball speed
- const int PADDLE_MOVE_DELTA = 3;
- typedef struct _BALLINFO
- {
- int width;
- int height;
- int x;
- int y;
- int dx;
- int dy;
- }BALLINFO;
- BALLINFO g_ballInfo;
- typedef struct _PADDLEINFO
- {
- int width;
- int height;
- int x;
- int y;
- int dx;
- int dy;
- }PADDLEINFO;
- PADDLEINFO g_leftPaddleInfo, g_rightPaddleInfo;
- TCHAR buff[10];
- int sOne = 0;
- int sTwo = 0;
- string message = " - ";
- /* Score tracking */
- //string scoreOne = sOne;
- //string scoreTwo = sTwo;
- /* crazy hack attempt to fix score displaying */
- /*char scoreOne[1];
- sprintf_s(scoreOne, sizeof(scoreOne) "%d", sOne);*/
- void DrawBall(HDC hdc, RECT* prc)
- {
- HBRUSH blueBrush=CreateSolidBrush(RGB(0,0,255));
- HGDIOBJ old=SelectObject(hdc,blueBrush);
- Ellipse(hdc, g_ballInfo.x, g_ballInfo.y, g_ballInfo.x+g_ballInfo.width, g_ballInfo.y+g_ballInfo.height);
- DeleteObject(blueBrush);
- SelectObject(hdc,old);
- }
- void DrawLeftPaddle(HDC hdc, RECT* prc)
- {
- HBRUSH redBrush=CreateSolidBrush(RGB(255,0,0));
- HGDIOBJ old=SelectObject(hdc,redBrush);
- Rectangle(hdc, g_leftPaddleInfo.x, g_leftPaddleInfo.y, g_leftPaddleInfo.x + g_leftPaddleInfo.width, g_leftPaddleInfo.y+g_leftPaddleInfo.height);
- DeleteObject(redBrush);
- SelectObject(hdc,old);
- }
- void DrawRightPaddle(HDC hdc, RECT* prc)
- {
- HBRUSH redBrush=CreateSolidBrush(RGB(255,0,0));
- HGDIOBJ old=SelectObject(hdc,redBrush);
- Rectangle(hdc, g_rightPaddleInfo.x, g_rightPaddleInfo.y, g_rightPaddleInfo.x + g_rightPaddleInfo.width, g_rightPaddleInfo.y+g_rightPaddleInfo.height);
- DeleteObject(redBrush);
- SelectObject(hdc,old);
- }
- void UpdateBall(RECT* prc)
- {
- g_ballInfo.x += g_ballInfo.dx;
- g_ballInfo.y += g_ballInfo.dy;
- if((g_ballInfo.x < 30) && (g_ballInfo.y > g_leftPaddleInfo.y && g_ballInfo.y < g_leftPaddleInfo.y+60))
- {
- g_ballInfo.x = 30;
- g_ballInfo.dx = BALL_MOVE_DELTA;
- }
- if((g_ballInfo.x > 580) && (g_ballInfo.y > g_rightPaddleInfo.y && g_ballInfo.y < g_rightPaddleInfo.y+60))
- {
- g_ballInfo.x = 580;
- g_ballInfo.dx = -BALL_MOVE_DELTA;
- }
- if(g_ballInfo.x < 0)
- {
- g_ballInfo.x = 0;
- g_ballInfo.dx = BALL_MOVE_DELTA;
- }
- else if(g_ballInfo.x + g_ballInfo.width > prc->right)
- {
- g_ballInfo.x = prc->right - g_ballInfo.width;
- g_ballInfo.dx = -BALL_MOVE_DELTA;
- }
- if(g_ballInfo.y < 0)
- {
- g_ballInfo.y = 0;
- g_ballInfo.dy = BALL_MOVE_DELTA;
- }
- else if(g_ballInfo.y + g_ballInfo.height > prc->bottom)
- {
- g_ballInfo.y = prc->bottom - g_ballInfo.height;
- g_ballInfo.dy = -BALL_MOVE_DELTA;
- }
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch(msg)
- {
- case WM_CREATE:
- {
- UINT_PTR ret; //unsigned int to hold result of timer creation
- g_leftPaddleInfo.width = 10;
- g_leftPaddleInfo.height = 60;
- g_leftPaddleInfo.x = 20;
- g_leftPaddleInfo.y = 100;
- g_rightPaddleInfo.width = 10;
- g_rightPaddleInfo.height = 60;
- g_rightPaddleInfo.x = 590;
- g_rightPaddleInfo.y = 100;
- g_ballInfo.width = 10;
- g_ballInfo.height = 10;
- g_ballInfo.x = 300;
- g_ballInfo.dx = BALL_MOVE_DELTA;
- g_ballInfo.dy = BALL_MOVE_DELTA;
- ret = SetTimer(hwnd, ID_TIMER, 20, NULL); // 20 milliseconds
- if(ret == 0)
- MessageBox(hwnd, "Could not SetTimer()!", "Error", MB_OK | MB_ICONEXCLAMATION);
- }
- break;
- case WM_CLOSE:
- DestroyWindow(hwnd);
- break;
- case WM_PAINT:
- {
- RECT rcClient; //struct to hold rectangle coordinates
- PAINTSTRUCT ps; //struct representing client area of window
- HDC hdc = BeginPaint(hwnd, &ps);
- GetClientRect(hwnd, &rcClient); //get coordinates of client area of window
- DrawBall(hdc, &rcClient);
- EndPaint(hwnd, &ps);
- }
- break;
- case WM_TIMER:
- {
- if(g_leftPaddleInfo.y < 1)
- {
- }
- else if (GetAsyncKeyState(VK_UP) < 0)
- {
- g_leftPaddleInfo.y-=6;
- }
- if(g_leftPaddleInfo.y > 380)
- {
- }
- else if(GetAsyncKeyState(VK_DOWN) < 0)
- {
- g_leftPaddleInfo.y+=6;
- }
- /* Computer Paddle Control (Perfect. Imperfect not implemented) */
- g_rightPaddleInfo.y = g_ballInfo.y-10;
- /* score updating */
- /*if (g_ballInfo.x < 20)
- {
- sTwo++;
- }
- if (g_ballInfo.x > 590)
- {
- sOne++;
- }*/
- /* trivial Score detection */
- if (g_ballInfo.x == 0)
- {
- sTwo++;
- }
- if (g_ballInfo.x == 640)
- {
- sOne++;
- }
- RECT rcClient; //struct to hold rectangle coordinates
- HDC hdc = GetDC(hwnd); //grab a handle to the window (Device Context)
- GetClientRect(hwnd, &rcClient); //get coordinates of client area of window
- // and store them in rcClient
- UpdateBall(&rcClient); //calculate next ball position
- FillRect(hdc, &rcClient, (HBRUSH)GetStockObject(WHITE_BRUSH));
- TextOut(hdc,20, 20, buff, wsprintf(buff, "%d", sOne));
- TextOut(hdc,500, 20, buff, wsprintf(buff, "%d", sTwo));
- DrawBall(hdc, &rcClient); //clear screan and draw ball in new position
- DrawLeftPaddle(hdc, &rcClient);
- DrawRightPaddle(hdc, &rcClient);
- ReleaseDC(hwnd, hdc); //release the handle to the window (Device Context)
- }
- break;
- case WM_KEYDOWN:
- /* if(wParam == VK_DOWN) // try this way instead - See the problem?
- y+=6;
- if(wParam == VK_UP)
- y-=6;
- */
- break;
- case WM_KEYUP:
- break;
- case WM_DESTROY:
- KillTimer(hwnd, ID_TIMER);
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpCmdLine, int nCmdShow)
- {
- WNDCLASSEX wc;
- HWND hwnd;
- MSG Msg;
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = 0;
- wc.lpfnWndProc = WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wc.lpszMenuName = NULL;
- wc.lpszClassName = g_szClassName;
- wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- if(!RegisterClassEx(&wc))
- {
- MessageBox(NULL, "Window Registration Failed!", "Error!",
- MB_ICONEXCLAMATION | MB_OK);
- return 0;
- }
- hwnd = CreateWindowEx(
- WS_EX_CLIENTEDGE,
- g_szClassName,
- "PONG!",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
- NULL, NULL, hInstance, NULL);
- if(hwnd == NULL)
- {
- MessageBox(NULL, "Window Creation Failed!", "Error!",
- MB_ICONEXCLAMATION | MB_OK);
- return 0;
- }
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
- while(GetMessage(&Msg, NULL, 0, 0) > 0)
- {
- TranslateMessage(&Msg);
- DispatchMessage(&Msg);
- }
- return Msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment