使用C语言编写简单小游戏

合集下载

C语言实现简单扫雷小游戏

C语言实现简单扫雷小游戏

C语⾔实现简单扫雷⼩游戏本⽂实例为⼤家分享了C语⾔实现扫雷⼩游戏的具体代码,供⼤家参考,具体内容如下#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <windows.h>#include <time.h>/*⽤ C 语⾔写⼀个简单的扫雷游戏*/// 1.写⼀个游戏菜单 Menu()// 2.开始游戏// 1.初始化⼆维数组 Init_Interface()// 2.打印游戏界⾯ Print_Interface()// 3.玩家掀起指定位置 Play() --> 指定输⼊坐标(判断合法性)// 1.判断该位置是否是雷// 2.判断是否掀掉所有空地// 3.如果掀开的是空地,则判断该空地周围是否有雷// 1.如果周围有雷,则统计周围雷的个数// 2.如果周围没有雷,则掀开周围除了雷的所有空地,并且统计所掀开空地周围雷的个数// 4.更新地图// 5.继续 3 的循环//定义全局变量://定义扫雷地图的长和宽#define MAX_ROW 9#define MAX_COL 9//定义默认的雷数#define DEFAULT_MINE 9//定义两个⼆维数组,分别存放初始地图和雷阵char show_map[MAX_ROW + 2][MAX_COL + 2];char mine_map[MAX_ROW + 2][MAX_COL + 2];//写⼀个游戏菜单int Menu() {printf("=========\n");printf("1.开始游戏\n");printf("0.结束游戏\n");printf("=========\n");printf("请选择游戏菜单选项:");int choice = 0;while (1) {scanf("%d", &choice);if (choice != 0 && choice != 1) {printf("您的输⼊有误, 请重新输⼊\n");continue;}break;}return choice;}//开始游戏//初始化数组void Init_Interface() {for (int row = 0; row < MAX_ROW + 2; row++) {for (int col = 0; col < MAX_COL + 2; col++) {show_map[row][col] = '*';}}for (int row = 0; row < MAX_ROW + 2; row++) {for (int col = 0; col < MAX_COL + 2; col++) {mine_map[row][col] = '0';}}int mine_count = DEFAULT_MINE;while (mine_count > 0) {int row = rand() % MAX_ROW + 1;int col = rand() % MAX_COL + 1;if (mine_map[row][col] == '1') { //将雷设置为 1//此处已经有雷continue;}mine_count--;mine_map[row][col] = '1';}}//打印初始界⾯void Print_Interface(char map[MAX_ROW + 2][MAX_COL + 2]) {printf(" ");for (int col = 1; col <= MAX_COL; col++) {printf("%d ", col);}printf("\n ");for (int col = 1; col <= MAX_COL; col++) {printf("--");}printf("\n");for (int row = 1; row <= MAX_ROW ; row++) {printf("%02d |", row);for (int col = 1; col <= MAX_COL; col++) {printf("%c ", map[row][col]);}printf("\n");}}//写⼀个统计周围雷数个数的函数int Around_Mine_count(int row, int col) {return (mine_map[row - 1][col - 1] - '0'+ mine_map[row - 1][col] - '0'+ mine_map[row - 1][col + 1] - '0'+ mine_map[row][col - 1] - '0'+ mine_map[row][col + 1] - '0'+ mine_map[row + 1][col - 1] - '0'+ mine_map[row + 1][col] - '0'+ mine_map[row + 1][col + 1] - '0');}//写⼀个判断该位置周围是否有雷的函数int No_Mine(int row, int col) {if (Around_Mine_count(row, col) == 0) {return 1;}return 0;}//写⼀个掀开该位置周围空地的函数void Open_Blank(int row, int col) {show_map[row - 1][col - 1] = '0' + Around_Mine_count(row - 1, col - 1); show_map[row - 1][col] = '0' + Around_Mine_count(row - 1, col);show_map[row - 1][col + 1] = '0' + Around_Mine_count(row - 1, col + 1); show_map[row][col - 1] = '0' + Around_Mine_count(row, col - 1);show_map[row][col + 1] = '0' + Around_Mine_count(row, col + 1);show_map[row + 1][col - 1] = '0' + Around_Mine_count(row + 1, col - 1); show_map[row + 1][col] = '0' + Around_Mine_count(row + 1, col);show_map[row + 1][col + 1] = '0' + Around_Mine_count(row + 1, col + 1); }//写⼀个判断游戏结束的函数int Success_Sweep(char show_map[MAX_ROW + 2][MAX_COL + 2]) { int count = 0;for (int row = 1; row <= MAX_ROW; row++) {for (int col = 1; col <= MAX_COL; col++) {if (show_map[row][col] == '*') {count++;}}}if (count == DEFAULT_MINE) {return 1;}return 0;}//开始游戏void StartGame() {while (1) {printf("请输⼊您要掀开的坐标:");int row = 0;int col = 0;while (1) {scanf("%d %d", &row, &col);if (row < 1 || row > MAX_ROW || col < 1 || col > MAX_COL) {printf("您的输⼊有误,请重新输⼊!\n");continue;}if (show_map[row][col] != '*') {printf("该位置已被掀开,请重新选择\n");continue;}break;}//判断该地⽅是否有雷if (mine_map[row][col] == '1') {Print_Interface(mine_map);printf("该地⽅有雷,游戏结束\n");break;}if (No_Mine(row, col)) {show_map[row][col] = '0';Open_Blank(row, col);}show_map[row][col] = '0' + Around_Mine_count(row, col);//判断是否掀开所有空地if (Success_Sweep(show_map) == 1) {Print_Interface(mine_map);printf("您已成功扫雷\n");break;}system("cls");//更新地图Print_Interface(show_map);}}int main() {if (Menu() == 0) {exit(0);}srand((unsigned int)time(NULL));Init_Interface();Print_Interface(show_map);StartGame();system("pause");return 0;}效果图:数字代表周围雷的个数以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

