打磚塊游戲是一種動作電子游戲的名稱,玩家操作一根螢幕上水平的“棒子”,讓一顆不斷彈來彈去的“球”在撞擊作為過關目標消去的“磚塊”的途中不會落到螢幕底下,球碰到磚塊、棒子與底下以外的三邊會反彈,落到底下會失去一顆球,把磚塊全部消去就可以破關,

始祖是美國英寶格公司(en:Atari Games,ja:アタリ (ゲーム))于1976年推出的街機游戲“Breakout”(en:Breakout),由該公司在1972年發行的“PONG”(en:PONG,ja:ポン (ゲーム),世界上第一款電子游戲,類似臺球)改良而來,相較于其前作,一個人就可以玩與變化豐富這兩項特點讓Breakout相當賣座,使各家公司競相模仿,
因為規則簡單與游戲性,現在許多移動電話都有內建打磚塊游戲,也有許多因特網小游戲版本,目前在網上可以輕易查到,
我們今天就來自己寫經典游戲《打磚塊》
游戲目標:消除所有的方塊即可過關,操作指南:游戲中使用鍵盤方向鍵←→控制移動
OK,了解游戲的基本操作以及游戲玩法之后就可以開始我們的編程之旅了,今天我會一步代碼一個圖片的來分布展示,希望這種方式可以讓大家更容易的理解,如果有其他更好的方式,也歡迎大家向我提出建議
首先是創建一個游戲視窗,我們用EasyX圖形庫來做,只需要一行代碼
hwnd = initgraph(800, 800);
這樣我們就創建了一個800*800的視窗,非常簡單,非常好用,這也是非常適合初學者去嘗試的,這里我們寫在主函式里面就可以了

接下來就是我們的老朋友結構體了,木板、球、以及磚塊,這沒什么好說的,不管啥專案用結構體都是很常見的
//木板的程序
struct Board
{
int x;
int y;
int speed;
COLORREF color;
int width;
int height;
};
//struct Board board = { 300, 800 - 25,1, WHITE, 200, 25 };
struct Board* createBoard(int x, int y, int speed, COLORREF color, int width, int height)
{
struct Board* pBoard = (struct Board*)malloc(sizeof(struct Board));
//結構體指標->成員 ->指標指向運算子
//(*指標).成員;
pBoard->x = x;
pBoard->y = y;
pBoard->speed = speed;
pBoard->color = color;
//結構體變數.成員
(*pBoard).width = width;
(*pBoard).height = height;
return pBoard;
}
//球:
struct Ball
{
int x;
int y;
int r; //半徑
int dx;
int dy;
COLORREF color;
};
struct Ball* createBall(int x, int y, int r, int dx, int dy, COLORREF color)
{
struct Ball* pBall = (struct Ball*)malloc(sizeof(struct Ball));
pBall->x = x;
pBall->y = y;
pBall->r = r;
pBall->dx = dx;
pBall->dy = dy;
pBall->color = color;
return pBall;
}
后面就是我們來畫我們的游戲界面了(磚塊、球、以及木板),這我是分開寫的,可以更好的理解
void drawMap()
{
setlinestyle(PS_SOLID, 2);
setlinecolor(WHITE);
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 8; j++)
{
int x = 100 * j; //j=x/100
int y = 25 * i; //i=y/i
switch (map[i][j]) //map[i][j]!=0
{
case 0: //做消除用的
break;
case 1:
setfillcolor(YELLOW);
fillrectangle(x, y, x + 100, y + 25);
break;
case 2:
setfillcolor(LIGHTBLUE);
fillrectangle(x, y, x + 100, y + 25);
break;
case 3:
setfillcolor(LIGHTGREEN);
fillrectangle(x, y, x + 100, y + 25);
break;
}
}
}
}
void drawBoard(struct Board* pBoard)
{
setfillcolor(pBoard->color);
fillrectangle(pBoard->x, pBoard->y,
pBoard->x + pBoard->width, pBoard->y + pBoard->height);
}
void drawBall(struct Ball* pBall)
{
setfillcolor(pBall->color);
solidcircle(pBall->x, pBall->y, pBall->r);
}
做完之后我們就可以看到這樣的界面了

