C语言小恶搞之猜数字游戏
Posted 根号四十八
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言小恶搞之猜数字游戏相关的知识,希望对你有一定的参考价值。
这是一个用C语言做的带惩罚的猜数字游戏
下面是咱运用到的新知识!
1、自动关机部分:可搜索C语言实现电脑自动关机程序
system("shutdown -s -t 60");//注意-s与-t前有空格!
//60是指60秒,猛男可以改成3秒什么的
当然,既要学会关机,也要学会取消关机!防止被揍!
system("shutdown -a");//同样注意-a前要加空格!
2、Sleep(时间)时停部分(#include<windows.h>)
3、随机生成数组部分
#include<time.h>
#include<stdlib.h>
srand((int)time(NULL));
int answer = rand() % 100 + 1;//生成1~~~100的随机数
4、渐变标题的实现
void title() {
Sleep(5000);
system("cls");
char arr1[] = "Welcome To Little Nine's Guess Number Game";
char arr2[] = "******************************************";
int left = 0;
int right = strlen(arr1) - 1;
while (left <= right) { //实现渐变效果
arr2[left] = arr1[left];
arr2[right] = arr1[right];
left++;
right--;
Sleep(30);//注意S大写
system("cls");
printf("\\033[47;34;1m%s\\033[0m\\n", arr2);
}
}
5、彩色字体处应用
此处我参考了其他大佬的总结
点击连接了解:行者三个石的博客
完整源代码:
#include <stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
//1、渐变标题+清空屏幕
//2、猜数字游戏:欢迎+界面+游戏
//3、自动关机
void title() {
Sleep(5000);
system("cls");
char arr1[] = "Welcome To Little Nine's Guess Number Game";
char arr2[] = "******************************************";
int left = 0;
int right = strlen(arr1) - 1;
while (left <= right) { //实现渐变效果
arr2[left] = arr1[left];
arr2[right] = arr1[right];
left++;
right--;
Sleep(30);//注意S大写
system("cls");
printf("\\033[47;34;1m%s\\033[0m\\n", arr2);
}
}
void menu() {
printf("******************************************\\n");
printf("*****************1.Play*******************\\n");
printf("*****************0.Exit*******************\\n");
printf("******************************************\\n");
}
void Punishment() {
char arr[50];
system("shutdown -s -t 60");//注意-s与-t前有空格!
printf("Your Computer Will Shutdown After 60s\\n");
while (1) {
printf("\\033[31m If You Want To Get Safe ,Input :小九天下第一!(含中文感叹号)\\033[0m\\n");
printf("You Input:>");
scanf("%s", arr);
if (strcmp("小九天下第一!", arr) == 0) {
system("shutdown -a");
break;
} else {
printf("Are You serious?");
}
}
}
void game() {
system("cls");
printf("Game Start!\\n");
printf("\\033[31m Please Take This Game Seriously Or Else You Will Get Punishment\\033[0m\\n The Number Is Between 1 and 100\\n");
srand((int)time(NULL));
int answer = rand() % 100 + 1;//生成1~~~100的随机数
int input = 0;
int i = 0;
while (1) {
printf("You Guess:>");
scanf("%d", &input);
if (input < answer) {
printf("Guess A Litle\\n");
i++;
} else if (input > answer) {
printf("Guess A Bigger\\n");
i++;
} else {
printf("You are Right!\\n");
i++;
break;
}
}
if (i >= 8) {
printf("\\033[31m Why You Need To Guess %d Times?!\\nYou Will Get Punishment!!!\\033[0m \\n", i);
Sleep(1000);
Punishment();
} else {
printf("Good Job! You Only Guess %d Times!!\\n You Miss The Punishment!\\n", i);
}
}
int main() {
int input = 1;
while (input) {
title();
menu();
scanf("%d", &input);
switch (input) {
case 1:
game();
break;
case 0:
printf("Goodbye~~~\\n");
break;
default:
printf("please input 1/0 ?\\n");
break;
}
}
}
以上是关于C语言小恶搞之猜数字游戏的主要内容,如果未能解决你的问题,请参考以下文章