翻到开学之初写的一些小游戏,陆续给大家发出来,也便提高新手们的编程兴趣。(已详细注释)
几个对应头文件需要注意一下,但不必太过恐慌,因为,,,很多,用的时候找需要的即可
(在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++小游戏内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!