我 刚学游戏编程时写的程序,对初学者可能有用,希望大家喜欢 :-)
执行文件下载:
http://www.cppblog.com/Files/billhsu/2037.rar
源代码下载:
http://www.cppblog.com/Files/billhsu/Source_2037.rar
/*
********************
紫日2037 RPG 游戏
合肥八中 许仕鹏 制作
********************
*/
#define WIN32_LEAN_AND_MEAN //定义不使用MFC库
#include <windows.h>
#include "Pub_Def.h"
#include <stdio.h>
#include<stdlib.h>
//定义人物方向
#define Role_UP 0
#define Role_DOWN 1
#define Role_LEFT 2
#define Role_RIGHT 3
//定义动作
#define Act_1 0
#define Act_2 1
#define Act_3 2
//定义图片的宽度和高度
#define Bmp_Width 32
#define Bmp_Height 64
RECT rect;
//全局变量,用来存储窗口的句柄
HWND hwnd;
//地图的Struct
struct Map_struct{
char name[10];
int NpcNo;
int Width;
int Height;
int **Data;
}Map[MAX_MAPS];
//人物的Struct
struct NPCs_struct{
int seeable;
int name;
int NPC_Status;
int Pos_y;
int Pos_x;
};
//指向一个场景中的人物
struct NPC_struct{
NPCs_struct *NPCs;
}NPC[MAX_MAPS];
HDC windowDC = NULL;//窗口DC
HDC MainRoleDC = NULL;//主角DC
HDC memDC =NULL;//记忆DC
HDC msgboxDC=NULL;//消息框DC
HDC MapDC=NULL;//装地图图片的DC
HDC TmpMapDC=NULL;//画出地图的DC
HDC TmpSceneDC=NULL;//画出场景DC
HDC FaceDC=NULL;//人物脸DC
HDC Boy1DC=NULL;//男孩1DC
HDC Boy2DC=NULL;//男孩2DC
HDC Girl1DC=NULL;//女孩1DC
HDC Girl2DC=NULL;//女孩2DC
HDC DiagDC=NULL;//DiagDC
HFONT Font;//字体物件
//以下为图片物件,和上面的DC对应
HBITMAP MainRoleBMP = NULL;
HBITMAP memBMP=NULL;
HBITMAP msgboxBMP=NULL;
HBITMAP MapBMP=NULL;
HBITMAP TmpMapBMP=NULL;
HBITMAP TmpSceneBMP=NULL;
HBITMAP FaceBMP=NULL;
HBITMAP Boy1BMP=NULL;
HBITMAP Boy2BMP=NULL;
HBITMAP Girl1BMP=NULL;
HBITMAP Girl2BMP=NULL;
HBITMAP DiagBMP=NULL;
unsigned int active;//本窗口是否为活动
UINT width;//屏幕宽
UINT height;//屏幕高
int Role_X;//人物X坐标
int Role_Y;//人物Y坐标
int Role_Act;//人物动作
int Role_Status;//人物状态(上、下、左、右)
int Map_y=0;//地图Y坐标
int Map_x=0;//地图X坐标
int Scene=0;//场景编号
int ObjTmp;
int ObjTmp_x;
int ObjTmp_y;
int hStory=0;//故事编号
//以下为判断状态的变量
bool enter_1=false;
bool enter_2=false;
bool enter_3=false;
bool enter_4=false;
bool enter_5=false;
bool enter_6=false;
bool enter_7=false;
bool enter_8=false;
bool enter_9=false;
bool enter_10=false;
bool visit_1=false;
bool visit_2=false;
bool visit_3=false;
bool visit_4=false;
bool Get_Sanshi=false;
bool Get_Sunzi=false;
bool enter_zuo=false;
bool visit_zuo=false;
bool talk_apsh=false;
bool talk_czn=false;
bool talk_xz=false;
bool Get_sun=false;
//以下为函数定义
HRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);//消息处理
void DoMain(void);//主循环
void Move(void);//移动主角
void Delay(long time);//延时
void ClearScr(void);//清屏
void Draw(void);//将记忆DC画到窗口DC
void GameTitle(void);//游戏开头字幕
void PrintText(char * chr,int x,int y);//打印文字
void TalkText(int face,char * chr,char * Obj_Name);//人物对话
void MoveNPC(int NPC_Num,int NPC_Dt);//移动人物
void CheckThings(void);//检查物品
bool InitMap(void);//装载地图*.map
void DrawTmpMap(int Old_Scene);//画临时地图
int GetObj(int Obj_y,int Obj_x);//获取物品
void CheckEvent(int Obj_y,int Obj_x);//检查事件
bool CanMove(int Obj_y,int Obj_x);//可以走吗?
void CheckSysMsg(void);//检察系统消息
void ReleaseObjs(void);//释放物件及DC
void DrawTmpScene(int Old_Scene);//画临时场景
void DrawInCheckEvent(void);//CheckEvent函数中的画函数
void DrawInMove(void);//Move函数中的画函数
void DrawInTalkText(void);//TalkText函数中的画函数
void Enter_Scene(char * chr);
HDC const NameToDC(int tempint) ;//将图片号变成DC
int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow)
{
int x;
int y;
int x1;
int y1;
MSG msg;
WNDCLASSEX WinClass;
WinClass.cbSize = sizeof(WNDCLASSEX);
WinClass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; // 标准设置
WinClass.lpfnWndProc = WndProc; //
WinClass.cbClsExtra = 0; // 附加窗口信息,这里不使用
WinClass.cbWndExtra = 0; // 附加窗口信息,这里不使用
WinClass.hInstance = hInstance;
WinClass.hIcon = LoadIcon(NULL, IDI_WINLOGO); //图标
WinClass.hCursor = LoadCursor(NULL, IDC_ARROW); // 鼠标指针
WinClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
WinClass.lpszMenuName = NULL; // 不使用菜单
WinClass.lpszClassName = "Window Class"; //
WinClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); //
RegisterClassEx(&WinClass);
width=GetSystemMetrics(SM_CXSCREEN);
height= GetSystemMetrics(SM_CYSCREEN);
if (!(hwnd = CreateWindowEx(NULL, // 附加样式,不使用
"Window Class", // 类定义
"紫日2037", // 标题
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU| WS_VISIBLE,
width/2-160,height/2-160,325,320 , // 窗口大小位置
NULL, //
NULL, //
hInstance, //
NULL))) //
ShowWindow(hwnd, nCmdShow);
UpdateWindow (hwnd);
windowDC=GetDC(hwnd);
//创建字体
Font=CreateFont(20,
0,
0,
0,
400,
FALSE,
FALSE,
0,
GB2312_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS,
"黑体");
SelectObject(windowDC,Font);
SetBkMode(windowDC,TRANSPARENT);
SetTextColor(windowDC,RGB(255,0,0));
TextOut(windowDC,100,130,"Loading...",strlen("Loading..."));
MainRoleBMP=CreateCompatibleBitmap(windowDC,width,height);
MainRoleDC=CreateCompatibleDC(windowDC);
memDC=CreateCompatibleDC(windowDC);
memBMP=CreateCompatibleBitmap(windowDC,width,height);
msgboxDC=CreateCompatibleDC(windowDC);
MapDC=CreateCompatibleDC(windowDC);
TmpMapDC=CreateCompatibleDC(windowDC);
TmpMapBMP=CreateCompatibleBitmap(windowDC,width,height);
TmpSceneDC=CreateCompatibleDC(windowDC);
TmpSceneBMP=CreateCompatibleBitmap(windowDC,width,height);
FaceDC=CreateCompatibleDC(windowDC);
FaceBMP=CreateCompatibleBitmap(windowDC,width,height);
Boy1DC=CreateCompatibleDC(windowDC);
Boy1BMP=CreateCompatibleBitmap(windowDC,width,height);
Boy2DC=CreateCompatibleDC(windowDC);
Boy2BMP=CreateCompatibleBitmap(windowDC,width,height);
Girl1DC=CreateCompatibleDC(windowDC);
Girl1BMP=CreateCompatibleBitmap(windowDC,width,height);
Girl2DC=CreateCompatibleDC(windowDC);
Girl2BMP=CreateCompatibleBitmap(windowDC,width,height);
DiagDC=CreateCompatibleDC(windowDC);
DiagBMP=CreateCompatibleBitmap(windowDC,width,height);
//绑定字体和DC
SelectObject(memDC,Font);
SetBkMode(memDC,TRANSPARENT);
SetTextColor(memDC,RGB(255,0,0));
InitMap();
//装载图片
MainRoleBMP=(HBITMAP)LoadImage(NULL,"Res//Role.bmp",IMAGE_BITMAP,Bmp_Width*6,Bmp_Height*4,LR_LOADFROMFILE);
msgboxBMP=(HBITMAP)LoadImage(NULL,"Res//msgbox.bmp",IMAGE_BITMAP,320,84,LR_LOADFROMFILE);
MapBMP=(HBITMAP)LoadImage(NULL,"Res//Map.bmp",IMAGE_BITMAP,32,1280,LR_LOADFROMFILE);
FaceBMP=(HBITMAP)LoadImage(NULL,"Res//Face.bmp",IMAGE_BITMAP,32,320,LR_LOADFROMFILE);
Boy1BMP=(HBITMAP)LoadImage(NULL,"Res//Boy1.bmp",IMAGE_BITMAP,Bmp_Width*6,Bmp_Height*4,LR_LOADFROMFILE);
Boy2BMP=(HBITMAP)LoadImage(NULL,"Res//Boy2.bmp",IMAGE_BITMAP,Bmp_Width*6,Bmp_Height*4,LR_LOADFROMFILE);
Girl1BMP=(HBITMAP)LoadImage(NULL,"Res//Girl1.bmp",IMAGE_BITMAP,Bmp_Width*6,Bmp_Height*4,LR_LOADFROMFILE);
Girl2BMP=(HBITMAP)LoadImage(NULL,"Res//Girl2.bmp",IMAGE_BITMAP,Bmp_Width*6,Bmp_Height*4,LR_LOADFROMFILE);
DiagBMP=(HBITMAP)LoadImage(NULL,"Res//Diag.bmp",IMAGE_BITMAP,230,40,LR_LOADFROMFILE);
//绑定图片和DC
SelectObject(MainRoleDC,MainRoleBMP);
SelectObject(memDC,memBMP);
SelectObject(msgboxDC,msgboxBMP);
SelectObject(MapDC,MapBMP);
SelectObject(TmpMapDC,TmpMapBMP);
SelectObject(FaceDC,FaceBMP);
SelectObject(TmpSceneDC,TmpSceneBMP);
SelectObject(Boy1DC,Boy1BMP);
SelectObject(Boy2DC,Boy2BMP);
SelectObject(Girl1DC,Girl1BMP);
SelectObject(Girl2DC,Girl2BMP);
SelectObject(DiagDC,DiagBMP);
//初始值设置
GameTitle();
Role_X=4;
Role_Y=3;
Role_Status=Role_DOWN;
Role_Act=Act_1;
x=Role_X*Bmp_Width;
y=Role_Y*Bmp_Width;
x1=Role_Act*Bmp_