使用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;}。

C语言小游戏源代码《打砖块》

C语言小游戏源代码《打砖块》

C 语言小游戏源代码《打砖块》#include "graphics.h"#include "stdio.h"#include "conio.h" /* 所需的头文件*/int on; /* 声明具有开关作用的全局变量*/ static int score;/* 声明静态的记分器变量*//* 定义开始界面函数*/int open(){setviewport(100,100,500,380,1); /* 设置图形窗口区域*/setcolor(4); /* 设置作图色*/rectangle(0,0,399,279); /* 以矩形填充所设的图形窗口区域*/ setfillstyle(SOLID_FILL,7); /* 设置填充方式*/floodfill(50,50,4); /* 设置填充范围*/setcolor(8);settextstyle(0,0,9); /* 文本字体设置*/outtextxy(90,80,"BALL"); /* 输出文本内容*/settextstyle(0,0,1);outtextxy(110,180,"version 1.0");outtextxy(110,190,"made by ddt");setcolor(128);settextstyle(0,0,1);outtextxy(120,240,"Press any key to continue "); }/* 定义退出界面函数*/int quitwindow(){char s[100]; /* 声明用于存放字符串的数组*/setviewport(100,150,540,420,1);setcolor(YELLOW);rectangle(0,0,439,279);setfillstyle(SOLID_FILL,7);floodfill(50,50,14);setcolor(12);settextstyle(0,0,8);outtextxy(120,80,"End");settextstyle(0,0,2);outtextxy(120,200,"quit? Y/N");sprintf(s,"Your score is:%d",score);/* 格式化输出记分器的值*/outtextxy(120,180,s);on=1; /* 初始化开关变量*/}/* 主函数*/main(){int gdriver,gmode;gdriver=DETECT; /* 设置图形适配器*/gmode=VGA; /* 设置图形模式*/registerbgidriver(EGAVGA_driver); /* 建立独立图形运行程序*/ initgraph(&gdriver,&gmode,""); /* 图形系统初试化*/ setbkcolor(14);open(); /* 调用开始界面函数*/getch(); /* 暂停*/while(1) /* 此大循环体控制游戏的反复重新进行*/{int driver,mode,l=320,t=400,r,a,b,dl=5,n,x=200,y=400,r1=10,dx=- 2,dy=-2;/* 初始化小球相关参数*/intleft[100],top[100],right[100],bottom[100],i,j,k,off=1,m,num[100][10 0];/*方砖阵列相关参数*/static int pp;static int phrase; /* 一系列起开关作用的变量*/ int oop=15;pp=1;score=0;driver=DETECT;mode=VGA;registerbgidriver(EGAVGA_driver);initgraph(&driver,&mode,"");setbkcolor(10);cleardevice(); /**/ clearviewport(); /* 清除现行图形窗口内容*/b=t+6;r=l+60;setcolor(1);rectangle(0,0,639,479);setcolor(4);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,1);floodfill(l+2,t+2,4);for(i=0,k=0;i<=6;i++) /* 此循环绘制方砖阵列*/ {top[i]=k;bottom[i]=top[i]+20;k=k+21;oop--;for(j=0,m=0;j<=7;j++){left[j]=m;right[j]=left[j]+80;m=m+81;setcolor(4);rectangle(left[j],top[i],right[j],bottom[i]);setfillstyle(SOLID_FILL,j+oop); floodfill(left[j]+1,top[i]+1,4);num[i][j]=pp++;}}while(1) /* 此循环控制整个动画*/ {while(!kbhit()){x=x+dx; /* 小球运动的圆心变量控制*/ y=y+dy;if(x+r1>r||x+r1<r)phrase=0;} {if((x-r1<=r||x+r1<=r)&&x+r1>=l){if(y<t)phrase=1;if(y+r1>=t&&phrase==1){dy=-dy;y=t-1-r1;}}if(off==0)continue;for(i=0;i<=6;i++) /*for(j=0;j<=7;j++) 此循环用于判断、控制方砖阵列的撞击、擦除* /if((x+r1<=right[j]&&x+r1>=left[j])||(x-r1<=right[j]&&x- r1>=left[j])){if(( y-r1>top[i]&&y-r1<=bottom[i])||(y+r1>=top[i]&&y+r1<=bottom[i] )) {if(num[i][j]==0){continue; }setcolor(10);rectangle(left[j],top[i],right[j],bottom[i]);setfillstyle(SOLID_FILL,10);floodfill(left[j]+1,top[i]+1,10); dy=-dy;num[i][j]=0;score=score+10;printf("%d\b\b\b",score);}}if((y+r1>=top[i]&&y+r1<=bottom[i])||(y-r1>=top[i]&&y-r1<=bottom[i])){if((x+r1>=left[j]&&x+r1<right[j])||(x-r1<=right[j]&&x-r1>left[j])){if(num[i][j]==0){ continue;}setcolor(10);rectangle(left[j],top[i],right[j],bottom[i]);setfillstyle(SOLID_FILL,10);floodfill(left[j]+1,top[i]+1,10);dx=-dx;num[i][j]=0;score=score+10;printf("%d\b\b\b",score);}}}if(x+r1>639) /* 控制小球的弹射范围*/{dx=-dx;x=638-r1;}if(x<=r1){dx=-dx;x=r1+1;}if(y+r1>=479){off=0;quitwindow();break;}if(y<=r1){dy=-dy;y=r1+1;}if(score==560){off=0;quitwindow();break;} setcolor(6); circle(x,y,r1);setfillstyle(SOLID_FILL,14);floodfill(x,y,6);delay(1000);setcolor(10);circle(x,y,r1);setfillstyle(SOLID_FILL,10);floodfill(x,y,10); }a=getch();setcolor(10);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,10);floodfill(l+2,t+2,10);if(a==77&&l<=565) /* 键盘控制设定*/ {dl=20;l=l+dl;}if(a==75&&l>=15){dl=-20;l=l+dl;}if(a=='y'&&on==1)break;if(a=='n'&&on==1)break;if(a==27){quitwindow();off=0;}r=l+60;setcolor(4);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,1);floodfill(l+5,t+5,4);delay(100);}if(a=='y'&&on==1) /* 是否退出游戏*/ {break;}if(a=='n'&&on==1){ continue;}}closegraph();}。

C语言游戏代码(里面揽括扫雷_俄罗斯方块_推箱子_五子棋_贪吃蛇)

C语言游戏代码(里面揽括扫雷_俄罗斯方块_推箱子_五子棋_贪吃蛇)

