每天一個(gè)C語(yǔ)言小項(xiàng)目,提升你的編程能力!
用VS寫了一個(gè)小小的游戲,在界面右側(cè)有運(yùn)行時(shí)間,接到的小球個(gè)數(shù)等信息,有 10 個(gè)小球下落,玩家可以控制一個(gè)盒子左右移動(dòng)(方向鍵),來(lái)接小球,按 Esc 鍵退出,最后會(huì)顯示接到的小球的數(shù)目/分?jǐn)?shù)。
游戲就是這么簡(jiǎn)單,不過(guò)也很考驗(yàn)大家的反應(yīng)能力的哦!
游戲運(yùn)行截圖如下:
簡(jiǎn)單了解游戲后我們就來(lái)試試吧!
本項(xiàng)目編譯環(huán)境:Visual Studio 2019/2022,EasyX插件
代碼展示:
#include#include #include #include // 定義常量 #define NUM 10 #define CMD_LEFT 1 #define CMD_RIGHT 2 #define CMD_QUIT 4 int box_x = 10; int box_y = 420; // 定義球的結(jié)構(gòu)體 struct Ball { int x, y, v; }; // 獲取用戶控制 int GetCommand() { int c = 0; if (GetAsyncKeyState(VK_LEFT) & 0x8000) c |= CMD_LEFT; if (GetAsyncKeyState(VK_RIGHT) & 0x8000) c |= CMD_RIGHT; if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) c |= CMD_QUIT; return c; } // 倒計(jì)時(shí) int Time(int t) { char strsec[10]; int sec = 20 - (GetTickCount() - t) / 1000; itoa(sec, strsec, 10); outtextxy(570, 110, " "); outtextxy(570, 110, strcat(strsec, "s")); return sec; } // 介紹 void menu() { line(449, 0, 449, 480); char runTime[] = "游戲倒計(jì)時(shí) : ", receiveBallNum[] = "接到的球的數(shù)量:", copyRight[] = "版權(quán)所有:C語(yǔ)言編程", finishWorkDate[] = "完成日期:2023年1月7日", introductiona[] = "按方向鍵控制盒子移動(dòng)接住", introductionb[] = "小球,倒計(jì)時(shí)為0時(shí)游戲結(jié)束"; settextcolor(GREEN); outtextxy(450, 10, introductiona); outtextxy(450, 30, introductionb); outtextxy(450, 110, runTime); outtextxy(450, 210, receiveBallNum); outtextxy(450, 310, copyRight); outtextxy(450, 410, finishWorkDate); } // 產(chǎn)生隨機(jī)球 void ballRandom(Ball ball[], int i) { ball[i].x = 16 + 45 * i; ball[i].y = 8 + rand() % 32; ball[i].v = 1 + rand() % 5; } // 畫球,并計(jì)算得分 void calculateScore(Ball ball[], int& score) { for (int i = 0; i < NUM; i++) { fillcircle(ball[i].x, ball[i].y, 8); if (ball[i].y >= 472) { ballRandom(ball, i); continue; } if (box_x + 8 <= ball[i].x && ball[i].x <= box_x + 72 && ball[i].y >= 412) { score++; ballRandom(ball, i); } } } // 主函數(shù) int main() { // 初始化 initgraph(640, 480); srand(time(NULL)); BeginBatchDraw(); setlinecolor(GREEN); setfillcolor(WHITE); menu(); Ball ball[NUM]; int dx, i, c, score = 0; bool flag = true; for (i = 0; i < NUM; i++) { ballRandom(ball, i); } int t = GetTickCount(); char strScore[10], str[] = "your score:"; // 游戲主循環(huán) while (flag) { dx = 0; // 顯示得分 char strScore[10]; itoa(score, strScore, 10); outtextxy(570, 210, strScore); // 畫球,并計(jì)算得分 calculateScore(ball, score); // 畫盒子 fillrectangle(box_x, box_y, box_x + 80, box_y + 60); FlushBatchDraw(); // 獲取用戶控制命令 c = GetCommand(); if (c & CMD_LEFT) dx = -10; if (c & CMD_RIGHT) dx = 10; if (c & CMD_QUIT) flag = false; if (!Time(t)) flag = false; // 延時(shí) Sleep(25); // 擦除游戲區(qū) clearrectangle(0, 0, 448, 480); // 計(jì)算球的新坐標(biāo) for (i = 0; i < NUM; i++) { ball[i].y += ball[i].v; } // 移動(dòng)盒子 box_x += dx; if (box_x < 0) box_x = 0; if (box_x > 368) box_x = 368; } // 清空鍵盤緩沖區(qū) FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE)); // 輸出游戲結(jié)果 itoa(score, strScore, 10); outtextxy(222, 240, strcat(str, strScore)); outtextxy(220, 300, "按任意鍵退出"); EndBatchDraw(); // 按任意鍵退出 getch(); closegraph(); return 0; }
大家趕緊去動(dòng)手試試吧!
-
游戲
+關(guān)注
關(guān)注
2文章
767瀏覽量
26690 -
C語(yǔ)言
+關(guān)注
關(guān)注
180文章
7630瀏覽量
140256 -
編程
+關(guān)注
關(guān)注
88文章
3679瀏覽量
94862 -
源碼
+關(guān)注
關(guān)注
8文章
667瀏覽量
30136
原文標(biāo)題:C語(yǔ)言項(xiàng)目:接球小游戲(自制)!詳細(xì)思路+源碼分享
文章出處:【微信號(hào):cyuyanxuexi,微信公眾號(hào):C語(yǔ)言編程學(xué)習(xí)基地】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
評(píng)論