第一人称射击游戏 C++控制台版(未完成)

视频:https://www.bilibili.com/video/BV14z4y1r7wX?p=2&t=188

一、在控制台中显示画面

使用cout方式显示时刷新速度较慢,不能满足游戏需求。 游戏的显示借助windows函数。

#include<iostream>
using namespace std;

#include<Windows.h>

//定义屏幕宽度、高度
int nScreenWidth = 120;
int nScreenHeight = 40;

int main()
{
	// Create Screen Buffer 创建屏幕缓冲区
	wchar_t *screen = new wchar_t[nScreenWidth*nScreenHeight];
	HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
	SetConsoleActiveScreenBuffer(hConsole);
	DWORD dwBytesWritten = 0;

	//游戏循环
	while (1)
	{
		// Display Frame    显示
		screen[nScreenWidth * nScreenHeight - 1] = '\0';
		WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);

	}
	
}

二、

地图

//地图
int nMapWidth = 16;				// World Dimensions
int nMapHeight = 16;
//玩家位置X、Y、角度
float fPlayerX = 0.0f;			// Player Start Position
float fPlayerY = 0.0f;
float fPlayerA = 0.0f;			// Player Start Rotation


// 创建地图   Create Map of world space # = wall block, . = space
	wstring map;
	map += L"################";
	map += L"#..............#";
	map += L"#..............#";
	map += L"#..............#";
	map += L"#..............#";
	map += L"#..............#";
	map += L"#..............#";
	map += L"#..............#";
	map += L"#..............#";
	map += L"#..............#";
	map += L"#..............#";
	map += L"#..............#";
	map += L"#..............#";
	map += L"#..............#";
	map += L"#..............#";
	map += L"################";

地图与实际的对应

 

  

(玩家视角有限)           

(玩家视线)

通过视线与墙面的碰撞检测,生成玩家看到的画面。

for (int x = 0; x < nScreenWidth; x++)
		{
			// For each column, calculate the projected ray angle into world space
			float fRayAngle = (fPlayerA - fFOV / 2.0f) + ((float)x / (float)nScreenWidth) * fFOV;//从左半边到右半边
			float fDistanceToWall = 0.0f; //  
			bool bHitWall = false;

			float fEyeX = sinf(fRayAngle); //视线方向对应的单位分量 Unit vector for ray in player space
			float fEyeY = cosf(fRayAngle);

			//增量方式  判断视线撞墙
			while (!bHitWall && fDistanceToWall<fDepth) 
			{
				fDistanceToWall += 0.1f;
				int nTestX = (int)(fPlayerX + fEyeX * fDistanceToWall);
				int nTestY = (int)(fPlayerY + fEyeY * fDistanceToWall);

				// 检查视线达到边界  Test if ray is out of bounds
				if (nTestX < 0 || nTestX >= nMapWidth || nTestY < 0 || nTestY >= nMapHeight)
				{
					bHitWall = true;			// Just set distance to maximum depth
					fDistanceToWall = fDepth;
				}
				else
				{
					//检查视线是否遇到墙体
					if (map[nTestY*nMapWidth + nTestX] == '#')
					{
						bHitWall = true;
					}
				}
			}
			//计算到天花板和地板的距离
			int nCeiling = (float)(nScreenHeight / 2.0) - nScreenHeight/((float)fDistanceToWall);
			int nFloor = nScreenHeight - nCeiling;



			for (int y = 0; y < nScreenHeight; y++)
			{
				// Each Row
				if (y < nCeiling) //天花板
					screen[y*nScreenWidth + x] = ' ';
				else if (y > nCeiling && y <= nFloor)
					screen[y*nScreenWidth + x] = '#';
				else // 地板 Floor
				{
					
					screen[y*nScreenWidth + x] = ' ';
				}
			}
		}
		

     三、左右移动,其中用tp2-tp1来获得一帧花费的时间,使得运动看起来更流畅。