五子棋#include <stdio.h>#include <bios.h>#include <ctype.h>#include <conio.h>#include <dos.h>#define CROSSRU 0xbf /*右上角点*/#define CROSSLU 0xda /*左上角点*/#define CROSSLD 0xc0 /*左下角点*/#define CROSSRD 0xd9 /*右下角点*/#define CROSSL 0xc3 /*左边*/#define CROSSR 0xb4 /*右边*/#define CROSSU 0xc2 /*上边*/#define CROSSD 0xc1 /*下边*/#define CROSS 0xc5 /*十字交叉点*//*定义棋盘左上角点在屏幕上的位置*/#define MAPXOFT 5#define MAPYOFT 2/*定义1号玩家的操作键键码*/#define PLAY1UP 0x1157/*上移--'W'*/#define PLAY1DOWN 0x1f53/*下移--'S'*/#define PLAY1LEFT 0x1e41/*左移--'A'*/#define PLAY1RIGHT 0x2044/*右移--'D'*/#define PLAY1DO 0x3920/*落子--空格键*//*定义2号玩家的操作键键码*/#define PLAY2UP 0x4800/*上移--方向键up*/#define PLAY2DOWN 0x5000/*下移--方向键down*/ #define PLAY2LEFT 0x4b00/*左移--方向键left*/#define PLAY2RIGHT 0x4d00/*右移--方向键right*/ #define PLAY2DO 0x1c0d/*落子--回车键Enter*//*若想在游戏中途退出, 可按Esc 键*/#define ESCAPE 0x011b/*定义棋盘上交叉点的状态, 即该点有无棋子*//*若有棋子, 还应能指出是哪个玩家的棋子*/#define CHESSNULL 0 /*没有棋子*/#define CHESS1 'O'/*一号玩家的棋子*/#define CHESS2 'X'/*二号玩家的棋子*//*定义按键类别*/#define KEYEXIT 0/*退出键*/#define KEYFALLCHESS 1/*落子键*/#define KEYMOVECURSOR 2/*光标移动键*/#define KEYINV ALID 3/*无效键*//*定义符号常量: 真, 假--- 真为1, 假为0 */#define TRUE 1#define FALSE 0/**********************************************************/ /* 定义数据结构*//*棋盘交叉点坐标的数据结构*/struct point{int x,y;};/**********************************************************/ /*自定义函数原型说明*/void Init(void);int GetKey(void);int CheckKey(int press);int ChangeOrder(void);int ChessGo(int Order,struct point Cursor);void DoError(void);void DoOK(void);void DoWin(int Order);void MoveCursor(int Order,int press);void DrawCross(int x,int y);void DrawMap(void);int JudgeWin(int Order,struct point Cursor);int JudgeWinLine(int Order,struct point Cursor,int direction);void ShowOrderMsg(int Order);void EndGame(void);/**********************************************************//**********************************************************/ /* 定义全局变量*/int gPlayOrder; /*指示当前行棋方*/struct point gCursor; /*光标在棋盘上的位置*/char gChessBoard[19][19];/*用于记录棋盘上各点的状态*//**********************************************************//**********************************************************/ /*主函数*/void main(){int press;int bOutWhile=FALSE;/*退出循环标志*/printf("Welcome ");Init();/*初始化图象,数据*/while(1){press=GetKey();/*获取用户的按键值*/switch(CheckKey(press))/*判断按键类别*/{/*是退出键*/case KEYEXIT:clrscr();/*清屏*/bOutWhile = TRUE;break;/*是落子键*/case KEYFALLCHESS:if(ChessGo(gPlayOrder,gCursor)==FALSE)/*走棋*/DoError();/*落子错误*/else{DoOK();/*落子正确*//*如果当前行棋方赢棋*/if(JudgeWin(gPlayOrder,gCursor)==TRUE){DoWin(gPlayOrder);bOutWhile = TRUE;/*退出循环标志置为真*/}/*否则*/else/*交换行棋方*/ChangeOrder();ShowOrderMsg(gPlayOrder);}break;/*是光标移动键*/case KEYMOVECURSOR:MoveCursor(gPlayOrder,press);break;/*是无效键*/case KEYINV ALID:break;}if(bOutWhile==TRUE)break;}/*游戏结束*/EndGame();}/**********************************************************//*界面初始化,数据初始化*/void Init(void){int i,j;char *Msg[]={"Player1 key:"," UP----w"," DOWN--s"," LEFT--a"," RIGHT-d"," DO----space","","Player2 key:"," UP----up"," DOWN--down"," LEFT--left"," RIGHT-right"," DO----ENTER","","exit game:"," ESC",NULL,/* 先手方为1号玩家*/gPlayOrder = CHESS1;/* 棋盘数据清零, 即棋盘上各点开始的时候都没有棋子*/ for(i=0;i<19;i++)for(j=0;j<19;j++)gChessBoard[i][j]=CHESSNULL;/*光标初始位置*/gCursor.x=gCursor.y=0;/*画棋盘*/textmode(C40);DrawMap();/*显示操作键说明*/i=0;textcolor(BROWN);while(Msg[i]!=NULL){gotoxy(25,3+i);cputs(Msg[i]);i++;}/*显示当前行棋方*/ShowOrderMsg(gPlayOrder);/*光标移至棋盘的左上角点处*/gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);}/*画棋盘*/void DrawMap(void){int i,j;clrscr();for(i=0;i<19;i++)for(j=0;j<19;j++)DrawCross(i,j);}/*画棋盘上的交叉点*/void DrawCross(int x,int y){gotoxy(x+MAPXOFT,y+MAPYOFT); /*交叉点上是一号玩家的棋子*/if(gChessBoard[x][y]==CHESS1) {textcolor(LIGHTBLUE);putch(CHESS1);return;}/*交叉点上是二号玩家的棋子*/if(gChessBoard[x][y]==CHESS2) {textcolor(LIGHTBLUE);putch(CHESS2);return;}textcolor(GREEN);/*左上角交叉点*/if(x==0&&y==0){putch(CROSSLU);return;}/*左下角交叉点*/if(x==0&&y==18){putch(CROSSLD);return;}/*右上角交叉点*/if(x==18&&y==0){putch(CROSSRU);return;}/*右下角交叉点*/if(x==18&&y==18){putch(CROSSRD); return;}/*左边界交叉点*/if(x==0){putch(CROSSL); return;}/*右边界交叉点*/if(x==18){putch(CROSSR); return;}/*上边界交叉点*/if(y==0){putch(CROSSU); return;}/*下边界交叉点*/if(y==18){putch(CROSSD); return;}/*棋盘中间的交叉点*/ putch(CROSS);}/*交换行棋方*/int ChangeOrder(void) {if(gPlayOrder==CHESS1) gPlayOrder=CHESS2; elsegPlayOrder=CHESS1;return(gPlayOrder);}/*获取按键值*/int GetKey(void){char lowbyte;int press;while (bioskey(1) == 0);/*如果用户没有按键,空循环*/press=bioskey(0);lowbyte=press&0xff;press=press&0xff00 + toupper(lowbyte); return(press);}/*落子错误处理*/void DoError(void){sound(1200);delay(50);nosound();}/*赢棋处理*/void DoWin(int Order){sound(1500);delay(100);sound(0); delay(50);sound(800); delay(100);sound(0); delay(50);sound(1500);delay(100);sound(0); delay(50);sound(800); delay(100);sound(0); delay(50);nosound();textcolor(RED+BLINK);gotoxy(25,20);if(Order==CHESS1)cputs("PLAYER1 WIN!");elsecputs("PLAYER2 WIN!");gotoxy(25,21);cputs(" \\<^+^>/");getch();}/*走棋*/int ChessGo(int Order,struct point Cursor){/*判断交叉点上有无棋子*/if(gChessBoard[Cursor.x][Cursor.y]==CHESSNULL){/*若没有棋子, 则可以落子*/gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT); textcolor(LIGHTBLUE);putch(Order);gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT); gChessBoard[Cursor.x][Cursor.y]=Order;return TRUE;}elsereturn FALSE;}/*判断当前行棋方落子后是否赢棋*/int JudgeWin(int Order,struct point Cursor){int i;for(i=0;i<4;i++)/*判断在指定方向上是否有连续5个行棋方的棋子*/if(JudgeWinLine(Order,Cursor,i))return TRUE;return FALSE;}/*判断在指定方向上是否有连续5个行棋方的棋子*/int JudgeWinLine(int Order,struct point Cursor,int direction) {int i;struct point pos,dpos;const int testnum = 5;int count;switch(direction){case 0:/*在水平方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y;dpos.x=1;dpos.y=0;break;case 1:/*在垂直方向*/pos.x=Cursor.x;pos.y=Cursor.y-(testnum-1);dpos.x=0;dpos.y=1;break;case 2:/*在左下至右上的斜方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y+(testnum-1);dpos.x=1;dpos.y=-1;break;case 3:/*在左上至右下的斜方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y-(testnum-1);dpos.x=1;dpos.y=1;break;}count=0;for(i=0;i<testnum*2+1;i++)/*????????i<testnum*2-1*/ {if(pos.x>=0&&pos.x<=18&&pos.y>=0&&pos.y<=18) {if(gChessBoard[pos.x][pos.y]==Order){count++;if(count>=testnum)return TRUE;}elsecount=0;}pos.x+=dpos.x;pos.y+=dpos.y;}return FALSE;}/*移动光标*/void MoveCursor(int Order,int press) {switch(press){case PLAY1UP:if(Order==CHESS1&&gCursor.y>0) gCursor.y--;break;case PLAY1DOWN:if(Order==CHESS1&&gCursor.y<18) gCursor.y++;break;case PLAY1LEFT:if(Order==CHESS1&&gCursor.x>0) gCursor.x--;break;case PLAY1RIGHT:if(Order==CHESS1&&gCursor.x<18) gCursor.x++;break;case PLAY2UP:if(Order==CHESS2&&gCursor.y>0) gCursor.y--;break;case PLAY2DOWN:if(Order==CHESS2&&gCursor.y<18) gCursor.y++;break;case PLAY2LEFT:if(Order==CHESS2&&gCursor.x>0) gCursor.x--;break;case PLAY2RIGHT:if(Order==CHESS2&&gCursor.x<18) gCursor.x++;break;}gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT); }/*游戏结束处理*/void EndGame(void){textmode(C80);}/*显示当前行棋方*/void ShowOrderMsg(int Order){gotoxy(6,MAPYOFT+20);textcolor(LIGHTRED);if(Order==CHESS1)cputs("Player1 go!");elsecputs("Player2 go!");gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT); }/*落子正确处理*/void DoOK(void){sound(500);delay(70);sound(600);delay(50);sound(1000);delay(100);nosound();}/*检查用户的按键类别*/int CheckKey(int press){if(press==ESCAPE)return KEYEXIT;/*是退出键*/elseif( ( press==PLAY1DO && gPlayOrder==CHESS1) || ( press==PLAY2DO && gPlayOrder==CHESS2))return KEYFALLCHESS;/*是落子键*/elseif( press==PLAY1UP || press==PLAY1DOWN || press==PLAY1LEFT || press==PLAY1RIGHT || press==PLAY2UP || press==PLAY2DOWN ||press==PLAY2LEFT || press==PLAY2RIGHT)return KEYMOVECURSOR;/*是光标移动键*/elsereturn KEYINV ALID;/*按键无效*/}贪吃蛇#define N 200#include <graphics.h>#include <stdlib.h>#include <dos.h>#define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011bint i,key;int score=0;/*得分*/int gamespeed=50000;/*游戏速度自己调整*/ struct Food{int x;/*食物的横坐标*/int y;/*食物的纵坐标*/int yes;/*判断是否要出现食物的变量*/ }food;/*食物的结构体*/struct Snake{int x[N];int y[N];int node;/*蛇的节数*/int direction;/*蛇移动方向*/int life;/* 蛇的生命,0活着,1死亡*/}snake;void Init(void);/*图形驱动*/void Close(void);/*图形结束*/void DrawK(void);/*开始画面*/void GameOver(void);/*结束游戏*/void GamePlay(void);/*玩游戏具体过程*/void PrScore(void);/*输出成绩*//*主函数*/void main(void){Init();/*图形驱动*/DrawK();/*开始画面*/GamePlay();/*玩游戏具体过程*/Close();/*图形结束*/}/*图形驱动*/void Init(void){int gd=DETECT,gm;initgraph(&gd,&gm,"c:\\tc");cleardevice();}/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/ void DrawK(void){/*setbkcolor(LIGHTGREEN);*/setcolor(11);setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/for(i=50;i<=600;i+=10)/*画围墙*/{rectangle(i,40,i+10,49); /*上边*/rectangle(i,451,i+10,460);/*下边*/}for(i=40;i<=450;i+=10){rectangle(50,i,59,i+10); /*左边*/rectangle(601,i,610,i+10);/*右边*/}}/*玩游戏具体过程*/void GamePlay(void){randomize();/*随机数发生器*/food.yes=1;/*1表示需要出现新食物,0表示已经存在食物*/snake.life=0;/*活着*/snake.direction=1;/*方向往右*/snake.x[0]=100;snake.y[0]=100;/*蛇头*/snake.x[1]=110;snake.y[1]=100;snake.node=2;/*节数*/PrScore();/*输出得分*/while(1)/*可以重复玩游戏,压ESC键结束*/{while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/{if(food.yes==1)/*需要出现新食物*/{food.x=rand()%400+60;food.y=rand()%350+60;while(food.x%10!=0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/ food.x++;while(food.y%10!=0)food.y++;food.yes=0;/*画面上有食物了*/}if(food.yes==0)/*画面上有食物了就要显示*/{setcolor(GREEN);rectangle(food.x,food.y,food.x+10,food.y-10);}for(i=snake.node-1;i>0;i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/{snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];}/*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/switch(snake.direction){case 1:snake.x[0]+=10;break;case 2: snake.x[0]-=10;break;case 3: snake.y[0]-=10;break;case 4: snake.y[0]+=10;break;}for(i=3;i<snake.node;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/{if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]){GameOver();/*显示失败*/snake.life=1;break;}}if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455)/*蛇是否撞到墙壁*/{GameOver();/*本次游戏结束*/snake.life=1; /*蛇死*/}if(snake.life==1)/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/ break;if(snake.x[0]==food.x&&snake.y[0]==food.y)/*吃到食物以后*/{setcolor(0);/*把画面上的食物东西去掉*/rectangle(food.x,food.y,food.x+10,food.y-10);snake.x[snake.node]=-20;snake.y[snake.node]=-20;/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/snake.node++;/*蛇的身体长一节*/food.yes=1;/*画面上需要出现新的食物*/score+=10;PrScore();/*输出新得分*/}setcolor(4);/*画出蛇*/for(i=0;i<snake.node;i++)rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);delay(gamespeed);setcolor(0);/*用黑色去除蛇的的最后一节*/rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);} /*endwhile(!kbhit)*/if(snake.life==1)/*如果蛇死就跳出循环*/break;key=bioskey(0);/*接收按键*/if(key==ESC)/*按ESC键退出*/break;elseif(key==UP&&snake.direction!=4)/*判断是否往相反的方向移动*/snake.direction=3;elseif(key==RIGHT&&snake.direction!=2)snake.direction=1;elseif(key==LEFT&&snake.direction!=1)snake.direction=2;elseif(key==DOWN&&snake.direction!=3)snake.direction=4;}/*endwhile(1)*/}/*游戏结束*/void GameOver(void){cleardevice();PrScore();setcolor(RED);settextstyle(0,0,4);outtextxy(200,200,"GAME OVER");getch();}/*输出成绩*/void PrScore(void){char str[10];setfillstyle(SOLID_FILL,YELLOW);bar(50,15,220,35);setcolor(6);settextstyle(0,0,2);sprintf(str,"score:%d",score);outtextxy(55,20,str);}/*图形结束*/void Close(void){getch();closegraph();}扫雷游戏/*模拟扫雷游戏*/#include <graphics.h>#include <math.h>#include <stdio.h>#include <dos.h>#include <stdlib.h>#include <conio.h>#include <alloc.h>union REGS regs;int size=15;/*用于表示每个方块的大小(正方形的边长)*/int pix,piy=50;/*pix,piy是矩阵的偏移量*/char b[2]="1";/*用于显示方格周围的雷的个数*/int pan[30][16];/*用于记录盘面的情况:0:没有、9:有雷、1~8:周围雷的个数*/int pan1[30][16];/*pan1[][]纪录当前的挖雷情况,0:没有操作、1:打开了、2:标记了*/int tt;/*纪录时间参数*/int Eflags;/*用于标记鼠标按钮的有效性,0:有效,1:无效,2:这是鼠标的任意键等于重新开始*/int Msinit();void Draw(int x,int y,int sizex,int sizey);void Facedraw(int x,int y,int sizel,int k);void Dead(int sizel,int x,int y);void Setmouse(int xmax,int ymax,int x,int y);int Msread(int *xp,int *yp,int *bup,struct time t1,int k);void Draw1(int x,int y);int Open(int x,int y);float Random();void Have(int sum,int x,int y,int xx,int yy);void Help();void Coread();void Ddraw2(int x,int y);/*下面是主函数*/main(){int mode=VGAHI,devices=VGA;/*图形模式初始化的变量*/char ams; /*鼠标操作中的标志变量*/int xms,yms,bms; /*鼠标的状态变量*/int i,j,k,k1=0; /*i,j,k是循环变量*/int x=9,y=9,flags=0; /*x,y矩阵的大小*/int sum=10; /*sum 盘面的雷的总数目,是个x,y的函数*/int x1=0,y1=0; /*用于记录光标当前的位置*/int x11=0,y11=0; /*暂时保存鼠标位置的值*/int sizel=10; /*脸的大小*/int cflags=1; /*这是菜单操作标志变量,没有弹出1,弹出0*/struct time t1={0,0,0,0}; /*时间结构体,头文件已定义*/int co[3]; /*暂时纪录历史纪录*/void far *Map; /*用于保存鼠标图片*/char name[3][20]; /*名字字符串,用于记录名字*/FILE * p; /*文件指针用于文件操作*/Msinit(); /*鼠标初始化*//*registerbgidriver(EGA VGA_driver);*/initgraph(&devices,&mode,"C:\\tc"); /*图形模式初始化*//*为图片指针分配内存*/if((Map=farmalloc(imagesize(0,0,20,20)))==NULL)/*图片的大小是20*20*/{printf("Memory ererr!\n");printf("Press any key to out!\n");exit(1);}/*用于检验文件是否完整*/while((p = fopen("score.dat", "r")) == NULL) /*如果不能打开就新建一个*/{if((p = fopen("score.dat", "w")) == NULL)/*如果不能新建就提示错误并推出*/{printf("The file cannot open!\n");printf("Presss any key to exit!\n");getch();exit(1);}/*写入初始内容*/fprintf(p,"%d %d %d,%s\n%s\n%s\n",999,999,999,"xiajia","xiajia","xiajia");fclose(p);}/*暂时读出历史纪录。

