C语言实现数字炸弹小游戏

 更新时间:2020年11月27日 16:35:47   作者:m0_52352462  
这篇文章主要为大家详细介绍了C语言实现数字炸弹小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言实现数字炸弹小游戏的具体代码,供大家参考,具体内容如下

使用的是C语言

# 内容
#include<stdio.h>
#include<stdlib.h>
int main(){
 //变量
 char c_TheRequest = 1;//开始界面的字符
 int in_Deltar = 1,in_EnteredNumber,in_Sig;//游戏中输入数字及其判定所需要的数字
 int in_GivenNum = 0,in_An = 0,in_Bn = 0;//出的数字,和数字区间
 int in_Mid1,in_Mid2;//用来交换An、Bn的值以满足An<Bn的中间量
 int in_DistantAn,in_DistantBn;//An与Bn离num的距离
 int in_TemporaryAnSaver,in_TemporaryBnSaver;//临时用于比较的An、Bn储存器
 
 //开始界面
 printf("If you just happen to open the game and do not have the intention to play it.\n");
 printf("You can enter a q to quit or you can enter an s to start the game now!\n");
 scanf("%c",&c_TheRequest);
 getchar();
 while (c_TheRequest != 's' && c_TheRequest != 'q'){
 printf("Please do not enter a irrelevant letter.\n");
 scanf("%c",&c_TheRequest);
 getchar();
 }
 if(c_TheRequest == 'q'){
 printf("So sad then.Hope you can play with me next time.\n");
 getchar();
 }

 //游戏
 if(c_TheRequest == 's'){
 
 //输入“数字炸弹”
 system("cls");
 printf("(^ o ^) # *** NOW LET\'S PLAY! *** # (^ o ^)\nAttention!You can only enter numbers in the following steps.\nEnter to start.\n");
 getchar();
 system("cls");
 printf("Game : The number boom!(Another Life.)\n");
 printf("Rule:\n");
 printf("The first player should enter a number.Then he should give to numbers and guarantee the number above is between them.\n");
 printf("The rest should guess and enter the numbers.And the one who enter the exact number the first player entered\n");
 printf("is supported to be the winner!\n");
 printf("Please Enter a number to start the game.(You is ought not to expose it to other players.The number should bigger than 0.)\n");
 printf("__________\b\b\b\b\b\b\b\b\b\b\b");
 while(scanf("%d",&in_GivenNum) != 1 || in_GivenNum < 0){
  system("cls");
  printf("Please Enter a NUMBER which is bigger than 0.\n");
  printf("num:___________\b\b\b\b\b\b\b\b\b\b\b");
  getchar();
 }
 system("cls");
 //判定是否为数字

 //输入“数字炸弹”所在的区间
 printf("And where is the number?Please enter two numbers,and ensure that the number above is between them.\n");
 printf("Additionally,there should be at least 100 numbers between the two numbers you will enter.\n");
 do{
  printf("num = %d\n",in_GivenNum);
  printf("A:__________\b\b\b\b\b\b\b\b\b\b");
  while(scanf("%d",&in_An)!=1){
  printf("Please Enter a NUMBER which is bigger or smaller than the \"num\"!!!!\n");
  printf("A:__________\b\b\b\b\b\b\b\b\b\b");
  scanf("%d",&in_An);
  getchar();
  }
  printf("B:__________\b\b\b\b\b\b\b\b\b\b");
  while(scanf("%d",&in_Bn)!=1){
  printf("Please Enter a NUMBER which is bigger or smaller than the \"num\"!!!!\n");
  printf("B:__________\b\b\b\b\b\b\b\b\b\b");
  scanf("%d",&in_Bn);
  getchar();
  }//记录An和Bn
  if(!(( in_An < in_GivenNum && in_Bn > in_GivenNum ) || ( in_An > in_GivenNum && in_Bn < in_GivenNum ))){
  system("cls");
  printf("Your math is so poor.Enter a again!\nThe \"num\" must be between the two numbers.\n");
  printf("Enter to restart.\n");
  getchar();
  getchar();
  system("cls");
  }//区间如果错误就会报错
 }while(!(( in_An < in_GivenNum && in_Bn > in_GivenNum ) || ( in_An > in_GivenNum && in_Bn < in_GivenNum )));//判定区间是不是对的

 //排序
 in_Mid1 = (in_An > in_Bn)?in_Bn : in_An;
 in_Mid2 = (in_Bn > in_An)?in_Bn : in_An;
 in_An = in_Mid1;
 in_Bn = in_Mid2;

 //开始猜数字
 system("cls");
 printf("Now the game starts!\n");
 printf("The number is somewhere between %d and %d\n",in_An,in_Bn);
 printf("Please enter the number.\n");
 printf("__________\b\b\b\b\b\b\b\b\b\b");
 while(in_Deltar != 0){
  while(scanf("%d",&in_EnteredNumber)!=1){
  printf("Please Enter a NUMBER!!!!\n");
  scanf("%d",&in_EnteredNumber);
  getchar();
  }//判断是否为数字

  //差的运算
  in_Deltar = ((in_GivenNum - in_EnteredNumber)<0)?in_EnteredNumber - in_GivenNum:in_GivenNum - in_EnteredNumber;
  in_Sig = in_GivenNum - in_EnteredNumber;
  in_DistantAn = in_GivenNum - in_An;
  in_DistantBn = in_Bn - in_GivenNum;

  //判定差的大小
  if(in_Deltar >= 1000){
  if(in_Sig > 0)
  printf("Too small!Next!\n");
  else
  printf("Too big!Next!\n");
  }
  else if(in_Deltar >= 100){
  if(in_Sig > 0)
  printf("Small.Next!\n");
  else
  printf("Big.Next!\n");
  }
  else if(in_Deltar >= 10){
  if(in_Sig > 0)
  printf("A little small.Next!\n");
  else
  printf("A little big.Next!\n");
  }
  else if(in_Deltar > 0){
  printf("Almost there!NEXT!!!\n");
  }

  //临时存储,以便后面判断所给数字是否满足条件
  in_TemporaryAnSaver = in_An;
  in_TemporaryBnSaver = in_Bn;

  if(( in_Deltar < in_DistantAn && in_Sig > 0 ) || ( in_Deltar < in_DistantBn && in_Sig < 0 )){
  if(in_Sig > 0)
  in_An = in_EnteredNumber;
  else
  in_Bn = in_EnteredNumber;
  }//这是修改上下限
  if((in_TemporaryAnSaver == in_An && in_TemporaryBnSaver == in_Bn) && in_Deltar){
  system("cls");
  printf("Do not cheat!\nYou should play it again.\n");
  }//判定所猜的数字是否在区间内

  if(in_Deltar == 0)
  break;//猜中
  printf("Enter to continue.\n");
  getchar();
  getchar();
  system ("cls");
  printf("Between %d and %d\n__________\b\b\b\b\b\b\b\b\b\b",in_An,in_Bn);//区间修正
 }
 printf("You are the one !!!");
 getchar();
 getchar();
 }
}

