C語言小遊戲之小熊跳板功能的實現
阿新 • • 發佈:2020-12-23
上篇文章給大家介紹了C語言實現桌面貪吃蛇小遊戲,感興趣的朋友可以點選檢視,今天給大家介紹c語言實現小熊跳板功能。
C語言程式碼,有點簡陋,還有許多可以優化的地方,註釋也沒寫全,後期會補充,後期也可能新增更多功能,我會去儘量的完善程式碼。
測試工具:VS2019,語言:C語言
#include<stdio.h> #include<stdlib.h> #include<Windows.h> #include<conio.h> #include<time.h> #include<string.h> #define High 40 #define Width 60 //畫布大小 int people_x,people_y; //小球座標 int canvas[High + 1][Width + 1]; //標記 int b1_left[3]; //橫線闆闆起點 int b1_right[3]; //橫線闆闆終點 int score = 0; //得分 int vv = 15,v_b = 8; //vv大小可以改變闆闆上升速度 void HideCursor() { CONSOLE_CURSOR_INFO cursor_info = { 1,0 }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); } void gotoxy(int x,int y) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle,pos); } void startup() { int i,j,k; srand(time(NULL)); for (i = 3; i <= High; i += 3) { int t = 0; int k; for (k = 0; k < 3; k++) { b1_left[k] = rand() % (Width - 6); b1_right[k] = b1_left[k] + rand()%3+5; } t = 3; while (t--) { for (j = b1_left[t]; j < b1_right[t]; j++) { canvas[i][j] = 2; //標記為橫線闆闆 } } } people_x = 2; people_y = 6; // 小球橫縱座標 canvas[people_x][people_y] = 1; // 小球所在不能為橫線闆闆 for (i = 0; i < High; i++) // 左右邊框為束線 { canvas[i][0] = -2; canvas[i][Width - 1] = -2; } for (j = 0; j < Width; j++) //上下邊框為T { canvas[0][j] = -1; canvas[High - 1][j] = -1; } } void show() { gotoxy(0,0);//把游標啟動到(0,0)點,在這裡功能為清屏 int i,j; for (i = 0; i < High; i++) { for (j = 0; j < Width; j++) { if (canvas[i][j] == 2) printf("-"); else if (canvas[i][j] == 1) printf("o"); else if (canvas[i][j] == -1) printf("T"); else if (canvas[i][j] == -2) printf("|"); else printf(" "); } printf("\n"); } printf("得分:%d\n",score); } int updataWithoutInput() { int i,k; static int v_bord = 0,v_ball = 0; int flag = 0; int oldps_x,oldps_y; if (v_bord < vv) //設定更新各個部件的位置 v_bord++; if (v_bord == vv) { for (i = 1; i <= High - 2; i++) { for (j = 1; j <= Width - 2; j++) { if (canvas[i][j] == 2 && i - 1 != 0) { canvas[i][j] = 0; if (canvas[i - 1][j] == 1) { people_x = i - 2; people_y = j; canvas[people_x][people_y] = 1; canvas[i - 1][j] = 2; } else canvas[i - 1][j] = 2; } else { if (i - 1 == 0 && canvas[i][j] == 2) { canvas[i][j] = 0; flag = 1; } } } } v_bord = 0; } for (i = 1; i <= Width - 2; i++) { if (canvas[people_x][i] == 2) { score++; break; } } if (v_ball < v_b) v_ball++; if (v_ball == v_b && canvas[people_x + 1][people_y] == 0) { canvas[people_x][people_y] = 0; people_x++; canvas[people_x][people_y] = 1; v_ball = 0; } int cnt = 0;//判斷遊戲是否結束 if (people_x == 0 || people_x == High - 2) { printf("GAMEOVER!\n"); system("pause"); cnt = 1; goto restart; } if (flag) { int t = 0; int k; for (k = 0; k < 3; k++) { b1_left[k] = rand() % (Width - 7) + 1; b1_right[k] = b1_left[k] + rand() % 3 + 5; } t = 3; while (t--) { for (j = b1_left[t]; j < b1_right[t]; j++) { canvas[High/3*3-3][j] = 2; } } } //Sleep(200); restart: return cnt; } void updataWithInput() { char key; if (_kbhit()) { key = _getch(); if ((key == 'a' || key == 75) && canvas[people_x][people_y-1] == 0) { canvas[people_x][people_y] = 0; people_y--; canvas[people_x][people_y] = 1; } if ((key == 'd' || key == 77) && canvas[people_x][people_y+1] == 0) { canvas[people_x][people_y] = 0; people_y++; canvas[people_x][people_y] = 1; } } } void init() { system("cls"); memset(canvas,sizeof canvas); memset(b1_left,sizeof b1_left); memset(b1_right,sizeof b1_right); score = 0; startup(); //初始化介面 } int main() { HideCursor(); start: init(); while (1) { show();//畫介面 int cnt = updataWithoutInput(); updataWithInput(); if (cnt == 1) goto start; } }
執行效果:
到此這篇關於C語言小遊戲之小熊跳板功能的實現的文章就介紹到這了,更多相關C語言小熊跳板內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!