双手奉上!一个好玩的小游戏(纯C语言编写)!

双手奉上!一个好玩的小游戏(纯C语言编写)!

双⼿奉上!⼀个好玩的⼩游戏(纯C语⾔编写)!效果演⽰源代码#include<stdio.h>#include<string.h>#include<conio.h>#include<windows.h>#include<stdlib.h>#define MAX 100long long int speed = 0;//控制敌机的速度int position_x, position_y;//飞机的所在位置int high, width;//地图的⼤⼩int bullet_x, bullet_y;//⼦弹的位置int enemy_x, enemy_y;//敌⼈的位置int map[MAX][MAX];/*0表⽰空⽩,1表⽰战机*的区域,2表⽰敌⼈战机的位置。

3表⽰上下围墙,4表⽰左右围墙,5表⽰⼦弹的位置*/int score;void starup()//初始化所有的信息{high = 20;width = 30;position_x = high / 2;position_y = width / 2;bullet_x = 0;bullet_y = position_y;enemy_x = 2;enemy_y = position_y - 1;score = 0;{int i, j;for (i = 1; i <= high - 1; i++){map[i][1] = 4;for (j = 2; j <= width - 1; j++)map[i][j] = 0;map[i][width] = 4;}//下⽅围墙的初始化i = high;for (j = 1; j <= width; j++)map[i][j] = 3;map[bullet_x][bullet_y] = 5;/*这⾥是战机⼤⼩的初始化开始*/map[position_x - 1][position_y] = 1;i = position_x;for (j = position_y - 2; j <= position_y + 2; j++)map[i][j] = 1;map[position_x + 1][position_y - 1] = 1;map[position_x + 1][position_y + 1] = 1;/*** 初始化结束 **//* 敌⼈战机的初始化 */map[enemy_x][enemy_y] = 2;map[enemy_x - 1][enemy_y - 1] = 2;map[enemy_x - 1][enemy_y + 1] = 2;/* 敌⼈战机初始化结束*/}void HideCursor()//隐藏光标{CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); }void gotoxy(int x, int y)//清理⼀部分屏幕{HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(handle, pos);}void updateWithoutInput()//于输⼊⽆关的跟新{if (bullet_x > 0)bullet_x--;if ((bullet_x == enemy_x) && (bullet_y == enemy_y))//当敌⼈的飞机被击中时{enemy_y = rand() % width;bullet_x = 0;}if (enemy_x > high)//当飞机超出区域{enemy_x = 0;enemy_y = rand() % width;}if (speed == 1)for (int i = 1; i <= 10000; i++)//⽤来控制敌机的速度 {for (int j = 1; j <= 3000; j++){speed = 1;}}speed = 0;if (speed == 0){enemy_x++;speed = 1;}}void updateWithInput()//与输⼊有关的更新{char input;if (kbhit())//在VC6.0++下,为_kbhit(){input = getch();//在VC6.0++下为_getch();if (input == 'a')position_y--;if (input == 's')position_x++;if (input == 'd')position_y++;if (input == 'w')position_x--;if (input == ''){bullet_x = position_x - 1;bullet_y = position_y;}}}void show()//展⽰的内容{gotoxy(0, 0);for (i = 1; i <= high; i++){for (j = 1; j <= width; j++){if (map[i][j] == 0)printf("");if (map[i][j] == 1)printf("*");if (map[i][j] == 2)printf("#");if (map[i][j] == 3)printf("~");if (map[i][j] == 4)printf("|");if (map[i][j] == 5)printf("|");}printf("\n");}printf("\n你的得分:%d\n\n", score);printf("操作说明: ASDW分别操作左下右上四个的移动\n");printf("**空格是发出⼦弹**\n");}int main(){starup();while (1){HideCursor();startMap();show();updateWithoutInput();updateWithInput();}return0;}—————————————关注我,参观更多源码项⽬!- End -—————————————不管你是转⾏也好,初学也罢,进阶也可,如果你想学编程,进阶程序员~【值得关注】我的!【点击进⼊】C语⾔⼊门资料(⽹盘链接免费分享):C语⾔推荐书籍(PDF免费分享):。

