c語言消除遊戲(帶排行榜)
阿新 • • 發佈:2018-12-14
程式截圖
先來看下思路
1.初始化視窗
2.開始遊戲
利用方向鍵移動游標
對於可以消除的情況的判斷
記錄相同顏色的位置
小球下降補充新的小球
記錄分數
3.如果計時結束後顯示遊戲結束
4.釋放資源
首先我們需要graphics標頭檔案 需要使用easyx
幫助文件和下載地址在這裡
具體函式的用法可以看幫助文件自己查閱
首先寫好程式的框架
#include<graphics.h> #include<conio.h> void init(void);//初始化介面 void gamebegin(void);//遊戲開始介面 void gameplay(void);//玩遊戲的具體過程 void close(void);//釋放資源 void main() { init(); gamebegin(); gameplay(); close(); } void init() { initgraph(1000,700); } void gamebegin() { //繪製邊框 setlinecolor(RGB(50,50,50)); setlinestyle(PS_SOLID,10); rectangle(255,45,745,655); //繪製小球 int x , y; setlinecolor(RGB(250,0,0)); setfillcolor(RGB(250,0,0)); setlinestyle(PS_SOLID); for(x = 280 ; x < 740 ; x+=40) { for(y = 70 ; y < 650 ; y+=40) { fillcircle(x,y,20); } } //繪製時間 //繪製分數 } void gameplay() { } void close() { getch(); closegraph(); }
完成遊戲部分
#include<graphics.h> #include<conio.h> #include<time.h> #include<stdio.h> typedef struct position { int x; int y; } posType; typedef struct inf { char name[20]; int score; }info; #define KEY_DOWN(vk_c) (GetAsyncKeyState(vk_c)&0x8000) void init(void); // 初始化介面 void gamebegin(void); //遊戲開始介面 void gameplay(void); //玩遊戲的具體過程 void close(void); //釋放資源 void drawcursor(posType,COLORREF); //重繪游標 void drawtime(int); //重繪時間 void drawscore(int); //重繪分數 void getsamecolorballs(posType,COLORREF); //獲取同色小球 int isValid(posType cur,COLORREF cl); //判斷是否合法 void ballsfall(); //小球下落 int rank();//將分數寫入排行榜 int readrank();//讀出排行榜 COLORREF colorArr[6]={RGB(200,0,0),RGB(0,200,0),RGB(0,0,200), RGB(200,200,0),RGB(200,0,200),RGB(0,200,200)}; posType cur; //當前游標所在位置 posType ballsArr[180]; //同色小球座標 int index = 0; //同色小球的個數(靜態變數不賦值是自動賦0或空字元) int score = 0; //記錄分數 int main() { char select; printf("****************************************************************************\n"); printf("* *\n"); printf("* 1.開始遊戲 *\n"); printf("* 2.排行榜 *\n"); printf("* *\n"); printf("****************************************************************************\n"); select = getch(); if(select == '1') { init(); gamebegin(); gameplay(); close(); rank(); } if(select == '2' ) { readrank(); } return 0; } void init() { initgraph(1000,700); } void gamebegin() { //繪製邊框 setlinecolor(RGB(50,50,50)); setlinestyle(PS_SOLID | PS_JOIN_ROUND, 10); rectangle(255,45,745,655); //繪製小球 setlinestyle(PS_SOLID); srand((unsigned) time(NULL)); //提供隨機數種子 for(int x=280;x<740;x+=40) { for(int y=70;y<650;y+=40) { COLORREF cl=colorArr[rand()%6]; setlinecolor(cl); setfillcolor(cl); fillcircle(x,y,18); } } //繪製游標 cur.x=480; cur.y=390; drawcursor(cur,RGB(255,255,255)); //繪製時間 drawtime(30); //繪製分數 drawscore(0); } void gameplay() { for(int i=299;i>-1;i--) { if(i%10==0) { drawtime(i/10); } if(KEY_DOWN(VK_UP)&&cur.y>70) { drawcursor(cur,RGB(0,0,0)); cur.y-=40; drawcursor(cur,RGB(255,255,255)); } else if(KEY_DOWN(VK_DOWN)&&cur.y<630) { drawcursor(cur,RGB(0,0,0)); cur.y+=40; drawcursor(cur,RGB(255,255,255)); } else if(KEY_DOWN(VK_LEFT)&&cur.x>280) { drawcursor(cur,RGB(0,0,0)); cur.x-=40; drawcursor(cur,RGB(255,255,255)); } else if(KEY_DOWN(VK_RIGHT)&&cur.x<720) { drawcursor(cur,RGB(0,0,0)); cur.x+=40; drawcursor(cur,RGB(255,255,255)); } else if(KEY_DOWN(VK_RETURN)||KEY_DOWN(VK_SPACE)) { //獲得游標所在位置周圍的同色小球座標,存入陣列,並記錄個數 getsamecolorballs(cur,getpixel(cur.x,cur.y)); //將陣列中元素依次置黑一段時間 if(index>1) { for(int k=0;k<index;k++) { setlinecolor(RGB(0,0,0)); setfillcolor(RGB(0,0,0)); fillcircle(ballsArr[k].x,ballsArr[k].y,18); } Sleep(500); //上方的小球下落 ballsfall(); //重新整理分數 score+=index; drawscore(score); } index=0; } Sleep(100); } //遊戲結束 cleardevice(); settextcolor(RGB(255,0,0)); settextstyle(80, 0, _T("黑體")); outtextxy(300, 330, "Game Over"); drawscore(score); } void close() { getch(); closegraph(); } void drawcursor(posType cur,COLORREF cl) { setlinecolor(cl); rectangle(cur.x-20,cur.y-20,cur.x+20,cur.y+20); } void drawtime(int sec) { char str[30]; settextcolor(RGB(255,255,0)); settextstyle(25, 0, _T("黑體")); sprintf(str,"剩餘時間:%2d s",sec); outtextxy(30, 50, str); } void drawscore(int score) { char str[30]; settextcolor(RGB(255,0,0)); settextstyle(25, 0, _T("黑體")); sprintf(str,"分數:%d",score); outtextxy(30, 600, str); } void getsamecolorballs(posType cur,COLORREF cl) //《資料結構》---迷宮求解 { ballsArr[index].x=cur.x; ballsArr[index].y=cur.y; index++; posType tmpPos; for(int k=0;k<4;k++) { switch(k) { case 0:tmpPos.x=cur.x;tmpPos.y=cur.y-40;break; //上 case 1:tmpPos.x=cur.x;tmpPos.y=cur.y+40;break; //下 case 2:tmpPos.x=cur.x-40;tmpPos.y=cur.y;break; //左 case 3:tmpPos.x=cur.x+40;tmpPos.y=cur.y;break; //右 } if(isValid(tmpPos,cl)) { getsamecolorballs(tmpPos,cl);//遞迴呼叫 } } } int isValid(posType cur,COLORREF cl) { if(getpixel(cur.x,cur.y)!=cl) { return 0; } else { for(int i=0;i<index;i++) //判斷陣列中之前是否已存在 { if(cur.x==ballsArr[i].x&&cur.y==ballsArr[i].y)//進行遍歷 { return 0; } } return 1; } } void turn() { int i,j;posType temp; for(j=0;j<index-1;j++) for(i=0;i<index-1-j;i++) { if(ballsArr[i].x>ballsArr[i+1].x) { temp=ballsArr[i]; ballsArr[i]=ballsArr[i+1]; ballsArr[i+1]=temp; } if(ballsArr[i].y>ballsArr[i+1].y) { temp=ballsArr[i]; ballsArr[i]=ballsArr[i+1]; ballsArr[i+1]=temp; } } } void ballsfall() { turn(); for(int i=0;i<index;i++) { for(int k=ballsArr[i].y;k>70;k-=40) { COLORREF cl=getpixel(ballsArr[i].x,k-40); setlinecolor(cl); setfillcolor(cl); fillcircle(ballsArr[i].x,k,18); } COLORREF cl=colorArr[rand()%6]; setlinecolor(cl); setfillcolor(cl); fillcircle(ballsArr[i].x,70,18); } } int rank() { info tmp; FILE *fp; printf("大俠請輸入您的大名:"); scanf("%s",&tmp.name); tmp.score = score; fp=fopen("rank.txt","a"); if(fp==NULL) { printf("cannot open the files\n"); system("pause"); return -1;//如果檔案出現錯誤返回-1 } fprintf(fp,"%s %d",tmp.name,tmp.score); fclose(fp); return 0; } int readrank() { info tmp[10]; info temp; int n = 0,j = 0,i = 0,k = 0; FILE *fp; fp = fopen("rank.txt","rb"); if(fp==NULL) { printf("cannot open the files\n"); system("pause"); return -1; } for(i=0;i<n-1;i++)//採用冒泡法進行排序 { k=i; for(j=i+1;j<n;j++) if(tmp[j].score>tmp[k].score)//排序比較大小 k=j; temp=tmp[k]; tmp[k]=tmp[i]; tmp[i]=temp;//當符合條件時進行資料交換 } i = 0; while(i <= 10&&fscanf(fp,"%s%d",tmp[i].name,&tmp[i].score)!=EOF) { printf("%3d%15s%10d\n",i+1,tmp[i].name,tmp[i].score); i++; } fclose(fp); return 0; }
下面加入排行榜
#include<graphics.h> #include<conio.h> #include<time.h> #include<stdio.h> typedef struct position { int x; int y; } posType; typedef struct inf { char name[20]; int score; }info; #define KEY_DOWN(vk_c) (GetAsyncKeyState(vk_c)&0x8000) void init(void); // 初始化介面 void gamebegin(void); //遊戲開始介面 void gameplay(void); //玩遊戲的具體過程 void close(void); //釋放資源 void drawcursor(posType,COLORREF); //重繪游標 void drawtime(int); //重繪時間 void drawscore(int); //重繪分數 void getsamecolorballs(posType,COLORREF); //獲取同色小球 int isValid(posType cur,COLORREF cl); //判斷是否合法 void ballsfall(); //小球下落 int rank();//將分數寫入排行榜 int readrank();//讀出排行榜 COLORREF colorArr[6]={RGB(200,0,0),RGB(0,200,0),RGB(0,0,200), RGB(200,200,0),RGB(200,0,200),RGB(0,200,200)}; posType cur; //當前游標所在位置 posType ballsArr[180]; //同色小球座標 int index = 0; //同色小球的個數(靜態變數不賦值是自動賦0或空字元) int score = 0; //記錄分數 int main() { char select; printf("****************************************************************************\n"); printf("* *\n"); printf("* 1.開始遊戲 *\n"); printf("* 2.排行榜 *\n"); printf("* *\n"); printf("****************************************************************************\n"); select = getch(); if(select == '1') { init(); gamebegin(); gameplay(); close(); rank(); } if(select == '2' ) { readrank(); } return 0; } void init() { initgraph(1000,700); } void gamebegin() { //繪製邊框 setlinecolor(RGB(50,50,50)); setlinestyle(PS_SOLID | PS_JOIN_ROUND, 10); rectangle(255,45,745,655); //繪製小球 setlinestyle(PS_SOLID); srand((unsigned) time(NULL)); //提供隨機數種子 for(int x=280;x<740;x+=40) { for(int y=70;y<650;y+=40) { COLORREF cl=colorArr[rand()%6]; setlinecolor(cl); setfillcolor(cl); fillcircle(x,y,18); } } //繪製游標 cur.x=480; cur.y=390; drawcursor(cur,RGB(255,255,255)); //繪製時間 drawtime(30); //繪製分數 drawscore(0); } void gameplay() { for(int i=299;i>-1;i--) { if(i%10==0) { drawtime(i/10); } if(KEY_DOWN(VK_UP)&&cur.y>70) { drawcursor(cur,RGB(0,0,0)); cur.y-=40; drawcursor(cur,RGB(255,255,255)); } else if(KEY_DOWN(VK_DOWN)&&cur.y<630) { drawcursor(cur,RGB(0,0,0)); cur.y+=40; drawcursor(cur,RGB(255,255,255)); } else if(KEY_DOWN(VK_LEFT)&&cur.x>280) { drawcursor(cur,RGB(0,0,0)); cur.x-=40; drawcursor(cur,RGB(255,255,255)); } else if(KEY_DOWN(VK_RIGHT)&&cur.x<720) { drawcursor(cur,RGB(0,0,0)); cur.x+=40; drawcursor(cur,RGB(255,255,255)); } else if(KEY_DOWN(VK_RETURN)||KEY_DOWN(VK_SPACE)) { //獲得游標所在位置周圍的同色小球座標,存入陣列,並記錄個數 getsamecolorballs(cur,getpixel(cur.x,cur.y)); //將陣列中元素依次置黑一段時間 if(index>1) { for(int k=0;k<index;k++) { setlinecolor(RGB(0,0,0)); setfillcolor(RGB(0,0,0)); fillcircle(ballsArr[k].x,ballsArr[k].y,18); } Sleep(500); //上方的小球下落 ballsfall(); //重新整理分數 score+=index; drawscore(score); } index=0; } Sleep(100); } //遊戲結束 cleardevice(); settextcolor(RGB(255,0,0)); settextstyle(80, 0, _T("黑體")); outtextxy(300, 330, "Game Over"); drawscore(score); } void close() { getch(); closegraph(); } void drawcursor(posType cur,COLORREF cl) { setlinecolor(cl); rectangle(cur.x-20,cur.y-20,cur.x+20,cur.y+20); } void drawtime(int sec) { char str[30]; settextcolor(RGB(255,255,0)); settextstyle(25, 0, _T("黑體")); sprintf(str,"剩餘時間:%2d s",sec); outtextxy(30, 50, str); } void drawscore(int score) { char str[30]; settextcolor(RGB(255,0,0)); settextstyle(25, 0, _T("黑體")); sprintf(str,"分數:%d",score); outtextxy(30, 600, str); } void getsamecolorballs(posType cur,COLORREF cl) //《資料結構》---迷宮求解 { ballsArr[index].x=cur.x; ballsArr[index].y=cur.y; index++; posType tmpPos; for(int k=0;k<4;k++) { switch(k) { case 0:tmpPos.x=cur.x;tmpPos.y=cur.y-40;break; //上 case 1:tmpPos.x=cur.x;tmpPos.y=cur.y+40;break; //下 case 2:tmpPos.x=cur.x-40;tmpPos.y=cur.y;break; //左 case 3:tmpPos.x=cur.x+40;tmpPos.y=cur.y;break; //右 } if(isValid(tmpPos,cl)) { getsamecolorballs(tmpPos,cl);//遞迴呼叫 } } } int isValid(posType cur,COLORREF cl) { if(getpixel(cur.x,cur.y)!=cl) { return 0; } else { for(int i=0;i<index;i++) //判斷陣列中之前是否已存在 { if(cur.x==ballsArr[i].x&&cur.y==ballsArr[i].y)//進行遍歷 { return 0; } } return 1; } } void turn() { int i,j;posType temp; for(j=0;j<index-1;j++) for(i=0;i<index-1-j;i++) { if(ballsArr[i].x>ballsArr[i+1].x) { temp=ballsArr[i]; ballsArr[i]=ballsArr[i+1]; ballsArr[i+1]=temp; } if(ballsArr[i].y>ballsArr[i+1].y) { temp=ballsArr[i]; ballsArr[i]=ballsArr[i+1]; ballsArr[i+1]=temp; } } } void ballsfall() { turn(); for(int i=0;i<index;i++) { for(int k=ballsArr[i].y;k>70;k-=40) { COLORREF cl=getpixel(ballsArr[i].x,k-40); setlinecolor(cl); setfillcolor(cl); fillcircle(ballsArr[i].x,k,18); } COLORREF cl=colorArr[rand()%6]; setlinecolor(cl); setfillcolor(cl); fillcircle(ballsArr[i].x,70,18); } } int rank() { info tmp; FILE *fp; printf("大俠請輸入您的大名:"); scanf("%s",&tmp.name); tmp.score = score; fp=fopen("rank.txt","a"); if(fp==NULL) { printf("cannot open the files\n"); system("pause"); return -1;//如果檔案出現錯誤返回-1 } fprintf(fp,"%s %d",tmp.name,tmp.score); fclose(fp); return 0; } int readrank() { info tmp[10]; info temp; int n = 0,j = 0,i = 0,k = 0; FILE *fp; fp = fopen("rank.txt","rb"); if(fp==NULL) { printf("cannot open the files\n"); system("pause"); return -1; } for(i=0;i<n-1;i++)//採用冒泡法進行排序 { k=i; for(j=i+1;j<n;j++) if(tmp[j].score>tmp[k].score)//排序比較大小 k=j; temp=tmp[k]; tmp[k]=tmp[i]; tmp[i]=temp;//當符合條件時進行資料交換 } i = 0; while(i <= 10&&fscanf(fp,"%s%d",tmp[i].name,&tmp[i].score)!=EOF) { printf("%3d%15s%10d\n",i+1,tmp[i].name,tmp[i].score); i++; } fclose(fp); return 0; }