⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.cpp

📁 这是我用hoho5.2.0.6引擎写的坦克游戏,采用c语言,写了个矩形碰撞函数可以方便游戏里处理碰撞事件.游戏写的很简单.对想熟悉hoho引擎或游戏编程的人有很大的帮助
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// test.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "test.h"
#define MAX_LOADSTRING 100
#define TIMER1 1100 //用于子弹
#define TIMER2 1101  //用于bot选取方向
#define TIMER3 1102  //用于bot移动
#define KEY_DOWN(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
// 全局变量:
HINSTANCE hInst;								// 当前实例
TCHAR szTitle[MAX_LOADSTRING];					// 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING];			// 主窗口类名
   HWND hWnd;
LP_FONT	g_pFont	= NULL;  //输出文字的句柄
POINT				pointMousePos;				// 鼠標坐標數據的保存結構
bool g_bActive = true;  //窗口状态
LP_BITMAPX			g_pEggBitmap	= NULL;

LP_BITMAPX         pic_tank_up =NULL;        //tank
LP_BITMAPX         pic_tank_down =NULL;      //tank
LP_BITMAPX         pic_tank_left =NULL;      //tank
LP_BITMAPX         pic_tank_right =NULL;     //坦克
LP_BITMAPX         pic_tank_bullet =NULL;    //子弹图片

LP_BITMAPX         pic_bot_tank_up =NULL;        //机器人坦克
LP_BITMAPX         pic_bot_tank_down =NULL;      //机器人坦克
LP_BITMAPX         pic_bot_tank_left =NULL;      //机器人坦克
LP_BITMAPX         pic_bot_tank_right =NULL;     //机器人坦克


int xoffset=13;      //坦克子弹发射与坦克x坐标的偏移
int yoffset=13;      //坦克子弹发射点与坦克y坐标的偏移
int tank_way=0;      //坦克方向
int tank_speed=2;    //坦克移动速度
int x=0,y=0;
bool isBulletshot=false;  //子弹是否发射
struct
{
 int inway_x;    //前进中的x
 int inway_y;    //前进中的y
 int bot_way;      //前进方向
 int bot_waysize;   //前进路径长度
}Bot,Bot2;
struct
{
 int start_x;    //开始x
 int start_y;    //开始y
 int end_x;      //结束x
 int end_y;      //结束y
 int inway_x;    //前进中的x
 int inway_y;    //前进中的y
 int buletway;   //子弹方向
 int buletwaysize;   //子弹路径长度
}Bullet;//子弹结构
// 此代码模块中包含的函数的前向声明:
void KeyBoard();//键盘事件
struct MYRECT//矩形
{
   float x,y;//左上角坐标
   float width;//宽
   float height;//高
};
bool    IsInArea(MYRECT rect1,MYRECT rect2);	//判断是否相交
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
long SystemMessage(); //系统消息循环
void render();  //渲染
void shot();  //发射
int bulletrun();//子弹发射
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: 在此放置代码。
	MSG msg;
	HACCEL hAccelTable;

	// 初始化全局字符串
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_TEST, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// 执行应用程序初始化:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TEST);
	render();

