您的位置:首页 > 编程语言 > C语言/C++

Visual C++游戏编程基础之追逐移动

2019-06-15 21:53 1436 查看

一、基本思路

1.追逐移动的实现与飞机追随鼠标光标原理类似;

2.现在小鸟作为追逐者,设定其贴图坐标为(nowX-25,nowY-16),其中(nowX,nowY)是飞机的贴图坐标;

3.通过判断小鸟贴图坐标的X坐标与nowX-25谁大谁小,来决定小鸟在飞机左边还是右边,从而决定贴哪张图;

二、效果

三、代码实现

[code]
#include "stdafx.h"
#include <stdio.h>

HINSTANCE hInst;
HBITMAP bg,ship,bird;
HDC		hdc,mdc,bufdc;
HWND	hWnd;
DWORD	tPre,tNow;
int		x,y,nowX,nowY;
int		w=0;
POINT	p[3];

ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
void				MyPaint(HDC hdc);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR     lpCmdLine,
int       nCmdShow)
{
MSG msg;

MyRegisterClass(hInstance);

if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
tNow = GetTickCount();
if(tNow-tPre >= 40)
MyPaint(hdc);
}
}

return msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style			= CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc	= (WNDPROC)WndProc;
wcex.cbClsExtra		= 0;
wcex.cbWndExtra		= 0;
wcex.hInstance		= hInstance;
wcex.hIcon			= NULL;
wcex.hCursor		= NULL;
wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName	= NULL;
wcex.lpszClassName	= "canvas";
wcex.hIconSm		= NULL;

return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HBITMAP bmp;
POINT pt,lt,rb;
RECT	rect;

hInst = hInstance;

hWnd = CreateWindow("canvas", "绘图窗口" , WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

MoveWindow(hWnd,100,100,640,480,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

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,648,480,LR_LOADFROMFILE);
ship = (HBITMAP)LoadImage(NULL,"ship.bmp",IMAGE_BITMAP,100,148,LR_LOADFROMFILE);
bird = (HBITMAP)LoadImage(NULL,"bird.bmp",IMAGE_BITMAP,122,122,LR_LOADFROMFILE);

x = 300;
y = 300;
nowX = 300;
nowY = 300;

pt.x = 300;
pt.y = 300;
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,&lt);
ClientToScreen(hWnd,&rb);
rect.left = lt.x;
rect.top = lt.y;
rect.right = rb.x;
rect.bottom = rb.y;
ClipCursor(&rect);//限制鼠标光标的移动区域,该函数参数是相对屏幕坐标而言的

//设置3只小鸟的贴图坐标
p[0].x = 30;
p[0].y = 100;

p[1].x = 250;
p[1].y = 250;

p[2].x = 500;
p[2].y = 400;

MyPaint(hdc);

return TRUE;
}

void MyPaint(HDC hdc)
{
int i;

SelectObject(bufdc,bg);
BitBlt(mdc,0,0,w,480,bufdc,640-w,0,SRCCOPY);
BitBlt(mdc,w,0,640-w,480,bufdc,0,0,SRCCOPY);

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;
}
SelectObject(bufdc,ship);
BitBlt(mdc,nowX,nowY,100,74,bufdc,0,74,SRCAND);
BitBlt(mdc,nowX,nowY,100,74,bufdc,0,0,SRCPAINT);

SelectObject(bufdc,bird);
for(i=0;i<3;i++)
{
if(rand()%3 != 1)
{
if(p[i].y > nowY-16)
p[i].y -= 5;
else
p[i].y += 5;

if(p[i].x > nowX-25)//鸟的贴图坐标为(nowX+25,nowY+16)
p[i].x -= 5;
else
p[i].x += 5;
}

if(p[i].x > nowX-25)    //飞机在小鸟的左边,贴方向向左的小鸟图片
{
BitBlt(mdc,p[i].x,p[i].y,61,61,bufdc,61,61,SRCAND);
BitBlt(mdc,p[i].x,p[i].y,61,61,bufdc,0,61,SRCPAINT);
}
else
{
BitBlt(mdc,p[i].x,p[i].y,61,61,bufdc,61,0,SRCAND);//贴方向向右的小鸟图片
BitBlt(mdc,p[i].x,p[i].y,61,61,bufdc,0,0,SRCPAINT);
}
}

BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);

tPre = GetTickCount();

w += 10;
if(w==640)
w = 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_KEYDOWN:
if(wParam==VK_ESCAPE)
PostQuitMessage(0);
break;
case WM_MOUSEMOVE:
x = LOWORD(lParam);
if(x > 530)
x = 530;
else if(x < 0)
x = 0;

y = HIWORD(lParam);
if(y > 380)
y = 380;
else if(y < 0)
y = 0;

break;
case WM_DESTROY:
ClipCursor(NULL);

DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(bg);
DeleteObject(ship);
DeleteObject(bird);
ReleaseDC(hWnd,hdc);

PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: