C++编写的小游戏
- 格式:doc
- 大小:33.00 KB
- 文档页数:4
简单的c语言迷宫小游戏一、内容:1、本游戏主要实现了人控制键盘方向键使小人(*)走出迷宫。
2、具有的功能: 1)、在游戏菜单里人可以选择不同难度的游戏进行游戏;2)、在游戏过程中,可以通过键盘方向键使小人移动,走出迷宫;3)、在游戏过程中,当人碰到墙壁(#)的时候小人过不去;4)、当人顺利完成游戏之后,输出“========you are win!======”字样,30秒钟后自动返回到游戏菜单;5)、在游戏过程中,人可以通过按Esc键返回游戏菜单;也可以可以按0直接退出游戏;6)、在游戏菜单里,按0键可以退出游戏。
3、具体应用: 1)、人主要同过键盘的1,2,3数字键来选择游戏难度;2)、在游戏中通过Esc键来返回菜单;3)、同过0键退出游戏。
二、上机环境操作系统:windows7 开发工具:VC6.0三、各函数功能说明main() 主函数;menu() 游戏菜单;roadcake() 消去小人路径;introduce() 游戏介绍;system(“cls”)消屏函数;exit(0) 退出游戏;drawmg1() 画初级难度迷宫;drawmg2() 画中级难度迷宫;drawmg3() 画高级难度迷宫;control1() 控制初级难度游戏;control2() 控制中级难度游戏;control3() 控制高级难度游戏;四、算法流程图首先定义三个全局数组mg1[20][20]、mg2[30][30]、mg3[30][30]用于画出迷宫的地图;1表示墙(#),0表示空地();Introduce( )函数里如果按Enter键,则调用menu( )函数,从键盘中输入相应的提示数字,进入难度不同的游戏;游戏的执行在此只初级难度进行描述,其余的难度与其类似;选了1后调用system(”cls”)进行清屏;drawmg1()函数进行迷宫的地图的绘制, for(i=0;i<20;i++){printf("\t\t\t");for(j=0;j<20;j++){ch=mg1[i][j];switch(ch){case 4: printf("*");break;case 0: printf(" ");break;case 1: printf("#");break;}if(num++%20==0) /*每行只能输出20个字符*/printf(”\n”);}}之后调用控制函数control1()进行控制小人移动路径,在control1()函数里用do—while()语句进行循环,while(1)进行死循环,再定义四个整型a,b,m,n即:for(n=0;n<30;n++)for(m=0;m<30;m++)if(mg3[n][m]==4){a=n; /*为后面调用roadcake()实现消除小人路径进行初始赋值*/b=m;break;}用switch()语句选择方向键,在调用roadcake()函数进行消除小人路径 void roadcake(int *i,int *j){int temp;temp=*i; /*进行值交换,即数组值为0和值为4进行交换*i=*j;*j=temp;}完成第一个控制之后进行清屏和画新的游戏状态图,若在游戏进行中要退出游戏,只需按0键,调用exit(0)函数结束;若要返回游戏菜单,按Esc键掉用menu()函数即可,重复以上步骤,直达游戏结束,此时掉用menu()函数自动返回菜单。
用C和SFML制作迷宫小游戏迷宫小游戏制作指南迷宫小游戏是一种经典的游戏类型,以其挑战性和趣味性而备受玩家喜爱。
这里将介绍使用C语言和SFML库来制作迷宫小游戏的步骤。
一、概述迷宫小游戏的基本原理是玩家通过键盘操作控制角色在迷宫中寻找出口。
玩家可以使用方向键或WASD键控制角色的移动,并避开迷宫中的障碍物。
游戏的难度可以根据迷宫的复杂程度和障碍物的设置来调整。
二、环境搭建1. 安装C编译器和SFML库:在开始制作游戏之前,需要安装C编译器(如GCC或Clang)以及SFML库。
GCC和Clang是常用的C语言编译器,在安装过程中会有相应的说明文档。
SFML是一个跨平台的多媒体库,提供了许多功能丰富的图形和音频接口。
2. 配置开发环境:在安装完成后,需要配置开发环境,包括设置编译器和库文件的路径。
具体操作请参考相关文档。
三、游戏设计在开始编写代码之前,我们需要先设计游戏的基本框架和功能。
1. 创建游戏窗口:使用SFML库可以方便地创建一个游戏窗口,并设置窗口的大小和标题。
2. 绘制迷宫地图:迷宫地图可以使用二维数组来表示,其中不同的数值代表不同的方块类型,比如墙壁、通道和出口。
在游戏开始时,需要根据地图数组来绘制迷宫。
3. 控制角色移动:通过监听键盘事件,可以让玩家使用方向键或WASD键来控制角色的移动。
需要注意的是,角色移动时需要检测是否与墙壁或边界发生碰撞。
4. 碰撞检测:在角色移动过程中,需要判断角色是否与墙壁或障碍物发生碰撞。
如果发生碰撞,则需要相应地处理角色的移动。
5. 胜利条件判断:游戏的胜利条件是角色到达迷宫的出口。
可以通过判断角色与出口的位置关系来判断玩家是否胜利。
四、编写代码在完成游戏设计之后,我们可以开始编写代码来实现游戏功能。
1. 引入SFML库和相关头文件:在代码文件的开头,引入所需的SFML库和相关头文件。
2. 创建游戏窗口:使用SFML库中的窗口类来创建游戏窗口,并设置窗口的大小和标题。
本文将给出一个使用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函数来创建房间、物品和玩家,并进入游戏循环,在每一次迭代中读取玩家输入并执行命令。
#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;}}}。
用vc6.0新建一个Win32 Application工程,把附件代码拷贝进去即可。
上下左右键控制蛇的方向,空格键用于启动或者停止游戏。
上下左右空格键#include <windows.h>#include <stdio.h>#include<time.h>#define C_W 516#define C_H 548//#define C_W 1024//#define C_H 1024#define GO_RIGHT 0x01#define GO_DOWN 0x02#define GO_LEFT 0x03#define GO_UP 0x04#define SNAKE_NUMBER 30typedef struct node_struct{unsigned char direction;unsigned char cnt;}s_node,*s_node_handle;s_node s_count[SNAKE_NUMBER ];typedef struct SNAKE{unsigned char Head_X;unsigned char Head_Y;unsigned char Tail_X;unsigned char Tail_Y;unsigned char h_index;unsigned char t_index;unsigned char food_state;unsigned char score;unsigned char snake_state;} Snake_Data,*Snake_Data_handle;Snake_Data snk_1;#define MAP_X 64#define MAP_Y 64unsigned char game_map[MAP_Y][MAP_X];LRESULT CALLBACK Win_tetris_Proc(HWND hwnd, // handle to windowUINT uMsg, // message identifierWPARAM wParam, // first message parameter LPARAM lParam // second message parameter);int WINAPI WinMain(HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command lineint nCmdShow // show state){snk_1.Head_X = 0x01;//head xsnk_1.Head_Y = 0x00;//head ysnk_1.Tail_X = 0x00;//tail xsnk_1.Tail_Y = 0x00;//tail ysnk_1.h_index=0;snk_1.t_index=0;snk_1.food_state=0;snk_1.score=0;snk_1.snake_state = 1;s_count[snk_1.h_index].cnt=2;s_count[snk_1.h_index].direction=GO_RIGHT;s_count[snk_1.t_index].cnt=2;s_count[snk_1.t_index].direction=GO_RIGHT;WNDCLASS wndcls;wndcls.cbClsExtra=0;wndcls.cbWndExtra=0;wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);wndcls.hInstance=hInstance;wndcls.lpfnWndProc=Win_tetris_Proc;wndcls.lpszClassName="Game_tetris";wndcls.lpszMenuName=NULL;wndcls.style=CS_HREDRAW | CS_VREDRAW;RegisterClass(&wndcls);//Game_TetrisHWND hwnd;hwnd=CreateWindow("Game_tetris","Game_Snake(/zook0k/)",WS _OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX & ~WS_THICKFRAME,0,0,C_W,C_H,NULL,NULL,hInstance,NULL);ShowWindow(hwnd,SW_SHOWNORMAL);//initial snakeHDC hdc;hdc=GetDC(hwnd);HBRUSH hbr;RECT rect;rect.left = 0;rect.top = 0;rect.right = 16;rect.bottom = 8;hbr= CreateSolidBrush(RGB(255,0,0));FillRect(hdc,&rect,hbr);ReleaseDC(hwnd,hdc);game_map[0][0]=1;game_map[0][1]=1;//initial randSetTimer(hwnd,1,100,NULL) ;srand((int)time(0));UpdateWindow(hwnd);MSG msg;while(GetMessage(&msg,NULL,0,0)){// TranslateMessage(&msg);DispatchMessage(&msg);}return 0;}LRESULT CALLBACK Win_tetris_Proc(HWND hwnd, // handle to windowUINT uMsg, // message identifierWPARAM wParam, // first message parameterLPARAM lParam // second message parameter){char szChar[20] = "score:";unsigned char xx,yy;switch(uMsg){case WM_KEYDOWN:{if(32 == wParam){if(1 == snk_1.snake_state){snk_1.snake_state = 0;}else{snk_1.snake_state = 1;}}if(1 == snk_1.snake_state){if((wParam > 36)&&(wParam < 41)){if(38 == wParam){if((s_count[snk_1.h_index].direction == GO_RIGHT)||(s_count[snk_1.h_index].direction == GO_LEFT)){snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;s_count[snk_1.h_index].direction = GO_UP;s_count[snk_1.h_index].cnt = 1;}}else if(40 == wParam){if((s_count[snk_1.h_index].direction == GO_RIGHT)||(s_count[snk_1.h_index].direction == GO_LEFT)){snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;s_count[snk_1.h_index].direction = GO_DOWN;s_count[snk_1.h_index].cnt = 1;}}else if(39 == wParam){if((s_count[snk_1.h_index].direction == GO_DOWN)||(s_count[snk_1.h_index].direction == GO_UP)){snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;s_count[snk_1.h_index].direction = GO_RIGHT;s_count[snk_1.h_index].cnt = 1;}}else if(37 == wParam){if((s_count[snk_1.h_index].direction == GO_DOWN)||(s_count[snk_1.h_index].direction == GO_UP)){snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;s_count[snk_1.h_index].direction = GO_LEFT;s_count[snk_1.h_index].cnt = 1;}}}}break;}case WM_TIMER://case WM_PAINT:time_t t;HDC hdc;hdc=GetDC(hwnd);HBRUSH hbr;RECT rect;if(1 == snk_1.snake_state){//headCHECK:switch(s_count[snk_1.h_index].direction){case GO_RIGHT:{if(snk_1.Head_X < 63)snk_1.Head_X++;else snk_1.Head_X = 0;break;}case GO_LEFT:{if(snk_1.Head_X >0 )snk_1.Head_X--;else snk_1.Head_X = 63;break;}case GO_DOWN :{if(snk_1.Head_Y < 63)snk_1.Head_Y++;else snk_1.Head_Y = 0;break;}case GO_UP:{if(snk_1.Head_Y > 0)snk_1.Head_Y--;else snk_1.Head_Y = 63;break;}default:{break;}}s_count[snk_1.h_index].cnt++;if(0 == game_map[snk_1.Head_Y][snk_1.Head_X])//no point{game_map[snk_1.Head_Y][snk_1.Head_X] = 1;}else if(1 == game_map[snk_1.Head_Y][snk_1.Head_X])//game over{KillTimer(hwnd,1);sprintf(szChar,"score:%d",snk_1.score);MessageBox(hwnd,szChar,"GAME OVER!",0);}else if(2 == game_map[snk_1.Head_Y][snk_1.Head_X])//eat food{game_map[snk_1.Head_Y][snk_1.Head_X] = 1;snk_1.food_state = 0;snk_1.score++;goto CHECK;}rect.left = snk_1.Head_X*8;rect.top = snk_1.Head_Y*8;rect.right = rect.left + 8;rect.bottom = rect.top + 8;hbr= CreateSolidBrush(RGB(255,0,0));FillRect(hdc,&rect,hbr);//show head point//tailgame_map[snk_1.Tail_Y][snk_1.Tail_X] = 0;rect.left = snk_1.Tail_X*8;rect.top = snk_1.Tail_Y*8;rect.right = rect.left + 8;rect.bottom = rect.top + 8;//hbr= CreateSolidBrush(RGB(0,100,0));hbr= CreateSolidBrush(RGB(0,0,0));//clear tail pointFillRect(hdc,&rect,hbr);switch(s_count[snk_1.t_index].direction){case GO_RIGHT:{if(snk_1.Tail_X < 63)snk_1.Tail_X++;else snk_1.Tail_X = 0;break;}case GO_LEFT:{if(snk_1.Tail_X >0 )snk_1.Tail_X--;else snk_1.Tail_X = 63;break;}case GO_DOWN :{if(snk_1.Tail_Y < 63)snk_1.Tail_Y++;else snk_1.Tail_Y = 0;break;}case GO_UP:{if(snk_1.Tail_Y > 0)snk_1.Tail_Y--;else snk_1.Tail_Y = 63;break;}default:{break;}}if(s_count[snk_1.t_index].cnt == 2){snk_1.t_index = (snk_1.t_index + 1)%SNAKE_NUMBER ;}else{s_count[snk_1.t_index].cnt--;}//output foodif(0 == snk_1.food_state){snk_1.food_state = 1;do{xx = rand()%3970%63;yy = rand()%3970/63;}while(1 == game_map[yy][xx]);game_map[yy][xx]=2;rect.left = xx*8;rect.top = yy*8;rect.right = rect.left + 8;rect.bottom = rect.top + 8;hbr= CreateSolidBrush(RGB(155,110,10));FillRect(hdc,&rect,hbr);//show foodsrand((unsigned) time(&t));}ReleaseDC(hwnd,hdc);}break;case WM_CLOSE:DestroyWindow(hwnd);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hwnd,uMsg,wParam,lParam);}return 0;}。
火柴人大战用C编程实现的动作小游戏火柴人大战是一款经典的动作游戏,通过使用C语言编程,我们可以实现这个有趣的小游戏。
本文将介绍火柴人大战的功能、实现过程和游戏设计。
以下是对游戏功能的讨论。
1. 游戏界面游戏界面需要清晰明了,以便玩家能够方便地进行操作。
在屏幕上绘制火柴人和敌人的图像,以及游戏背景和其他动态元素。
通过使用图形库,我们可以创建一个有吸引力的游戏界面。
2. 火柴人移动玩家需要能够通过按下指定的按键来控制火柴人的移动。
根据玩家的输入,我们可以将火柴人向左、向右或向上移动。
在C语言中,我们可以使用switch语句来实现根据玩家按键输入执行相应的操作。
3. 敌人游戏中的敌人应该具有一定的AI,能够自主地移动和攻击火柴人。
我们可以使用随机数来控制敌人的行为。
当火柴人靠近敌人时,敌人会自动攻击并减少火柴人的生命值。
4. 生命与能量玩家的火柴人应该有一定的生命值和能量。
生命值显示火柴人的当前健康状况,而能量则表示火柴人可以使用的特殊技能或道具。
在游戏中,我们需要根据玩家的操作来减少或增加生命和能量值。
5. 特殊技能和道具为增加游戏的乐趣和挑战性,我们可以在游戏中引入一些特殊技能和道具,如火焰球、冰冻魔法或生命恢复道具。
玩家可以通过收集这些特殊物品来增强火柴人的能力或恢复生命值。
6. 计分系统在游戏中加入计分系统可以激励玩家更积极地参与游戏。
根据火柴人击败敌人的数量或通过关卡的难度,我们可以为玩家提供相应的得分。
分数可以在游戏结束时显示出来,并保存在排行榜中。
通过以上功能的实现,我们可以创建一个令人兴奋的火柴人大战游戏。
下面是实现这个游戏的步骤。
1. 设计游戏界面:使用图形库绘制游戏所需的图像,如火柴人、敌人、游戏背景等。
2. 实现火柴人的移动:利用C语言中的输入函数获取玩家的键盘输入,并根据输入进行相应的操作。
3. 设计敌人的AI:使用随机数生成敌人的行动,包括移动和攻击火柴人。
4. 设计生命和能量系统:创建变量来存储火柴人的生命和能量值,并根据游戏操作相应地增加或减少这些值。
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();}。
2345678#include<stdio.h>main() { int a=0,b=0,c=0; if(a++>0&&b++>0)//此处先判断a>0为假,执行a+1,与运算,第一个判断为假,其值肯定为假,后面的都不执行了,而且跳出if 语句体,也就是b++没有 执行,++c 也没有执行 ++c;printf("\na=%d,b=%d,c=%d",a,b,c);C 语言实现的猜拳游戏(剪子锤子布),让你与电脑对决 这是一个简单的猜拳游戏(剪子包子锤),让你与电脑对决。
你出的拳头由你自己决定,电脑则随机出拳,最后判断胜负。
下面的代码会实现一个猜拳游戏,让你与电脑对决。
你出的拳头由你自己决定,电脑则随机出拳,最后判断胜负。
启动程序后,让用户出拳,截图:用户出拳,显示对决结果:截图:代码实现:#include <stdio.h>#include <stdlib.h>#include <time.h>int main(){char gamer; // 玩家出拳int computer; // 电脑出拳int result; // 比赛结果// 为了避免玩一次游戏就退出程序,可以将代码放在循环中while (1){printf("这是一个猜拳的小游戏,请输入你要出的拳头:\n"); printf("A:剪刀\nB:石头\nC:布\nD:不玩了\n");scanf("%c%*c",&gamer);switch (gamer){case 65 | 97: // A | agamer=4; break;case 66 | 98: // B | bgamer=7; break;case 67 | 99: // C | cgamer=10; break;case 68 | 100: // D | dreturn 0;default:printf("你的选择为 %c 选择错误,退出...\n",gamer);getchar();system("cls"); // 清屏return 0;break;}srand((unsigned)time(NULL)); // 随机数种子computer=rand()%3; // 产生随机数并取余,得到电脑出拳result=(int)gamer+computer; // gamer 为 char 类型,数学运算时要强制转换类型printf("电脑出了");switch (computer){case 0:printf("剪刀\n");break; //4 1case 1:printf("石头\n");break; //7 2case 2:printf("布\n");break; //10 3}printf("你出了");switch (gamer){case 4:printf("剪刀\n");break;case 7:printf("石头\n");break;case 10:printf("布\n");break;}if (result==6||result==7||result==11) printf("你赢了!");else if (result==5||result==9||result==10) printf("电脑赢了!");else printf("平手");system("pause>nul&&cls"); // 暂停并清屏}return 0;}代码分析1) 首先,我们需要定义3个变量来储存玩家出的拳头(gamer)、电脑出的拳头(computer)和最后的结果(result),然后给出文字提示,让玩家出拳。
#include <iostream>
using namespace std;
const char MOUSE = '*';
const char WAY = ' ';
const char WALL = '@';
const char PASS='.';
const char IMPASS='X';
class Game {
public:
Game (size_t uWidth,size_t uHeight) : m_uWidth(uWidth),m_uHeight(uHeight),m_pcMaze(new char[uWidth*uHeight]),m_mouse(0,1){ srand(time(NULL));
char (*pcMaze)[m_uWidth] = reinterpret_cast<char (*)[m_uWidth]>(m_pcMaze);
for(size_t i=0;i<m_uHeight;i++)
for(size_t j=0;j<m_uWidth;j++)
if((i==1&&j<4)||(i==m_uHeight-2&&j>m_uWidth-5))
pcMaze[i][j] = WAY;
else if(i==0||i==m_uHeight-1||j==0||j==m_uWidth-1)
pcMaze[i][j] = WALL;
else
pcMaze[i][j] = rand()%4 ? W AY:WALL;
}
~Game (void) {
if(m_pcMaze){
delete[] m_pcMaze;
m_pcMaze = NULL;
}
}
void Run(void){
for(Show();!Quit()&&Step(););
}
private:
class Mouse{
public:
Mouse(size_t x,size_t y) : m_x(x),m_y(y){}
size_t GetX(void){return m_x;}
size_t GetY(void){return m_y;}
void StepRight(void){
m_x++;
m_brain.Remember (EDIR_RIGHT);
}
void StepDown(void){
m_y++;
m_brain.Remember (EDIR_DOWN);
}
void StepLeft(void){
m_x--;
m_brain.Remember (EDIR_LEFT);
}
void StepUp(void){
m_y--;
m_brain.Remember (EDIR_UP);
}
void StepBack(void){
switch (m_brain.Recollect()){
case EDIR_RIGHT:
m_x--;
break;
case EDIR_DOWN:
m_y--;
break;
case EDIR_LEFT:
m_x++;
break;
case EDIR_UP:
m_y++;
break;
}
}
private:
typedef enum tag_Direction {
EDIR_RIGHT,
EDIR_DOWN,
EDIR_LEFT,
EDIR_UP,
EDIR_STILL
} EDIR;
class Brain {
public:
Brain(void) : m_pMemory(NULL){}
~Brain(void){
for(Step* pStep = m_pMemory,*pLast;pStep;pStep = pLast){
pLast = pStep->m_pLast;
delete pStep;
}
}
void Remember (EDIR eDir) {
m_pMemory = new Step (eDir,m_pMemory);
}
EDIR Recollect(void){
EDIR eDir = EDIR_STILL;
if(m_pMemory){
Step* pStep = m_pMemory;
m_pMemory = m_pMemory->m_pLast;
eDir = pStep->m_eDir;
delete pStep;
}
return eDir;
}
private:
class Step {
public:
Step (EDIR eDir,Step* pLast) : m_eDir(eDir),m_pLast(pLast){}
EDIR m_eDir;
Step* m_pLast;
};
Step* m_pMemory;
};
size_t m_x;
size_t m_y;
Brain m_brain;
};
void Show(void){
char (*pcMaze)[m_uWidth] = reinterpret_cast<char (*)[m_uWidth]>(m_pcMaze);
for(size_t i=0;i<m_uHeight;i++){
for(size_t j=0;j<m_uWidth;j++)
cout<<pcMaze[i][j];
cout<<endl;
}
}
bool Quit(void){
cout<<"鎸塓閫€鍑猴紝鍏朵粬閿户缁?"<<flush;
int ch = getchar ();
return ch=='Q'||ch=='q';
}
bool Step(void){
char (*pcMaze)[m_uWidth] = reinterpret_cast<char (*)[m_uWidth]>(m_pcMaze);
size_t x = m_mouse.GetX();
size_t y = m_mouse.GetY();
if(x+1<=m_uWidth-1&&pcMaze[y][x+1]==W AY){
pcMaze[y][x] = PASS;
m_mouse.StepRight();
}
else if(y+1<=m_uHeight-1&&pcMaze[y+1][x]==W AY){ pcMaze[y][x] = PASS;
m_mouse.StepDown();
}
else if(x-1>=0&&pcMaze[y][x-1]==WAY){
pcMaze[y][x] = PASS;
m_mouse.StepLeft();
}
else if(y-1>=0&&pcMaze[y-1][x]==W AY){
pcMaze[y][x] = PASS;
m_mouse.StepUp();
}
else{
pcMaze[y][x]=IMPASS;
m_mouse.StepBack();
}
x = m_mouse.GetX();
y = m_mouse.GetY();
pcMaze[y][x]=MOUSE;
Show();
if(x==0&&y==1){
cout<<"******小老鼠赢了*****";
return false;
}
if(x==m_uWidth-1&&y==m_uHeight-2){
cout<<"*****小老鼠失败了*****"<<endl;
return false;
}
return true;
}
size_t m_uWidth;
size_t m_uHeight;
char* m_pcMaze;
Mouse m_mouse;
};
int main(void){
Game game(30,15);
game.Run();
return 0;
}。