憋死牛游戏(C语言小游戏)

憋死牛游戏(C语言小游戏)

#include<stdio.h>#include<windows.h>#include<stdlib.h>char qipan[23][23];void csh();//初始化棋盘void printfqipan();//输出棋盘void yxjs();//游戏介绍int main(){int a,b,c,d,p,q;char e;//用于存储谁先走bool who;int x=1;int n=0;static bool t=true;//定义静态变量,递归调用时不会被初始化SetConsoleTitle("石家庄学院---“憋死牛”儿时游戏"); // 设置控制台标题system("mode con cols=78 lines=43");//设置控制台缓冲区大小system("color 02");//设置字体颜色yxjs(); //输出游戏介绍if(t)//再来一局时不会执行{printf("按回车键开始游戏!");getchar();t=false;}csh();//初始化棋盘printfqipan();//输出棋盘printf("请输入谁先开始:");while(x)//获取先走的一方{fflush(stdin);//清空键盘缓冲区e=getchar();fflush(stdin);//清空键盘缓冲区if(e=='a'||e=='A'){who=true;x--;}//获取到有效值则终止循环else if(e=='b'||e=='B'){who=false;x--;}//获取到有效值则终止循环elseprintf("请输入A或B:");}printf("输入要走棋子的坐标,使其补到空位。