SAFE_DELETE(g_pDisplay); //释放资源
return (int) msg.wParam;
}
long SystemMessage()
{
	MSG msg;
	if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
		{
			if( GetMessage( &msg, NULL, 0, 0 ) )
			{
				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
			else
			{
				return 1;
			}
		}
	return 0;

}
void render()
{

	while(true)
	{
		SystemMessage();
		if(g_bActive)//窗口激活状态
				{
					KeyBoard();
					GetGraphics()->ClearScreenMMX(0); // 用黑色清屏
                
					g_pDisplay->Render();
					switch (tank_way)
					{
					case 0:
					GetGraphics()->DrawBitmapMMX( x, y, pic_tank_up, SCREENBUFFER,true);//使用color键显示
						//	GetGraphics()->PutBitmap( x, y, pic_tank_up, PUT_TYPE_BITMAP);
						break;
					case 1:
							GetGraphics()->DrawBitmapMMX( x, y, pic_tank_down, SCREENBUFFER,true);//使用color键显示
						//		GetGraphics()->DrawBitmapMMX( x, y, pic_tank_down, SCREENBUFFER );
						//	GetGraphics()->PutBitmap( x, y, pic_tank_down, PUT_TYPE_BITMAP);
						break;
					case 2:
						GetGraphics()->DrawBitmapMMX( x, y, pic_tank_left, SCREENBUFFER,true);//使用color键显示
							//	GetGraphics()->DrawBitmapMMX( x, y, pic_tank_left, SCREENBUFFER );
						//	GetGraphics()->PutBitmap( x, y, pic_tank_left, PUT_TYPE_BITMAP);
						break;
					case 3:
							GetGraphics()->DrawBitmapMMX( x, y, pic_tank_right, SCREENBUFFER,true);//使用color键显示
							//	GetGraphics()->DrawBitmapMMX( x, y, pic_tank_right, SCREENBUFFER );
						//	GetGraphics()->PutBitmap( x, y, pic_tank_right, PUT_TYPE_BITMAP);
						break;
					}
					//GetGraphics()->PutBitmap( 120, 90, g_pEggBitmap, PUT_TYPE_BITMAP );
					if(isBulletshot)
					{
						GetGraphics()->DrawBitmapMMX( Bullet.inway_x, Bullet.inway_y, pic_tank_bullet, SCREENBUFFER,true);//使用color键显示
					}
					switch (Bot.bot_way)
					{
					case 0:
						GetGraphics()->DrawBitmapMMX( Bot.inway_x, Bot.inway_y, pic_bot_tank_up, SCREENBUFFER,true);//使用color键显示
						break;
					case 1:
						GetGraphics()->DrawBitmapMMX( Bot.inway_x, Bot.inway_y, pic_bot_tank_down, SCREENBUFFER,true);//使用color键显示
					
						break;
					case 2:
						GetGraphics()->DrawBitmapMMX(Bot.inway_x,Bot.inway_y, pic_bot_tank_left, SCREENBUFFER,true);//使用color键显示
						break;
					case 3:
							GetGraphics()->DrawBitmapMMX( Bot.inway_x, Bot.inway_y, pic_bot_tank_right, SCREENBUFFER,true);//使用color键显示
						break;
					}
					
					switch (Bot2.bot_way)
					{
					case 0:
						GetGraphics()->DrawBitmapMMX( Bot2.inway_x, Bot2.inway_y, pic_bot_tank_up, SCREENBUFFER,true);//使用color键显示
						break;
					case 1:
						GetGraphics()->DrawBitmapMMX( Bot2.inway_x, Bot2.inway_y, pic_bot_tank_down, SCREENBUFFER,true);//使用color键显示
					
						break;
					case 2:
						GetGraphics()->DrawBitmapMMX(Bot2.inway_x,Bot2.inway_y, pic_bot_tank_left, SCREENBUFFER,true);//使用color键显示
						break;
					case 3:
							GetGraphics()->DrawBitmapMMX( Bot2.inway_x, Bot2.inway_y, pic_bot_tank_right, SCREENBUFFER,true);//使用color键显示
						break;
					}
						char temp[200];
						sprintf(temp,"Bot1 x:%d  y:%d",Bot.inway_x,Bot.inway_y);
						GetGraphics()->DrawFont(SCREENBUFFER,30,60,g_pFont,RGB2Hi(12,238,0),temp);
						char temp2[200];
						sprintf(temp2,"Bot2 x:%d  y:%d",Bot2.inway_x,Bot2.inway_y);
							GetGraphics()->DrawFont(SCREENBUFFER,30,80,g_pFont,RGB2Hi(12,238,0),temp2);

		// 更新缓冲区
		GetGraphics()->UpdateScreen( );
		GetGraphics()->Present( );
				}else
					Sleep(1);

	}
}
void shot()//发射
{
	isBulletshot=true;
	
	if(tank_way==0)//上
	{
		Bullet.start_x=x+xoffset;
		Bullet.start_y=y;
		Bullet.inway_x=x+xoffset;
		Bullet.inway_y=y;
		Bullet.buletway=0;
		Bullet.buletwaysize=y;
		Bullet.end_x=x+xoffset;
		Bullet.end_y=0;
	}else if(tank_way==1)//下
	{
		Bullet.start_x=x+xoffset;
		Bullet.start_y=y+20;
		Bullet.inway_x=x+xoffset;
		Bullet.inway_y=y+20;
		Bullet.buletway=1;
		Bullet.buletwaysize=572-y-20;
		Bullet.end_x=x+xoffset;
		Bullet.end_y=572;
	}else if(tank_way==2)//左
	{
		Bullet.start_x=x+xoffset;
		Bullet.start_y=y+yoffset;
		Bullet.inway_x=x+xoffset;
		Bullet.inway_y=y+yoffset;
		Bullet.buletway=2;
		Bullet.buletwaysize=x+xoffset;
		Bullet.end_x=0;
		Bullet.end_y=y+yoffset;
	}else if(tank_way==3)//右
	{
		Bullet.start_x=x+xoffset;
		Bullet.start_y=y+yoffset;
		Bullet.inway_x=x+xoffset;
		Bullet.inway_y=y+yoffset;
		Bullet.buletway=3;
		Bullet.buletwaysize=770-x-xoffset;
		Bullet.end_x=770;
		Bullet.end_y=y+yoffset;
	}
  SetTimer(hWnd,TIMER1,10,0);
}
void KeyBoard()
//770 572
{
	if(KEY_DOWN('W'))
	{
	    tank_way=0;//向上
		if(y<=0) y=0;
		else y-=tank_speed;
	}else if(KEY_DOWN('S'))
	{
	    tank_way=1;//向下
		if(y>=572) y=571;
		else y+=tank_speed;
	}else if(KEY_DOWN('A'))
	{
	   tank_way=2;//左
	    if(x<=0) x=0;
			else x-=tank_speed;
	}else if(KEY_DOWN('D'))
	{
	  tank_way=3;//右
	    if(x>=770) x=769;
			else  x+=tank_speed;
	}
	if(KEY_DOWN(' ')&&isBulletshot==false)
	{
	   shot();//发射
	}
}
//
//  函数: MyRegisterClass()
//
//  目的: 注册窗口类。
//
//  注释: 
//
//    仅当希望在已添加到 Windows 95 的
//    “RegisterClassEx”函数之前此代码与 Win32 系统兼容时,
//    才需要此函数及其用法。调用此函数
//    十分重要,这样应用程序就可以获得关联的
//   “格式正确的”小图标。
//
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			= LoadIcon(hInstance, (LPCTSTR)IDI_TEST);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;//(LPCTSTR)IDC_TEST;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}
//
//   函数: InitInstance(HANDLE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释: 
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{


   hInst = hInstance; // 将实例句柄存储在全局变量中

	hWnd = CreateWindow(szWindowClass, szTitle, WS_POPUP,//WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);
    g_pDisplay=CreatePlane2D(hWnd,800,600,1);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -