每天一個(gè)C語言小項(xiàng)目,提升你的編程能力!
《吃豆游戲》是一款休閑小游戲,和貪吃蛇,球球大作戰(zhàn)吃食物都是有差不多的游戲邏輯。
這個(gè)游戲本身很簡(jiǎn)單,一共3關(guān),吃掉畫面上全部小豆子就過關(guān),被敵人碰到就over。吃大豆子可以讓敵人暫時(shí)能被你吃,但是后面關(guān)卡,敵人恢復(fù)時(shí)間很短,敵人速度也很快。
當(dāng)然現(xiàn)在你用代碼自己開發(fā)的話,設(shè)置多少關(guān)卡,什么樣的難度都看你自己咯!
游戲中使用了圖片【https://codebus.cn/f/a/0/0/135/boxman.zip】
本項(xiàng)目編譯環(huán)境:Visual Studio 2019/2022,EasyX插件
代碼展示:
#include#include #include #include #define M 640 //開屏大小 #define N 480 #define rank 1 //游戲等級(jí) int g_sum = 0; //統(tǒng)計(jì)吃豆的個(gè)數(shù) int g_score = 0; //分?jǐn)?shù)統(tǒng)計(jì) bool g_win = true; //成敗的標(biāo)志 IMAGE image[5]; //裝載圖片 enum {LEFT=1, RIGHT, UP, DOWN}; //方向的控制 struct figure //人物屬性 { int x; //人物圖紙坐標(biāo) int y; int x1; //人物方向移動(dòng) int y1; int x2; //人物物理坐標(biāo) int y2; }you,enemy[5]; /*0豆子,1可以移動(dòng)地方,2墻壁,3自己,4敵人*/ int a[15][20]= {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,1,0,0,0,0,1,0,1,0,0,0,1,1,1,1,1,1,0,2, 2,1,2,2,2,1,1,2,1,1,0,0,0,1,1,4,1,1,0,2, 2,1,1,0,2,1,1,2,0,1,1,2,2,2,2,2,0,0,0,2, 2,4,1,0,2,1,1,2,1,1,1,0,1,1,1,1,0,1,1,2, 2,1,2,1,2,1,1,2,1,3,2,2,1,1,1,1,2,2,1,2, 2,1,2,1,2,1,1,1,1,1,1,1,1,0,0,0,1,1,1,2, 2,1,2,1,0,1,1,1,1,2,1,0,1,2,2,2,1,1,1,2, 2,1,0,1,0,1,2,1,1,2,1,0,1,2,1,1,4,1,1,2, 2,1,0,2,0,1,2,1,1,2,1,0,1,2,1,1,1,1,1,2, 2,1,0,2,1,1,2,1,1,2,1,0,2,2,1,0,0,0,1,2, 2,1,1,2,1,1,2,1,1,2,1,0,2,1,1,2,2,1,1,2, 2,1,2,2,1,2,2,1,1,1,1,0,1,4,1,2,0,0,1,2, 2,1,0,0,0,0,0,4,0,1,1,0,1,1,1,1,0,0,1,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};/*數(shù)組就是地圖*/ int b[15][20]= {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,1,0,0,0,0,1,0,1,0,0,0,1,1,1,1,1,1,0,2, 2,1,2,2,2,1,1,2,1,1,0,0,0,1,1,4,1,1,0,2, 2,1,1,0,2,1,1,2,0,1,1,2,2,2,2,2,0,0,0,2, 2,4,1,0,2,1,1,2,1,1,1,0,1,1,1,1,0,1,1,2, 2,1,2,1,2,1,1,2,1,3,2,2,1,1,1,1,2,2,1,2, 2,1,2,1,2,1,1,1,1,1,1,1,1,0,0,0,1,1,1,2, 2,1,2,1,0,1,1,1,1,2,1,0,1,2,2,2,1,1,1,2, 2,1,0,1,0,1,2,1,1,2,1,0,1,2,1,1,4,1,1,2, 2,1,0,2,0,1,2,1,1,2,1,0,1,2,1,1,1,1,1,2, 2,1,0,2,1,1,2,1,1,2,1,0,2,2,1,0,0,0,1,2, 2,1,1,2,1,1,2,1,1,2,1,0,2,1,1,2,2,1,1,2, 2,1,2,2,1,2,2,1,1,1,1,0,1,4,1,2,0,0,1,2, 2,1,0,0,0,0,0,4,0,1,1,0,1,1,1,1,0,0,1,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};/*數(shù)組就是地圖*/ ////////////////////////////函數(shù)聲明///////////////////////////////// void Init_begin(); //初始化開始界面 void Initia(); //初始化界面 void Init_score(); //記分板 void Init_loss(); //失敗的界面 void Init_win(); //成功的界面 void Init_leave(); //離開的界面 void Init_copy(); //圖形復(fù)制 void enemymove(); //敵人運(yùn)動(dòng) void randdire(struct figure *sth); //使方向隨即改變 bool judgemeet(); //相遇判斷 void youmove(int k ,int speed) ; //對(duì)自己進(jìn)行移動(dòng) void mobile(); //自己移動(dòng) void eatbean(); //判斷是否吃小黃點(diǎn) void play(); //開始游戲 int sjs(int a,int b); //產(chǎn)生a,b之間的數(shù) void enemymeetroad(int i); //敵人與路 void enemymeetbean(int i) ; //敵人遇豆子 void load_image(); //加載圖片 void Game_levels(); //游戲等級(jí)設(shè)定 ///////////////////////////////////////////////////////////////////// void Init_begin() //初始化開始界面 { setbkcolor(BLACK); char init[20] = "吃豆子游戲"; int x = M/2 - 250,y = 100; setcolor(CYAN); setfont(100, 0,"楷體",0,0,FW_DEMIBOLD,false,false,false); outtextxy(x,y,init); Sleep(400); RECT r = {x + 50, 250, 540, 460}; setfont(24, 0, "宋體"); setcolor(BROWN); outtextxy(x + 50, 250,"吃豆子游戲規(guī)則:"); setfont(18, 0, "宋體"); drawtext(" 1:游戲中,操作者可以通過方向鍵(上、下、左、右)或字母鍵(‘w’、‘s’、‘a(chǎn)’、‘d’)來調(diào)整方向 2:游戲中途可以按‘空格鍵’來暫停游戲 3:按‘ESC’推出游戲", &r, DT_WORDBREAK); setfont(24, 0,"楷體"); setcolor(DARKGRAY); outtextxy(M/2 - 107,y + 300, "按"); setcolor(RED); outtextxy(M/2 - 80, y + 300, "回車鍵"); setcolor(DARKGRAY); outtextxy(M/2 , y + 300, "開始游戲"); char put ; put = getch(); while(put != 13) put = getch(); } /*0豆子,1可以移動(dòng)地方,2墻壁,3自己,4敵人*/ void Initia() //初始化界面 { cleardevice(); setbkcolor(RGB(72,12,12)); for( int i = 0;i < 15;i++) for(int j = 0;j < 20;j++) putimage(100+j*20,100+i*20,&image[a[i][j]]); you.x=9; you.y=5; //敵人和自己的初始圖紙坐標(biāo) enemy[0].x=15; enemy[0].y=2; enemy[1].x=1; enemy[1].y=4; enemy[2].x=16; enemy[2].y=8; enemy[3].x=13; enemy[3].y=12; enemy[4].x=7; enemy[4].y=13; you.x2 = you.x * 20 + 100; //圖紙坐標(biāo)轉(zhuǎn)化為屏幕坐標(biāo) you.y2 = you.y * 20 + 100; for(i=0;i<5;i++) { enemy[i].x2 = enemy[i].x * 20 + 100; enemy[i].y2 = enemy[i].y * 20 + 100; enemy[i].x1 = 0; enemy[i].y1 = 1; } Init_score(); } void load_image() //加載圖片 { loadimage(&image[0],"IMAGE", "0.jpg"); loadimage(&image[1],"IMAGE", "1.jpg"); loadimage(&image[2],"IMAGE", "2.jpg"); loadimage(&image[3],"IMAGE", "3.jpg"); loadimage(&image[4],"IMAGE", "4.jpg"); } void Init_score() //記分板 { setbkcolor(RGB(72,12,12)); char infor[10]; itoa(g_score,infor,10); setcolor(CYAN); setfont(28,0,"Arial"); outtextxy(M - 140,90,"當(dāng)前分?jǐn)?shù)為"); setfillstyle(RGB(72,12,12)); bar(M - 40,140,M - 20,170); outtextxy(M - 80,140,infor); } int sjs(int a,int b) //產(chǎn)生a,b之間的數(shù) { return a+rand()%(b-a+1); } void Init_copy() //圖形復(fù)制 { for(int i = 0;i < 15;i++) for(int j = 0;j < 20;j++) a[i][j] = b[i][j]; } void enemymeetbean(int i) //敵人遇豆子 { int x = enemy[i].x2 ; int y = enemy[i].y2 ; for(int j = 0;j<20;j++) { putimage(x,y,&image[0]); enemy[i].x2 += enemy[i].x1; enemy[i].y2 += enemy[i].y1; putimage(enemy[i].x2,enemy[i].y2,&image[4]); } } void enemymeetroad(int i) //敵人遇路 { int x = enemy[i].x2 ; int y = enemy[i].y2 ; for( int j = 0;j < 20;j++) { putimage(x,y,&image[1]); enemy[i].x2 += enemy[i].x1; enemy[i].y2 += enemy[i].y1; putimage(enemy[i].x2,enemy[i].y2,&image[4]); } } void enemymove() //敵人運(yùn)動(dòng) { Game_levels(); for(int i = 0;i < 5;i++) //判斷是否與墻壁相遇,如果相遇,隨即改變方向 { if(a[enemy[i].y+enemy[i].y1][enemy[i].x+enemy[i].x1] == 2 || abs(enemy[i].y1) == abs(enemy[i].x1)) randdire(&enemy[i]); } for(i = 0;i < 5;i++) { if(a[enemy[i].y ][enemy[i].x] == 0) enemymeetbean(i); else enemymeetroad(i); enemy[i].x+=enemy[i].x1; enemy[i].y+=enemy[i].y1; if(judgemeet()) break; } Sleep(180); } void Game_levels() //游戲等級(jí)設(shè)定 { if( sjs (1,rank) == 1) { int t = rand() % 5; enemy[t].x1 = enemy[t].x - you.x > 0 ? -1 : 1; enemy[t].y1 = enemy[t].y - you.y > 0 ? -1 : 1; } for(int i = 0;i < 5;i++) { if(enemy[i].x == you.x && enemy[i].y + 1 == you.y) {enemy[i].x1 = 0 ; enemy[i].y1 = 1;} if(enemy[i].x == you.x && enemy[i].y - 1 == you.y) {enemy[i].x1 = 0 ; enemy[i].y1 = -1;} if(enemy[i].x + 1 == you.x && enemy[i].y == you.y) {enemy[i].x1 = 1 ; enemy[i].y1 = 0;} if(enemy[i].x - 1 == you.x && enemy[i].y == you.y) {enemy[i].x1 = -1 ; enemy[i].y1 = 1;} } } void randdire( struct figure *sth) //使方向隨即改變 { int b=1; int x,y; while(b) { x =sjs(-1,1); y=sjs(-1,1); if(a[sth->y + y][sth->x + x] != 2 && abs(y) != abs(x)) { b = 0; sth->x1 = x; sth->y1 = y; } else b = 1; } } bool judgemeet () //相遇判斷 { for(int i = 0;i < 5;i++) { if(enemy[i].x == you.x && enemy[i].y == you.y ) { g_win = false; Init_loss(); return true; break; } } return false; } void eatbean() //判斷是否吃小黃點(diǎn) { if(a[you.y][you.x] == 0) { a[you.y][you.x] = 1; g_sum++; g_score += 10; Init_score(); a[you.y][you.x] = 1; } } void youmove(int k) //對(duì)自己進(jìn)行移動(dòng) { switch(k) { case LEFT: you.x1 = -1;you.y1 = 0; break; case RIGHT: you.x1 = 1;you.y1 = 0; break; case DOWN: you.x1 = 0;you.y1 = 1; break; case UP: you.x1 = 0;you.y1 = -1; break; } if(a[you.y + you.y1][you.x + you.x1] != 2) { int x = you.x2 ; int y = you.y2 ; for(int j = 0 ;j < 10;j++) { putimage(x,y,&image[1]); you.x2 += you.x1 * 2; you.y2 += you.y1 * 2; putimage(you.x2,you.y2,&image[3]); } you.x += you.x1; you.y += you.y1; eatbean(); } } void Init_loss() //失敗的界面 { cleardevice(); char str[] = "挑戰(zhàn)失敗,還需繼續(xù)努力呀!"; LOGFONT f; getfont(&f); f.lfHeight = 48; strcpy(f.lfFaceName , "楷體"); f.lfQuality = ANTIALIASED_QUALITY; f.lfWeight = FW_NORMAL; setcolor(LIGHTGREEN); setfont(&f); outtextxy(20,200,str); char tryagain[] = "是否想繼續(xù)挑戰(zhàn)?(Y/N)"; f.lfHeight = 28; setfont(&f); setcolor(BROWN); outtextxy(90,300,tryagain); char yesorno ; int keep = 1 ; while(keep) { yesorno = getch(); if(yesorno == 'Y' || yesorno == 'y' || yesorno == 'N' || yesorno == 'n') keep = 0; } if(yesorno == 'Y'|| yesorno == 'y') { g_win = true; g_score = 0; g_sum = 0; cleardevice(); Init_copy(); Initia(); } else Init_leave(); } void Init_leave() { cleardevice(); char str[] = "下次繼續(xù)努力吧!"; LOGFONT f; getfont(&f); f.lfHeight = 48; strcpy(f.lfFaceName , "楷體"); f.lfQuality = ANTIALIASED_QUALITY; f.lfWeight = FW_NORMAL; setcolor(LIGHTGREEN); setfont(&f); outtextxy(150,200,str); Sleep(2000); exit(0); } void Init_win() //成功的界面 { cleardevice(); char str[] = "恭喜你通過了游戲!??!"; LOGFONT f; getfont(&f); f.lfHeight = 48; strcpy(f.lfFaceName , "楷體"); f.lfQuality = ANTIALIASED_QUALITY; f.lfWeight = FW_NORMAL; setcolor(LIGHTGREEN); setfont(&f); outtextxy(50,200,str); Sleep(4000); char tryagain[] = "是否想再次挑戰(zhàn)?(Y/N)"; f.lfHeight = 28; setfont(&f); setcolor(BROWN); outtextxy(70,300,tryagain); char yesorno ; yesorno = getch(); if(yesorno == 'y' || yesorno == 'Y') { g_win = true; g_score = 0; g_sum = 0; cleardevice(); Init_copy(); Initia(); } else exit(0); } void stop() //游戲暫停 { char key = 'a'; char stop[] = "游戲暫停中"; setcolor(RED); setfont(28,0,"楷體"); outtextxy(230,50,stop); Sleep(100); while(key != ' ') key = getch(); setcolor(RGB(72,12,12)); outtextxy(230,50,stop); } void play() { setbkcolor(RGB(72,12,12)); char key; //獲取字母 int t; //記錄方向 while(g_win) { enemymove(); judgemeet(); while(kbhit()) { key = getch(); switch(key) { case 'a': case 75: t = LEFT; youmove(t ); break; case 'd': case 77: t = RIGHT; ; youmove(t ); break; case 's': case 80: t = DOWN; youmove(t ); break; case 'w': case 72: t = UP; youmove(t ); break; case 27: Init_leave(); break; case 32: stop(); break; default : break; } } if(g_sum == 50) Init_win(); } } void main() { initgraph(M,N); load_image(); Init_begin(); setbkcolor(RGB(72,12,12)); srand((unsigned)time(NULL)); Initia(); play(); closegraph(); }
大家趕緊去動(dòng)手試試吧!
審核編輯:湯梓紅
-
游戲
+關(guān)注
關(guān)注
2文章
742瀏覽量
26312 -
C語言
+關(guān)注
關(guān)注
180文章
7604瀏覽量
136683 -
編程
+關(guān)注
關(guān)注
88文章
3614瀏覽量
93685 -
源碼
+關(guān)注
關(guān)注
8文章
639瀏覽量
29185
原文標(biāo)題:C語言零基礎(chǔ)項(xiàng)目:吃豆人小游戲!詳細(xì)思路+源碼分享
文章出處:【微信號(hào):cyuyanxuexi,微信公眾號(hào):C語言編程學(xué)習(xí)基地】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論