C语言代码实现简单2048游戏

C语言代码实现简单2048游戏

C语⾔代码实现简单2048游戏最近玩2048上瘾,于是尝试⽤C++写了⼀个2048⼩游戏操作⽅法很简单,通过wasd控制⽅块的⽅向,数据的上限为65536代码如下#include<bits/stdc++.h>#include<conio.h>#include <windows.h>void color(short x){if(x>=0 && x<=15)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);elseSetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);}using namespace std;int qp[4][4]={0};long long int gread=0;int pd(){int i,j;for(i=0;i<4;i++){for(j=0;j<4;j++){if(qp[i][j]==0){return 0;}if(i==0&&j==0){if(qp[i][j]==qp[i+1][j]||qp[i][j]==qp[i][j+1]){return 0;}}else if(i==0&&j==3){if(qp[i][j]==qp[i+1][j]||qp[i][j]==qp[i][j-1]){return 0;}}else if(i==0){if(qp[i][j]==qp[i+1][j]||qp[i][j]==qp[i][j+1]||qp[i][j]==qp[i][j-1]){return 0;}}else if(i==3&&j==0){if(qp[i][j]==qp[i][j+1]||qp[i][j]==qp[i-1][j]){return 0;}}else if(j==0){if(qp[i][j]==qp[i+1][j]||qp[i][j]==qp[i-1][j]||qp[i][j]==qp[i][j+1]){return 0;}}else if(i==3&&j==3){if(qp[i][j]==qp[i-1][j]||qp[i][j]==qp[i][j-1]){if(qp[i][j]==qp[i-1][j]||qp[i][j]==qp[i][j-1]||qp[i][j]==qp[i][j+1]) {return 0;}}else if(j==3){if(qp[i][j]==qp[i-1][j]||qp[i][j]==qp[i][j-1]||qp[i][j]==qp[i+1][j]) {return 0;}}}}return 1;}int sjs(){int num = rand() % 100 + 1;if(num<=5){return 4;}else{return 2;}}int sc(){for(;;){int n=rand()%4;int m=rand()%4;if(qp[n][m]==0){qp[n][m]=sjs();return 0;}}}void dy(int n){if(n==0){cout<<" ";}else if(n==2){color(7);cout<<" "<<n<<" ";color(7);}else if(n==4){color(14);cout<<" "<<n<<" ";color(7);}else if(n==8){color(6);cout<<" "<<n<<" ";color(7);}else if(n==16){color(12);cout<<" "<<n<<" ";cout<<" "<<n<<" ";color(7);}else if(n==64){color(13);cout<<" "<<n<<" ";color(7);}else if(n==128){color(5);cout<<" "<<n<<" ";color(7);}else if(n==256){color(9);cout<<" "<<n<<" ";color(7);}else if(n==512){color(3);cout<<" "<<n<<" ";color(7);}else if(n==1024){color(11);cout<<n<<" ";color(7);}else if(n==2048){color(10);cout<<n<<" ";color(7);}else if(n==4096){color(2);cout<<n<<" ";color(7);}else{color(15);cout<<n;color(7);}}int main(){srand(time(NULL));int i,j;cout<<"Game start!(输⼊w a s d进⾏控制)"<<endl; sc();sc();cout<<"-------------------------"<<endl;cout<<"|";dy(qp[0][0]);cout<<"|";dy(qp[0][1]);cout<<"|";dy(qp[0][2]);cout<<"|";dy(qp[0][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl;cout<<"|";dy(qp[1][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[2][0]);cout<<"|";dy(qp[2][1]);cout<<"|";dy(qp[2][2]);cout<<"|";dy(qp[2][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[3][0]);cout<<"|";dy(qp[3][1]);cout<<"|";dy(qp[3][2]);cout<<"|";dy(qp[3][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; for(;;){char n;n=getch();if(n=='w'){int g=0;for(i=0;i<4;i++){for(j=1;j<4;j++){if(qp[j][i]!=0){int k=j;while(qp[k-1][i]==0&&k!=0){k--;}qp[k][i]=qp[j][i];if(k!=j){qp[j][i]=0;g=1;}}}if(qp[0][i]==qp[1][i]&&qp[0][i]!=0) {qp[0][i]=qp[0][i]*2;gread+=qp[0][i];qp[1][i]=qp[2][i];qp[2][i]=qp[3][i];qp[3][i]=0;g=1;}if(qp[1][i]==qp[2][i]&&qp[1][i]!=0) {qp[1][i]=qp[1][i]*2;gread+=qp[1][i];qp[2][i]=qp[3][i];qp[3][i]=0;g=1;}if(qp[2][i]==qp[3][i]&&qp[2][i]!=0) {qp[2][i]=qp[2][i]*2;{cout<<"换个⽅向试试~"<<endl; continue;}else{system("cls");}}else if(n=='d'){int g=0;for(i=0;i<4;i++){for(j=2;j>=0;j--){if(qp[i][j]!=0){int k=j;while(qp[i][k+1]==0&&k!=3){k++;}qp[i][k]=qp[i][j];if(k!=j){qp[i][j]=0;g=1;}}}if(qp[i][3]==qp[i][2]&&qp[i][3]!=0) {qp[i][3]=qp[i][3]*2;gread+=qp[i][3];qp[i][2]=qp[i][1];qp[i][1]=qp[i][0];qp[i][0]=0;g=1;}if(qp[i][2]==qp[i][1]&&qp[i][2]!=0) {qp[i][2]=qp[i][2]*2;gread+=qp[i][2];qp[i][1]=qp[i][0];qp[i][0]=0;g=1;}if(qp[i][1]==qp[i][0]&&qp[i][1]!=0) {qp[i][1]=qp[i][1]*2;gread+=qp[i][1];qp[i][0]=0;g=1;}}if(g==0){cout<<"换个⽅向试试~"<<endl; continue;}else{system("cls");}}else if(n=='s'){int g=0;for(i=0;i<4;i++)while(qp[k+1][i]==0&&k!=3){k++;}qp[k][i]=qp[j][i];if(k!=j){qp[j][i]=0;g=1;}}}if(qp[3][i]==qp[2][i]&&qp[3][i]!=0) {qp[3][i]=qp[3][i]*2;gread+=qp[3][i];qp[2][i]=qp[1][i];qp[1][i]=qp[0][i];qp[0][i]=0;g=1;}if(qp[2][i]==qp[1][i]&&qp[2][i]!=0) {qp[2][i]=qp[2][i]*2;gread+=qp[2][i];qp[1][i]=qp[0][i];qp[0][i]=0;g=1;}if(qp[1][i]==qp[0][i]&&qp[1][i]!=0) {qp[1][i]=qp[1][i]*2;gread+=qp[1][i];qp[0][i]=0;g=1;}}if(g==0){cout<<"换个⽅向试试~"<<endl; continue;}else{system("cls");}}else if(n=='a'){int g=0;for(i=0;i<4;i++){for(j=1;j<4;j++){if(qp[i][j]!=0){int k=j;while(qp[i][k-1]==0&&k!=0){k--;}qp[i][k]=qp[i][j];if(k!=j){qp[i][j]=0;g=1;}}}if(qp[i][0]==qp[i][1]&&qp[i][0]!=0)qp[i][2]=qp[i][3];qp[i][3]=0;g=1;}if(qp[i][1]==qp[i][2]&&qp[i][1]!=0){qp[i][1]=qp[i][1]*2;gread+=qp[i][1];qp[i][2]=qp[i][3];qp[i][3]=0;g=1;}if(qp[i][2]==qp[i][3]&&qp[i][2]!=0){qp[i][2]=qp[i][2]*2;gread+=qp[i][2];qp[i][3]=0;g=1;}}if(g==0){cout<<"换个⽅向试试~"<<endl;continue;}else{system("cls");}}else{cout<<"请输⼊w、a、s、d"<<endl; continue;}sc();cout<<"分数:"<<gread<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[0][0]);cout<<"|";dy(qp[0][1]);cout<<"|";dy(qp[0][2]);cout<<"|";dy(qp[0][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[1][0]);cout<<"|";dy(qp[1][1]);cout<<"|";dy(qp[1][2]);cout<<"|";dy(qp[1][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[2][0]);cout<<"|";dy(qp[2][1]);cout<<"|";dy(qp[2][2]);cout<<"|";dy(qp[2][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[3][0]);cout<<"|";dy(qp[3][1]);cout<<"|"<<endl;cout<<"-------------------------"<<endl;if(pd()==1){break;}}cout<<"Game over~"<<endl;cout<<"请输⼊“quit”并回车退出游戏"<<endl;for(;;){char s[10000];cin>>s;if(strcmp(s,"quit")==0){break;}}return 0;}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

