這篇文章主要為大家詳細介紹了C語言實現——《打磚塊項目》,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下!
游戲介紹:
在游戲中,玩家通過按住并滑動擋板下的圓點控制擋板左右移動,接住擊打磚塊而改變飛行軌跡掉落下來的小球。在游戲界面的左側有個速度控制器,玩家可一邊接球,一邊控制它。上下滑動調整小球的飛行速度。速度越快風險越大,當然獎勵和風險是成正比的。越快的速度得分會越多,反之速度越慢得分會越少。(本項目并沒有設計速度調整這一塊,大家可以自己完善一下)
項目技術:
主要是數組、結構體、繪圖技術、按鍵操作和定時器等,對邏輯也是有一定的要求,但是這些在我們項目源碼里面都會有注釋,大家到時候學習的時候千萬不要忽略注釋,注釋可以更好地幫你理解代碼,尤其是C語言初學者。
本項目編譯環境:VS2019/VS2013;
插件:圖形庫插件easyX,涉及圖片素材可以自行百度找也可以關注文末領取;
源代碼示例:
//畫磚塊
int map[5][8]; //描述整個地圖
HWND hwnd = NULL;
//用1-3 給數組賦值
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));
//結構體指針->成員 ->指針指向運算符
//(*指針).成員;
pBoard->x = x;
pBoard->y = y;
pBoard->speed = speed;
pBoard->color = color;
//結構體變量.成員
(*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函數 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.當前下標下,數組中不等于表示有磚塊需要反射
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.收尾工作 :游戲結束
//5.定時器
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, "you die", "gameOver", MB_OK);
exit(0);
}
if (gameOver())
{
MessageBox(hwnd, "win game", "gameOver", MB_OK);
exit(0);
}
FlushBatchDraw();
}
EndBatchDraw();
closegraph();
return 0;
}
-
C語言
+關注
關注
180文章
7630瀏覽量
140197 -
代碼
+關注
關注
30文章
4886瀏覽量
70237
原文標題:C語言項目實戰:《打磚塊》零基礎項目!225 行源碼注釋示例
文章出處:【微信號:cyuyanxuexi,微信公眾號:C語言編程學習基地】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
單片機C語言實例(350+例)
深入理解C語言:C語言循環控制

為什么學了C語言,卻寫不出像樣的項目?

單片機C語言編程寶典大全,初學必備【強推下載!】
限時免積分下載:增量式與位置式PID算法的C語言實現分享
EE-188:使用C語言在ADSP-219x DSP上實現中斷驅動系統

AKI跨語言調用庫神助攻C/C++代碼遷移至HarmonyOS NEXT
基于FPGA實現圖像直方圖設計

FPGA打磚塊小游戲設計思路
深視智能SD-C系列微型激光位移傳感器在激光打標視覺引導中的應用

評論