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;
		}
	}
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值