C语言简易骰子游戏代码

C语言简易骰子游戏代码
f-=a;g+=a;
printf("\n\n您的积分为:%d 电脑的积分为:%d\n",f,g);
}
}
}
else
printf("\n对不起!\n您下注的分数不能超过您的积分!\n请重新下注!\n");
if(f<=10)
{
if(b==0)
break;
switch(b)
{
case 1 :printf("\n我买大\n");break;
case 2 :printf("\n我买小\n");break;
case 3 :printf("\n我买豹子\n");break;
default :printf("\n请选择0~3之间的数\n");
}
if(b>0&&b<=3)
{
srand((unsigned)time(NULL));
printf("\n电脑出:");
for(i=0;i<3;i++)
{
t[i]=rand()%6+1;
printf("\n\n您的积分为:%d 电脑的积分为:%d\n",f,g);
}
}
if(b==1&&c<10||b==2&&c>=10||b==3&&(t[0]!=t[1]||t[0]!=t[2]))
{
e++;
printf("\n您总共赢了%d局!输了%d局!",d,e);

2048小游戏C语言编程设计

2048小游戏C语言编程设计
for (b = i + 1; b < 4; b++) {
if (*(p + b) != 0) if (*(p + i) == *(p + b)) { score = score + (*(p + i)) + (*(p + b)); *(p + i) = *(p + i) + *(p + b); if (*(p + i) == 2048) gamew = 1; *(p + b) = 0; i = b + i; ++ifappear; break; } else { i = b; break; }
++ifappear; e++; } } } if (ifappear != 0) ++move; break; case 'd':
case 'D': case 77:
ifappear = 0; for (j = 0; j < 4; j++) {
for (i = 0; i < 4; i++) {
b[i] = num[j][i]; num[j][i] = 0; } add(b); e = 3; for (g = 3; g >=0; g--) { if (b[g] != 0) {
void menu(); system("cls"); printf("\t\t*****************************************\t\t\n"); printf("\t\t*****************************************\n"); printf("\t\t******************游戏规则***************\n"); printf("\t\t*****************************************\n"); printf("\t\t*****************************************\t\t\n"); printf("玩家可以选择上、下、左、右或 W、A、S、D 去移动滑块\n"); printf("玩家选择的方向上若有相同的数字则合并\n"); printf("合并所得的所有新生成数字相加即为该步的有效得分\n"); printf("玩家选择的方向行或列前方有空格则出现位移\n"); printf("每移动一步,空位随机出现一个 2 或 4\n"); printf("棋盘被数字填满,无法进行有效移动,判负,游戏结束\n"); printf("棋盘上出现 2048,获胜,游戏结束\n"); printf("按上下左右去移动滑块\n"); printf("请按任意键返回主菜单...\n"); getch(); system("cls"); main(); } void gamefaile() { int i, j; system("cls"); printf("\t\t*****************************************\t\t\n"); printf("\t\t*****************************************\n"); printf("\t\t******************you fail***************\n"); printf("\t\t*****************************************\n"); printf("\t\t*****************************************\t\t\n"); printf("\t\t\t---------------------\n\t\t\t"); for (j = 0; j<4; j++) {

C语言编写文字冒险游戏

C语言编写文字冒险游戏

本文将给出一个使用C语言编写的简单的文本冒险游戏的示例。

这个游戏的玩法是玩家在不同的房间中走动,并在每个房间中寻找物品。

在每个房间中,玩家可以输入命令来查看当前房间的情况、捡起物品或移动到其他房间。

首先,我们需要定义几个结构体来表示房间、物品和玩家。

struct Room {char* description;struct Room* north;struct Room* south;struct Room* east;struct Room* west;struct Item* items;};struct Item {char* description;struct Item* next;};struct Player {struct Room* current_room;struct Item* inventory;};然后我们需要定义一些函数来创建房间、物品和玩家,以及处理玩家的命令。

struct Room* create_room(char* description) {struct Room* room = malloc(sizeof(struct Room));room->description = description;room->north = NULL;room->south = NULL;room->east = NULL;room->west = NULL;room->items = NULL;return room;}struct Item* create_item(char* description) {struct Item* item = malloc(sizeof(struct Item));item->description = description;item->next = NULL;return item;}struct Player* create_player(struct Room* starting_room) {struct Player* player = malloc(sizeof(struct Player));player->current_room = starting_room;player->inventory = NULL;return player;}void free_room(struct Room* room) {// 释放房间中的物品struct Itemvoid free_room(struct Room* room) {// 释放房间中的物品struct Item* item = room->items;while (item != NULL) {struct Item* next = item->next;free(item);item = next;}// 释放房间本身free(room);}void free_item(struct Item* item) {free(item);}void free_player(struct Player* player) {// 释放玩家的物品struct Item* item = player->inventory;while (item != NULL) {struct Item* next = item->next;free(item);item = next;}// 释放玩家本身free(player);}void print_room(struct Room* room) {printf("%s\n", room->description);printf("There is a door to the north, south, east, and west.\n"); printf("There is also the following items here:\n");struct Item* item = room->items;while (item != NULL) {printf("- %s\n", item->description);item = item->next;}}void print_inventory(struct Player* player) {printf("You have the following items:\n");struct Item* item = player->inventory;while (item != NULL) {printf("- %s\n", item->description);item = item->next;}}void execute_command(struct Player* player, char* command) { if (strcmp(command, "north") == 0) {if (player->current_room->north != NULL) {player->current_room = player->current_room->north;printf("You go north.\n");} else {printf("There is no door to the north.\n");}} else if (strcmp(command, "south") == 0) {if (player->current_room->south != NULL) {player->current_room = player->current_room->south;printf("You go south.\n");} else {printf("There is no door to the south.\n");}} else if (strcmp(command, "east") == 0) {if (player->current_room->east != NULL) {player->current_room = player->current_room->east;printf("You go east.\n");} else {printf("There is no door to the east.\n");}} else if (strcmp(command, "west") == 0) {if (player->current_room->west != NULL) {player->current_room = player->current_room->west;printf("You go west.\n");}else if (strcmp(command, "west") == 0) {if (player->current_room->west != NULL) {player->current_room = player->current_room->west;printf("You go west.\n");} else {printf("There is no door to the west.\n");}} else if (strcmp(command, "look") == 0) {print_room(player->current_room);} else if (strcmp(command, "inventory") == 0) {print_inventory(player);} else if (strncmp(command, "pickup ", 7) == 0) {// 检查玩家是否正在尝试捡起房间中的物品char* item_name = command + 7;struct Item* item = player->current_room->items;while (item != NULL && strcmp(item->description, item_name) != 0) { item = item->next;}if (item == NULL) {printf("There is no item with that name in the room.\n");} else {// 从房间中删除物品if (player->current_room->items == item) {player->current_room->items = item->next;} else {struct Item* previous = player->current_room->items;while (previous->next != item) {previous = previous->next;}previous->next = item->next;}// 将物品添加到玩家的物品列表中item->next = player->inventory;player->inventory = item;printf("You pick up the %s.\n", item->description);}} else {printf("I don't understand that command.\n");}}else if (strcmp(command, "west") == 0) {if (player->current_room->west != NULL) {player->current_room = player->current_room->west;printf("You go west.\n");} else {printf("There is no door to the west.\n");}} else if (strcmp(command, "look") == 0) {print_room(player->current_room);} else if (strcmp(command, "inventory") == 0) {print_inventory(player);} else if (strncmp(command, "pickup ", 7) == 0) {// 检查玩家是否正在尝试捡起房间中的物品char* item_name = command + 7;struct Item* item = player->current_room->items;while (item != NULL && strcmp(item->description, item_name) != 0) {item = item->next;}if (item == NULL) {printf("There is no item with that name in the room.\n");} else {// 从房间中删除物品if (player->current_room->items == item) {player->current_room->items = item->next;} else {struct Item* previous = player->current_room->items;while (previous->next != item) {previous = previous->next;}previous->next = item->next;}// 将物品添加到玩家的物品列表中item->next = player->inventory;player->inventory = item;printf("You pick up the %s.\n", item->description);}} else {printf("I don't understand that command.\n");}}最后,我们可以编写一个main函数来创建房间、物品和玩家,并进入游戏循环,在每一次迭代中读取玩家输入并执行命令。

c语言课程设计 综合型小游戏

c语言课程设计 综合型小游戏

#include<stdio.h>#include<stdlib.h>#include<time.h>int money1=10000,money2=10000,money=10000;int main(){void game1(int put);void game2(int put);int put,game,i;printf("单人模式请输入1,双人模式请输入2.\n");scanf("%d",&put);if(put==1)printf("你的本钱有一万元,你的任务是翻一倍,达到两万元则游戏胜利\n");if(put==2)printf("最后金钱多者为胜者\n");system("pause");system("cls");for(i=0;i<=1000;i++){printf("请选择游戏:1.思维风暴2.猜数字3.退出\n");scanf("%d",&game);if(game==1){game1(put);}if(game==2){game2(put);}if(game==3){break;}}if(put==1){if(money>=20000)printf("恭喜你通关了\n");if(money>=10000&&money<20000)printf("很遗憾未能通关,不过至少没亏本了\n");}if(put==2){if(money1>money2)printf("恭喜玩家一,你实在太强势了\n");if(money1<money2)printf("恭喜玩家二,简直是虐菜啊\n");if(money1==money2)printf("二位简直势均力敌啊,真是好基友\n");}system("pause");}void game1(int put){int JudgeA(int a[4],int b[4]),JudgeB(int a[4],int b[4]);int a[4],b[4];int c,i,j,m,n,k,l,under,under1,under2;printf("游戏规则:系统将随机产生一个四位不重复数字,你输入猜想的数字后\n");printf("系统将判断你猜对的数字个数和正确位置数,系统将以-A-B的形式提示,其中A 前面的数字表示位置正确的数的个数");printf("而B前的数字表示数字正确而位置不对的数的个数,如正确答案为5234,而猜的人猜5346,则是1A2B.\n **记住你只有八次机会**\n");system("pause");system("cls");if(put==1){for(l=0;l<100;l++){printf("请压底,最高为五千\n");for(m=0;m<=20;m++){scanf("%d",&under);if(under>5000||under<=0){printf("超过上限,请重新输入\n");continue;}elsebreak;}printf("请输入四位数\n");srand(time(NULL));do{a[0]=rand()%10;//产生首位随机数,对10取模得0~9的数字}while(a[0]==0);//若首位为零则重新选择for(i = 1;i < 4; i++){do{a[i]=rand()%10;//产生其它几位随机数for(j = 0; j < i; j++){if(a[i]==a[j])//若与前几位相同则跳出,重置a[i]{k=0;break;}elsek=1;//若不同,则该位有效,置标记k为1}}while(k!=1);}k=a[0];for(i=1;i<4;i++){k=k*10+a[i];}for(n=0;n<=8;n++){if(n==8){printf("you are lost,the number is %d\n",k);money=money-under*2;break;}scanf("%d",&b[0]);b[3]=b[0]%10;b[2]=(b[0]%100-b[3])/10;b[1]=b[0]%1000/100;b[0]=b[0]/1000;printf("%dA%dB\n",JudgeA(a,b),JudgeB(a,b));if(JudgeA(a,b)==4){printf("you win\n");printf("the number is %d\n",k);money=money+under*2;break;}elsecontinue;}printf("your money:%d\n重玩请输入1,返回请输入2\n",money); scanf("%d",&c);if(c==1)continue;if(c==2)break;}}if(put==2){printf("请play1压底,最高为五千\n");scanf("%d",&under1);printf("请play2压底\n");scanf("%d",&under2);for(m=0;m<=10;m++){if(under1>5000||under2>5000){printf("超过上限,请重新输入\n");continue;}elsebreak;}printf("play1's turn\n");printf("请输入四位数\n");srand(time(NULL));do{a[0]=rand()%10;}while(a[0]==0);for(i = 1;i < 4; i++){do{a[i]=rand()%10;for(j = 0; j < i; j++){if(a[i]==a[j]){k=0;break;}elsek=1;}}while(k!=1);}k=a[0];for(i=1;i<4;i++){k=k*10+a[i];}for(n=0;n<=8;n++){if(n==8){printf("you are lost,the number is %d\n",k);money1=money1-under1*2;break;}scanf("%d",&b[0]);b[3]=b[0]%10;b[2]=(b[0]%100-b[3])/10;b[1]=b[0]%1000/100;b[0]=b[0]/1000;printf("%dA%dB\n",JudgeA(a,b),JudgeB(a,b));if(JudgeA(a,b)==4){printf("you win\n");printf("the number is %d\n",k);money1=money1+under1*2;break;}elsecontinue;}printf("play1's money:%d\n",money1);system("pause");printf("play2's turn\n");printf("请输入四位数\n");srand(time(NULL));do{a[0]=rand()%10;}while(a[0]==0);for(i = 1;i < 4; i++){do{a[i]=rand()%10;for(j = 0; j < i; j++){if(a[i]==a[j]){k=0;break;}elsek=1;}}while(k!=1);}k=a[0];for(i=1;i<4;i++){k=k*10+a[i];}for(n=0;n<=8;n++){if(n==8){printf("you are lost,the number is %d\n",k);money2=money2-under2*2;break;}scanf("%d",&b[0]);b[3]=b[0]%10;b[2]=(b[0]%100-b[3])/10;b[1]=b[0]%1000/100;b[0]=b[0]/1000;printf("%dA%dB\n",JudgeA(a,b),JudgeB(a,b));if(JudgeA(a,b)==4){printf("you win\n");printf("the number is %d\n",k);money2=money2+under2*2;break;}elsecontinue;}printf("play2's money:%d\n",money2);}}int JudgeA(int a[4],int b[4]){int i,result1=0;for(i=0;i<4;i++){if(b[i]==a[i]) result1++;}return result1;}int JudgeB(int a[4],int b[4]){int i,j,result=0;for(i=0;i<4;i++){for(j=0;j<4;j++){if(a[i]==b[j]&&i!=j)result++;}}return result;}void game2(int put){int i,j,k,l,a,num,down,down1,down2,random;int nu[6];int *p;p=nu;system("pause");system("cls");printf("游戏规则:单人模式为猜数,双人模式为比大小\n");if(put==1){for(i=0;i<=100;i++){for(k=0;k<=100;k++){printf("请下注,最高为500\n");scanf("%d",&down);if(down>0&&down<=500)break;else{printf("超过上线,请重新下注\n");continue;}}printf("请输入所猜数\n");for(j=0;j<100;j++){scanf("%d",&num);if(num>0&&num<=6)break;else{printf("错误,请重新输入\n");continue;}}for(l=0;l<100;l++){srand((unsigned)(time(NULL)));random = rand()%6+1;if(random>0&&random<=6)break;}printf("正确数为%d,继续玩请输入1,返回菜单输入2\n",random);if(num==random){printf("***********************YOUWIN************************\n");money=money+down*2;}else{printf("***********************YOULOST***********************\n");money=money-down*2;}printf("你的金钱为%d\n",money);scanf("%d",&a);if(a==1)continue;if(a==2)break;}}if(put==2){for(i=0;i<100;i++){printf("游戏规则:玩家分别得到三次随机数字,总和大者胜利\n");printf("请下注,最高为一千\n");scanf("%d",&down);system("pause");for(j=1;j<=6;j++){if(j%2==1){srand((unsigned)(time(NULL)));p[j-1] = rand()%6+1;printf("玩家一第%d次得数为%d\n",j/2+1,p[j-1]);}else{srand((unsigned)(time(NULL)));p[j-1] = rand()%6+1;printf("玩家二第%d次得数为%d\n",j/2,p[j-1]);}system("pause");}printf("玩家一总得数为%d\n玩家二总得数为%d\n",p[0]+p[2]+p[4],p[1]+p[3]+p[5]);if(p[0]+p[2]+p[4]>p[1]+p[3]+p[5]){printf("玩家一获胜\n");money1=money1+down*2;money2=money2-down*2;}if(p[0]+p[2]+p[4]<p[1]+p[3]+p[5]){printf("玩家二获胜\n");money1=money1-down*2;money2=money2+down*2;}if(p[0]+p[2]+p[4]==p[1]+p[3]+p[5]){printf("恭喜\n");money1=money1+down*2;money2=money2+down*2;}printf("玩家一金钱为%d\n玩家二金钱为%d\n重玩输入1,返回输入2\n",money1,money2);scanf("%d",&a);if(a==1)continue;if(a==2)break;}}}。

c语言简单小游戏(模拟魔塔)

c语言简单小游戏(模拟魔塔)
printf("请按任意键开始游戏"); char s; s=getch(); system("cls"); printf("很久很久以前,面对魔种入侵,一位名叫%s的英雄出现了\n",me->name);//开场剧情设置 Sleep(2000); printf("他承载着实现国家爱与和平的使命,从此踏上了征途\n"); Sleep(2000); printf("故事从此开始了.........\n"); printf("(按任意键继续)"); getch(); system("cls"); printf("欢迎来到中秋特别版,本版本福利多多哦\n"); printf("(按任意键继续)"); getch(); system("cls"); printf("是否膜拜制作人豪哥?膜拜有奖哦\n"); printf("1.膜拜 2.不膜拜 3.仍一个粑粑给制作人"); char ac; ac=getch(); switch(ac) { case '1':
int map[35][66];//定义地图数组 int batt=1;//关卡数 int cs=1;//层数(第三关开始有) int is=0;//判断是否初始化地图 int money=0;//将打怪获得的金钱放在一个整形变量中 int ak=0; int ac1=0; int flag=0; struct holysword {
2.操作说明**********************************************************************

C语言编程游戏代码

C语言编程游戏代码

#include <stdio.h>#include <stdlib.h>#include <dos.h>#include <conio.h>#include <time.h>#define L 1#define LX 15#define L Y 4static struct BLOCK{int x0,y0,x1,y1,x2,y2,x3,y3;int color,next;intb[]={{0,1,1,1,2,1,3,1,4,1},{1,0,1,3,1,2,1,1,4,0},{1,1,2,2,1,2,2,1,1,2},{0,1,1,1,1,0,2,0,2,4}, {0,0,0,1,1,2,1,1,2,3},{0,0,1,0,1,1,2,1,3,8},{1,0,1,1,2,2,2,1,2,5},{0,2,1,2,1,1,2,1,2,6},{0,1,0,2,1,1,1 ,0,3,9},{0,1,1,1,1,2,2,2,3,10},{1,1,1,2,2,1,2,0,3,7},{ 1,0,1,1,1,2,2,2,7,12},{0,1,1,1,2,1,2,0,7,13},{0 ,0,1,2,1,1,1,0,7,14},{0,1,0,2,1,1,2,1,7,11},{0,2,1,2,1,1,1,0,5,16},{0,1,1,1,2,2,2,1,5,17},{1,0,1,1,1, 2,2,0,5,18},{0,0,0,1,1,11,2,1,5,15},{0,1,1,1,1,0,2,1,6,2,0},{0,1,1,2,1,1,1,0,6,21},{0,1,1,2,1,1,2,1,6 ,22},{1,0,1,1,1,2,2,1,6,19}};static int d[10]={33000,3000,1600,1200,900,800,600,400,300,200};int Llevel,Lcurrent,Lnext,Lable,lx,ly,Lsum;unsigned Lpoint;int La[19][10],FLAG,sum;unsigned ldelay;void scrint(),datainit(),dispb(),eraseeb();void throw(),judge(),delayp(),move(0,note(0,show();int Ldrop(),Ljudge(),nextb(),routejudge();}main(){char c;datainit();Label=nextb();Label=Ldrop();while(1){delayp();if(Label!=0){Ljudge();Lable=nextb();}ldelay--;if(ldelay==0){Label=Ldrop();ldelay=d[0];}if(FLAG!=0) break;while(getch()!='\r');goto xy(38,16);cputs("again?");c=getch();while(c!='n'&&c!='N')c lscr();}int nextb(){if(La[(b[Lnext].y0)][(3+b[Lnext].x0)]!=0||La[(b[Lnext].y1)][(3+b[Lnext].x1)]!=0|| La[(b[Lnext].y2)][(3+b[Lnext].x2)]!=0||La[(b[Lnext].y3)][3+b[Lnext].x3)]!=0) {FLAG=L;return (-1);}erase b(0,3,5,Lnext);Lcurrent=Lnext;lx=3;ly=0;Label=0;ldelay=d[0];Lsum ++;Lpoint+=1;Lnext=random(23);dispb(0,3,5,Lnext);text color(7);goto xy(3,14);printf("%#5d",Lsum);goto xy(3,17);printf("%#5d",Lpoint);return(0);}void delayp(){char key;if(kbhit()!=0){key=getch();move(key);if(key=='\\')getch();}}void move(funkey)char funkey;{int tempcode;case 'k';if(lx+b[current].x0>0){if(La[ly+(b[Lcurrent].y0)][lx-1+(b[Lcurrent].x0)]==0&&La[(ly+b[current].y1)][(lx-1+b[curr ent].x1]==0&&La[ly+b[current].y2)][lx-1+b[Lcurrent].x2)]==0&&La[ly+(b[current].y3)][lx-1+(b [Lcurrent].x3)]==0){eraseb(L,lx,lyLcurrent);lx--;dispb(L,lx,ly,Lcurrent);}}break;case 0x20;tempcode=b[Lcurrent].next;if (lx+b[tempcode].x0>=0 && lx+b[tempcode].x3<=9 && ly+b[tempcode].y1<=19 && ly+b[tempcode].y2<=19){if(routejudge()!+-1){if(La+(b[tempcode].y0)][lx+(b[tempcode].x0)]==0 && La[ly+(b[tempcode].y1)][lx+(b[tempcode].x1)]==0 && La[ly+(b[tempcode].y2)][lx+(b[tempcode].x2)]==0 && La[ly+(b[tempcode].y3)][lx+(b[tempcode].x3)]==0)eraseb(L,lx,ly,Lcurrent);Lcurrent=tempcode;dispb(L,lx,ly,Lcurrent);}}break;case 'M';if(lx+b[Lcurrent].x3<9){if(La[ly+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)]==0 &&La[ly+(b[Lcurrent].y1)][lx+(b[Lcurrent].x1)]==0 && La[ly+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)]==0 && La[ly+(b[Lcurrent].y3)][lx+(b[Lcurrent].x3)]==0){eraseb(L,lx,ly,Lcurrent);lx++;disb(L,lx,ly,Lcurrent);}}break;case 'p';throw();break;case 0x1b;clrscr();exit(0);break;default:break;}void throw(){int tempy;tempy=ly;while(ly+b[Lcurrent].y1<19 && ly+b[current].y2<19&&La[ly+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)]==0 && La[ly+(b[Lcurrent].y1)][lx+(b[Lcurrent].x1)]==0 && La[ly+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)]==0 && La[ly+(b[Lcurrent].y3)][lx+(b[Lcurrent].x3)]==0)ly++;ly--;eraseb(L,lx,tempy,Lcurrent);dispb(L,lx,ly,Lcurrent);La[ly+b[Lcurrent].y0)][lx+(b[current].x0)]=La[ly+b[Lcurrent].y1)][lx+(b[current].x1)]=La[l y+b[Lcurrent].y2)][lx+(b[current].x2)]=La[ly+b[Lcurrent].y3)][lx+(b[current].x3)]=b[Lcurrent].c olor;Label=-1;}int routejudge(){int i,j;for(i=0;i<3;i++)for(j=0;j<3;j++)if(La[ly+i][lx+j]!=0)return(-1);else return(1);}int Ldrop(){if(ly+b[Lcurrent].y1>=18||ly+b[Lcurrent].y2>=18{La[ly+b[Lcurrent].y0)][lx+(b[current].x0)]=3;La[ly+b[Lcurrent].y1)][lx+(b[current].x1)]=1;La[ly+b[Lcurrent].y2)][lx+(b[current].x2)]=5;La[ly+b[Lcurrent].y3)][lx+(b[current].x3)]=b[Lcurrent].color;return(-1);}if(La(ly+1+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)]!=0||La(ly+1+(b[Lcurrent].y1)][lx+(b[Lcur rent].x1)]!=0||La(ly+1+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)]!=0||La(ly+1+(b[Lcurrent].y3)][lx+( b[Lcurrent].x3)]!=0){La[ly+b[Lcurrent].y0)][lx+(b[current].x0)]=La[ly+b[Lcurrent].y1)][lx+(b[cu rrent].x1)]=0BLa[ly+b[Lcurrent].y2)][lx+(b[current].x2)]=La[ly+b[Lcurrent].y3)][lx+(b[current].x3)]=b[Lcurren t].color){return(-1);eraseb(L,lx,ly,Lcurrent);dispb(L,lx,++ly,Lcurrent);return(0);}}int Ljudge(){int i,j,k,lines,f;static int p[5]={0,1,3,6,10};lines=0;for(k=0;k<=3;k++){f=0;if((ly+k)>18)continue;for(i=0;i<10;i++){if(La[ly+k]==0);i>0;i--){f++;break;}if(f==0){movetext(LX,L Y,LX+19,L Y+ly+k-1,LX,L Y+1);for(i=(ly+k);i>0;i--){for(j=0;j<10;j++)La[j]=La[i-1][j];{for(j=0;j<10;j++)La[0][j]=0;lines++;}}}}}Lpoint+=p[lines]*10;return(0);}void scrint(){int i;char lft[20];textbackground(1);clrscr();goto xy(30,9);cputs("enter your name");scanf("%s",lft);goto xy(25,14);scanf("%s",lft);textbackground(0);clrscr();goto xy(17,1);printf("%s",lft);goto xy(5,3);puts("next");goto xy(4,13);cputs("block");goto xy(4,16);cputs("point");for(i=0;i<19;i++){goto xy(LX-2,L Y+1);cputs("** **");}goto xy(LX-2,L Y+19);cputs("**********************");void datainit(){int i,j;)for(i=0;i<19;i++){for(j=0;j<10;j++){La[j]=0;Label=0;FLAG=0;ldelay=d[0];Lsum=0;Lpoint=0;randomize();Lnext=random(23);}}}void dispb(LRflag,x,y,blockcode){int realx,realy;if(LRflag==L){realx=LX+x*2;realy=L Y+y;}realx=x;raly=y;textcolor(b[blockcode].color);goto xy(realx+2*b[blockcode].x0,realy+b[blockcode].y0); cputs("**");goto xy(realx+2*b[blockcode].x1,realy+b[blockcode].y1); cputs("**");goto xy(realx+2*b[blockcode].x2,realy+b[blockcode].y2); cputs("**");goto xy(realx+2*b[blockcode].x3,realy+b[blockcode].y3); cputs("**");}void eraseb(LRflag,x,y,blockcode)int LRflag,x,y,blockcode;int realx,realy;if(LRflag==L){realx=LX+x*2;realy=L Y+y;}else{realx=Lx+x*2;realy=L Y+y;}textcolor(0);goto xy(realx+2*b[blockcode].x0,realy+b[blockcode].y0);cputs("**");goto xy(realx+2*b[blockcode].x1,realy+b[blockcode].y1);cputs("**");goto xy(realx+2*b[blockcode].x2,realy+b[blockcode].y2);cputs("**");goto xy(realx+2*b[blockcode].x3,realy+b[blockcode].y3);cputs("**");}。

简单的迷宫小游戏C语言程序源代码

简单的迷宫小游戏C语言程序源代码

简单的迷宫小游戏C语言程序源代码#include <stdio.h>#include <conio.h>#include <windows.h>#include <time.h>#define Height 31 //迷宫的高度,必须为奇数#define Width 25 //迷宫的宽度,必须为奇数#define Wall 1#define Road 0#define Start 2#define End 3#define Esc 5#define Up 1#define Down 2#define Left 3#define Right 4int map[Height+2][Width+2];void gotoxy(int x,int y) //移动坐标{COORD coord;coord.X=x;coord.Y=y;SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HA NDLE ), coord );}void hidden()//隐藏光标{HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO cci;GetConsoleCursorInfo(hOut,&cci); cci.bVisible=0;//赋1为显示,赋0为隐藏SetConsoleCursorInfo(hOut,&cci);}void create(int x,int y) //随机生成迷宫{int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向int i,j,t;//将方向打乱for(i=0;i<4;i++){j=rand()%4;t=c[i][0];c[i][0]=c[j][0];c[j][0]=t;t=c[i][1];c[i][1]=c[j][1];c[j][1]=t;}map[x][y]=Road;for(i=0;i<4;i++)if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall) {map[x+c[i][0]][y+c[i][1]]=Road; create(x+2*c[i][0],y+2*c[i][1]);}}int get_key() //接收按键{char c;while(c=getch()){if(c==27) return Esc; //Escif(c!=-32)continue;c=getch();if(c==72) return Up; //上if(c==80) return Down; //下if(c==75) return Left; //左if(c==77) return Right; //右}return 0;}void paint(int x,int y) //画迷宫{gotoxy(2*y-2,x-1);switch(map[x][y]){case Start:printf("入");break; //画入口case End:printf("出");break; //画出口case Wall:printf("※");break; //画墙case Road:printf(" ");break; //画路}}void game(){int x=2,y=1; //玩家当前位置,刚开始在入口处int c; //用来接收按键while(1){gotoxy(2*y-2,x-1);printf("☆"); //画出玩家当前位置if(map[x][y]==End) //判断是否到达出口{gotoxy(30,24);printf("到达终点,按任意键结束"); getch();break;}c=get_key();if(c==Esc){gotoxy(0,24);break;}switch(c){case Up: //向上走if(map[x-1][y]!=Wall){paint(x,y);x--;}break;case Down: //向下走if(map[x+1][y]!=Wall){paint(x,y);x++;}break;case Left: //向左走if(map[x][y-1]!=Wall){paint(x,y);y--;}break;case Right: //向右走if(map[x][y+1]!=Wall){paint(x,y);y++;}break;}}}int main(){int i,j;srand((unsigned)time(NULL)); //初始化随即种子hidden(); //隐藏光标for(i=0;i<=Height+1;i++)for(j=0;j<=Width+1;j++)if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫map[i][j]=Road;else map[i][j]=Wall;create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数for(i=0;i<=Height+1;i++) //边界处理{map[i][0]=Wall;map[i][Width+1]=Wall;}for(j=0;j<=Width+1;j++) //边界处理{map[0][j]=Wall;map[Height+1][j]=Wall;}map[2][1]=Start; //给定入口map[Height-1][Width]=End; //给定出口for(i=1;i<=Height;i++)for(j=1;j<=Width;j++) //画出迷宫paint(i,j);game(); //开始游戏getch();return 0;}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

纯真童趣的《泡泡堂》,还有武林情仇,笑傲江湖的《剑侠情缘on line》.它是e 时代常谈的话题,是交互式娱乐的主力军,是一种高层次的综合艺术,更是一个民族的文化,世界观的全新传播方式 .作为游戏玩家的我们,是不是想设计一个属于自己的游戏呢?
爱玩是人的天性,而C语言是我们计算机专业都要学习的一门基础学科.一般来说,是比较枯燥的.那么,我们能不能通过编一些小游戏来提高它的趣味性呢?这样学习程序设计,就不会是一件艰苦 ,枯燥的事,它变得象电脑游戏一样充满好奇,富有乐趣.
1,总是从Hello,world开始学习编程的第一个程序,一般就是打印一个亲切的词语——"Hell o,world!".让我们来看看这个最简单的C程序:
#incolude <> /*把输入输出函数的头文件包含进来*/
int main()
{
printf("Hello,
world!");/*在屏幕上输出字符串"Hello,world!"*/
return 0;/*退出main函数,并返回0*/
}
下面我们发现几个值得改进的地方,1,程序的运行结果一闪而过 .2,每执行这个程序一次都能看见上次运行留下的字符.3,我们还希望屏幕输出一个笑脸来欢迎我们. 让我们来改进一下这个程序吧!
1,在return语句的前面加一句:getch ();,表示按任意键结束.2,在printf语句前用clrscr函数清屏,要使用这个函数和getch函数,需要在程序开头再包含头文件码也有许多非常好玩的字符,比如ASCII码值为2的就是一个笑脸,我们可以用printf("%c", 2)来输出一个笑脸. 现在我们把Hello,world程序改成一个更好看的Hello,world了.下面让我们开始做游戏吧!
2,心动的开始,一个运动中的笑脸大家小时侯喜欢看动画片吗?哈哈,我猜你们都喜欢吧!下面就让我们来做一个小动画吧.在屏幕上显示一个运动的小笑脸,而且当它到达屏幕的边缘时会自动弹回来.先在程序定义一个在屏幕中运动的点的结构:
struct move_point
{
int x, y;/*该点的位置,包括x坐标和y坐标*/
int xv, yv;/*该点在x轴,y轴的速度*/
};
运动的原理是,先擦去物体先前的轨迹,让物体按其速度移动一段距离,再画出该物体.让我们看到以下代码:
gotoxy, ;/*把光标移到指定的坐标*/
printf(" ");/*输出一个空格,把先前的字符擦去*/
然后我们让物体按其速度运动:
+= ;/*水平方向按x轴的速度运动*/
+= ;/*垂直方向按y轴的速度运动*/
运动后还要判断物体是否出界,如果出了界,就令物体反弹,即让它下一刻的速度等于现在的速度的相反数.最后打印出这个笑脸:
gotoxy, ;
printf("%c\b", 2); /*输出ASCII码值为2的"笑脸"字符*/
怎么样?是不是很有趣呢?不过这个笑脸一直是自己运动,能不能让我们来控制它运动呢?答案是肯定的,让我们继续往下学吧!
3,交互的实现——让我们来控制笑脸运动
这个程序的主要功能是接受按键,如果接收的是方向键,就让笑脸顺着方向移动,如果接收的是ESC键就退出程序,其他按键则忽略处理.接受按键我们用以下两条语句:
while (bioskey(1) == 0);/*等待按键*/
key = bioskey(0);/*把接收的按键的键盘码赋给变量key*/
然后用switch语句来判断按键以及执行相关操作,如下:
switch (key) /*对变量key的值进行判断*/
{
case UP: /*如果按的是向上键*/
… break; /*让物体向上运动,并退出switch*/
case DOWN: /*如果按的是向下键*/
… break; /*让物体向下运动,并退出switch*/
case LEFT: /*向左键*/
… break;;/*向左运动*/
case RIGHT: /*向右键*/
… break;/*向右运动*/
default:
break;/*其他按键则忽略处理*/
}
怎么样,是不是有了玩游戏的感觉了?不过这个程序没有什么目的,也没有什么判断胜负的条件.下面我们就利用这个能控制它移动的笑脸来做一个更有趣的游戏吧!
4,在迷宫中探索
小时侯,我常在一些小人书和杂志上看见一些迷宫的游戏,非常喜欢玩,还常到一些书上找迷宫玩呢.好的,现在我们用C语言来编个迷宫的游戏,重温一下童年的乐趣.
首先,我们定义一个二维数组map,用它来保存迷宫的地图,其中map[x][y] == '#'表示在(x,y)坐标上的点是墙壁.DrawMap函数在屏幕上输出迷宫的地图和一些欢迎信息.在main函数里,我们定义了"小人"man的坐标和"目的地"des的坐标.在游戏循环中,我们增加了一些用来判断胜负的语句:
if == && == /*如果人的坐标等于目
的地的坐标*/
{
gotoxy(35, 3);
printf("Ok! You win!"); /*输出胜利信息*/
….
}
在判断按键时,如果玩家按的是方向键,我们还要先判断前面是不是有"墙壁",如果有的话,就不能往前移动了.好的,我们在判断按键的switch语句的各个分支加上了判断语句,如下:
if (map[…][…] == '#') break;/*如果前面是墙壁,就不执行下去*/
哇噻!真棒,我们做出了一个完整的游戏了.当然你还可以通过修改二维数组map 来修改迷宫的地图,让它更有挑战性.不过,我们要设计一个更好玩的游戏—— 5,聪明的搬运工
大家一定玩过"搬运工"的游戏吧!这是在电脑和电子字典上较流行的益智游戏,让我们动手做一个属于自己的"搬运工"吧!程序依然用数组map来保存地图,数组元素如果为空格则表示什么也没有,'b'表示箱子,'#'表示墙壁,'*'表示目的地,'i'表示箱子在目的地.我们以后每推一下箱子,不但要改变屏幕的显示,也要改变map相应元素的值.游戏的主循环依然是接受按键.当接收一个方向键,需要判断小人前面一格的状态,如果是空地或目的地,则人物可以直接移动;如果是墙壁,则不可移动;如果是箱子或目的地上的箱子,则需要继续判断箱子前面一格的状态:如果前一格是空地或目的地,则人推箱子前进,否则不可移动.好的,我们在switch中增加了这些判断语句.程序还有一个重要的功能就是判断胜利.数组Des用来记录全部目的地的坐标,我们每执行一步操作后,程序就要通过Des数组判断这些目的地上是否都有箱子了.真棒啊!我们可以做游戏了.而且是一个老少皆宜,趣味十足的游戏呢!当然,我们可以通过修改map数组来制作不同的游戏地图,我们还可以相互分享好的游戏地图呢.
尾声:
在C++等高级语言还没出来的时候,很多应用程序也是C语言开发的.C 语言在与硬件联系紧密的编程中,也占有重要地位.其实我觉得学习编程,可以通过一些小游戏,实用的例子来学习.象学习音乐的人,不是要等到把全部乐理学完后才演奏一个完整的曲子.而是刚开始学时就有一些简单的曲子让你演奏,让你立刻就有成就感,让你很快就能卖弄出来在别人面前表现自己了.通过编游戏来学习编程,把学习变成游戏,不失为学习计算机的一种好方法.
好了,编游戏就这么简单,希望大家也尝试用C语言或其他的语言来做几个自己喜欢的小游戏.。

相关文档
最新文档