這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)——《打磚塊項(xiàng)目》,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下!
游戲介紹:
在游戲中,玩家通過按住并滑動(dòng)擋板下的圓點(diǎn)控制擋板左右移動(dòng),接住擊打磚塊而改變飛行軌跡掉落下來的小球。在游戲界面的左側(cè)有個(gè)速度控制器,玩家可一邊接球,一邊控制它。上下滑動(dòng)調(diào)整小球的飛行速度。速度越快風(fēng)險(xiǎn)越大,當(dāng)然獎(jiǎng)勵(lì)和風(fēng)險(xiǎn)是成正比的。越快的速度得分會(huì)越多,反之速度越慢得分會(huì)越少。(本項(xiàng)目并沒有設(shè)計(jì)速度調(diào)整這一塊,大家可以自己完善一下)
項(xiàng)目技術(shù):
主要是數(shù)組、結(jié)構(gòu)體、繪圖技術(shù)、按鍵操作和定時(shí)器等,對邏輯也是有一定的要求,但是這些在我們項(xiàng)目源碼里面都會(huì)有注釋,大家到時(shí)候?qū)W習(xí)的時(shí)候千萬不要忽略注釋,注釋可以更好地幫你理解代碼,尤其是C語言初學(xué)者。
本項(xiàng)目編譯環(huán)境:VS2019/VS2013;
插件:圖形庫插件easyX,涉及圖片素材可以自行百度找也可以關(guān)注文末領(lǐng)?。?/p>
源代碼示例:
//畫磚塊
int map[5][8]; //描述整個(gè)地圖
HWND hwnd = NULL;
//用1-3 給數(shù)組賦值
void initMap()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 8; j++)
{
map[i][j] = rand() % 3 + 1;
}
}
}
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;
}
}
}
}
//木板的過程
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));
//結(jié)構(gòu)體指針->成員 ->指針指向運(yùn)算符
//(*指針).成員;
pBoard->x = x;
pBoard->y = y;
pBoard->speed = speed;
pBoard->color = color;
//結(jié)構(gòu)體變量.成員
(*pBoard).width = width;
(*pBoard).height = height;
return pBoard;
}
void drawBoard(struct Board* pBoard)
{
setfillcolor(pBoard->color);
fillrectangle(pBoard->x, pBoard->y,
pBoard->x + pBoard->width, pBoard->y + pBoard->height);
}
//木板的按鍵操作
void keyDown(struct Board* pBoard)
{
//C語言: scanf函數(shù) 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;
}
}
//球:
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 drawBall(struct Ball* pBall)
{
setfillcolor(pBall->color);
solidcircle(pBall->x, pBall->y, pBall->r);
}
//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;
}
int die(struct Ball* pBall)
{
if (pBall->y > 800 - 25)
{
return 1;
}
return 0;
}
//3.撞擊磚塊
int hitBricks(struct Ball* pBall)
{
//1.算出球的行的列是屬于地圖
int ballJ = pBall->x / 100;
int ballI = (pBall->y - pBall->r) / 25;
//2.當(dāng)前下標(biāo)下,數(shù)組中不等于表示有磚塊需要反射
if (ballJ < 8 && ballI < 5 && map[ballI][ballJ] != 0)
{
map[ballI][ballJ] = 0;
return 1;
}
return 0;
}
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;
}
//4.收尾工作 :游戲結(jié)束
//5.定時(shí)器
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)); //設(shè)置隨機(jī)數(shù)的范圍跟隨時(shí)間改變而改變
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, "you die", "gameOver", MB_OK);
exit(0);
}
if (gameOver())
{
MessageBox(hwnd, "win game", "gameOver", MB_OK);
exit(0);
}
FlushBatchDraw();
}
EndBatchDraw();
closegraph();
return 0;
}
-
C語言
+關(guān)注
關(guān)注
180文章
7605瀏覽量
136930 -
代碼
+關(guān)注
關(guān)注
30文章
4790瀏覽量
68653
原文標(biāo)題:C語言項(xiàng)目實(shí)戰(zhàn):《打磚塊》零基礎(chǔ)項(xiàng)目!225 行源碼注釋示例
文章出處:【微信號:cyuyanxuexi,微信公眾號:C語言編程學(xué)習(xí)基地】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論