#include<windows.h>
HDC hdc;
HWND hwnd;
RECT wndrect;
bool ISdrawfoot;
int direct=3;
typedef struct
{
int x,y;
int width,height;
bool exit;
} brick;
typedef struct
{
int x,y;
int movex,movey;
int r;
} ball;
brick barx;
brick * pbrick;
ball ballx;
bool gameover;
char title[]="create window";
void init();
void clearscreen();
void drawbrick();
void drawbar();
void movebar();
void testbrickcollide();
void testbarcollide();
void moveball();
void movebar();
void drawball();
void isgameover();
LRESULT Windowproc(HWND,UINT,WPARAM,LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpcmdLine,int nCmdShow)
{
MSG msg;
WNDCLASS wc;
wc.style =CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)Windowproc;
wc.cbWndExtra =0;
wc.cbClsExtra =0;
wc.hInstance = hInstance;
wc.hIcon =NULL;
wc.hCursor =NULL;
wc.hbrBackground =(HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszClassName =title;
wc.lpszMenuName =NULL;
if (!hInstance)
{
return 0;
}
RegisterClass(&wc);
hwnd=CreateWindow(title,title,WS_OVERLAPPEDWINDOW,0,0,3000,2400,NULL,NULL,hInstance,NULL);
if(!hwnd)
{
return 0;
}
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
init();
while (1)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message ==WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
clearscreen();
drawbrick();
drawball();
drawbar();
testbrickcollide();
testbarcollide();
moveball();
movebar();
isgameover();
Sleep(50);
}
}
return 0;
}
LRESULT Windowproc(HWND hwnd ,UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 1;
case WM_KEYDOWN:
{
switch (wParam)
{
case VK_ESCAPE:
PostMessage(hwnd,WM_CLOSE,0,0);
break;
case VK_LEFT:
direct=0;
break;
case VK_RIGHT:
direct=1;
break;
}
break;
}
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
void init()
{
pbrick =new brick[20];
hdc=GetDC(hwnd);
GetWindowRect(hwnd,&wndrect);
int gwidth;
int gheight;
gwidth=(wndrect.right)/11;
gheight=(wndrect.bottom )/18;
for(int i=0;i<20;i++)
{
(pbrick+i)->x =(i%5*2+1)*gwidth;
(pbrick+i)->y =(i/5*2+1)*gheight;
(pbrick+i)->width =gwidth;
(pbrick+i)->height =gheight;
(pbrick+i)->exit =true;
}
drawbrick();
barx.x=(wndrect.right) /3;
barx.y =wndrect.bottom -gheight-100;
barx.width =(wndrect.right) /4;
barx.height =gheight/2;
drawbar();
ballx.r =20;
ballx.x =(wndrect.right -ballx.r )/2;
ballx.y =barx.y -ballx.r-10;
ballx.movex =10;
ballx.movey=-10;
drawball();
}
void clearscreen()
{
HBRUSH hbrush=(HBRUSH)CreateSolidBrush(RGB(0,0,0));
SelectObject(hdc,hbrush);
Rectangle(hdc,wndrect.left ,wndrect.top ,wndrect.right ,wndrect.bottom );
SelectObject(hdc,hbrush);
DeleteObject(hbrush);
}
void drawbrick()
{
HBRUSH hbrush=(HBRUSH)CreateSolidBrush(RGB(100,100,100));
SelectObject(hdc,hbrush);
for(int i=0;i<20;i++)
{
if ((pbrick+i)->exit )
{Rectangle(hdc,(pbrick+i)->x ,(pbrick+i)->y ,
(pbrick+i)->x+(pbrick+i)->width ,
(pbrick+i)->y +(pbrick+i)->height );
}
}
SelectObject(hdc,hbrush);
DeleteObject(hbrush);
}
void drawbar()
{
HBRUSH hbrush=(HBRUSH)CreateSolidBrush(RGB(100,100,100));
SelectObject(hdc,hbrush);
Rectangle(hdc,barx.x ,barx.y ,barx.x +barx.width ,barx.y +barx.height );
SelectObject(hdc,hbrush);
DeleteObject(hbrush);
}
void movebar()
{
if (direct==0)
{
barx.x -=50;
direct=3;
}
if (direct==1)
{
barx.x +=50;
direct=3;
}
if (barx.x <0)
{
barx.x =0;
}
if (barx.x +barx.width >wndrect.right )
{ barx.x=wndrect.right -barx.width ;
}
}
void testbrickcollide()
{
if (ballx.y >=0.5*wndrect.bottom )
return;
for(int i=0;i<20;i++)
{
if ( (pbrick+i)->exit &&((pbrick+i)->x <=ballx.x +ballx.r ) &&
((pbrick+i)->x+(pbrick+i)->width>=ballx.x ) &&
((pbrick+i)->y<=ballx.y +ballx.r ) &&
((pbrick+i)->y+(pbrick+i)->height>=ballx.y ) )
{
ballx.movey=-ballx.movey ;
(pbrick+i)->exit=false;
}
}
}
void testbarcollide()
{
if ( (ballx.x +ballx.r >=barx.x ) &&
(ballx.x <=barx.x +barx.width ) &&
(ballx.y +ballx.r >=barx.y ) &&
(ballx.y <barx.y +barx.height) )
{
ballx.movey=-ballx.movey;
}
}
void moveball()
{
if ((ballx.x <=0)||(ballx.x +ballx.r >=wndrect.right ))
{
ballx.movex =-ballx.movex ;
}
if (ballx.y <=0)
{
ballx.movey =-ballx.movey ;
}
if (ballx.y +ballx.r >wndrect.bottom )
{
init();
}
ballx.x +=ballx.movex ;
ballx.y+=ballx.movey ;
}
void drawball()
{
HBRUSH hbrush=(HBRUSH)CreateSolidBrush(RGB(200,200,100));
SelectObject(hdc,hbrush);
Ellipse(hdc,ballx.x ,ballx.y ,ballx.x +ballx.r ,ballx.y +ballx.r );
SelectObject(hdc,hbrush);
DeleteObject(hbrush);
}
void isgameover()
{
int i=0;
for (i =0;i<20;i++)
{
if ((pbrick+i)->exit )
break;
}
if (i==20)
{
// MessageBox(hwnd,"game over ");
init();
}
}