#include<chrono>          //时间相关	
    auto tp1 = chrono::system_clock::now();
	auto tp2 = chrono::system_clock::now();

	//游戏循环
	while (1)
	{
		//计算时间
		tp2 = chrono::system_clock::now();
		chrono::duration<float>elapsedTime = tp2 - tp1;
		tp1 = tp2;
		float fElapsedTime = elapsedTime.count(); //一帧的时间

		// Handle CCW Rotation
		if (GetAsyncKeyState((unsigned short)'A') & 0x8000)
			fPlayerA -= (0.1f)*fElapsedTime;

		if (GetAsyncKeyState((unsigned short)'D') & 0x8000)
			fPlayerA += (0.1f)*fElapsedTime;

                 

做到这里(视频的19:06),当我继续往下做的时候发现图像显示的很奇怪,方块都串行显示,看起来像乱码。

但是参考之前的俄罗斯方块中的控制台设置方法,改变控制台之后还是没有改变。

没有找到解决方法,但猜测是和控制台相关的问题(使用作者代码运行也是乱码https://github.com/OneLoneCoder/videos/blob/master/OneLoneCoder_CommandLineFPS.cpp)。

 

 

用DDraw实现射击游戏说明文档 要点一:画图自动切割 IDirectDrawSurface7::BltFast()方法中没有自动切割功能,即当画图元素超出窗口以外时不会自动切割,DDraw选择自动忽略不画,造成一旦超出窗口,画图元素会突然消失。 解决这一问题的方法是手动切割,代码如下: //自动切割 RECT scRect; //存放当前窗口大小区域 ZeroMemory( &scRect, sizeof( scRect ) ); GetWindowRect( GetActiveWindow(), &scRect ); //防止图片左上角超过窗口左上角 if ( x < 0 ) { m_rect.left -= x; x = 0; } if ( y scRect.right ? scRect.right : x; y = y > scRect.bottom ? scRect.bottom : y; m_rect.right = x + m_rect.right - m_rect.left > scRect.right ? scRect.right - x + m_rect.left : m_rect.right; m_rect.bottom = y + m_rect.bottom - m_rect.top > scRect.bottom ? scRect.bottom - y + m_rect.top : m_rect.bottom; 只需将上述代码加在CGraphic::BltBBuffer() 中的m_bRect = m_rect; 前即可。 要点二:背景的滚轴实现 画背景可以分为以下三种情况: 情况一:背景图片与窗口等高 情况二:背景图片高度小于窗口高度 情况三:背景图片高度大于窗口高度 上述讲解图与代码相对应地看,有助于容易理解。 另外,要点一实现之后,由于已经可以自动切割,画背景可以用其它方法。 要点三:精灵图的实现 在游戏中,如RPG游戏中的人物图、射击游戏的飞机、爆炸等,叫做精灵图。 精灵图实际上是将所有帧的图片放在一个文件中,游戏时靠一个RECT来控制画图像文件中的哪一部分,进而控制游戏显示哪一帧图,只需控制好RECT的位置即可。如下图: 控制RECT的四个角的坐标的移动,有以下代码: if (m_timeEnd – m_timeStart > 100) //只有到了100ms之后才绘图 { m_ImageID++; if(m_ImageID - m_beginID >= num) { m_ImageID = m_beginID; //最后一帧的下一帧是第一帧 } m_timeStart = timeGetTime(); } int id = m_ImageID++; SetRect(&m_rect, 41 * id, 0, 41 * (id + 1), 41); //飞机精灵图大小是41×41 m_pGraph->BltBBuffer(m_pImageBuffer, true, m_Pos.x, m_Pos.y, m_rect); 这样就实现了精灵动画的效果。 要点四:拿STL进行子弹的实现 子弹的实现可以使用STL中的vector,当按下开火键时发出一颗子弹,就往vector中添加一个结点;当子弹飞出窗口或击中敌机时,再将结点从vector中删除。每帧游戏画面中子弹飞行时只需将vector中的所有子弹进行处理、绘画即可。 参考代码如下: 1.添加子弹 if (g_ctrlDown) //当ctrl键按下时开炮! { m_BulletEnd = m_Gtime->GetTime(); if ((m_BulletEnd - m_BulletStart) * 1000 > 120) //如果连续按着开火键不放,这里控制不会发出太多子弹 { m_BulletStart = m_BulletEnd; MBULLET tmpBullet; tmpBullet.pos.x = m_SPos.x - 1; //记录开火时的子弹位置 tmpBullet.pos.y = m_SPos.y - 26; tmpBullet.speed = 5; //该子弹的飞行速度 m_BulletList.push_back(tmpBullet); //将子弹添加到vector中
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只大鸽子

如有帮助,欢迎关注同名公众号

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值