到現在我們的基本游戲界面就已經出來了,現在差的就是判斷邏輯問題了,這也是我們的重點中的重點,包括球的移動、球的彈射角度、木板的移動、磚塊的消失、游戲的輸贏判斷都需要我們考慮到,希望大家可以好好看,好好學!
首先是木板的移動函式,我們就簡單控制了,因為他只用左右移就行
//木板的按鍵操作
void keyDown(struct Board* pBoard)
{
//C語言: scanf函式 getch() getchar() gets()
//異步的按鍵操作
if (GetAsyncKeyState('A') || GetAsyncKeyState(VK_LEFT) && pBoard->x >= 0)
{
pBoard->x -= pBoard->speed;
}
if (GetAsyncKeyState('D') || GetAsyncKeyState(VK_RIGHT) && pBoard->x <= 800 - 200)
{
pBoard->x += pBoard->speed;
}
}
接下來就是球的移動函式
void moveBall(struct Ball* pBall, struct Board* pBoard)
{
if (pBall->x - pBall->r <= 0 || pBall->x + pBall->r >= 800)
{
pBall->dx = -pBall->dx;
}
if (pBall->y - pBall->r <= 0 || hitBoard(pBall, pBoard) || hitBricks(pBall))
{
pBall->dy = -pBall->dy;
}
pBall->x += pBall->dx;
pBall->y += pBall->dy;
球的反射以及撞擊木板時的判斷函式
//1.反射
//2.撞擊木板
int hitBoard(struct Ball* pBall, struct Board* pBoard)
{
if (pBall->y + pBall->r == pBoard->y) //y滿足
{
if (pBall->x >= pBoard->x && pBall->x <= pBoard->x + pBoard->width)
{
return 1;
}
}
return 0;
}
球撞擊磚塊的判斷函式
//3.撞擊磚塊
int hitBricks(struct Ball* pBall)
{
//1.算出球的行的列是屬于地圖
int ballJ = pBall->x / 100;
int ballI = (pBall->y - pBall->r) / 25;
//2.當前下標下,陣列中不等于表示有磚塊需要反射
if (ballJ < 8 && ballI < 5 && map[ballI][ballJ] != 0)
{
map[ballI][ballJ] = 0;
return 1;
}
return 0;
}
在這個程序中還需要一個定時器,我們來定義一個定時器,記住呼叫頭檔案<time.h>
int Timer(time_t num, int id)
{
static time_t start[10];
time_t end = clock();
if (end - start[id]>num)
{
start[id] = end;
return 1;
}
return 0;
}
游戲結束的判斷函式
int gameOver()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 8; j++)
{
if (map[i][j] != 0)
{
return 0;
}
}
}
return 1;
}
最后是我們的主函式
int main()
{
srand((unsigned int)time(0)); //設定亂數的范圍跟隨時間改變而改變
hwnd = initgraph(800, 800);
struct Board* pBoard = createBoard(300, 800 - 25, 5, WHITE, 200, 25);
struct Ball* pBall = createBall(400, 600, 15, 5, -5, RED);
initMap();
BeginBatchDraw();
while (1)
{
cleardevice();
drawMap();
drawBoard(pBoard);
drawBall(pBall);
if (Timer(10, 0))
moveBall(pBall, pBoard);
keyDown(pBoard);
if (die(pBall))
{
MessageBox(hwnd, L"you die", L"gameOver", MB_OK);
exit(0);
}
if (gameOver())
{
MessageBox(hwnd, L"win game", L"gameOver", MB_OK);
exit(0);
}
FlushBatchDraw();
}
EndBatchDraw();
closegraph();
return 0;
}
經典游戲《打磚塊》完成,OK,簡單總結一下,代碼不難,邏輯也不難,重要是大家一定要自己動手去做,這是毋庸置疑的,編程沒有捷徑,只有不斷的學習熟練,加強自己的能力,有條件的話找個老師的話效果會更好,好了,希望大家可以在這里得到自己想要的知識以及快樂吧,也希望大家可以給UP主一個關注,非常感謝大家了!!!

后續UP主還會發布更多的專案原始碼以及學習資料,希望大家可以持續關注,有什么問題可以回帖留言,我盡量回答,想要C/C++學習資料以及其他專案的原始碼的可以加群【1083227756】了解,想要對程式員的未來發展有興趣的也可加群閑聊,也可以關注微信公眾號:【狐貍的編碼時光】,希望和大家一起學習進步!!!
點擊下方鏈接進群更快拿到學習資料以及專案素材
進群領取學習資料以及專案原始碼https://jq.qq.com/?_wv=1027&k=sttR3REF
https://jq.qq.com/?_wv=1027&k=sttR3REF
點擊下方鏈接進入視頻講解
C/C++游戲《打磚塊》視頻詳細教程https://www.bilibili.com/video/BV1ur4y1C727/
https://www.bilibili.com/video/BV1ur4y1C727/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/356846.html
標籤:其他
上一篇:關于掃雷的簡易實作
下一篇:掃雷 第二次完成較復雜游戲感悟
