1. 程式人生 > 程式設計 >神奇的c/c++小遊戲((提高你的程式設計興趣)

神奇的c/c++小遊戲((提高你的程式設計興趣)

目錄
  • 神奇的c/c++

神奇的c/c++

以下程式碼在Dev,codeblocks,VC上都能執行

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#include&lhttp://www.cppcns.comt;windows.h>   //下面Sleep()函式的標頭檔案
#include<mmsystem.h>

void menu()
{
	printf("   *****************************\n");
	printf("   *****************************\n");
	printf("   ************1.play******xoRTxKfUZu
*****\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; iwww.cppcns.com
nt tmp=0,t=0; while(1) { int flag=0; printf("請輸入你要猜的數字:"); scanf("%d",&tmp); if(tmp>rand_num) { printf("猜大了!\n"); flag=0; } http://www.cppcns.com 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,&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++小遊戲的資料請關注我們其它相關文章!