概述
上篇文章给大家介绍了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("得分:%dn", score); } int updataWithoutInput() { int i, j, 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, 0, sizeof canvas); memset(b1_left, 0, sizeof b1_left); memset(b1_right, 0, 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语言小熊跳板内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!
最后
以上就是害怕万宝路为你收集整理的C语言小游戏之小熊跳板功能的实现的全部内容,希望文章能够帮你解决C语言小游戏之小熊跳板功能的实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复