c语言小游戏代码

c语言小游戏代码

c语言小游戏代码#include <stdio.h>#include <stdlib.h>#include <windows.h>// 定义元素类型#define ELEMENT char// 游戏行数#define ROW 10// 游戏显示延迟#define SLEEPTIME 100int main(int argc, char *argv[]){// 定义游戏的棋盘,用数组存放ELEMENT array[ROW][ROW];// 定义获胜条件int winCondition = 5;// 初始化,把棋盘清空system("cls");int i,j;for(i = 0; i < ROW; i++){for(j = 0; j < ROW; j++){array[i][j] = ' ';}}// 循环游戏,当有一方满足胜利条件时终止int tmp;int count = 0; // 存放棋子数while(1){// 依次取出玩家记录的棋子int x, y;// 如果已经有子落下,则计算是第几步if(count > 0){printf("第%d步:\n", count);}// 显示游戏棋盘for(i = 0; i < ROW; i++){printf(" ");for(j = 0; j < ROW; j++){printf("---");}printf("\n|");for(j = 0; j < ROW; j++){printf("%c |", array[i][j]);}printf("\n");}printf(" ");for(j = 0; j < ROW; j++){printf("---");}printf("\n");// 要求玩家输入放下棋子的位置printf("请玩家输入要放弃棋子的位置(1-%d)\n", ROW); printf("横坐标:");scanf("%d", &x);printf("纵坐标:");scanf("%d", &y);// 判断棋子位置是否有效if(x < 1 || x > ROW || y < 1 || y > ROW || array[x-1][y-1] != ' '){printf("输入错误!\n");system("pause");system("cls");continue;}// 把棋子记录,并计数if(count % 2 == 0){array[x-1][y-1] = 'X';}else{array[x-1][y-1] = 'O';}count++;// 判断是否有获胜者int i, j, k;int tempx, tempy;for(i = 0; i < ROW; i++){for(j = 0; j < ROW; j++){if(array[i][j] == 'X' || array[i][j] == 'O') {// 判断横向是否有获胜者tmp = 1;for(k = 1; k < winCondition; k++){// 注意边界,必须验证范围有效if(j + k > ROW - 1) break;// 如果和前一个位置的棋子相同,则计数加1,否则跳出if(array[i][j+k] == array[i][j])tmp++;else break;}// 如果计数满足获胜条件,则显示获胜者if(tmp >= winCondition){printf("玩家 %c 获胜!\n", array[i][j]);system("pause");return 0;}// 判断纵向是否有获胜者tmp = 1;for(k。