总结

学习C的时候为了巩固所学知识而编得一个小游戏,内容全英文。

小编再为大家分享一段代码:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<time.h>
void menu()
{
 printf("###########################\n");
 printf("### 1. play 0. exit ###\n");
 printf("###########################\n");
}
void game()
{
 //1.生成一个随机数
 int ret = 0;
 int guess = 0;
 //拿时间戳来设置随机数的生成起点  //时间戳——(当前计算机的时间-计算机的起始时间(1970.1.1.0时0分0秒))=(xxxx)秒
 //time_t
 //srand((unsigned int)time(NULL));
 ret=rand()%100+1; //生成随机数 0---0x7fff(32767)
 //printf("%d\n",ret);
 //2.猜数字
 while (1)
 {
 printf("请猜数字: ");
 scanf("%d", &guess);
 if (guess > ret)
 {
 printf("big\n");
 }
 else if (guess < ret)
 {
 printf("small\n");
 }
 else
 {
 printf("you are die\n");
 break;
 }
 }
 
}

int main()
{
 int input = 0;
 srand((unsigned int)time(NULL));
 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;
}

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C语言之素数(质数)的判断以及输出

    C语言之素数(质数)的判断以及输出

    这篇文章主要介绍了C语言之素数(质数)的判断以及输出方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • C++实现LeetCode(79.词语搜索)

    C++实现LeetCode(79.词语搜索)

    这篇文章主要介绍了C++实现LeetCode(79.词语搜索),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • 使用C++一步步实现俄罗斯方块后续

    使用C++一步步实现俄罗斯方块后续

    本文主要给大家分享的是作者在使用C++制作俄罗斯方块小游戏的时候所需要的常用的函数,有需要的小伙伴可以借鉴下,希望大家能够喜欢。
    2017-12-12
  • Matlab控制电脑摄像实现实时人脸检测和识别详解

    Matlab控制电脑摄像实现实时人脸检测和识别详解

    人脸识别过程主要由四个阶段组成:人脸检测、图像预处理、面部特征提取和特征识别。这篇文章主要介绍了如何使用MATLAB控制笔记本电脑的摄像头,并进行实时人脸检测和识别,需要的可以参考一下
    2022-10-10
  • C++中的HTTP协议问题

    C++中的HTTP协议问题

    这篇文章主要介绍了C++中的HTTP协议问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • 基于C语言编写一个多功能计算器

    基于C语言编写一个多功能计算器

    这篇文章主要为大家详细介绍了如何基于C语言编写一个多功能计算器,除了四则混合运算之外,还支持三角函数和绝对值等函数,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-08-08
  • VC++ 2019 "const char*"类型的实参与"LPCTSTR"类型的形参不兼容解决

    VC++ 2019 "const char*"类型的实参与"LPCTSTR"

    这篇文章主要给大家介绍了关于VC++ 2019 "const char*"类型的实参与"LPCTSTR"类型的形参不兼容的解决方法,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2023-03-03
  • C++实现简易反弹小球游戏的示例代码

    C++实现简易反弹小球游戏的示例代码

    我们利用printf 函数实现一个在屏幕上弹跳的小球。弹跳的小球游戏比较简单、容易入门,也是反弹球消砖块、接金币、台球等很多游戏的基础,感兴趣的可以了解一下
    2022-10-10
  • C++广播通信实例

    C++广播通信实例

    这篇文章主要介绍了C++实现广播通信的方法,实例讲述了C++ socket广播通信的原理与实现方法,需要的朋友可以参考下
    2014-10-10
  • C语言make和Makefile介绍及使用

    C语言make和Makefile介绍及使用

    这篇文章介绍了C语言make和Makefile以及使用方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-01-01

最新评论