1.纯c语言
2.时间:一周
3.预算40
4.c语言的,要求用纯c写一个纯文字的小游戏(不用太长),不能是市面上出现过的,要求必须原创,至少六个函数(),要有存档功能,有必要的文字说明
针对后台你私信我的问题,我这里回答下我在选择的时候没有增加除1和2不能选的终端提示,已经修改了,有问题请在联系我
你可以去github上找,在那个基础上修改修改,说实话你等一周csdn系统会自动结题
这是我自己编写的游戏,你可以运行试玩一下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CHAPTER 4
#define MAX_OPTION 3
#define MAX_SAVE 10
typedef struct {
char title[100];
char content[1000];
int options[MAX_OPTION];
} Chapter;
Chapter chapters[MAX_CHAPTER] = {
{"第一章", "你是一个年轻的冒险者,正在探险途中。你来到了一个岔路口,左边通向一片森林,右边通向一座山峰。你该怎么做?\n1.向左走\n2.向右走\n", {1, 2}},
{"第二章", "你来到了森林,这里充满了危险和机遇。在森林中,你看到了一个神秘的洞穴,里面似乎有着珍贵的宝藏。但是洞穴深处有很多陷阱和怪物,你是否要冒险一试?\n1.冒险进入洞穴\n2.离开森林,前往山峰\n", {3, 2}},
{"第三章", "你进入了洞穴,这里黑暗潮湿,陷阱和怪物随处可见。你需要小心翼翼地前进,以免掉入陷阱或者被怪物攻击。在前进的过程中,你看到了一扇门,门上写着“禁止入内”。你会怎么做?\n1.进入门内探寻\n2.绕过门继续前进\n", {4, 5}},
{"第四章", "你进入了门内,发现这里是一个神秘的秘室。秘室中有一块珍贵的宝石,但是宝石被一个凶恶的魔兽守护着。你必须战胜魔兽才能获得宝石。你准备好了吗?\n1.战斗!\n2.逃跑!\n", {6, 5}}
};
int current_chapter = 0;
int save_count = 0;
int saved_chapter[MAX_SAVE] = {0};
int get_choice() {
int choice = 0;
do {
printf("请输入你的选择:");
scanf("%d", &choice);
if (choice != 1 && choice != 2) {
printf("无效的选择,请重新输入:");
}
} while (choice != 1 && choice != 2);
return choice;
}
void print_chapter(int chapter_id) {
printf("%s\n", chapters[chapter_id].title);
printf("%s", chapters[chapter_id].content);
}
void play_game() {
while (current_chapter < MAX_CHAPTER) {
print_chapter(current_chapter);
int choice = get_choice();
saved_chapter[save_count++] = current_chapter;
current_chapter = chapters[current_chapter].options[choice - 1] - 1;
}
printf("你已经完成了游戏!\n");
}
void load_game() {
printf("你有以下存档:\n");
for (int i = 0; i < save_count; i++) {
printf("%d. %s\n", i + 1, chapters[saved_chapter[i]].title);
}
int choice = 0;
printf("请输入你要加载的存档编号:");
scanf("%d", &choice);
while (choice < 1 || choice > save_count) {
printf("无效的选择,请重新输入:");
scanf("%d", &choice);
}
current_chapter = saved_chapter[choice - 1];
}
void save_game() {
if (save_count >= MAX_SAVE) {
printf("存档数量已达上限,无法保存!\n");
return;
}
saved_chapter[save_count++] = current_chapter;
printf("存档成功!\n");
}
void print_help() {
printf("这是一个冒险游戏,你需要做出不同的选择,每个选择都会影响游戏的进程和结局。\n");
printf("游戏中使用数字键来选择不同的选项,每个章节最多有%d个选项。\n", MAX_OPTION);
printf("游戏中可以随时进行存档和读档,最多可以保存%d个存档。\n", MAX_SAVE);
}
void print_menu() {
printf("欢迎来到冒险游戏!\n");
printf("1.开始新游戏\n");
printf("2.读取存档\n");
printf("3.保存游戏\n");
printf("4.游戏帮助\n");
printf("5.退出游戏\n");
}
int main() {
int choice = 0;
while (choice != 5) {
print_menu();
printf("请输入你的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
current_chapter = 0;
save_count = 0;
play_game();
break;
case 2:
load_game();
break;
case 3:
save_game();
break;
case 4:
print_help();
break;
case 5:
printf("谢谢游玩!\n");
break;
default:
printf("无效的选择,请重新输入!\n");
break;
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int guess, answer, num_guesses = 0;
srand(time(NULL));
answer = rand() % 100 + 1; // 生成1-100之间的随机数
printf("猜数字游戏:\n");
printf("我想了一个1-100之间的整数,你来猜猜是多少吧!\n");
do { printf("请输入你的猜测(1-100之间):");
scanf("%d", &guess);
num_guesses++;
if (guess > answer) { printf("哎呀,猜大了!\n");
} else if (guess < answer) {
printf("哎呀,猜小了!\n");
} } while (guess != answer);
printf("恭喜你,猜对了!答案就是%d,你一共猜了%d次。\n", answer, num_guesses);
return 0;
}
这篇博客是我自己写的,您可以参考一下
文字游戏,实现了2个游戏功能,一是游戏接龙,二是诗词补全。每个游戏会统计得分,分值如果超过历史最高分,则会更新到文件。一共7个函数,代码为纯手工现写,无重复!!
接龙游戏:系统给出一个初始成语,玩家接龙,接龙错误后结束游戏。
诗词补全:系统随机出题,诗词题目从文件中读取,玩家补全诗词,在本代码中,设置为出10个题目,并统计得分(可根据需要修改代码调整题目梳理,诗词文件会在下面给你提供一个)。
运行结果:
(1)菜单界面
(2)成语接龙
(3)诗词补全(截取部分)
代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define MAXSIZE 1000
int g_scoreTotal = 0; //总得分
int g_scorePer = 10; //答对每个题目的得分
int g_maxScoreGame1 = 0; //成语接龙的最高成绩
int g_maxScoreGame2 = 0; //诗词补全游戏的最高成绩
const char* g_scoreFile = "score.txt"; //成绩文件
const char* g_shiciFile = "shici.txt"; //诗词文件,两句诗词以空格分隔
//从文件中读取历史成绩
void readFile(const char* filename)
{
FILE* fp = fopen(filename, "r");
if (fp == 0)
return;
fscanf(fp, "%d %d", &g_maxScoreGame1, &g_maxScoreGame2);
fclose(fp);
}
//将最高成绩写入文件
void writeFile(const char* filename)
{
FILE* fp = fopen(filename, "w");
fprintf(fp, "%d %d", g_maxScoreGame1, g_maxScoreGame2);
fclose(fp);
}
//文字游戏菜单
void menu()
{
printf("*****************************\n");
printf("* 欢迎来到文字游戏世界 *\n");
printf("* 1.成语接龙 *\n");
printf("* 2.诗词补全 *\n");
printf("* 3.退出 *\n");
printf("*****************************\n");
printf("请选择:");
}
//成语接龙游戏入口
void ChengYuJielong()
{
char cur[40] = { 0 }; //当前成语
char pre[40] = { 0 }; //上一次成语
int len;
const char* all[] = {"语重心长","举一反三","胸有成竹","心安理得","积劳成疾","花好月圆","五湖四海"};
int index = rand() % 7; //all中只有7个,从其中选取一个作为开头
system("cls");
g_scoreTotal = 0;
strcpy(pre, all[index]);
printf("%s\n", pre);
len = strlen(pre);
//printf("len = %d\n", strlen(pre));
while (1)
{
scanf("%s", cur);
if (cur[0] == pre[len - 2] && cur[1] == pre[len - 1])
{
g_scoreTotal += g_scorePer;
strcpy(pre, cur);
}
else
{
printf("错误!!\n");
break;
}
}
printf("\n总得分:%d\n", g_scoreTotal);
printf("历史最高得分:%d\n", g_maxScoreGame1);
if (g_scoreTotal > g_maxScoreGame1)
{
printf("恭喜你,创造了新的记录!!!\n");
//写入文件
writeFile(g_scoreFile);
}
else if (g_scoreTotal == g_maxScoreGame1)
printf("恭喜你,追平了历史最高记录!!!\n");
else
printf("很遗憾,未能超过最高分,请再接再厉!\n");
}
//从文件中读取诗词
int getInitShici(const char* filename, char pre[][40], char bck[][40])
{
FILE* fp = fopen(filename, "r");
int n = 0;
system("cls");
if (fp == 0)
{
printf("诗词文件读取失败,请检查诗词文件是否存在,或者路径是否正确!\n");
return 0;
}
//开始读取
while (!feof(fp))
{
if (fscanf(fp, "%s %s", pre[n], bck[n])==2)
n++;
}
fclose(fp);
//printf("共读取诗词%d条\n", n);
return n;
}
//诗词补全
void ShiCiBuQuan()
{
//初始诗词
char pre[MAXSIZE][40] = { 0 }, bck[MAXSIZE][40] = { 0 };
char ans[40] = { 0 };
int n = 0;
int i = 0, index;
int k;
n = getInitShici(g_shiciFile, pre, bck);
k = n > 10 ? 10 : n;
g_scoreTotal = 0;
for (i = 0; i < k; i++)
{
index = rand() % n; //随机选取一个题目
printf("%s ", pre[index]);
scanf("%s", ans);
if (strcmp(ans, bck[index]) == 0)
{
g_scoreTotal += g_scorePer;
printf("正确\n");
}
else
printf("错误\n");
}
printf("\n总得分:%d\n", g_scoreTotal);
printf("历史最高得分:%d\n", g_maxScoreGame2);
if (g_scoreTotal > g_maxScoreGame2)
{
printf("恭喜你,创造了新的记录!!!\n");
//写入文件
writeFile(g_scoreFile);
}
else if (g_scoreTotal == g_maxScoreGame2)
printf("恭喜你,追平了历史最高记录!!!\n");
else
printf("很遗憾,未能超过最高分,请再接再厉!\n");
}
int main()
{
int op;
srand((unsigned int)time(0)); //生成随机数种子
readFile(g_scoreFile); //读取最高历史成绩
while (1)
{
system("cls");
menu();
scanf("%d", &op);
if (op == 3)
break;
else if (op == 1)
ChengYuJielong();
else if (op == 2)
ShiCiBuQuan();
system("pause");
}
return 0;
}
诗词文件(shici.txt),注意,txt文件需要用ANSI格式保存,如下:
shici.txt文件内容:
白日依山尽 黄河入海流
举杯邀明月 对影成三人
会当凌绝顶 一览众山小
洛阳亲友如相问 一片冰心在玉壶
老骥伏枥 志在千里
愿得一人心 白首不相离
少壮不努力 老大徒伤悲
采菊东篱下 悠然见南山
盛年不重来 一日难再晨
举头望明月 低头思故乡
故人西辞黄鹤楼 烟花三月下扬州
天生我材必有用 千金散尽还复来
桃花潭水深千尺 不及汪伦送我情
常见思路:
1,从前往后扫描,见到0元素先记数,再将后续一非0元素,前移并继续扫描,全部扫完后再把后续部分清0``
2,从前往后扫描,见到0元素则与尾部非0元素互换
前期准备
#include<stdio.h>
#include<malloc.h>
typedef struct Lnode {
int date;
struct Lnode *next;
}Londe,*Linklist;//create a single list
//尾插法建立链表
Linklist CreateList(int n)
{
Linklist Head;//头指针
Londe *p, *r;//建立链表的指针
Head = NULL, r = NULL;
if (n < 0)
{
return 0;
}
for (int i = 0; i < n+1; i++)
{
if (Head == NULL)
{
p = (Linklist)malloc(sizeof(Linklist));
p->date = NULL;//建立头节点
r = p;
Head = p;
}
else {
p = (Linklist)malloc(sizeof(Linklist));
scanf("%d", p->date);
r->next = p;
r = p;
}
}
if (r != NULL)
r->next = NULL;
return Head;
}
void list_sort(Linklist L)
{
int count = 0;
Londe *p;
L = L->next;//移动到首元节点
p = L;
while (L->next != NULL)
{
if (L->date != 0) {
p->date = L->date;
p = p->next;
}
else count++;
L = L->next;
}
for (; count > 0; count--)
{
p->date = 0;
p = p->next;
}
}
void print(Linklist L)
{
L = L->next;
while (L != NULL)
{
printf("%d ", L->date);
L = L->next;
}
}
int main()
{
Londe *L = CreateList(5);
print(L);
list_sort(L);
printf("\n");
print(L);
}
欢迎各位大佬的指正与优化
中南大学人工智能系19级学生学习笔记
来源CatGPT:
下面是一个简单的示例游戏,其中玩家需要猜一个指定的数字:
这段代码提供了游戏的玩法和逻辑。玩家需要输入猜测的数字,程序会将其与预设的目标数字进行比较。如果猜测数字低于目标数字,程序会提示玩家太低了;如果猜测数字高于目标数字,程序会提示玩家太高了。如果玩家猜中了目标数字,则游戏结束。
#include <stdio.h>
int main() {
int target = 10;
int guess;
printf("Guess a number between 1 and 20\n");
do {
printf("Enter your guess: ");
scanf("%d", &guess);
if(guess < target) {
printf("Too low, try again.\n");
} else if(guess > target) {
printf("Too high, try again.\n");
} else {
printf("You guessed it, congratulations!\n");
}
} while(guess != target);
return 0;
}
游戏一:迷宫寻宝纯C游戏
我可以为你设计一个简单的纯C语言文字游戏。这个游戏的基本思想是“文本冒险游戏”,游戏中的地点、物品和事件全部以文字形式展现。
这个游戏叫做“迷宫寻宝”。
首先,我们需要几个基本的函数:startGame(), playGame(), move(), getTreasure(), saveGame(), loadGame()
以下是代码的一部分:
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAZE_SIZE 5
#define FILE_NAME "game_save.txt"
typedef struct {
int x;
int y;
} Player;
typedef struct {
int x;
int y;
int treasure;
} Room;
Player player;
Room maze[MAZE_SIZE][MAZE_SIZE];
void startGame() {
// initialize game
player.x = 0;
player.y = 0;
for (int i = 0; i < MAZE_SIZE; i++) {
for (int j = 0; j < MAZE_SIZE; j++) {
maze[i][j].x = i;
maze[i][j].y = j;
maze[i][j].treasure = rand() % 2; // randomly place treasures
}
}
printf("游戏开始!你已进入迷宫,你需要找到并收集所有的宝藏。\n");
}
void playGame() {
// main game loop
char command[10];
while (1) {
printf("输入命令: ");
scanf("%s", command);
if (strcmp(command, "move") == 0) {
move();
} else if (strcmp(command, "get") == 0) {
getTreasure();
} else if (strcmp(command, "save") == 0) {
saveGame();
} else if (strcmp(command, "load") == 0) {
loadGame();
} else if (strcmp(command, "quit") == 0) {
break;
} else {
printf("无效命令!\n");
}
}
}
void move() {
// handle player movement
char direction[10];
printf("输入方向 (north, south, east, west): ");
scanf("%s", direction);
if (strcmp(direction, "north") == 0 && player.y > 0) {
player.y--;
} else if (strcmp(direction, "south") == 0 && player.y < MAZE_SIZE - 1) {
player.y++;
} else if (strcmp(direction, "west") == 0 && player.x > 0) {
player.x--;
} else if (strcmp(direction, "east") == 0 && player.x < MAZE_SIZE - 1) {
player.x++;
} else {
printf("你不能那样走!\n");
}
}
void getTreasure() {
// pick up treasure in current room
if (maze[player.x][player.y].treasure == 1) {
printf("你找到了一个宝藏!\n");
maze[player.x][player.y].treasure = 0;
} else {
printf("这个房间里没有宝藏。\n");
}
}
void saveGame(){
// save game to file
FILE *file = fopen(FILE_NAME, "w");
if (file == NULL) {
printf("无法打开文件!\n");
return;
}
// save player position
fprintf(file, "%d %d\n", player.x, player.y);
// save maze state
for (int i = 0; i < MAZE_SIZE; i++) {
for (int j = 0; j < MAZE_SIZE; j++) {
fprintf(file, "%d ", maze[i][j].treasure);
}
fprintf(file, "\n");
}
fclose(file);
printf("游戏已保存!\n");
}
void loadGame() {
// load game from file
FILE *file = fopen(FILE_NAME, "r");
if (file == NULL) {
printf("无法打开文件!\n");
return;
}
// load player position
fscanf(file, "%d %d\n", &player.x, &player.y);
// load maze state
for (int i = 0; i < MAZE_SIZE; i++) {
for (int j = 0; j < MAZE_SIZE; j++) {
fscanf(file, "%d ", &maze[i][j].treasure);
}
}
fclose(file);
printf("游戏已加载!\n");
}
int main() {
startGame();
playGame();
return 0;
}
这是一个简单的文字冒险游戏,玩家需要通过指令在迷宫中移动,找到并获取所有的宝藏。游戏有保存和加载功能,可以随时保存当前的游戏状态,也可以从之前保存的状态开始游戏。
游戏二:寻宝冒险
在这个游戏中,玩家需要在一个5x5的地图上探索,寻找到隐藏的宝藏。每一步,玩家可以选择向北,南,西或东移动。地图上也有随机分布的陷阱,如果玩家不小心走入陷阱,则游戏结束。如果找到宝藏,则玩家胜利。
游戏有以下功能:玩家可以随时查看地图,可以保存当前游戏状态,并在下次打开游戏时读取存档。
因为篇幅的限制,我将只提供主要的函数。你可以根据需要进一步扩展这个游戏。
c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 5
#define EMPTY ' '
#define PLAYER 'P'
#define TREASURE 'T'
#define TRAP 'X'
#define WALL '*'
char map[SIZE][SIZE];
int player_x, player_y;
int treasure_x, treasure_y;
void init_game() {
srand(time(NULL));
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
map[i][j] = EMPTY;
}
}
player_x = rand() % SIZE;
player_y = rand() % SIZE;
do {
treasure_x = rand() % SIZE;
treasure_y = rand() % SIZE;
} while (treasure_x == player_x && treasure_y == player_y);
map[player_x][player_y] = PLAYER;
map[treasure_x][treasure_y] = TREASURE;
}
void print_map() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%c ", map[i][j]);
}
printf("\n");
}
}
int move_player(int dx, int dy) {
int new_x = player_x + dx;
int new_y = player_y + dy;
if (new_x < 0 || new_x >= SIZE || new_y < 0 || new_y >= SIZE) {
return 0;
}
if (map[new_x][new_y] == TREASURE) {
return 2;
}
map[player_x][player_y] = EMPTY;
player_x = new_x;
player_y = new_y;
map[player_x][player_y] = PLAYER;
return 1;
}
void save_game() {
FILE* file = fopen("save.dat", "w");
if (file == NULL) {
printf("无法保存游戏\n");
return;
}
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
fprintf(file, "%c", map[i][j]);
}
fprintf(file, "\n");
}
fprintf(file, "%d %d\n", player_x, player_y);
fprintf(file, "%d %d\n", treasure_x, treasure_y);
fclose(file);
printf("游戏已保存\n");
}
int load_game() {
FILE* file = fopen("save.dat", "r");
if (file == NULL) {
printf("无法加载游戏\n");
return 0;
}
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
fscanf(file, " %c", &map[i][j]);
}
}
fscanf(file, "%d %d", &player_x, &player_y);
fscanf(file, "%d %d", &treasure_x, &treasure_y);
fclose(file);
printf("游戏已加载\n");
return 1;
}
// 主函数入口
int main() {
// init_game();
// print_map();
// 具体游戏逻辑,存档和读档逻辑略
return 0;
游戏中每个方块可以是空的(表示为 ' '),玩家(表示为 'P'),宝藏(表示为 'T'),陷阱(表示为 'X')。玩家通过键盘输入命令进行移动。注意,此示例只包含了基础的游戏引擎,具体的游戏逻辑(如玩家如何进行输入,游戏如何响应等)你需要根据你的需求自行实现。
python的可以么???有个纯手工的
这是一个猜数字小游戏。程序先随机生成一个1到100之间的数字,然后向用户问候并询问用户的名字。接着程序会输出欢迎语和游戏规则。在用户输入猜测的数字后,程序会根据用户的猜测结果告诉用户是猜大了还是猜小了,并记录猜测次数。如果用户猜中了,程序会输出猜测次数和等级,以及随机一种祝福语。如果用户没有猜中,程序会降低用户的等级,直到等级降到1为止,但用户可以一直继续猜测。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int answer, guess, count = 0, level = 10;
char name[20], greet[5][20] = {"你好", "欢迎", "嗨", "你来啦", "Hello"};
char wish[5][50] = {"祝你猜中哦!", "希望你赢哦!", "加油!", "你一定行的!", "Good luck!"};
srand(time(NULL)); // 设置随机数种子
answer = rand() % 100 + 1; // 生成1到100的随机数
printf("欢迎来到猜数字小游戏!\n");
printf("请问你叫什么名字?");
scanf("%s", name);
printf("%s,%s!\n", greet[rand()%5], name); // 随机选择一种欢迎语
printf("我想了一个1到100之间的数字,你来猜猜看吧!\n");
printf("现在你的等级为%d,猜测次数越少等级越高哦!\n", level);
do {
if (count >= 10) { // 猜测次数达到10次后,降低等级
level--;
printf("糟糕,你已经猜了10次了,你的等级降为%d了!\n", level);
}
printf("请输入你的猜测:");
scanf("%d", &guess);
count++; // 记录猜测次数
if (guess > answer) {
printf("猜大了!\n");
} else if (guess < answer) {
printf("猜小了!\n");
} else {
printf("恭喜你,猜对了!\n");
printf("你一共猜了%d次,你的等级为%d!\n", count, level); // 输出猜测次数和等级
printf("%s\n", wish[rand()%5]); // 随机选择一种祝福语
break;
}
} while(1);
return 0;
}
基于您的要求,我可以为您提供一个简单的原创文字小游戏的示例,使用纯C语言编写,并包含六个以上的函数以及存档功能。由于时间和预算的限制,这将是一个基本的文字冒险游戏。
以下是一个示例:
#include <stdio.h>
// 游戏状态
enum GameState {
MENU,
PLAYING,
GAME_OVER
};
// 存档结构体
struct SaveData {
int score;
int level;
};
// 打印游戏菜单
void printMenu() {
printf("欢迎来到文字冒险游戏!\n");
printf("1. 开始游戏\n");
printf("2. 退出游戏\n");
}
// 开始游戏
void startGame() {
printf("游戏开始!\n");
// 游戏逻辑代码
// ...
}
// 保存游戏进度到文件
void saveGame(struct SaveData data) {
FILE* file = fopen("save.txt", "w");
if (file != NULL) {
fprintf(file, "%d\n%d\n", data.score, data.level);
fclose(file);
printf("游戏已保存!\n");
} else {
printf("保存失败!\n");
}
}
// 从文件加载游戏进度
struct SaveData loadGame() {
struct SaveData data;
FILE* file = fopen("save.txt", "r");
if (file != NULL) {
fscanf(file, "%d\n%d\n", &data.score, &data.level);
fclose(file);
printf("游戏已加载!\n");
} else {
printf("加载失败!\n");
}
return data;
}
int main() {
enum GameState state = MENU;
struct SaveData save;
while (state != GAME_OVER) {
if (state == MENU) {
printMenu();
int choice;
scanf("%d", &choice);
if (choice == 1) {
state = PLAYING;
save = loadGame(); // 加载游戏进度
startGame();
// 游戏结束后保存进度
saveGame(save);
} else if (choice == 2) {
state = GAME_OVER;
}
} else if (state == PLAYING) {
// 游戏逻辑代码
// ...
state = GAME_OVER;
}
}
printf("游戏结束!\n");
return 0;
}
请注意,这只是一个简单的示例,您可以根据自己的需求进行修改和扩展。游戏逻辑部分需要您自己设计,并在代码中实现。
此示例包含一个GameState
枚举来表示游戏的不同状态,一个SaveData
结构体用于保存游戏进度,以及用于打印菜单、开始游戏、保存游戏进度和加载游戏进度的函数。
在main
函数中,使用一个while
循环来控制游戏的状态转换,根据不同的状态执行相应的操作。
存
档功能通过使用文件操作函数来实现,游戏进度可以保存到一个文本文件中,并在需要时加载。
请注意,这只是一个基本的示例,您可以根据自己的需求和创意进行修改和扩展,实现更丰富的游戏功能和逻辑。
以下答案由GPT-4.5Plus大模型(语音)与博主A努力中.......共同编写:
游戏名称:打猎
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 定义全局变量
int health = 10; // 生命值
int attack = 5; // 攻击力
int gold = 0; // 金钱
char name[20]; // 玩家名
// 函数声明
void startGame(); // 开始游戏
void saveGame(); // 存档功能
void loadGame(); // 读取存档
void hunt(); // 打猎
void buy(); // 购买
void gg(); // 游戏结束
int main() {
startGame(); // 开始新游戏
while (health > 0) {
printf("请选择:\n");
printf("1. 打猎\n2. 购买\n3. 存档\n4. 读取存档\n");
int choice;
scanf("%d", &choice);
if (choice == 1) {
hunt(); // 打猎
} else if (choice == 2) {
buy(); // 购买
} else if (choice == 3) {
saveGame();// 存档
} else if (choice == 4) {
loadGame();// 读取存档
}
}
gg(); // 游戏结束
}
// 其他函数定义
// ...
int main() {
startGame();
while (health > 0) {
printf("请选择:\n");
printf("1. 打猎\n2. 购买\n3. 存档\n4. 读取存档\n");
int choice;
scanf("%d", &choice);
if (choice == 1) {
hunt();
} else if (choice == 2) {
buy();
} else if (choice == 3) {
saveGame();
} else if (choice == 4) {
loadGame();
}
}
gg();
}
贪吃蛇
#include<windows.h>
#include<stdlib.h>
#include<fstream>
#include<stdio.h>
#include<conio.h>
#include<time.h>
using namespace std;
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
#define X 23
#define Y 40
#define MAXLEN 200
#define MINTIME 75
unsigned int snake[MAXLEN][2];
unsigned int len;
unsigned int lastt[2];
unsigned int score;
unsigned int max_score;
unsigned int way;
double wait;
int input;
unsigned int food[2];
bool empty=true;
void color(int _color){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),_color);
return;
}
void gotoxy(int xx,int yy){
COORD position={yy*2,xx};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
return;
}
void welcome(){
printf("游戏规则:\n");
printf("w,a,s,d,控制移动\n");
getch();
system("cls");
return;
}
void init(){
system("title 贪吃蛇");
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
fstream in("score.txt",ios::in);
if(!in.fail())
in>>max_score;
else
welcome();
in.close();
len=4;
snake[0][0]=X>>1;
snake[0][1]=Y>>1;
snake[1][0]=(X>>1)+1;
snake[1][1]=Y>>1;
snake[2][0]=(X>>1)+2;
snake[2][1]=Y>>1;
snake[3][0]=(X>>1)+3;
snake[3][1]=Y>>1;
way=UP;
wait=150.0;
return;
}
void drawmap(){
color(255);
for(unsigned int xx=0;xx<X;xx++){
gotoxy(xx,0);
printf(" ");
gotoxy(xx,Y-1);
printf(" ");
}
for(unsigned int yy=0;yy<Y;yy++){
gotoxy(0,yy);
printf(" ");
gotoxy(X-1,yy);
printf(" ");
}
return;
}
void drawsnake(){
color(255);
gotoxy(0,0);
printf(" ");
color(10);
gotoxy(snake[0][0],snake[0][1]);
printf("□");
gotoxy(snake[1][0],snake[1][1]);
printf("■");
return;
}
void move(){
lastt[0]=snake[len-1][0];
lastt[1]=snake[len-1][1];
for(unsigned int tc=len-1;tc>0;tc--){
snake[tc][0]=snake[tc-1][0];
snake[tc][1]=snake[tc-1][1];
}
switch(way){
case UP:{
snake[0][0]--;
break;
}
case DOWN:{
snake[0][0]++;
break;
}
case LEFT:{
snake[0][1]--;
break;
}
case RIGHT:{
snake[0][1]++;
break;
}
}
return;
}
void clt(){
color(0);
gotoxy(lastt[0],lastt[1]);
printf(" ");
return;
}
void getin(){
if(kbhit()!=0){
while(kbhit()!=0)
input=getch();
switch(input){
case 'W':case 'w':{
if(way!=DOWN)
way=UP;
break;
}
case 'S':case 's':{
if(way!=UP)
way=DOWN;
break;
}
case 'A':case 'a':{
if(way!=RIGHT)
way=LEFT;
break;
}
case 'D':case 'd':{
if(way!=LEFT)
way=RIGHT;
break;
}
}
}
return;
}
void putfood(){
if(empty){
bool flag=true;
srand(time(NULL));
while(flag){
food[0]=rand()%(X-2)+1;
food[1]=rand()%(Y-2)+1;
flag=false;
for(unsigned int tc=0;tc<len;tc++)
if(snake[tc][0]==food[0]&&snake[tc][1]==food[1])
flag=true;
}
empty=false;
color(14);
gotoxy(food[0],food[1]);
printf("◆");
}
return;
}
void eatfood(){
if(snake[0][0]==food[0]&&snake[0][1]==food[1]){
empty=true;
score++;
if(wait>=MINTIME)
wait-=0.5;
if(len<MAXLEN)
len++;
}
return;
}
void gameover(){
bool over=false;
if(snake[0][0]==0||snake[0][0]==X-1||snake[0][1]==0||snake[0][1]==Y-1)
over=true;
for(int tc=1;tc<len;tc++)
if(snake[0][0]==snake[tc][0]&&snake[0][1]==snake[tc][1])
over=true;
if(over==true){
system("cls");
while(kbhit()!=0)
getch();
printf("Game over!\n");
printf("Score:%d\n",score);
printf("Max score:%d\n",max_score);
getch();
system("cls");
printf("Save...\n");
if(score>max_score){
fstream write;
write.open("score.txt",ios::out);
write<<score;
write.close();
max_score=score;
}
exit(0);
}
return;
}
int main(){
init();
drawmap();
_sleep(3000);
while(true){
clt();
drawsnake();
putfood();
eatfood();
getin();
move();
gameover();
_sleep(int(wait));
}
return 0;
}
参考GPT
基于C语言的纯文字小游戏示例,游戏名为“流浪的勇者”:
用到的C语言知识点:文件操作、条件判断、随机数、结构体、函数调用等。
游戏规则:玩家扮演一名流浪的勇者,需要在藏宝图上找到宝藏并获取奖励,同时要避开随机出现的陷阱。如果成功找到宝藏,则通关并获得奖励;否则,游戏失败。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAP_SIZE 10 // 地图大小(正方形)
#define TREASURE_NUM 1 // 宝藏数量
#define TREASURE_SYMBOL '$' // 宝藏标志符
#define TRAP_NUM 3 // 陷阱数量
#define TRAP_SYMBOL '#' // 陷阱标志符
#define PLAYER_SYMBOL 'o' // 玩家标志符
#define SAVE_FILENAME "save.dat" // 存档文件名
// 地图结构体
struct Map {
char name[50];
char data[MAP_SIZE][MAP_SIZE];
};
// 玩家结构体
struct Player {
int x;
int y;
int is_dead;
int has_treasure;
};
// 陷阱结构体
struct Trap {
int x;
int y;
};
// 存档结构体
struct Save {
int is_saved;
struct Player player;
struct Trap traps[TRAP_NUM];
};
// 初始化地图
void init_map(struct Map* map) {
int i, j;
for (i = 0; i < MAP_SIZE; i++) {
for (j = 0; j < MAP_SIZE; j++) {
map->data[i][j] = '-';
}
}
}
// 输出地图
void print_map(struct Map* map, struct Player* player) {
int i, j;
printf(" ");
for (i = 0; i < MAP_SIZE; i++) {
printf("%d ", i);
}
printf("\n");
for (i = 0; i < MAP_SIZE; i++) {
printf("%d ", i);
for (j = 0; j < MAP_SIZE; j++) {
if (i == player->y && j == player->x) {
printf("%c ", PLAYER_SYMBOL);
} else {
printf("%c ", map->data[i][j]);
}
}
printf("\n");
}
}
// 随机生成位置
int random_pos() {
return rand() % MAP_SIZE;
}
// 随机放置宝藏和陷阱
void put_treasures_and_traps(struct Map* map, struct Player* player, struct Trap* traps) {
int i, x, y;
for (i = 0; i < TREASURE_NUM; i++) {
x = random_pos();
y = random_pos();
if (map->data[y][x] == TREASURE_SYMBOL || (player->x == x && player->y == y)) {
i--;
} else {
map->data[y][x] = TREASURE_SYMBOL;
}
}
for (i = 0; i < TRAP_NUM; i++) {
x = random_pos();
y = random_pos();
if (map->data[y][x] == TREASURE_SYMBOL || map->data[y][x] == TRAP_SYMBOL || (player->x == x && player->y == y)) {
i--;
} else {
map->data[y][x] = TRAP_SYMBOL;
traps[i].x = x;
traps[i].y = y;
}
}
}
// 移动玩家
void move_player(struct Player* player, int dx, int dy, struct Map* map, struct Trap* traps) {
int face_traps = 0;
int i;
// 判断是否到达地图边缘
if (player->x + dx < 0 || player->x + dx >= MAP_SIZE || player->y + dy < 0 || player->y + dy >= MAP_SIZE) {
printf("你撞墙了,请换个方向!\n");
return;
}
// 判断是否被陷阱覆盖
for (i = 0; i < TRAP_NUM; i++) {
if (player->x + dx == traps[i].x && player->y + dy == traps[i].y) {
face_traps = 1;
break;
}
}
// 判断移动是否合法
if (map->data[player->y + dy][player->x + dx] == TRAP_SYMBOL && face_traps) {
player->is_dead = 1;
printf("你中了陷阱,游戏失败!\n");
} else if (map->data[player->y + dy][player->x + dx] == TREASURE_SYMBOL) {
player->has_treasure = 1;
map->data[player->y + dy][player->x + dx] = '-';
printf("你找到了宝藏!\n");
} else {
player->x += dx;
player->y += dy;
}
}
// 检查是否获胜
int check_win(struct Player* player) {
return player->has_treasure;
}
// 存档
void save_game(struct Save* save, struct Player* player, struct Trap* traps) {
FILE* fp = fopen(SAVE_FILENAME, "wb");
save->is_saved = 1;
save->player = *player;
int i;
for (i = 0; i < TRAP_NUM; i++) {
save->traps[i] = traps[i];
}
fwrite(save, sizeof(struct Save), 1, fp);
fclose(fp);
printf("游戏已存档!\n");
}
// 读档
void load_game(struct Save* save, struct Player* player, struct Trap* traps, struct Map* map) {
FILE* fp = fopen(SAVE_FILENAME, "rb");
if (fp == NULL) {
printf("读档失败,未找到存档文件!\n");
return;
}
fread(save, sizeof(struct Save), 1, fp);
*player = save->player;
int i;
for (i = 0; i < TRAP_NUM; i++) {
traps[i] = save->traps[i];
}
printf("读档成功!\n");
fclose(fp);
}
int main() {
srand(time(NULL));
struct Map map;
struct Player player = {0, 0, 0, 0};
struct Trap traps[TRAP_NUM];
struct Save save = {0};
int choice;
init_map(&map);
put_treasures_and_traps(&map, &player, traps);
printf("欢迎来到流浪的勇者!\n");
while (1) {
printf("请选择操作:\n1. 查看地图\n2. 移动\n3. 存档\n4. 读档\n5. 退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
print_map(&map, &player);
break;
case 2:
if (player.is_dead) {
printf("您已死亡,游戏结束!\n");
return 0;
}
printf("请输入移动方向(上:w,下:s,左:a,右:d):\n");
char dir;
scanf(" %c", &dir);
switch (dir) {
case 'w':
move_player(&player, 0, -1, &map, traps);
break;
case 's':
move_player(&player, 0, 1, &map, traps);
break;
case 'a':
move_player(&player, -1, 0, &map, traps);
break;
case 'd':
move_player(&player, 1, 0, &map, traps);
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
if (check_win(&player)) {
printf("你找到了宝藏,恭喜通关!\n");
return 0;
}
if (player.is_dead) {
return 0;
}
break;
case 3:
save_game(&save, &player, traps);
break;
case 4:
load_game(&save, &player, traps, &map);
break;
case 5:
printf("游戏结束!\n");
return 0;
default:
printf("输入错误,请重新输入!\n");
break;
}
}
}
温馨提示:由于本代码未考虑部分异常情况,如玩家输入无效命令等,故在使用时需注意输入的正确性,随时注意游戏进度,切勿尝试操作不当,以免影响游戏体验。