这是一个用C语言做的带惩罚的猜数字游戏
下面是咱运用到的新知识! 1、自动关机部分:可搜索C语言实现电脑自动关机程序
system("shutdown -s -t 60");
当然,既要学会关机,也要学会取消关机!防止被揍!
system("shutdown -a");
2、Sleep(时间)时停部分(#include<windows.h>)
3、随机生成数组部分
#include<time.h>
#include<stdlib.h>
srand((int)time(NULL));
int answer = rand() % 100 + 1;
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);
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>
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);
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");
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;
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;
}
}
}
|