Code:
#include <windows.h>
#include <ctime>
#include <cstdlib>
const char g_szClassName[] = "myWindowClass";
struct hball{ int xslope; int yslope; int xpos; int ypos; RECT ballrect;
int size; int dir; int diry; int speed; };
struct paddle{ int xpos; int length; int ypos; int speed; RECT hitdet; int score; };
paddle paddle1;
paddle paddle2;
HDC OFFSCRN;
HDC SCREEN;
HDC dc;
HPEN ppen = CreatePen(PS_SOLID, 4, RGB(255, 255, 255));
HPEN bpen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
HPEN ballpen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
HBRUSH bbrush = CreateSolidBrush(RGB(0, 0, 0));
HBRUSH wbrush = CreateSolidBrush(RGB(255, 255, 255));
RECT brect;
RECT rectbuff;
HBITMAP offscreenbm;
HBITMAP screenbm;
PAINTSTRUCT ps;
bool gamestart;
HFONT font = CreateFont( 0, 0, 0, 0, 700, FALSE, FALSE, FALSE, 0,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH,
NULL);
hball ball;
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
DeleteDC(OFFSCRN);
DeleteDC(SCREEN);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
BeginPaint(hwnd, &ps);
// Sizing hdcs and bitmaps to the screen so it will draw to the whole
// windows client area
GetClientRect(hwnd, &brect);
SCREEN = GetDC(hwnd);
OFFSCRN = CreateCompatibleDC(SCREEN);
offscreenbm = CreateCompatibleBitmap(OFFSCRN, brect.right, brect.bottom);
screenbm = CreateCompatibleBitmap(SCREEN, brect.right, brect.bottom);
SelectObject(OFFSCRN, offscreenbm);
SelectObject(SCREEN, screenbm);
//Prepare to draw the background
SelectObject(OFFSCRN, bpen);
SelectObject(OFFSCRN, bbrush);
//Draw the background
Rectangle(OFFSCRN, brect.left, brect.top, brect.right, brect.bottom);
//Prepare to draw the board
SelectObject(OFFSCRN, ballpen);
//Draw the board
Rectangle(OFFSCRN, 5, 5, 700, 400);
MoveToEx(OFFSCRN, 350, 5, NULL);
LineTo(OFFSCRN, 350, 400);
//Prepare to draw the paddles
SelectObject(OFFSCRN, ppen);
SelectObject(OFFSCRN, bbrush);
//Paddle 1
MoveToEx(OFFSCRN, paddle1.xpos, paddle1.ypos - paddle1.length / 2, NULL);
LineTo(OFFSCRN, paddle1.xpos, paddle1.ypos + paddle1.length);
//Paddle 2
MoveToEx(OFFSCRN, paddle2.xpos, paddle2.ypos - paddle2.length / 2, NULL);
LineTo(OFFSCRN, paddle2.xpos, paddle2.ypos + paddle2.length);
//Ball
SelectObject(OFFSCRN, wbrush);
SelectObject(OFFSCRN, ballpen);
Ellipse(OFFSCRN, ball.ballrect.left, ball.ballrect.top, ball.ballrect.right, ball.ballrect.bottom);
if(gamestart == false)
{
SelectObject(OFFSCRN, font);
TextOut(OFFSCRN, 290, 190, "Hit <Enter> to start!", 21);
}
BitBlt(SCREEN, 0, 0, brect.right, brect.bottom, OFFSCRN, 0, 0, SRCCOPY);
ReleaseDC(hwnd, SCREEN);
DeleteObject(offscreenbm);
DeleteObject(screenbm);
DeleteDC(OFFSCRN);
EndPaint(hwnd, &ps);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
break;
}
return 0;
}
MSG Msg;
WNDCLASSEX wc;
HWND hwnd;
int oldtick;
int currtick;
int newtick;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
oldtick = GetTickCount();
ball.dir = 1;
ball.diry = 2;
paddle1.xpos = 10;
paddle1.ypos = 197;
paddle1.length = 45;
paddle1.speed = 4;
paddle1.hitdet.left = 10;
paddle1.hitdet.top = paddle1.ypos - paddle1.length / 2;
paddle1.hitdet.right = 20;
paddle1.hitdet.bottom = paddle1.ypos + paddle1.length / 2;
paddle2.xpos = 693;
paddle2.ypos = 197;
paddle2.length = 46;
paddle2.speed = 4;
paddle1.hitdet.left = 693;
paddle2.hitdet.top = paddle2.ypos - paddle1.length / 2;
paddle2.hitdet.right = 697;
paddle2.hitdet.bottom = paddle2.ypos + paddle1.length / 2;
ball.xpos = 347;
ball.ypos = 197;
ball.size = 7;
ball.ballrect.left = ball.xpos - (ball.size / 2);
ball.ballrect.top = ball.ypos - (ball.size / 2);
ball.ballrect.right = ball.xpos + (ball.size / 2);
ball.ballrect.bottom = ball.ypos + (ball.size / 2);
gamestart = false;
// Registering the Window Class
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;
}
// Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Pong",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 750, 510,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
PeekMessage(&Msg, NULL, 0, 0, PM_NOREMOVE);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// The Message Loop
while(Msg.message != WM_QUIT)
{
newtick = GetTickCount();
currtick = newtick - oldtick;
if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
else
{
if(currtick >= 10) {
if(ball.ypos >= 395)
{
ball.diry = 2;
}
if(ball.ypos <= 10)
{
ball.diry = 1;
}
if(ball.xpos >= 685)
{
ball.dir = 1;
}
if(ball.dir == 1)
{
ball.xpos = ball.xpos - ball.xslope;
}
else if(ball.dir == 2)
{
ball.xpos = ball.xpos + ball.xslope;
}
if(ball.diry == 1)
{
ball.ypos = ball.ypos + ball.yslope;
}
else if(ball.diry == 2)
{
ball.ypos = ball.ypos - ball.yslope;
}
ball.ballrect.left = ball.xpos - (ball.size / 2);
ball.ballrect.top = ball.ypos - (ball.size / 2);
ball.ballrect.right = ball.xpos + ball.size;
ball.ballrect.bottom = ball.ypos + ball.size;
if(GetAsyncKeyState(VK_UP) < 0)
{
if(paddle1.ypos - paddle1.length / 2 <=8) { }
else { paddle1.ypos = paddle1.ypos - paddle1.speed; }
}
else if(GetAsyncKeyState(VK_DOWN) < 0)
{
if(paddle1.ypos + paddle1.length >= 397) { }
else { paddle1.ypos = paddle1.ypos + paddle1.speed; }
}
else if(GetAsyncKeyState(VK_RETURN) < 0)
{
ball.yslope = rand() % (3 - 9);
ball.xslope = 5;
gamestart = true;
}
paddle1.hitdet.left = 10;
paddle1.hitdet.top = paddle1.ypos - paddle1.length / 2;
paddle1.hitdet.right = 15;
paddle1.hitdet.bottom = paddle1.ypos + paddle1.length;
if(ball.ballrect.top > paddle2.ypos)
{
if(paddle2.hitdet.bottom >= 397) { }
else { paddle2.ypos = paddle2.ypos + paddle2.speed; }
}
else if(ball.ypos < paddle2.ypos)
{
if(paddle2.hitdet.bottom <= 8) { }
else { paddle2.ypos = paddle2.ypos - paddle2.speed; }
}
if(ball.ballrect.left <= 10)
{
if(IntersectRect(&rectbuff, &ball.ballrect, &paddle1.hitdet))
{
ball.dir = 2;
}
else
{
paddle2.score++;
gamestart = false;
ball.xslope = 0;
ball.yslope = 0;
ball.xpos = 347;
ball.ypos = 197;
}
}
paddle2.hitdet.left = 693;
paddle2.hitdet.top = paddle2.ypos - paddle2.length / 2;
paddle2.hitdet.right = 697;
paddle2.hitdet.bottom = paddle2.ypos + paddle2.length;
if(ball.ballrect.right >= 693)
{
if(IntersectRect(&rectbuff, &ball.ballrect, &paddle2.hitdet))
{
ball.dir = 1;
}
else
{
paddle1.score++;
gamestart = false;
ball.xslope = 0;
ball.yslope = 0;
ball.xpos = 347;
ball.ypos = 197;
}
}
RedrawWindow(hwnd, 0, 0, RDW_INVALIDATE);
oldtick = newtick;
}
}
}
return Msg.wParam;
}