目錄
一、簡介
二、如何建立一個地圖包
三、關(guān)于碰撞圖的繪制
四、游戲時的說明
五、如何更好地繪制賽場圖與碰撞圖?
游戲截圖
源碼下載
此游戲是《2D 賽車》的”魔改版“——2.5D 雙人賽車!
原作實現(xiàn)了 2D 視角的賽車游戲,但是我覺得不夠真實、操縱感不強,故擠出數(shù)個周末完成了這個”魔改版“,實現(xiàn)了第一人稱的視角。
二、如何建立一個地圖包1. 選擇賽車,音樂,地圖的素材
2. 在程序的 map 文件夾下建立一個文件夾將這些素材放入
3. 建立 set.ini 文件
詳情參考工程下的文件夾 a 和 b
碰撞圖色彩格式:
黑色(0xFFFFFF)為賽道,減速較小
黃色(0xFFFF00)為沙地,會減速
藍(lán)色(0x0000FF)為冰面,會滑動
紅色(0xFF0000)為圍欄,無法通過
綠色(0xFFFFFF)為終點線
灰色(0xAAAAAA)為終點線的兩端,用于判定方向(對應(yīng)色值 RGB(170,170,170))
紫色(0xFF00FF)為玩家 2 起點
白色(0x000000)為玩家 1 起點
注意事項:
終點線最好為寬度為一的直線!
灰色只有兩個像素點,分布在終點線的兩端點
畫碰撞圖寧可把道路畫粗一點,因為實際游戲中可能會因為碰撞圖畫的太窄,導(dǎo)致玩家好像在路上但是被卡住了
此外,設(shè)置玩家起始位置也要考慮玩家車輛的長度,如果玩家的起始位置離終點線太近,以至于玩家的車尾超過了終點線的話,那么開動車子的時候就會觸發(fā)一次車子越過終點的判定。
上/W 向前開
下/S 倒車
左/A 左轉(zhuǎn)
右/D 右轉(zhuǎn)
如果卡在墻里就按住一個方向鍵加向前直到出去
預(yù)設(shè)的地圖包為 a、b,在工程文件夾下
利用 PS 的自由路徑和描邊,一定要用鉛筆。
游戲截圖#include // 引用圖形庫頭文件
#include#include#include#include#include// 播放 MP3 所需
#include#pragma comment(lib,"Winmm.lib")
using namespace std;
#define CMD_UP 1
#define CMD_DOWN 2
#define CMD_LEFT 4
#define CMD_RIGHT 8
#define sCMD_UP 16
#define sCMD_DOWN 32
#define sCMD_LEFT 64
#define sCMD_RIGHT 128
#define CMD_QUIT 256
#define PI 3.1415926
int MaxSpeed = 8; // 大速度
int FinSand = 5; // 在沙上的摩擦力
int FinRoad = 1; // 在路上的摩擦力
int FinIce = -2; // 在冰上的摩擦力
int SpeedAdd = 2; // 加速度
int Rota = 64; // 轉(zhuǎn)動速度的 -1 次方
int NeedR = 5; // 目標(biāo)圈數(shù)
int WIDE = 1280;
int HEIGHT = 960;
double EndLineForward = 0; // 終點角度
bool inIce;
bool inRoad;
bool inSand;
bool inWall;
bool inEndline;
IMAGE Racing; // 賽場地圖
IMAGE Toucher; // 碰撞圖
IMAGE car1;
IMAGE car2;
IMAGE Player1;
int Px = 150;
int Py = 150;
double PForward = 0; // 方向
int Pspeed = 0; // 速度
int Ppass = 0; // 通過幾次終點
bool Pwrong = false; // 是否逆行
bool PHadPass = false; // 是否通過終點
bool PWaitOut = false; // 是否等待通過終點
bool Pover = false; // 是否結(jié)束
clock_t Ptime = 0;
clock_t Ptime2 = 0;
IMAGE Player2;
int Cx = 170;
int Cy = 170;
double CForward = 0;
int Cspeed = 0;
int Cpass = 0;
bool Cwrong = false;
bool CHadPass = false;
bool CWaitOut = false;
bool Cover = false;
clock_t Ctime = 0;
clock_t Ctime2 = 0;
bool TwoPlayer = true;
bool isres = true;
bool chexit = false;
bool MeumMod = false;
clock_t Start = 0;
clock_t Now = 0;
clock_t MeumUsed = 0;
struct bottom // 簡易的按鈕實現(xiàn)
{
int ID;
int x;
int y;
int wide;
int heigh;
RECT a;
wstring str;
COLORREF fillcolor;
COLORREF linecolor;
COLORREF textcolor;
LOGFONT textstyle;
UINT uFormat;
bottom(int gID, int gx, int gy, int gw, int gh, wstring gs)
{
fillcolor = getfillcolor();
linecolor = getlinecolor();
textcolor = gettextcolor();
gettextstyle(&textstyle);
uFormat = DT_CENTER | DT_VCENTER | DT_SINGLELINE;
ID = gID;
x = gx;
y = gy;
wide = gw;
heigh = gh;
str = gs;
a = { x, y, x + wide, y + heigh };
}
};
struct page
{
vectorbotlist;
bool MouseTouch(int left, int top, int right, int bottom, MOUSEMSG m) // 鼠標(biāo)區(qū)域判定
{
for (int i1 = left; i1< right; i1++)
{
for (int i2 = top; i2< bottom; i2++)
{
if (m.x == i1 && m.y == i2)
{
return true;
}
}
}
return false;
}
int ShownPage() // 顯示并等待按鍵被響應(yīng),返回相應(yīng)的ID值
{
COLORREF fillcolor = getfillcolor();
COLORREF linecolor = getlinecolor();
COLORREF textcolor = gettextcolor();
LOGFONT textstyle;
gettextstyle(&textstyle);
MOUSEMSG m;
setbkmode(TRANSPARENT);
for (unsigned int i = 0; i< botlist.size(); i++)
{
setfillcolor(botlist[i].fillcolor);
setlinecolor(botlist[i].linecolor);
settextcolor(botlist[i].textcolor);
settextstyle(&botlist[i].textstyle);
fillrectangle(botlist[i].x, botlist[i].y, botlist[i].x + botlist[i].wide, botlist[i].y + botlist[i].heigh);
drawtext(botlist[i].str.c_str(), &botlist[i].a, botlist[i].uFormat);
}
FlushBatchDraw();
while (true)
{
FlushMouseMsgBuffer();
m = GetMouseMsg();
if (m.mkLButton)
{
for (unsigned int i = 0; i< botlist.size(); i++)
{
if (MouseTouch(botlist[i].x, botlist[i].y, botlist[i].x + botlist[i].wide, botlist[i].y + botlist[i].heigh, m))
{
return botlist[i].ID;
}
}
}
}
setfillcolor(fillcolor);
setlinecolor(linecolor);
settextcolor(textcolor);
settextstyle(&textstyle);
}
};
struct intro // 地圖的介紹信息
{
wstring filename;
wstring title;
wstring intr;
wstring inipath;
};
vectorIntroList;
class timer // 計時器
{
private:
bool is_start = false;
clock_t start;
public:
bool WaitFor(clock_t s)
{
if (is_start)
{
if ((start + s)<= clock())
{
is_start = false;
return true;
}
else
{
return false;
}
}
else
{
start = clock();
is_start = true;
return false;
}
}
};
void init();
void gaming();
int GetCommand();
void DispatchCommand(int _cmd);
void OnLeft(bool player); // false 玩家 1,true 玩家 2
void OnRight(bool player);
void OnUp(bool player);
void OnDown(bool player);
void MoveCheck(bool player); // 碰撞判定
int PointTsm(int x, int y, int wide, int high); // 坐標(biāo)與數(shù)值的轉(zhuǎn)換
void Draw();
void End();
void PutImgWithout(IMAGE &obj, int px, int py, COLORREF withouter, DWORD* pbWnd, int wX, int wY, DWORD bitsub); // 放置圖片,除了
void SetBirth(); // 第一次讀取
void StartWord();
void Loading(); // 加載地圖
int ChooseMap(); // 選擇地圖
void LoadIntro(string File);
BOOL SearchFilesByWildcard(string wildcardPath); // 搜索文件,參考自https://blog.csdn.net/faithzzf/article/details/54290084
IMAGE zoomImage(IMAGE* pImg, int newWidth, int newHeight); // 圖片縮放
void showhelp(); // 顯示幫助文件
void clean(); // 清空緩沖區(qū)
void restart(); // 用于重新開始游戲
bool CanRota (bool player);//是否可以旋轉(zhuǎn)
源碼下載群文件自行下載
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
分享名稱:C語言零基礎(chǔ)項目:2D賽車游戲,詳細(xì)思路+源碼分享-創(chuàng)新互聯(lián)
分享鏈接:http://www.rwnh.cn/article8/dghsop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、域名注冊、網(wǎng)站設(shè)計、App設(shè)計、網(wǎng)站內(nèi)鏈、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容