這是一個用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;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/290522.html
標籤:其他
上一篇:使用GD32內嵌IIC控制器
