浣熊市-生化危机
Chenguoyao44 · · 科技·工程
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// 全局常量(避免编译器歧义)
#define MAX_INVENTORY 10
#define SAVE_FILE "biohazard_save.dat"
#define CRITICAL_CHANCE 20
#define ESCAPE_BASE_CHANCE 30
#define MAX_NAME_LEN 20
#define MAX_ITEM_NAME_LEN 30
// 道具枚举(添加typedef兼容旧编译器)
typedef enum _ItemType {
ITEM_NONE,
ITEM_FIRST_AID,
ITEM_HERB,
ITEM_BULLET,
ITEM_SHOTGUN_AMMO,
ITEM_KEY1,
ITEM_KEY2,
ITEM_VIRUS_SAMPLE,
ITEM_GEM
} ItemType;
// 敌人类型枚举(添加typedef兼容旧编译器)
typedef enum _EnemyType {
ENEMY_ZOMBIE,
ENEMY_DOG,
ENEMY_LICKER,
ENEMY_BOSS_G_MUTT
} EnemyType;
// 道具结构体(明确字段长度)
typedef struct _Item {
ItemType type;
char name[MAX_ITEM_NAME_LEN];
int count;
} Item;
// 敌人结构体(明确字段长度)
typedef struct _Enemy {
EnemyType type;
char name[MAX_ITEM_NAME_LEN];
int hp;
int max_hp;
int attack;
int speed;
int drop_rate;
Item drop_item;
} Enemy;
// 玩家结构体(明确字段长度,避免对齐问题)
typedef struct _Player {
char name[MAX_NAME_LEN];
int hp;
int max_hp;
int attack;
int dodge_rate;
int location;
Item inventory[MAX_INVENTORY];
int inventory_count;
int has_shotgun;
int key_count;
int virus_sample;
int gem_count;
int game_progress;
} Player;
// 全局变量(简化传递)
Player player;
Enemy current_enemy;
int game_over = 0;
int battle_win = 0;
// 函数声明(严格按调用顺序,避免未声明警告)
void init_game(void);
void load_game(void);
void save_game(void);
void print_title(void);
void main_menu(void);
void create_character(void);
void display_status(void);
void display_inventory(void);
void use_item(int index);
void remove_item(int index);
int add_item(ItemType type, int count);
void location_description(void);
void explore_location(void);
void random_encounter(void);
Enemy generate_enemy(void);
void battle_system(void);
void player_attack(int is_shotgun);
void enemy_attack(void);
int calculate_critical(void);
int calculate_dodge(int speed);
int try_escape(void);
void boss_battle(void);
void game_ending(void);
void clear_screen(void);
void press_any_key(void);
void delay(int ms); // 纯C延迟函数
// 主函数(程序入口)
int main(void) {
srand((unsigned int)time(NULL));
init_game();
return 0;
}
// 初始化游戏(纯数字选择,无兼容性问题)
void init_game(void) {
int choice;
clear_screen();
print_title();
printf("\n\t1. 新游戏");
printf("\n\t2. 读取存档");
printf("\n\t3. 退出游戏");
printf("\n\n\t请选择(1-3):");
// 强制读取整数,避免输入错误
while(scanf("%d", &choice) != 1 || choice < 1 || choice > 3) {
printf("\n\t选择无效!请输入1-3:");
while(getchar() != '\n'); // 清空输入缓冲区
}
switch(choice) {
case 1:
create_character();
break;
case 2:
load_game();
break;
case 3:
printf("\n\t感谢游玩!再见!\n");
exit(0);
default:
printf("\n\t选择无效!2秒后重新初始化...\n");
delay(2000);
init_game();
}
}
// 打印标题(纯字符串拼接,无语法问题)
void print_title(void) {
printf("======================================================");
printf("\n\t\t生化危机:浣熊市突围");
printf("\n\t\t Resident Evil: Raccoon Escape");
printf("\n======================================================");
}
// 主菜单(核心流程)
void main_menu(void) {
int choice;
while(!game_over) {
clear_screen();
print_title();
printf("\n\n【当前位置】:");
switch(player.location) {
case 1: printf("浣熊市警察局 - 大厅"); break;
case 2: printf("浣熊市警察局 - 西翼走廊"); break;
case 3: printf("下水道 - 主通道"); break;
case 4: printf("Umbrella 地下实验室"); break;
case 5: printf("实验室 - 最终Boss房间"); break;
default: printf("未知区域"); break;
}
printf("\n【剧情进度】:%d%%", player.game_progress);
printf("\n\n【主菜单】");
printf("\n\t1. 查看状态");
printf("\n\t2. 查看背包");
printf("\n\t3. 探索当前区域");
printf("\n\t4. 保存游戏");
printf("\n\t5. 退出游戏");
printf("\n\n\t请选择(1-5):");
while(scanf("%d", &choice) != 1 || choice < 1 || choice > 5) {
printf("\n\t选择无效!请输入1-5:");
while(getchar() != '\n');
}
switch(choice) {
case 1:
display_status();
break;
case 2:
display_inventory();
break;
case 3:
explore_location();
break;
case 4:
save_game();
break;
case 5:
{
int confirm;
printf("\n\t确定要退出吗?(1=是/0=否):");
while(scanf("%d", &confirm) != 1 || confirm < 0 || confirm > 1) {
printf("\n\t输入无效!请输入1或0:");
while(getchar() != '\n');
}
if(confirm == 1) {
printf("\n\t感谢游玩!再见!\n");
exit(0);
}
}
break;
default:
printf("\n\t选择无效!2秒后返回主菜单...\n");
delay(2000);
}
}
game_ending();
}
// 创建角色(初始化属性)
void create_character(void) {
clear_screen();
print_title();
printf("\n\n【创建角色】");
printf("\n\t请输入你的名字(不超过19个字符):");
scanf("%s", player.name);
// 截断过长名字,避免溢出
player.name[MAX_NAME_LEN-1] = '\0';
// 初始化玩家属性
player.max_hp = 150;
player.hp = 150;
player.attack = 20;
player.dodge_rate = 10;
player.location = 1;
player.inventory_count = 0;
player.has_shotgun = 0;
player.key_count = 0;
player.virus_sample = 0;
player.gem_count = 0;
player.game_progress = 10;
// 初始道具
add_item(ITEM_HERB, 2);
add_item(ITEM_BULLET, 30);
printf("\n\t角色创建成功!%s,欢迎来到浣熊市!", player.name);
printf("\n\t你发现自己被困在警局大厅,周围充满了丧尸的嘶吼...");
press_any_key();
main_menu();
}
// 显示玩家状态
void display_status(void) {
clear_screen();
print_title();
printf("\n\n【玩家状态】");
printf("\n\t姓名:%s", player.name);
printf("\n\t生命值:%d/%d", player.hp, player.max_hp);
printf("\n\t攻击力:%d", player.attack);
printf("\n\t闪避率:%d%%", player.dodge_rate);
printf("\n\t当前位置:");
switch(player.location) {
case 1: printf("浣熊市警察局 - 大厅"); break;
case 2: printf("浣熊市警察局 - 西翼走廊"); break;
case 3: printf("下水道 - 主通道"); break;
case 4: printf("Umbrella 地下实验室"); break;
case 5: printf("实验室 - 最终Boss房间"); break;
default: printf("未知区域"); break;
}
printf("\n\t剧情进度:%d%%", player.game_progress);
printf("\n\t持有钥匙:%d把", player.key_count);
printf("\n\t特殊道具:%s(病毒样本) | %s(散弹枪) | %d个(隐藏宝石)",
player.virus_sample ? "?" : "?",
player.has_shotgun ? "?" : "?",
player.gem_count);
press_any_key();
}
// 显示背包
void display_inventory(void) {
int choice, index;
clear_screen();
print_title();
printf("\n\n【背包】(容量:%d/%d)", player.inventory_count, MAX_INVENTORY);
if(player.inventory_count == 0) {
printf("\n\t背包为空!");
} else {
for(int i=0; i<player.inventory_count; i++) {
printf("\n\t%d. %s(数量:%d)", i+1, player.inventory[i].name, player.inventory[i].count);
switch(player.inventory[i].type) {
case ITEM_FIRST_AID: printf(" - 恢复100点生命值"); break;
case ITEM_HERB: printf(" - 恢复50点生命值"); break;
case ITEM_BULLET: printf(" - 手枪弹药"); break;
case ITEM_SHOTGUN_AMMO: printf(" - 散弹枪弹药"); break;
case ITEM_KEY1: printf(" - 打开警局西翼大门"); break;
case ITEM_KEY2: printf(" - 解锁实验室入口"); break;
case ITEM_VIRUS_SAMPLE: printf(" - 关键剧情道具"); break;
case ITEM_GEM: printf(" - 隐藏彩蛋道具"); break;
default: break;
}
}
}
printf("\n\n\t1. 使用道具 2. 返回主菜单");
printf("\n\t请选择:");
while(scanf("%d", &choice) != 1 || choice < 1 || choice > 2) {
printf("\n\t选择无效!请输入1或2:");
while(getchar() != '\n');
}
if(choice == 1 && player.inventory_count > 0) {
printf("\n\t请选择要使用的道具编号(1-%d):", player.inventory_count);
while(scanf("%d", &index) != 1 || index < 1 || index > player.inventory_count) {
printf("\n\t编号无效!请输入1-%d:", player.inventory_count);
while(getchar() != '\n');
}
use_item(index-1);
}
}
// 使用道具
void use_item(int index) {
Item *item = &player.inventory[index];
switch(item->type) {
case ITEM_FIRST_AID:
player.hp += 100;
if(player.hp > player.max_hp) player.hp = player.max_hp;
printf("\n\t使用了急救喷雾!生命值恢复至%d/%d", player.hp, player.max_hp);
break;
case ITEM_HERB:
player.hp += 50;
if(player.hp > player.max_hp) player.hp = player.max_hp;
printf("\n\t使用了绿色草药!生命值恢复至%d/%d", player.hp, player.max_hp);
break;
case ITEM_BULLET:
case ITEM_SHOTGUN_AMMO:
printf("\n\t弹药不能直接使用!战斗时自动消耗!");
press_any_key();
return;
case ITEM_KEY1:
case ITEM_KEY2:
case ITEM_VIRUS_SAMPLE:
case ITEM_GEM:
printf("\n\t该道具不能使用!");
press_any_key();
return;
default:
printf("\n\t无法使用该道具!");
press_any_key();
return;
}
item->count--;
if(item->count <= 0) {
remove_item(index);
}
press_any_key();
}
// 移除道具
void remove_item(int index) {
for(int i=index; i<player.inventory_count-1; i++) {
player.inventory[i] = player.inventory[i+1];
}
player.inventory_count--;
}
// 添加道具
int add_item(ItemType type, int count) {
if(player.inventory_count >= MAX_INVENTORY) {
printf("\n\t背包已满!无法拾取该道具!");
return 0;
}
// 叠加相同道具
for(int i=0; i<player.inventory_count; i++) {
if(player.inventory[i].type == type) {
player.inventory[i].count += count;
printf("\n\t获得了%d个%s!当前数量:%d", count, player.inventory[i].name, player.inventory[i].count);
return 1;
}
}
// 添加新道具
Item new_item;
new_item.type = type;
new_item.count = count;
switch(type) {
case ITEM_FIRST_AID: strncpy(new_item.name, "急救喷雾", MAX_ITEM_NAME_LEN-1); break;
case ITEM_HERB: strncpy(new_item.name, "绿色草药", MAX_ITEM_NAME_LEN-1); break;
case ITEM_BULLET: strncpy(new_item.name, "手枪弹药", MAX_ITEM_NAME_LEN-1); break;
case ITEM_SHOTGUN_AMMO: strncpy(new_item.name, "散弹枪弹药", MAX_ITEM_NAME_LEN-1); break;
case ITEM_KEY1: strncpy(new_item.name, "警局西翼钥匙", MAX_ITEM_NAME_LEN-1); break;
case ITEM_KEY2: strncpy(new_item.name, "下水道密码卡", MAX_ITEM_NAME_LEN-1); break;
case ITEM_VIRUS_SAMPLE: strncpy(new_item.name, "G病毒样本", MAX_ITEM_NAME_LEN-1); break;
case ITEM_GEM: strncpy(new_item.name, "神秘宝石", MAX_ITEM_NAME_LEN-1); break;
default: strncpy(new_item.name, "未知道具", MAX_ITEM_NAME_LEN-1); break;
}
new_item.name[MAX_ITEM_NAME_LEN-1] = '\0'; // 确保字符串结束符
player.inventory[player.inventory_count++] = new_item;
printf("\n\t获得了%s x%d!", new_item.name, count);
// 特殊道具效果
if(type == ITEM_KEY1 || type == ITEM_KEY2) player.key_count++;
if(type == ITEM_VIRUS_SAMPLE) player.virus_sample = 1;
if(type == ITEM_GEM) {
player.gem_count++;
if(player.gem_count == 3) {
printf("\n\t集齐3颗神秘宝石!解锁隐藏剧情!");
player.dodge_rate += 20;
player.attack += 10;
printf("\n\t你的属性大幅提升!闪避率+20%%,攻击力+10!");
}
}
return 1;
}
// 场景描述
void location_description(void) {
clear_screen();
print_title();
printf("\n\n【场景描述】");
switch(player.location) {
case 1:
printf("\n\t浣熊市警察局大厅 - 曾经的安全区如今一片狼藉。");
printf("\n\t地面散落着文件和血迹,远处传来丧尸的低吼。");
printf("\n\t大厅东侧是锁住的办公室,西侧有一扇需要钥匙的大门。");
printf("\n\t墙角似乎藏着什么东西...");
break;
case 2:
printf("\n\t警局西翼走廊 - 灯光闪烁,墙壁上布满抓痕。");
printf("\n\t走廊两侧的房间里不时传出异响,似乎有生物在移动。");
printf("\n\t尽头是通往地下室的楼梯,楼梯口被杂物堵塞。");
printf("\n\t地上有一把生锈的散弹枪...");
break;
case 3:
printf("\n\t下水道主通道 - 弥漫着刺鼻的污水味和腐烂气息。");
printf("\n\t水面漂浮着不明物体,墙壁上的管道不断滴水。");
printf("\n\t通道分为左右两条路:左边通往深处,右边有一扇电子门。");
printf("\n\t电子门需要密码卡才能打开...");
break;
case 4:
printf("\n\tUmbrella 地下实验室 - 高科技设施与生物污染并存。");
printf("\n\t实验室里有各种实验设备,屏幕上显示着病毒数据。");
printf("\n\t中央培养皿中似乎有巨大的生物在蠕动,墙角的保险箱里可能有关键道具。");
printf("\n\t实验室深处传来令人毛骨悚然的咆哮...");
break;
case 5:
printf("\n\t实验室 - 最终Boss房间 - 这里是G病毒实验的核心区域。");
printf("\n\t地面覆盖着生物黏液,巨大的培养舱破碎不堪。");
printf("\n\t前方,G病毒突变体正盯着你,它的身体不断扭曲变异!");
printf("\n\t这是最后的决战,要么生存,要么成为病毒的一部分...");
break;
default:
printf("\n\t未知区域 - 充满未知的危险...");
break;
}
}
// 探索场景
void explore_location(void) {
int choice;
location_description();
printf("\n\n【探索选项】");
switch(player.location) {
case 1:
printf("\n\t1. 搜索墙角(可能有道具)");
printf("\n\t2. 尝试打开东侧办公室");
printf("\n\t3. 寻找西翼钥匙");
printf("\n\t4. 返回主菜单");
break;
case 2:
printf("\n\t1. 拾取散弹枪");
printf("\n\t2. 清理楼梯口杂物(前往下水道)");
printf("\n\t3. 搜索两侧房间");
printf("\n\t4. 返回大厅");
break;
case 3:
printf("\n\t1. 探索左侧通道(可能遇敌)");
printf("\n\t2. 使用密码卡打开电子门(前往实验室)");
printf("\n\t3. 搜索污水旁的箱子");
printf("\n\t4. 返回警局西翼");
break;
case 4:
printf("\n\t1. 打开保险箱(获取关键道具)");
printf("\n\t2. 检查中央培养皿");
printf("\n\t3. 前往Boss房间");
printf("\n\t4. 返回下水道");
break;
case 5:
printf("\n\t1. 发起攻击(进入Boss战)");
printf("\n\t2. 使用道具(战前准备)");
printf("\n\t3. 查看状态");
printf("\n\t4. 返回实验室");
break;
default:
printf("\n\t1. 四处游荡 2. 返回主菜单");
break;
}
printf("\n\n\t请选择:");
while(scanf("%d", &choice) != 1) {
printf("\n\t输入无效!请输入数字:");
while(getchar() != '\n');
}
switch(player.location) {
case 1:
switch(choice) {
case 1:
printf("\n\t你仔细搜索了墙角,发现了1个绿色草药!");
add_item(ITEM_HERB, 1);
press_any_key();
break;
case 2:
printf("\n\t办公室门锁着,你需要找到钥匙才能打开...");
if(rand()%100 < 30) {
printf("\n\t突然,一只丧尸从办公室窗户扑了出来!");
press_any_key();
random_encounter();
}
press_any_key();
break;
case 3:
printf("\n\t你在接待台的抽屉里翻找...");
if(rand()%100 < 60) {
add_item(ITEM_KEY1, 1);
player.game_progress += 10;
} else {
printf("\n\t什么也没找到,反而惊动了附近的丧尸!");
press_any_key();
random_encounter();
}
press_any_key();
break;
case 4:
break;
default:
printf("\n\t选择无效!2秒后返回...");
delay(2000);
}
break;
case 2:
switch(choice) {
case 1:
if(!player.has_shotgun) {
player.has_shotgun = 1;
player.attack += 30;
add_item(ITEM_SHOTGUN_AMMO, 10);
printf("\n\t你获得了散弹枪!攻击力大幅提升!");
player.game_progress += 15;
} else {
printf("\n\t你已经拥有散弹枪了!");
}
press_any_key();
break;
case 2:
printf("\n\t你清理了楼梯口的杂物,通往下水道的路打开了!");
player.location = 3;
player.game_progress += 10;
press_any_key();
break;
case 3:
printf("\n\t你进入房间搜索,发现了急救喷雾!");
add_item(ITEM_FIRST_AID, 1);
if(rand()%100 < 40) {
printf("\n\t房间深处传来利爪摩擦地面的声音...");
press_any_key();
do {
current_enemy = generate_enemy();
} while(current_enemy.type != ENEMY_LICKER);
battle_system();
}
press_any_key();
break;
case 4:
player.location = 1;
press_any_key();
break;
default:
printf("\n\t选择无效!2秒后返回...");
delay(2000);
}
break;
case 3:
switch(choice) {
case 1:
printf("\n\t你走进黑暗的通道,污水漫过脚踝...");
random_encounter();
press_any_key();
break;
case 2:
{
int has_key2 = 0;
for(int i=0; i<player.inventory_count; i++) {
if(player.inventory[i].type == ITEM_KEY2) {
has_key2 = 1;
break;
}
}
if(has_key2) {
printf("\n\t密码卡验证通过,电子门缓缓打开!");
player.location = 4;
player.game_progress += 15;
} else {
printf("\n\t你没有密码卡,无法打开电子门!");
}
press_any_key();
}
break;
case 3:
printf("\n\t你打开了污水旁的箱子,发现了下水道密码卡和手枪弹药!");
add_item(ITEM_KEY2, 1);
add_item(ITEM_BULLET, 20);
player.game_progress += 5;
press_any_key();
break;
case 4:
player.location = 2;
press_any_key();
break;
default:
printf("\n\t选择无效!2秒后返回...");
delay(2000);
}
break;
case 4:
switch(choice) {
case 1:
printf("\n\t你破解了保险箱密码,获得了G病毒样本!");
add_item(ITEM_VIRUS_SAMPLE, 1);
player.game_progress += 10;
if(rand()%100 < 50) {
printf("\n\t病毒样本的气味吸引了变异生物!");
press_any_key();
do {
current_enemy = generate_enemy();
} while(current_enemy.type != ENEMY_DOG);
battle_system();
}
press_any_key();
break;
case 2:
printf("\n\t培养皿中是变异的病毒载体,你从中找到了1颗神秘宝石!");
add_item(ITEM_GEM, 1);
press_any_key();
break;
case 3:
printf("\n\t你走向实验室深处,Boss的气息越来越浓...");
player.location = 5;
player.game_progress += 10;
press_any_key();
break;
case 4:
player.location = 3;
press_any_key();
break;
default:
printf("\n\t选择无效!2秒后返回...");
delay(2000);
}
break;
case 5:
switch(choice) {
case 1:
boss_battle();
break;
case 2:
display_inventory();
break;
case 3:
display_status();
break;
case 4:
player.location = 4;
press_any_key();
break;
default:
printf("\n\t选择无效!2秒后返回...");
delay(2000);
}
break;
default:
switch(choice) {
case 1:
printf("\n\t你在未知区域游荡,遭遇了怪物!");
press_any_key();
random_encounter();
break;
case 2:
break;
default:
printf("\n\t选择无效!2秒后返回...");
delay(2000);
}
break;
}
}
// 随机遇敌
void random_encounter(void) {
current_enemy = generate_enemy();
printf("\n\t遭遇了%s!战斗开始!", current_enemy.name);
press_any_key();
battle_system();
}
// 生成敌人
Enemy generate_enemy(void) {
Enemy enemy;
int type = rand()%3;
switch(type) {
case 0:
enemy.type = ENEMY_ZOMBIE;
strncpy(enemy.name, "普通丧尸", MAX_ITEM_NAME_LEN-1);
enemy.max_hp = 80 + rand()%40;
enemy.hp = enemy.max_hp;
enemy.attack = 15 + rand()%10;
enemy.speed = 20;
enemy.drop_rate = 40;
enemy.drop_item.type = ITEM_HERB;
enemy.drop_item.count = 1;
break;
case 1:
enemy.type = ENEMY_DOG;
strncpy(enemy.name, "丧尸犬", MAX_ITEM_NAME_LEN-1);
enemy.max_hp = 60 + rand()%30;
enemy.hp = enemy.max_hp;
enemy.attack = 25 + rand()%15;
enemy.speed = 60;
enemy.drop_rate = 50;
enemy.drop_item.type = ITEM_BULLET;
enemy.drop_item.count = 10;
break;
case 2:
enemy.type = ENEMY_LICKER;
strncpy(enemy.name, "舔食者", MAX_ITEM_NAME_LEN-1);
enemy.max_hp = 150 + rand()%50;
enemy.hp = enemy.max_hp;
enemy.attack = 40 + rand()%20;
enemy.speed = 40;
enemy.drop_rate = 70;
enemy.drop_item.type = ITEM_SHOTGUN_AMMO;
enemy.drop_item.count = 5;
break;
default:
enemy.type = ENEMY_ZOMBIE;
strncpy(enemy.name, "普通丧尸", MAX_ITEM_NAME_LEN-1);
enemy.max_hp = 100;
enemy.hp = 100;
enemy.attack = 20;
enemy.speed = 20;
enemy.drop_rate = 40;
enemy.drop_item.type = ITEM_HERB;
enemy.drop_item.count = 1;
break;
}
enemy.name[MAX_ITEM_NAME_LEN-1] = '\0';
return enemy;
}
// 战斗系统
void battle_system(void) {
int choice;
int has_shotgun_ammo = 0;
battle_win = 0;
// 检查散弹枪弹药
for(int i=0; i<player.inventory_count; i++) {
if(player.inventory[i].type == ITEM_SHOTGUN_AMMO && player.inventory[i].count > 0) {
has_shotgun_ammo = 1;
break;
}
}
while(current_enemy.hp > 0 && player.hp > 0) {
clear_screen();
print_title();
printf("\n\n【战斗中】");
printf("\n\t玩家:%s HP:%d/%d", player.name, player.hp, player.max_hp);
printf("\n\t敌人:%s HP:%d/%d", current_enemy.name, current_enemy.hp, current_enemy.max_hp);
printf("\n\n【战斗选项】");
printf("\n\t1. 普通攻击(手枪)");
if(player.has_shotgun && has_shotgun_ammo) {
printf("\n\t2. 散弹枪攻击(高伤害,消耗弹药)");
}
printf("\n\t3. 使用道具");
printf("\n\t4. 尝试逃跑");
printf("\n\n\t请选择:");
while(scanf("%d", &choice) != 1) {
printf("\n\t输入无效!请输入数字:");
while(getchar() != '\n');
}
// 刷新弹药状态
has_shotgun_ammo = 0;
for(int i=0; i<player.inventory_count; i++) {
if(player.inventory[i].type == ITEM_SHOTGUN_AMMO && player.inventory[i].count > 0) {
has_shotgun_ammo = 1;
break;
}
}
switch(choice) {
case 1:
{
int has_bullet = 0;
int bullet_index = -1;
for(int i=0; i<player.inventory_count; i++) {
if(player.inventory[i].type == ITEM_BULLET && player.inventory[i].count > 0) {
has_bullet = 1;
bullet_index = i;
break;
}
}
if(!has_bullet) {
printf("\n\t手枪弹药耗尽!无法攻击!");
press_any_key();
break;
}
// 消耗弹药
player.inventory[bullet_index].count--;
if(player.inventory[bullet_index].count <= 0) {
remove_item(bullet_index);
}
player_attack(0);
if(current_enemy.hp <= 0) {
battle_win = 1;
break;
}
enemy_attack();
press_any_key();
}
break;
case 2:
if(player.has_shotgun && has_shotgun_ammo) {
int ammo_index = -1;
for(int i=0; i<player.inventory_count; i++) {
if(player.inventory[i].type == ITEM_SHOTGUN_AMMO && player.inventory[i].count > 0) {
ammo_index = i;
break;
}
}
player.inventory[ammo_index].count--;
if(player.inventory[ammo_index].count <= 0) {
remove_item(ammo_index);
}
player_attack(1);
if(current_enemy.hp <= 0) {
battle_win = 1;
break;
}
enemy_attack();
press_any_key();
} else {
printf("\n\t没有散弹枪或弹药不足!");
press_any_key();
}
break;
case 3:
display_inventory();
// 刷新弹药
has_shotgun_ammo = 0;
for(int i=0; i<player.inventory_count; i++) {
if(player.inventory[i].type == ITEM_SHOTGUN_AMMO && player.inventory[i].count > 0) {
has_shotgun_ammo = 1;
break;
}
}
break;
case 4:
if(try_escape()) {
printf("\n\t你成功逃跑了!");
press_any_key();
return;
} else {
printf("\n\t逃跑失败!被敌人追上了!");
enemy_attack();
press_any_key();
}
break;
default:
printf("\n\t选择无效!2秒后重新选择...");
delay(2000);
}
}
// 战斗结束
if(player.hp <= 0) {
player.hp = 0;
printf("\n\t你被%s击败了!游戏结束!", current_enemy.name);
game_over = 1;
} else if(battle_win) {
printf("\n\t你击败了%s!", current_enemy.name);
// 掉落道具
if(rand()%100 < current_enemy.drop_rate) {
add_item(current_enemy.drop_item.type, current_enemy.drop_item.count);
} else {
printf("\n\t敌人没有掉落任何道具。");
}
// 角色成长
int grow_type = rand()%3;
switch(grow_type) {
case 0:
player.max_hp += 10;
player.hp = player.max_hp;
printf("\n\t你的生命值上限提升了10点!");
break;
case 1:
player.attack += 5;
printf("\n\t你的攻击力提升了5点!");
break;
case 2:
player.dodge_rate += 3;
printf("\n\t你的闪避率提升了3%%!");
break;
}
}
press_any_key();
}
// 玩家攻击
void player_attack(int is_shotgun) {
int critical = calculate_critical();
int damage = player.attack;
if(is_shotgun) {
damage *= 2;
printf("\n\t散弹枪近距离爆发!");
}
if(critical) {
damage = (damage * 3) / 2;
printf("\n\t暴击!");
}
current_enemy.hp -= damage;
if(current_enemy.hp < 0) current_enemy.hp = 0;
printf("\n\t你对%s造成了%d点伤害!", current_enemy.name, damage);
}
// 敌人攻击
void enemy_attack(void) {
int dodge = calculate_dodge(current_enemy.speed);
if(dodge) {
printf("\n\t你成功闪避了%s的攻击!", current_enemy.name);
return;
}
int damage = current_enemy.attack;
player.hp -= damage;
if(player.hp < 0) player.hp = 0;
printf("\n\t%s对你造成了%d点伤害!", current_enemy.name, damage);
if(player.hp == 0) {
printf("\n\t你受到了致命伤害!");
}
}
// 计算暴击
int calculate_critical(void) {
return (rand()%100 < CRITICAL_CHANCE) ? 1 : 0;
}
// 计算闪避
int calculate_dodge(int speed) {
int rate = player.dodge_rate - (speed / 5);
if(rate < 0) rate = 0;
return (rand()%100 < rate) ? 1 : 0;
}
// 尝试逃跑
int try_escape(void) {
int rate = ESCAPE_BASE_CHANCE + player.dodge_rate - (current_enemy.speed / 4);
if(rate < 10) rate = 10;
if(rate > 70) rate = 70;
return (rand()%100 < rate) ? 1 : 0;
}
// Boss战
void boss_battle(void) {
current_enemy.type = ENEMY_BOSS_G_MUTT;
strncpy(current_enemy.name, "G病毒突变体", MAX_ITEM_NAME_LEN-1);
current_enemy.name[MAX_ITEM_NAME_LEN-1] = '\0';
current_enemy.max_hp = 800;
current_enemy.hp = 800;
current_enemy.attack = 80;
current_enemy.speed = 30;
current_enemy.drop_rate = 100;
current_enemy.drop_item.type = ITEM_GEM;
current_enemy.drop_item.count = 1;
printf("\n\tG病毒突变体咆哮着向你冲来!最终决战开始!");
press_any_key();
battle_system();
if(battle_win) {
player.game_progress = 100;
game_over = 1;
}
}
// 游戏结局
void game_ending(void) {
clear_screen();
print_title();
printf("\n\n【游戏结局】");
if(player.hp <= 0) {
printf("\n\t很遗憾,你没能逃离浣熊市。");
printf("\n\t你的身体被病毒感染,最终成为了丧尸的一员...");
printf("\n\t结局:悲剧的陨落");
} else if(player.game_progress == 100) {
if(player.virus_sample && player.gem_count >= 3) {
printf("\n\t你成功击败了G病毒突变体,并且持有完整的病毒样本!");
printf("\n\t凭借3颗神秘宝石的力量,你免疫了病毒感染,成功联系到救援直升机。");
printf("\n\t你将病毒样本交给了国际防疫组织,阻止了更大的灾难。");
printf("\n\t结局:救世主");
} else if(player.virus_sample) {
printf("\n\t你成功击败了G病毒突变体,持有病毒样本逃离了实验室。");
printf("\n\t救援直升机将你带离了浣熊市,你成为了病毒研究的关键证人。");
printf("\n\t结局:幸存者");
} else {
printf("\n\t你成功击败了G病毒突变体,但没有获取病毒样本。");
printf("\n\t你逃离了浣熊市,但Umbrella公司的阴谋仍在继续...");
printf("\n\t结局:逃离者");
}
} else {
printf("\n\t你在逃离过程中放弃了,浣熊市的阴影将永远笼罩着你。");
printf("\n\t结局:半途而废");
}
printf("\n\n\t你的最终状态:");
printf("\n\t姓名:%s 生命值:%d/%d 攻击力:%d 闪避率:%d%%",
player.name, player.hp, player.max_hp, player.attack, player.dodge_rate);
printf("\n\t剧情进度:%d%% 收集宝石:%d颗", player.game_progress, player.gem_count);
printf("\n\n\t按回车键退出游戏...");
while(getchar() != '\n');
getchar();
}
// 保存游戏
void save_game(void) {
FILE *file = fopen(SAVE_FILE, "wb");
if(file == NULL) {
printf("\n\t存档失败!无法创建文件!");
press_any_key();
return;
}
fwrite(&player, sizeof(Player), 1, file);
fclose(file);
printf("\n\t存档成功!进度已保存至 %s", SAVE_FILE);
press_any_key();
}
// 读取存档
void load_game(void) {
FILE *file = fopen(SAVE_FILE, "rb");
if(file == NULL) {
int choice;
printf("\n\t没有找到存档文件!是否创建新游戏?(1=是/0=否):");
while(scanf("%d", &choice) != 1 || choice < 0 || choice > 1) {
printf("\n\t输入无效!请输入1或0:");
while(getchar() != '\n');
}
if(choice == 1) {
create_character();
} else {
printf("\n\t退出游戏...");
exit(0);
}
return;
}
fread(&player, sizeof(Player), 1, file);
fclose(file);
// 确保字符串结束符
player.name[MAX_NAME_LEN-1] = '\0';
printf("\n\t存档读取成功!欢迎回来,%s!", player.name);
press_any_key();
main_menu();
}
// 清屏(Dev-C++ 5.9.2 稳定支持)
void clear_screen(void) {
system("cls");
}
// 按任意键继续(纯C实现,无依赖)
void press_any_key(void) {
printf("\n\n\t按回车键继续...");
while(getchar() != '\n');
getchar();
}
// 纯C延迟函数(毫秒级,无系统依赖)
void delay(int ms) {
clock_t start = clock();
while((clock() - start) * 1000 / CLOCKS_PER_SEC < ms);
}