C语言小游戏源代《俄罗斯方块》

C语言小游戏源代《俄罗斯方块》

C语言小游戏源代码《俄罗斯方块》#include <stdlib.h>#include <stdio.h>#include <graphics.h>#define ESC 27#define UP 328#define DOWN 336#define LEFT 331#define RIGHT 333#define BLANK 32#define BOTTOM 2#define CANNOT 1#define CAN 0#define MAX 30#define F1 315#define ADD 43#define EQUAL 61#define DEC 45#define SOUNDs 115#define SOUNDS 83#define PAUSEP 80#define PAUSEp 112void Init();void Down();void GoOn();void ksdown();void Display(int color);void Give();int Touch(int x,int y,int dx,int dy);int GeyKey();void Select();void DetectFill();void GetScores();void Fail();void Help();void Quit();void DrawBox(int x,int y,int Color);void OutTextXY(int x,int y,char *String); void DispScore(int x,int y,char Ch);void DrawNext(int Color);int Heng=12,Shu=20; /*横竖*/int Position[MAX][MAX];int middle[MAX][MAX];int ActH,ActS;int Act,Staus;int i,j,k;int Wid=10;int NoPass=CAN;float Delays=15000;int BeginH=250,BeginS=7;float Seconds=0;int Scores=0;int flag=1;int Sounds=CAN;int PreAct,NextAct;int a[8][4][4][4]={{{1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0}, {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},{1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0},{1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0}},{{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0}},{{1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0},{0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0},{0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0},{1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0}},{{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0},{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0},{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0}},{{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0},{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0},{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0}},{{1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},{1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0}}, {{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}, {1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0}, {1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0}}, {{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}}; int b[4][4];main(int argc,char *argv[]){if (argc!=1){if (argv[1]!="")Heng=atoi(argv[1]);if (argv[2]!="")Shu=atoi(argv[2]);}Init(); /*初始化界面*/PreAct=random(8); /*取得当前的方块*/ for(;;) /*以下是游戏流程*/{NextAct=random(8); /*取得下一个方块*/ DrawNext(1); /*画出下一个方块*/Act=PreAct;if (Heng%2==0) ActH=Heng/2;else ActH=(Heng-1)/2;ActS=0; /*方块开始从游戏空间的中间下落*/Staus=0; /*取开始的状态*/NoPass=CAN; /*物体可以下落*/Give(); /*取得当前的方块*/Display(Act+1); /*显示当前的方块,每种方块的颜色不同*/ GoOn(); /*游戏的算法精髓所在*/PreAct=NextAct; /*方块下落完毕,取得下一个方块*/ DrawNext(0);}}void Init(){int GraphDriver=DETECT,GraphMode;registerbgidriver(EGAVGA_driver);initgraph(&GraphDriver,&GraphMode,"");if (kbhit()) Sounds=CANNOT;setcolor(1);OutTextXY(10,10,"Tetris");OutTextXY(30,30,"Version 2.0");OutTextXY(10,120,"Help:");OutTextXY(20,140,"+ :Faster");OutTextXY(20,160,"- :Slower");OutTextXY(20,180,"Esc :Quit");OutTextXY(20,200,"F1 :Help");OutTextXY(10,310,"Copyright(c) 1998.2.22");OutTextXY(10,320,"By Mr. Unique");outtextxy(10,250,"Score: 00000");rectangle(BeginH-3,BeginS-3,BeginH+Heng*(Wid+2)+2,BeginS+Shu*(Wid+2)+2);rectangle(BeginH-5,BeginS-5,BeginH+Heng*(Wid+2)+4,BeginS+Shu*(Wid+2)+4);rectangle(BeginH+(Heng+4)*(Wid+2)-2,BeginS+10,BeginH+(Heng+8)*(Wid+2)+2,BeginS+12+4*( Wid+2));for (i=0;i<MAX;i++)for (j=0;j<MAX;j++){Position[i][j]=1;middle[i][j]=-1;}for (i=0;i<Heng;i++)for (j=0;j<Shu;j++)Position[i][j]=0;for (i=0;i<Heng;i++)for (j=0;j<Shu;j++)DrawBox(i,j,0);randomize();}void GoOn(){for(;;){Seconds+=0.2; /*控制方块的下落速度*/if (Seconds>=Delays){Down();Seconds=0;if (NoPass==BOTTOM){DetectFill();middle[ActH][ActS]=Act;if (ActS==0)Fail();return;}}if (kbhit())Select();}}void Down() /*方块下降*/{Display(0);if (Touch(ActH,ActS,0,1)==CAN) ActS++;elsemiddle[ActH][ActS]=Act; Display(Staus+1);}int Touch(int x,int y,int dx,int dy) {NoPass=CAN;for (i=0;i<4;i++)for (j=0;j<4;j++)Position[x+dx+i][y+dy+j]+=b[i][j];for (i=0;i<MAX;i++)for (j=0;j<MAX;j++)if (Position[i][j]>1)NoPass=CANNOT;for (i=0;i<4;i++)for (j=0;j<4;j++){Position[x+dx+i][y+dy+j]-=b[i][j]; middle[x+dx+i][y+dy+j]=Act;}if (NoPass==CANNOT && dx==0 && dy==1) {for (i=0;i<4;i++)for (j=0;j<4;j++)Position[x+i][y+j]+=b[i][j];NoPass=BOTTOM;}return NoPass;}int GetKey(void){int Ch,Low,Hig;Ch=bioskey(0);Low=Ch&0x00ff;Hig=(Ch&0xff00)>>8;return(Low==0?Hig+256:Low);}void Select(){int OldStaus,acts=ActS;switch(GetKey()){case ESC :Quit();break;case DOWN :Seconds+=14500;break;case LEFT :Display(0);if (ActH>0 && Touch(ActH,ActS,-1,0)==CAN) { ActH--;}Display(Act+1);break;case RIGHT :Display(0);if (ActH<Heng && Touch(ActH,ActS,1,0)==CAN) { ActH++;}Display(Act+1);break;case BLANK : Display(0);ksdown();Display(Act+1);break;case F1 :Help();break;case EQUAL :case ADD :if (Delays>300) Delays-=100;break; case DEC :if (Delays<3000) Delays+=100;break; case PAUSEP :case PAUSEp :getch();break;case SOUNDS :case SOUNDs :if (Sounds==CAN)Sounds=CANNOT;elseSounds=CAN;break;case UP :if(Act==7){while(acts<Shu-1&&Position[ActH][acts]!=1) acts++;Position[ActH][acts]=0;DrawBox(ActH,acts,0);acts=ActS;break;}else{Display(0);OldStaus=Staus;switch(Act){case 0:case 3:case 4:if (Staus==1) Staus=0;else Staus=1;break; case 1:break;case 2:case 5:case 6:if (Staus==3) Staus=0;else Staus++;break; }Give();if (Touch(ActH,ActS,0,0)==CANNOT){Staus=OldStaus;Give();}Display(Act+1);break;}}}void ksdown(){while(flag){if(Touch(ActH,ActS,0,0)==CAN){ActS++;}else {ActS--;flag=0;}}flag=1;}void Quit(){int ch,TopScore;FILE *fp;if ((fp=fopen("Russian.scr","r+"))!=NULL) {fscanf(fp,"%d",&TopScore);if (Scores>TopScore){setcolor(1);outtextxy(470,80,"Hello !");outtextxy(470,100,"In all the players,"); outtextxy(470,120,"You are the First !"); outtextxy(470,140,"And your score will"); outtextxy(470,160,"be the NEW RECORD !"); fseek(fp,0L,0);fprintf(fp,"%d",Scores);}fclose(fp);}setcolor(1);OutTextXY(470,220,"Are You Sure (Yes/no)?");ch=getch();if (ch=='y'||ch=='Y'){closegraph();delay(20);exit(0);}setcolor(0);outtextxy(470,220,"Are You Sure (Yes/no)?"); }void OutTextXY(int x,int y,char *String) {int i=0;char a[2];moveto(x,y);a[1]='\0';while (*(String+i)!='\0'){a[0]=*(String+i);outtext(a);if (Sounds==CAN && a[0]!=' '){sound(3000);delay(50);nosound();}i++;}}void Help(){unsigned Save;void *Buf;Save=imagesize(160,120,500,360);Buf=malloc(Save);getimage(160,120,500,360,Buf);setfillstyle(1,1);bar(160,120,500,280);setcolor(0);OutTextXY(170,130," About & Help");OutTextXY(170,150," # # # ########## # # # "); OutTextXY(170,160," # ## # # # # # # ###### ### "); OutTextXY(170,170," ########## ########## ## # # "); OutTextXY(170,180," # # # # # # # ## #### "); OutTextXY(170,190," # ## # #### ## # # # "); OutTextXY(170,200," # ## # # # # # ## # # # "); OutTextXY(170,210," # # # ## ## # ###### # # # "); OutTextXY(170,220," ## # ## # ## # # # # "); OutTextXY(170,230," # ## # #### # ## # "); OutTextXY(170,260," Good Luckly to You !!! ");getch();putimage(160,120,Buf,0);free(Buf);}void GetScores(){int Sec10000,Sec1000,Sec100,Sec10,Sec1; setfillstyle(0,1);bar(60,250,109,260);Sec1=Scores%10;Sec10=(Scores%100-Scores%10)/10;Sec100=(Scores%1000-Scores%100)/100;Sec1000=(Scores%10000-Scores%1000)/1000; Sec10000=(Scores%100000-Scores%10000)/10000; DispScore(60,250,'0'+Sec10000);DispScore(70,250,'0'+Sec1000);DispScore(80,250,'0'+Sec100);DispScore(90,250,'0'+Sec10);DispScore(100,250,'0'+Sec1);DispScore(110,250,'0');DispScore(120,250,'0');}void DispScore(int x,int y,char Ch){char a[2];a[1]='\0';a[0]=Ch;outtextxy(x,y,a);void Give(){for (i=0;i<4;i++)for (j=0;j<4;j++)b[i][j]=a[Act][Staus][i][j];}void Display(int color){for (i=0;i<4;i++)for (j=0;j<4;j++)if (b[i][j]==1) DrawBox(ActH+i,ActS+j,color); }void DrawBox(int x,int y,int Color){x=BeginH+x*(Wid+2);y=BeginS+y*(Wid+2);setfillstyle(1,Color);bar(x+2,y+2,x+Wid-1,y+Wid-1);if (Color==0)setcolor(9);elsesetcolor(Act+1);rectangle(x+4,y+4,x+Wid-4,y+Wid-4);}void DrawNext(int Color)for (i=0;i<4;i++)for (j=0;j<4;j++)if (a[NextAct][0][i][j]==1) DrawBox(Heng+4+i,1+j,Color); }void DetectFill(){int Number,Fall,FallTime=0;for (i=Shu-1;i>=0;i--){Number=0;for (j=0;j<Heng;j++)if (Position[j][i]==1) Number++;if (Number==Heng){FallTime++;if (Sounds==CAN){sound(500);delay(500);nosound();}for (Fall=i;Fall>0;Fall--)for (j=0;j<Heng;j++){Position[j][Fall]=Position[j][Fall-1];middle[j][Fall]=middle[j][Fall-1];if (Position[j][Fall]==0) DrawBox(j,Fall,0); else DrawBox(j,Fall,middle[j][Fall]+1);}i++;}}switch(FallTime){case 0:break;case 1:Scores+=1;break;case 2:Scores+=3;break;case 3:Scores+=6;break;case 4:Scores+=10;break;}if (FallTime!=0){GetScores();if (Scores%100==0) Delays-=100;}}void Fail(){if (Sounds==CAN){for (k=0;k<3;k++){sound(300);delay(200);nosound();}}setcolor(1);OutTextXY(440,200,"Game over!"); Quit();closegraph();exit(0);}。

