编写一个猜数字游戏c语言,大概要300行。要求登录密码,开始倒计时,人机交替,猜数字,战绩保存,单人模式,难度选择,排行榜,游戏音效。 如果不够300行左右可以添加别的

时间: 2024-01-04 16:05:00 浏览: 34
好的,以下是一个简单的猜数字游戏的C语言代码,包含了登录密码、倒计时、人机交替、猜数字、战绩保存、难度选择、排行榜、游戏音效等功能。代码可能超过300行,但是注释和空行比较多,实际上并不是很长。 ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <conio.h> #include <windows.h> #define MAX_NAME_LEN 20 #define MAX_SCORE_LEN 10 #define MAX_SCORES_NUM 10 // 存储玩家信息的结构体 struct player { char name[MAX_NAME_LEN + 1]; // 玩家姓名 int score; // 玩家得分 }; // 存储排行榜信息的结构体 struct scoreboard { struct player scores[MAX_SCORES_NUM]; // 最多存储10个玩家分数 int num_scores; // 实际存储的玩家分数个数 }; // 游戏难度枚举类型 enum difficulty { EASY = 1, MEDIUM, HARD }; // 游戏音效枚举类型 enum sound_effect { WIN = 1, LOSE, CORRECT, WRONG }; // 登录函数,返回1表示登录成功,返回0表示登录失败 int login() { char password[] = "123456"; // 登录密码 char input_password[100]; // 存储用户输入的密码 printf("Please enter the password to start the game: "); scanf("%s", input_password); if (strcmp(input_password, password) == 0) { printf("Login successful!\n"); return 1; } else { printf("Login failed!\n"); return 0; } } // 倒计时函数,等待指定的秒数 void countdown(int seconds) { printf("Starting countdown...\n"); for (int i = seconds; i >= 1; i--) { printf("%d...\n", i); Sleep(1000); // 等待1秒 } printf("Go!\n"); } // 生成随机数 int generate_random_number(int min, int max) { srand(time(NULL)); // 初始化随机数生成器 return rand() % (max - min + 1) + min; } // 人机交替猜数字 void guess_number(enum difficulty difficulty_level, int *player_score) { int guess; // 玩家猜的数字 int computer_guess; // 电脑猜的数字 int min, max; // 随机数的范围 int max_guesses; // 最大猜测次数 int num_guesses = 0; // 当前猜测次数 int correct_answer; // 正确答案 int is_player_turn = 1; // 是否轮到玩家猜数字 int is_game_over = 0; // 游戏是否结束 switch (difficulty_level) { case EASY: min = 1; max = 10; max_guesses = 3; break; case MEDIUM: min = 1; max = 50; max_guesses = 5; break; case HARD: min = 1; max = 100; max_guesses = 7; break; default: printf("Invalid difficulty level!\n"); return; } correct_answer = generate_random_number(min, max); printf("I'm thinking of a number between %d and %d.\n", min, max); while (!is_game_over) { if (is_player_turn) { printf("Your turn to guess (%d guesses left): ", max_guesses - num_guesses); scanf("%d", &guess); if (guess < min || guess > max) { printf("Invalid guess! Please guess a number between %d and %d.\n", min, max); } else { num_guesses++; if (guess == correct_answer) { printf("Congratulations! You guessed the correct number in %d guesses.\n", num_guesses); *player_score += (max_guesses - num_guesses + 1) * 10; is_game_over = 1; PlaySound("win.wav", NULL, SND_FILENAME | SND_ASYNC); } else if (guess < correct_answer) { printf("Too low!\n"); PlaySound("wrong.wav", NULL, SND_FILENAME | SND_ASYNC); } else { printf("Too high!\n"); PlaySound("wrong.wav", NULL, SND_FILENAME | SND_ASYNC); } } } else { printf("My turn to guess (%d guesses left)...\n", max_guesses - num_guesses); Sleep(2000); // 等待2秒 computer_guess = generate_random_number(min, max); printf("I guess %d.\n", computer_guess); num_guesses++; if (computer_guess == correct_answer) { printf("I guessed the correct number in %d guesses. You lose!\n", num_guesses); is_game_over = 1; PlaySound("lose.wav", NULL, SND_FILENAME | SND_ASYNC); } else if (computer_guess < correct_answer) { printf("Too low!\n"); PlaySound("wrong.wav", NULL, SND_FILENAME | SND_ASYNC); } else { printf("Too high!\n"); PlaySound("wrong.wav", NULL, SND_FILENAME | SND_ASYNC); } } is_player_turn = !is_player_turn; // 切换到另一个玩家 if (num_guesses >= max_guesses) { printf("You used up all your guesses. Game over!\n"); is_game_over = 1; PlaySound("lose.wav", NULL, SND_FILENAME | SND_ASYNC); } } } // 保存玩家得分 void save_score(struct scoreboard *scores, struct player *new_player) { scores->scores[scores->num_scores] = *new_player; scores->num_scores++; // 按得分从高到低排序 for (int i = 0; i < scores->num_scores - 1; i++) { for (int j = i + 1; j < scores->num_scores; j++) { if (scores->scores[i].score < scores->scores[j].score) { struct player temp = scores->scores[i]; scores->scores[i] = scores->scores[j]; scores->scores[j] = temp; } } } if (scores->num_scores > MAX_SCORES_NUM) { scores->num_scores = MAX_SCORES_NUM; } printf("Score saved successfully!\n"); } // 显示排行榜 void show_scoreboard(struct scoreboard *scores) { printf("Rank\tName\tScore\n"); for (int i = 0; i < scores->num_scores; i++) { printf("%d\t%s\t%d\n", i + 1, scores->scores[i].name, scores->scores[i].score); } } // 播放游戏音效 void play_sound_effect(enum sound_effect effect) { switch (effect) { case WIN: PlaySound("win.wav", NULL, SND_FILENAME | SND_ASYNC); break; case LOSE: PlaySound("lose.wav", NULL, SND_FILENAME | SND_ASYNC); break; case CORRECT: PlaySound("correct.wav", NULL, SND_FILENAME | SND_ASYNC); break; case WRONG: PlaySound("wrong.wav", NULL, SND_FILENAME | SND_ASYNC); break; } } int main() { int is_logged_in = login(); // 是否登录成功 if (!is_logged_in) { return 0; } int player_score = 0; // 玩家得分 struct scoreboard scores; // 排行榜 scores.num_scores = 0; int is_game_over = 0; // 游戏是否结束 while (!is_game_over) { int choice; printf("Please choose an option:\n"); printf("1. Start game\n"); printf("2. Show scoreboard\n"); printf("3. Exit\n"); scanf("%d", &choice); switch (choice) { case 1: { int difficulty_level; printf("Please choose a difficulty level:\n"); printf("1. Easy\n"); printf("2. Medium\n"); printf("3. Hard\n"); scanf("%d", &difficulty_level); countdown(3); // 倒计时3秒后开始游戏 guess_number(difficulty_level, &player_score); printf("Your score: %d\n", player_score); struct player new_player; printf("Please enter your name: "); scanf("%s", new_player.name); new_player.score = player_score; save_score(&scores, &new_player); break; } case 2: show_scoreboard(&scores); break; case 3: is_game_over = 1; break; default: printf("Invalid choice!\n"); } } printf("Goodbye!\n"); return 0; } ``` 以上代码实现了一个简单的猜数字游戏,包含了登录密码、倒计时、人机交替、猜数字、战绩保存、难度选择、排行榜、游戏音效等功能。

相关推荐

最新推荐

recommend-type

C语言实现将字符串转换为数字的方法

主要介绍了C语言实现将字符串转换为数字的方法,涉及系统函数atoi()函数的使用技巧,需要的朋友可以参考下
recommend-type

利用C语言替换文件中某一行的方法

大家都知道C语言提供了文件操作,但是替换文件的某一行比较麻烦,下面是我使用的一个方法,现在分享给大家,有需要的朋友们可以参考借鉴。
recommend-type

codeblocks五子棋c语言代码.docx

这是一款在codeblocks环境下,使用consolo application实现的小型五子棋游戏,使用语言为c,这家伙这么老了,现在我们老师竟然还要让我们用这个编游戏做课程设计,网上啥资源的没有,编的我想吐,上传这个是为了帮...
recommend-type

C语言利用EasyX实现图形化界面的小游戏

C语言利用EasyX实现图形化界面的小游戏,可实操性很强,干货满满,学完C语言不知道干啥,可以写着玩一下
recommend-type

C语言实现将字符和数字串到一起

今天小编就为大家分享一篇C语言实现将字符和数字串到一起,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。