C語言實現(xiàn)2D賽車游戲的示例代碼
一、簡介
此游戲是《2D 賽車》的”魔改版“——2.5D 雙人賽車!
原作實現(xiàn)了 2D 視角的賽車游戲,但是我覺得不夠真實、操縱感不強,故擠出數(shù)個周末完成了這個”魔改版“,實現(xiàn)了第一人稱的視角。
二、如何建立一個地圖包
1. 選擇賽車,音樂,地圖的素材
2. 在程序的 map 文件夾下建立一個文件夾將這些素材放入
3. 建立 set.ini 文件
三、關(guān)于碰撞圖的繪制
碰撞圖色彩格式:
- 黑色(0xFFFFFF)為賽道,減速較小
- 黃色(0xFFFF00)為沙地,會減速
- 藍色(0x0000FF)為冰面,會滑動
- 紅色(0xFF0000)為圍欄,無法通過
- 綠色(0xFFFFFF)為終點線
- 灰色(0xAAAAAA)為終點線的兩端,用于判定方向(對應色值 RGB(170,170,170))
- 紫色(0xFF00FF)為玩家 2 起點
- 白色(0x000000)為玩家 1 起點
注意事項:
終點線最好為寬度為一的直線!
灰色只有兩個像素點,分布在終點線的兩端點
畫碰撞圖寧可把道路畫粗一點,因為實際游戲中可能會因為碰撞圖畫的太窄,導致玩家好像在路上但是被卡住了
此外,設置玩家起始位置也要考慮玩家車輛的長度,如果玩家的起始位置離終點線太近,以至于玩家的車尾超過了終點線的話,那么開動車子的時候就會觸發(fā)一次車子越過終點的判定。
四、游戲時的說明
- 上/W 向前開
- 下/S 倒車
- 左/A 左轉(zhuǎn)
- 右/D 右轉(zhuǎn)
如果卡在墻里就按住一個方向鍵加向前直到出去
五、如何更好地繪制賽場圖與碰撞圖
利用 PS 的自由路徑和描邊,一定要用鉛筆。
游戲截圖
六、實現(xiàn)代碼
#include <graphics.h> // 引用圖形庫頭文件 #include <ctime> #include <sstream> #include <fstream> #include <vector> #include <conio.h> // 播放 MP3 所需 #include <mmsystem.h> #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; // 目標圈數(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 { vector<bottom> botlist; bool MouseTouch(int left, int top, int right, int bottom, MOUSEMSG m) // 鼠標區(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() // 顯示并等待按鍵被響應,返回相應的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; }; vector<intro> IntroList; 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); // 坐標與數(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)
到此這篇關(guān)于C語言實現(xiàn)2D賽車游戲的示例代碼的文章就介紹到這了,更多相關(guān)C語言賽車游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++中為何推薦要把基類析構(gòu)函數(shù)設置成虛函數(shù)
這篇文章主要介紹了C++中為何推薦要把基類析構(gòu)函數(shù)設置成虛函數(shù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12C語言報錯:Format String Vulnerability的多種解決方案
Format String Vulnerability(格式化字符串漏洞)是C語言中常見且嚴重的安全漏洞之一,它通常在程序使用不受信任的輸入作為格式化字符串時發(fā)生,本文將詳細介紹Format String Vulnerability的產(chǎn)生原因,提供多種解決方案,需要的朋友可以參考下2024-06-06一文詳解C++關(guān)鍵字nullptr及與NULL的區(qū)別
這篇文章主要給大家詳細介紹了C++關(guān)鍵字nullptr,及?NULL與nullptr的區(qū)別,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下2023-06-06