目标
使用Visual C++ 仿造植物大战僵尸的模式构造一个有基本布景,游戏消息处理,物理现象处理等基本功能的游戏。在完成基本功能的基础上实现游戏的娱乐性和可玩性,丰富游戏机制,完善游戏规则。
基本概念
由于C++程序编译后的文件是可直接运行的机器码,并非Java等语言编译后生成的中间码,所以VC++程序运行速度要较优越。Windows API是Windows操作系统提供的动态链接函数库(.DLL文件),包含了windows内核及所有应用程序所需要的功能。并且操作简单
具体操作实现
Windows API的程序架构,主程序文件"canvas.cpp",由以下几个函数组成: WinMain(主程序,程序起始点),WinProc(自定义函数,处理程序消息),MyRegisterClass(自定义函数,注册窗口类别),Init(自定义函数,初始化)
项目源代码如下
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h> //随机数种子用
#include "mmsystem.h"//导入声音头文件
#pragma comment(lib,"winmm.lib")//导入声音头文件库
struct BULLET //子弹结构
{
int x,y;
bool exist;
};
struct MONSTER //僵尸结构
{
int x,y;
int blood;
bool exist;
};
struct STONE
{
int x,y;
bool exist;
};
HINSTANCE hInst; //各种变量初始化
HBITMAP dra[6],bg[2],pl[5],bullet,k[2],over,win,life[5],stone;
HDC hdc,mdc,bufdc,buffdc;
HWND hWnd;
DWORD tPre,tNow;
int x,y;
int num,bcount=300;
int count=0;
BULLET b[300];
STONE s;
MONSTER m[6];
int q[5]={-50,20,110,200,300},qi=rand()%5,wi=rand()%5,ei=rand()%5,ri=rand()%5,ti=rand()%5;
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 >= 100)
MyPaint(hdc);
}
}
return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
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)
{
char filename[20] = "";
HBITMAP bmp;
hInst = hInstance;
hWnd = CreateWindow("canvas", "菜园保卫战" , WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
MoveWindow(hWnd,10,10,640,480,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);
buffdc = CreateCompatibleDC(hdc);
bmp = CreateCompatibleBitmap(hdc,640,480);
SelectObject(mdc,bmp);
k[0]=(HBITMAP)LoadImage(NULL,"k0.bmp",IMAGE_BITMAP,700,150,LR_LOADFROMFILE);
k[1]=(HBITMAP)LoadImage(NULL,"k1.bmp",IMAGE_BITMAP,700,150,LR_LOADFROMFILE);
life[0]=(HBITMAP)LoadImage(NULL,"life1.bmp",IMAGE_BITMAP,310,60,LR_LOADFROMFILE);
life[1]=(HBITMAP)LoadImage(NULL,"life2.bmp",IMAGE_BITMAP,310,60,LR_LOADFROMFILE);
life[2]=(HBITMAP)LoadImage(NULL,"life3.bmp",IMAGE_BITMAP,310,60,LR_LOADFROMFILE);
life[3]=(HBITMAP)LoadImage(NULL,"life4.bmp",IMAGE_BITMAP,310,60,LR_LOADFROMFILE);
life[4]=(HBITMAP)LoadImage(NULL,"life5.bmp",IMAGE_BITMAP,310,60,LR_LOADFROMFILE);
dra[0] = (HBITMAP)LoadImage(NULL,"dra1.bmp",IMAGE_BITMAP,800,300,LR_LOADFROMFILE);
dra[1] = (HBITMAP)LoadImage(NULL,"dra2.bmp",IMAGE_BITMAP,800,300,LR_LOADFROMFILE);
dra[2] = (HBITMAP)LoadImage(NULL,"dra3.bmp",IMAGE_BITMAP,800,300,LR_LOADFROMFILE);
dra[3] = (HBITMAP)LoadImage(NULL,"dra4.bmp",IMAGE_BITMAP,800,300,LR_LOADFROMFILE);
dra[4] = (HBITMAP)LoadImage(NULL,"dra5.bmp",IMAGE_BITMAP,800,300,LR_LOADFROMFILE);
dra[5] = (HBITMAP)LoadImage(NULL,"dra6.bmp",IMAGE_BITMAP,1200,300,LR_LOADFROMFILE);
bg[0] = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE);
bg[1] = (HBITMAP)LoadImage(NULL,"bg1.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE);
pl[0] = (HBITMAP)LoadImage(NULL,"pl1.bmp",IMAGE_BITMAP,150,100,LR_LOADFROMFILE);
pl[1] = (HBITMAP)LoadImage(NULL,"pl2.bmp",IMAGE_BITMAP,150,100,LR_LOADFROMFILE);
pl[2] = (HBITMAP)LoadImage(NULL,"pl3.bmp",IMAGE_BITMAP,150,100,LR_LOADFROMFILE);
pl[3] = (HBITMAP)LoadImage(NULL,"pl4.bmp",IMAGE_BITMAP,150,100,LR_LOADFROMFILE);
pl[4] = (HBITMAP)LoadImage(NULL,"pl5.bmp",IMAGE_BITMAP,150,100,LR_LOADFROMFILE);
bullet = (HBITMAP)LoadImage(NULL,"bullet.bmp",IMAGE_BITMAP,30,30,LR_LOADFROMFILE);
stone=(HBITMAP)LoadImage(NULL,"stone.bmp",IMAGE_BITMAP,150,100,LR_LOADFROMFILE);
over = (HBITMAP)LoadImage(NULL,"over.bmp",IMAGE_BITMAP,640,159,LR_LOADFROMFILE);
win = (HBITMAP)LoadImage(NULL,"win.bmp",IMAGE_BITMAP,640,160,LR_LOADFROMFILE);
m[0].x=m[1].x=m[2].x=m[3].x=m[4].x=m[5].x=640; //怪兽体征值初始化
m[5].y=110;
m[5].blood=20;
m[0].exist=m[1].exist=m[2].exist=m[3].exist=m[4].exist=true;
m[5].exist=false;
s.exist=false;
MyPaint(hdc);
return TRUE;
}
void MyPaint(HDC hdc)
{
int i = 0;
if(num == 8)
num = 0;
char str[50]="";
int score;
PlaySound("foot.wav", NULL, SND_FILENAME | SND_ASYNC);
srand((unsigned int)time(0)); &