c/c++小游戲源代碼
翻到開學(xué)之初寫的一些小游戲,陸續(xù)給大家發(fā)出來,也便提高新手們的編程興趣。(已詳細(xì)注釋)
幾個(gè)對(duì)應(yīng)頭文件需要注意一下,但不必太過恐慌,因?yàn)?,,,很多,用的時(shí)候找需要的即可
(在Dev,codeblocks,VC上都能運(yùn)行)
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h> //下面Sleep()函數(shù)的頭文件
#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("請(qǐng)選擇: \n");
}
void game()
{
system("cls");
system ( "color 33" ); //設(shè)置顏色
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("請(qǐng)輸入你要猜的數(shù)字:");
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" ); //設(shè)置顏色
printf("即將登陸系統(tǒng)?。?!\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" ); //設(shè)置顏色
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)//若不到文件結(jié)尾則繼續(xù)
{
printf("此玩家共進(jìn)行了%d次\n",t);
}
printf("\n按任意鍵返回目錄");
getch();
}
void qk()//清空
{
FILE *fp;
system("cls"); //清屏函數(shù),不管下面是否保存數(shù)據(jù),每次輸入新數(shù)據(jù)是屏幕上只能有此次的數(shù)據(jù)
system ( "color C0" ); //設(shè)置顏色
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("請(qǐng)選擇:");
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("輸入錯(cuò)誤!請(qǐng)重新輸入!");
}
//}
}while(input);
return 0;
}
C語言里比較重要的知識(shí)也就文件、指針、鏈表、結(jié)構(gòu)體和數(shù)組了,本文用的文件
希望大家能將知識(shí)化為己用。。。
相關(guān)的還有:
到此這篇關(guān)于c/c++小游戲源代碼的文章就介紹到這了,更多相關(guān)c++小游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Ubuntu18.04配置VSCode+CMake的C++開發(fā)環(huán)境
這篇文章主要介紹了詳解Ubuntu18.04配置VSCode+CMake的C++開發(fā)環(huán)境,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
QT使用QML實(shí)現(xiàn)地圖繪制虛線的示例代碼
QML提供了MapPolyline用于在地圖上繪制線段,這篇文章主要為大家詳細(xì)介紹了QT如何使用QML實(shí)現(xiàn)在地圖上繪制虛線,需要的小伙伴可以參考一下2023-07-07
C語言?超詳細(xì)講解算法的時(shí)間復(fù)雜度和空間復(fù)雜度
算法復(fù)雜度分為時(shí)間復(fù)雜度和空間復(fù)雜度。其作用:?時(shí)間復(fù)雜度是度量算法執(zhí)行的時(shí)間長(zhǎng)短;而空間復(fù)雜度是度量算法所需存儲(chǔ)空間的大小2022-03-03
基于VC 6.0使用C語言實(shí)現(xiàn)俄羅斯方塊
這篇文章主要為大家詳細(xì)介紹了基于VC 6.0使用C語言實(shí)現(xiàn)俄羅斯方塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
C++實(shí)現(xiàn)簡(jiǎn)單貪吃蛇小游戲
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
c++類型轉(zhuǎn)換及RTTI運(yùn)行階段類型識(shí)別
這篇文章主要為大家介紹了c++類型轉(zhuǎn)換及RTTI運(yùn)行階段類型識(shí)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2023-05-05
VS中scanf為何會(huì)報(bào)錯(cuò)詳解
在我們剛使用vs時(shí),在使用scanf函數(shù)時(shí)常會(huì)遇到報(bào)錯(cuò)提醒,下面這篇文章主要給大家介紹了關(guān)于VS中scanf為何會(huì)報(bào)錯(cuò)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02

