hdc = GetDC(hWnd);
mdc=CreateCompatibleDC(hdc);
bufdc=CreateCompatibleDC(hdc);
bmp=CreateCompatibleBitmap(hdc,640,480);
SelectObject(mdc,bmp);
bg=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE);
ship=(HBITMAP)LoadImage(NULL,"ship.bmp",IMAGE_BITMAP,300,100,LR_LOADFROMFILE);
bullet=(HBITMAP)LoadImage(NULL,"bullet.bmp",IMAGE_BITMAP,40,10,LR_LOADFROMFILE);
x=200;
y=200;
nowX=200;
nowY=200;
bcount=1;
//设定鼠标光标位置
pt.x=200;
pt.y=200;
ClientToScreen(hWnd,&pt);
SetCursorPos(pt.x,pt.y);
// ShowCursor(false);
//限制鼠标光标移动区域
GetClientRect(hWnd,&rect);
lt.x=rect.left;
lt.y=rect.top;
rb.x=rect.right;
rb.y=rect.bottom;
ClientToScreen(hWnd,<);
ClientToScreen(hWnd,&rb);
rect.left=lt.x;
rect.top=lt.y;
rect.right=rb.x;
rect.bottom=rb.y;
ClipCursor(&rect);
MyPaint(hdc);
void MyPaint(HDC hdc)
{
char str[20]="";
int i,distanceX,distanceY;
SelectObject(bufdc,bg);
BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);
SelectObject(bufdc,ship);
BitBlt(mdc,nowX-100,nowY-80,150,100,bufdc,150,0,SRCAND);
BitBlt(mdc,nowX-100,nowY-80,150,100,bufdc,0,0,SRCPAINT);
if (nowX<x)
{
nowX+=10;
if(nowX>x)
nowX=x;
}
else
{
nowX-=10;
if(nowX<x)
nowX=x;
}
if (nowY<y)
{
nowY+=10;
if(nowY>y)
nowY=y;
}
else
{
nowY-=10;
if(nowY<y)
nowY=y;
}
for (i=0;i<10;i++)
{
SelectObject(bufdc,bullet);
BitBlt(mdc,b[i].x,b[i].y,20,10,bufdc,20,0,SRCAND);
BitBlt(mdc,b[i].x,b[i].y,20,10,bufdc,0,0,SRCPAINT);
}
sprintf_s(str,"X坐标:%d ",x);
TextOut(mdc,0,0,str,strlen(str));
sprintf_s(str,"Y坐标:%d ",y);
TextOut(mdc,0,20,str,strlen(str));
BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);
tPre=GetTickCount();
}
void GameCycle()
{
int w,h,i;
w=20;
h=10;
SelectObject(bufdc,bg);
BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);
SelectObject(bufdc,ship);
BitBlt(mdc,nowX-100,nowY-80,150,100,bufdc,150,0,SRCAND);
BitBlt(mdc,nowX-100,nowY-80,150,100,bufdc,0,0,SRCPAINT);
for(i=0;i<10;i++)
{
SelectObject(bufdc,bullet);
BitBlt(mdc,b[i].x,b[i].y,20,10,bufdc,20,0,SRCAND);
BitBlt(mdc,b[i].x,b[i].y,20,10,bufdc,0,0,SRCPAINT);
b[i].exist=false;
}
BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);
tPre=GetTickCount();
tNow =tPre+400;
bcount=1;
if(bcount==10)
bcount=0;
}
case WM_LBUTTONDOWN:
x=LOWORD(lParam);
y=HIWORD(lParam);
if ((x>200)&&(y>185))
{
// ZeroMemory(&b,sizeof(b));
dX=(x-200)/10;
dY=(y=185)/10;
b[0].x=200;
b[0].y=185;
b[0].exist=true;
for (i=1;i<10;i++)
{
b[i].x=200+dX*i;
b[i].y=185+dY*i;
}
bcount=10;
}