服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 神奇的c/c++小游戏(提高你的编程兴趣)

神奇的c/c++小游戏(提高你的编程兴趣)

2021-12-20 15:28恪愚 C/C++

本文通过c/c++编写小游戏,可以提高新手们的编程兴趣,接下来我们一起来看看吧

神奇的c/c++

以下代码在Dev,codeblocks,VC上都能运行

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>   //下面Sleep()函数的头文件
#include<mmsystem.h>
 
void menu()
{
    printf("   *****************************\n");
    printf("   *****************************\n");
    printf("   ************1.play***********\n");
    printf("   ************0.exit***********\n");
    printf("   **********2.chakan***********\n");
    printf("   *********3.qingkong**********\n");
    printf("   *****************************\n");
    printf("请选择:          \n");
}
 
void game()
 {
    system("cls");
    system ( "color 33" ); //设置颜色
    FILE *fp;
    if((fp=fopen("D:\\caishuziyou.txt","a+"))==NULL)
    {
        printf("open file error\n");
        return ;
    }
    int rand_num=rand()%100;
    int tmp=0,t=0;
 
    while(1)
    {
        int flag=0;
        printf("请输入你要猜的数字:");
         scanf("%d",&tmp);
         if(tmp>rand_num)
         {
            printf("猜大了!\n");
            flag=0;
    }
         if(tmp<rand_num)
         {
            printf("猜小了!\n");
            flag=0;
    }
         if(tmp==rand_num)
         {
            printf("正确!恭喜!\n");
             printf("共用了%d次\n",t+1);
 
 
         FILE *fp;
fp=fopen("D:\\caishuziyou.txt","w");
if(fp==NULL)
{
    printf("create file failed\n");
    return ;
}
fprintf(fp,"%d",t+1);
fclose(fp);
         break;
        }
        if(flag==0) t++;
 
        printf(" 你再猜:\n");
     }
    printf("\n按任意键返回目录");
    getch();
 
 }
 
void menu2()
{
    system("cls");
    system ( "color B6" ); //设置颜色
    printf("即将登陆系统!!!\n");
    getch();
    for(int t=3;t>=1;t--)
    {
        system("cls");
        printf("%d",t);
        Sleep(700);
        system("cls");
    }
}
 
void chakan()
{
    system("cls");
    system ( "color E0" ); //设置颜色
 
int t=0;
FILE*fp;
if((fp=fopen("D:\\caishuziyou.txt","r"))==NULL)
{
    printf("can not to open the file!\n");
    exit(0);
}
while(fscanf(fp,"%d",&t)!=EOF)//若不到文件结尾则继续
{
    printf("此玩家共进行了%d次\n",t);
}
        printf("\n按任意键返回目录");
        getch();
}
 
void qk()//清空
{
    FILE *fp;
    system("cls");                        //清屏函数,不管下面是否保存数据,每次输入新数据是屏幕上只能有此次的数据
    system ( "color C0" ); //设置颜色
    if((fp=fopen("D:\\caishuziyou.txt","w"))==NULL)
    {
        printf("open file error\n");
        return;
    }
    fclose(fp);
    printf("\n按任意键返回!");
    getch();
}
 
int main()
{
    int input=0;
    srand((unsigned)time(NULL));
    do{
//while(1)
//{
    system("cls");
        menu();
 // printf("请选择:");
        scanf("%d",&input);
        switch(input)
        {
            case 1:
               menu2();
                game();
                break;
            case 2:
                chakan();
                 break;
            case 3:
                qk();
                break;
            case 0:
                exit(0);
                break;
            default:printf("输入错误!请重新输入!");
         }
//}
        }while(input);
    return 0;
}

以上就是神奇的c/c++小游戏的详细代码,更多关于c/c++小游戏的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/qq_43624878/article/details/85984388

延伸 · 阅读

精彩推荐