起因是我在学习C语言完成老师布置C语言写一个猜数字的作业,突发奇想,能不能在这个猜对了之后弹出一个不一样的页面,然后就试试看能不能实现。基本思路是这样的:
1:先写一个C语言的猜数字的小游戏,在我上个文章有思路代码如下:链接挂这了:C语言猜数字小游戏-CSDN博客
#include<stdio.h> #include<stdlib.h> #include<time.h> void menu() {printf("**** 猜数字游戏! ****\n");printf("**** 按1开始游戏 ****\n");printf("**** 按0退出游戏 ****\n"); } void game() {int ret = rand()%100+1;//printf("%d\n", ret);int num = 0;int cnt = 10;int sum = cnt;while (1){printf("你还有%d次机会\n", cnt);printf("请输入一个1-100之间的数:");scanf("%d", &num);if (num > ret){printf("数字太大了!\n");}else if (num < ret){printf("数字太小了!\n");}else{printf("恭喜你猜对了!\n");printf("猜对用了%d次\n", sum-cnt);break;}cnt--;if (cnt == 0){printf("你的机会用完了,游戏结束!\n");printf("正确的数字是%d\n\n\n", ret);break;}} } int main() {srand((unsigned int)time(NULL));int input = 0;do {menu();printf("请选择你需要的操作:");scanf("%d", &input);switch (input){case 1:game();break;case 0:printf("退出游戏!\n");break;default:printf("输入错误,请重新输入!\n");break;}} while (input);return 0; }
2:在此代码的基础上添加一些函数,在game()函数完成后实现弹出动画心形的思路
2.1:首先要定义出这个心形的基本参数
float f(float x, float y, float z) {float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z; }float h(float x, float z) {for (float y = 1.0f; y >= 0.0f; y -= 0.001f)if (f(x, y, z) <= 0.0f)return y;return 0.0f; }void color() {system("color c"); }
3:这三个函数就可以实现一个跳动的心,然后在心形的下面放置你想对他说的话,代码如下:
void sentance() {char title1[] = "============ 2021 12 05 ============";char title2[] = "============ 2023 12 05 ============"; //输入标题char word[] = "====== 两周年快乐,我爱你宝贝!======"; //输入想说的文字printf("\n\t "); //调整格式printf("%s", title1);printf("\n\t "); //调整格式printf("%s", title2);printf("\n\t ");printf("%s", word); }
4:文字都可以自己添加,你想再多加几行也可以,直接多写几个char字符串最后再打印出来就可以。然后就是函数调用的问题;
我们可以在设置一个函数,这个函数可以让你的心形具有跳动的功能,代码如下:
int langman() {HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);_TCHAR buffer[25][80] = { _T(' ') };_TCHAR ramp[] = _T(".:-=+*#%@");for (float t = 0.0f;; t += 0.1f) {int sy = 0;float s = sinf(t);float a = s * s * s * s * 0.2f;for (float z = 1.3f; z > -1.2f; z -= 0.1f) {_TCHAR* p = &buffer[sy++][0];float tz = z * (1.2f - a);for (float x = -1.5f; x < 1.5f; x += 0.05f) {float tx = x * (1.2f + a);float v = f(tx, 0.0f, tz);if (v <= 0.0f) {float y0 = h(tx, tz);float ny = 0.01f;float nx = h(tx + ny, tz) - y0;float nz = h(tx, tz + ny) - y0;float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);float d = (nx + ny - nz) * nd * 0.5f + 0.5f;*p++ = ramp[(int)(d * 5.0f)];}else*p++ = ' ';}}for (sy = 0; sy < 25; sy++) {COORD coord = { 0, sy };SetConsoleCursorPosition(o, coord);WriteConsole(o, buffer[sy], 79, NULL, 0);}Sleep(33);color();sentance();} }
5:写到这函数基本就成形了,只差一个主函数了,我们再把主函数给它加上
代码如下:
int main() {srand((unsigned int)time(NULL));int input = 0;do{menu();printf("请选择你需要的操作->");if (scanf("%d", &input) != 1 || (input != 0 && input != 1)){printf("输入错误,请重新输入!\n");while (getchar() != '\n');continue;}switch (input){case 1:game();break;case 0:printf("退出游戏!\n");break;default:/*printf("输入错误,请重新输入!\n");*/break;}} while (input != 0);return 0; }
当然还有猜数字游戏的主体函数以及菜单函数,这个我们先不着急,感兴趣的朋友可以直接复制下面的完整代码运行试试:
#include<stdio.h> #include<stdlib.h> #include<time.h> #include <math.h> #include <windows.h> #include <tchar.h>void menu() {printf("*********************************\n");printf("**** 玩一个猜数字游戏吧! ****\n");printf("**** 规定步数内完成有惊喜 ****\n");printf("**** 请按'1'可以开始游戏 ****\n");printf("**** 请按'0'可以退出游戏 ****\n");printf("*********************************\n"); }void game() {int ret = rand() % 100 + 1;int num = 0;int cnt = 10;int sum = cnt;while (cnt > 0){printf("你还有%d次机会\n", cnt);printf("请输入一个1-100之间的数:");if (scanf("%d", &num) != 1 || num < 1 || num > 100){printf("输入错误,请重新输入一个1-100之间的数\n");while (getchar() != '\n');continue;}if (num > ret){printf("\n数字太大了!\n\n");}else if (num < ret){printf("\n数字太小了!\n\n");}else{printf("\n恭喜你猜对了!\n\n");langman();printf("\n猜对用了%d次\n\n\n", sum - cnt + 1);return;}cnt--;}printf("你的机会用完了,游戏结束!\n");printf("正确的数字是%d\n\n\n", ret); }float f(float x, float y, float z) {float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z; }float h(float x, float z) {for (float y = 1.0f; y >= 0.0f; y -= 0.001f)if (f(x, y, z) <= 0.0f)return y;return 0.0f; }void color() {system("color c"); }void sentance() {char title1[] = "============ 2021 12 05 ============";char title2[] = "============ 2023 12 05 ============"; //输入标题char word[] = "====== 两周年快乐,我爱你宝贝!======"; //输入想说的文字printf("\n\t "); //调整格式printf("%s", title1);printf("\n\t "); //调整格式printf("%s", title2);printf("\n\t ");printf("%s", word); }int langman() {HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);_TCHAR buffer[25][80] = { _T(' ') };_TCHAR ramp[] = _T(".:-=+*#%@");for (float t = 0.0f;; t += 0.1f) {int sy = 0;float s = sinf(t);float a = s * s * s * s * 0.2f;for (float z = 1.3f; z > -1.2f; z -= 0.1f) {_TCHAR* p = &buffer[sy++][0];float tz = z * (1.2f - a);for (float x = -1.5f; x < 1.5f; x += 0.05f) {float tx = x * (1.2f + a);float v = f(tx, 0.0f, tz);if (v <= 0.0f) {float y0 = h(tx, tz);float ny = 0.01f;float nx = h(tx + ny, tz) - y0;float nz = h(tx, tz + ny) - y0;float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);float d = (nx + ny - nz) * nd * 0.5f + 0.5f;*p++ = ramp[(int)(d * 5.0f)];}else*p++ = ' ';}}for (sy = 0; sy < 25; sy++) {COORD coord = { 0, sy };SetConsoleCursorPosition(o, coord);WriteConsole(o, buffer[sy], 79, NULL, 0);}Sleep(33);color();sentance();} }int main() {srand((unsigned int)time(NULL));int input = 0;do{menu();printf("请选择你需要的操作->");if (scanf("%d", &input) != 1 || (input != 0 && input != 1)){printf("输入错误,请重新输入!\n");while (getchar() != '\n');continue;}switch (input){case 1:game();break;case 0:printf("退出游戏!\n");break;default:/*printf("输入错误,请重新输入!\n");*/break;}} while (input != 0);return 0; }
试运行之后我们发现有一个问题,就是猜出来数字之后,图像立马就出现了,没有惊喜感,那我们就再加一个定时器函数,让猜对了之后屏幕上暂停几秒,最好能在暂停的时候屏幕上出现一些文字提示,说干就干,代码如下:
void settime(int seconds) {for (int i = seconds; i > 0; i--){system("cls"); // 清空屏幕printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");printf("\t\t\t\t\t\t\t%d\n\n\n\n\n\n\n\n\n", i); // 放大居中显示倒计时数字Sleep(1000); // 暂停1秒}system("cls"); // 清空屏幕}
这样屏幕上会短暂的出现自定义秒数的倒计时,如果你想添加别的文字也可以直接在函数中printf,接下哎代码基本上就大功告成了。
完整代码如下:
#include<stdio.h> #include<stdlib.h> #include<time.h> #include <math.h> #include <windows.h> #include <tchar.h>void menu() {printf("*********************************\n");printf("**** 玩一个猜数字游戏吧! ****\n");printf("**** 规定步数内完成有惊喜 ****\n");printf("**** 请按'1'可以开始游戏 ****\n");printf("**** 请按'0'可以退出游戏 ****\n");printf("*********************************\n"); }void settime(int seconds) {for (int i = seconds; i > 0; i--){system("cls"); // 清空屏幕printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");printf("\t\t\t\t\t\t\t%d\n\n\n\n\n\n\n\n\n", i); // 放大居中显示倒计时数字Sleep(1000); // 暂停1秒}system("cls"); // 清空屏幕}void game() {int ret = rand() % 100 + 1;int num = 0;int cnt = 10;int sum = cnt;while (cnt > 0){printf("你还有%d次机会\n", cnt);printf("请输入一个1-100之间的数:");if (scanf("%d", &num) != 1 || num < 1 || num > 100){printf("输入错误,请重新输入一个1-100之间的数\n");while (getchar() != '\n');continue;}if (num > ret){printf("\n数字太大了!\n\n");}else if (num < ret){printf("\n数字太小了!\n\n");}else{printf("\n恭喜你猜对了!\n\n");settime(3);langman();printf("\n猜对用了%d次\n\n\n", sum - cnt + 1);return;}cnt--;}printf("你的机会用完了,游戏结束!\n");printf("正确的数字是%d\n\n\n", ret); }float f(float x, float y, float z) {float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z; }float h(float x, float z) {for (float y = 1.0f; y >= 0.0f; y -= 0.001f)if (f(x, y, z) <= 0.0f)return y;return 0.0f; }void color() {system("color c"); }void sentance() {char title1[] = "============ 2021 12 05 ============";char title2[] = "============ 2023 12 05 ============"; //输入标题char word[] = "====== 两周年快乐,我爱你宝贝!======"; //输入想说的文字printf("\n\t "); //调整格式printf("%s", title1);printf("\n\t "); //调整格式printf("%s", title2);printf("\n\t ");printf("%s", word); }int langman() {HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);_TCHAR buffer[25][80] = { _T(' ') };_TCHAR ramp[] = _T(".:-=+*#%@");for (float t = 0.0f;; t += 0.1f) {int sy = 0;float s = sinf(t);float a = s * s * s * s * 0.2f;for (float z = 1.3f; z > -1.2f; z -= 0.1f) {_TCHAR* p = &buffer[sy++][0];float tz = z * (1.2f - a);for (float x = -1.5f; x < 1.5f; x += 0.05f) {float tx = x * (1.2f + a);float v = f(tx, 0.0f, tz);if (v <= 0.0f) {float y0 = h(tx, tz);float ny = 0.01f;float nx = h(tx + ny, tz) - y0;float nz = h(tx, tz + ny) - y0;float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);float d = (nx + ny - nz) * nd * 0.5f + 0.5f;*p++ = ramp[(int)(d * 5.0f)];}else*p++ = ' ';}}for (sy = 0; sy < 25; sy++) {COORD coord = { 0, sy };SetConsoleCursorPosition(o, coord);WriteConsole(o, buffer[sy], 79, NULL, 0);}Sleep(33);color();sentance();} }int main() {srand((unsigned int)time(NULL));int input = 0;do{menu();printf("请选择你需要的操作->");if (scanf("%d", &input) != 1 || (input != 0 && input != 1)){printf("输入错误,请重新输入!\n");while (getchar() != '\n');continue;}switch (input){case 1:game();break;case 0:printf("退出游戏!\n");break;default:/*printf("输入错误,请重新输入!\n");*/break;}} while (input != 0);return 0; }
运行兄弟们自己试试,绝对惊喜!!!