C语言实现猜数字的小游戏

C语言实现猜数字的小游戏

C语⾔实现猜数字的⼩游戏使⽤C语⾔来实现⼀个猜数字的⼩游戏学习C语⾔有⼏天的时间了,在这期间对C语⾔的语法,程序结构有了了解,⾃⼰也练习过许多的代码,今天分享⼀个猜数字的代码。

⼀、猜数字游戏描述:由程序随机⽣成⼀个1~100之间的数字,由⽤户去猜,直⾄猜对为⽌1.代码代码如下:#include <stdio.h>#include <stdlib.h>#include <time.h>void menu(void){printf("|---------------------------------|\n");printf("|*********************************|\n");printf("|*************1、PLAY*************|\n");printf("|*************0、EXIT*************|\n");printf("|*********************************|\n");printf("|---------------------------------|\n");}void game(void){int randomNum = rand() % 100 + 1;int guessNum = 0;while (1){printf("请输⼊你猜的数字:>\n");scanf("%d",&guessNum);if (guessNum > randomNum)printf("猜⼤了!\n");else if (guessNum < randomNum)printf("猜⼩了!\n");else{printf("恭喜你!猜对了!\n");break;}}}int main(){srand((size_t)time(NULL));int choice = 0;do{menu();printf("请输⼊你的选择:>\n");scanf("%d", &choice);switch (choice){case 0:printf("退出游戏!\n");break;case 1:game();break;default:printf("选择错误!\n请重新选择!\n");break;}} while (choice);return 0;}分析:void menu(void);void game(void);⾸先定义两个函数,分别在main函数中调⽤。

  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语言或其他的语言来做几个自己喜欢的小游戏.。

相关文档
最新文档