c语言小游戏
- 格式:docx
- 大小:14.93 KB
- 文档页数:28
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语⾔程序设计】—最近超⽕的⼩游戏—【数字炸弹】!✍准备⼯作和建议⼀、程序的原理在动⼿编程之前,得先跟⼤家说⼀下这个程序是⼲什么的。
我们可以称呼这个游戏为《数字炸弹》。
游戏的原理是这样:每⼀轮电脑从 1 到 100 中随机抽⼀个整数。
电脑请求你猜这个数字,因此你要输⼊⼀个 1 到 100 之间的整数。
电脑将你输⼊的数和它抽取的数进⾏⽐较,并告知你的数⽐它的数⼤了还是⼩了。
然后它会再次让你输⼊数字,并告诉你⽐较的结果。
⼀直到你猜到这个数为⽌,⼀轮结束。
游戏的⽬的,当然就是⽤最少的次数猜到这个“神秘”数字。
虽然没有绚丽的图形界⾯,但是或多或少,这都是你的第⼀个游戏了,应该值得骄傲。
下⾯演⽰了⼀轮的样式,你要编程来实现它:这个数字是什么?50猜⼩了!这个数字是什么?75猜⼩了!这个数字是什么?85猜⼤了!这个数字是什么?80猜⼤了!这个数字是什么?78猜⼩了!这个数字是什么?79太棒了,你猜到了这个神秘数字!!⼆、随机抽取⼀个数但⼤家要问了:“如何随机地抽取⼀个数呢?不知道怎么办啊,⾂妾做不到啊。
”诚然,我们还没学习如何来产⽣⼀个随机数。
让亲爱的电脑兄来做这个是不简单的:它很会做运算,但是要它随机选择⼀个数,它还不知道怎么做呢。
事实上,为了“尝试”得到⼀个随机数,我们不得不让电脑来做⼀些复杂的运算。
好吧,归根结底还是做运算。
我们有两个解决⽅案:✎请⽤户通过 scanf 函数输⼊这个神秘数字,那么就需要两个玩家咯。
⼀个选数字,⼀个猜数字。
✎孤注⼀掷地让电脑来为我们⾃动产⽣⼀个随机数。
好处是:只需要⼀个玩家,可以⾃娱⾃乐。
缺点是:需要学习该怎么做...我们来学习⽤第⼆种⽅案编写这个游戏,当然你也可以之后⾃⼰编写第⼀种⽅案的代码。
为了⽣成⼀个随机数,我们要⽤到 rand() 函数(rand 是英语 random 的缩写,表⽰“随机的”)。
顾名思义,这个函数能为我们⽣成随机数。
但是我们还想要这个随机数是在 1 到 100 的整数范围内(如果没有限定范围,那会很复杂)。
用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库中的窗口类来创建游戏窗口,并设置窗口的大小和标题。
#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 语言小游戏源代码《打砖块》#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语言程序源代码#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_HANDLE ), 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语言小游戏#include<stdio.h>#include<windows.h>//基本型态定义。
支援型态定义函数。
使用者界面函数图形装置界面函数。
#include<conio.h> //用户通过按键盘产生的对应操作(控制台)#include<stdlib.h>#include<time.h> //日期和时间头文件#define LEN 30#define WID 25int Snake[LEN][WID] = {0}; //数组的元素代表蛇的各个部位char Sna_Hea_Dir = 'a';//记录蛇头的移动方向int Sna_Hea_X,Sna_Hea_Y;//记录蛇头的位置int Snake_Len = 3;//记录蛇的长度clock_t Now_Time;//记录当前时间,以便自动移动int Wait_Time ;//记录自动移动的时间间隔int Eat_Apple = 1;//吃到苹果表示为1int Level ;int All_Score = -1;int Apple_Num = -1; HANDLE hConsole = GetStdHandle(STD_OUTPUT _HANDLE); //获取标准输出的句柄<windows.h>//句柄:标志应用程序中的不同对象和同类对象中的不同的实例方便操控,void gotoxy(int x, int y)//设置光标位置COORD pos = {x,y}; //定义一个字符在控制台屏幕上的坐标POS SetConsoleCursorPosition(h Console, pos); //定位光标位置的函数<windows.h>}void Hide_Cursor()//隐藏光标固定函数CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(hCon sole, &cursor_info);}void SetColor(int color)//设置颜色{ SetConsoleTextAttribute(hC onsole, color);//是API设置字体颜色和背景色的函数格式:SetConsoleTextAttribute(句柄,颜色);}void Print_Snake()//打印蛇头和蛇的脖子和蛇尾{int iy, ix, color;for(iy = 0; iy < WID; ++iy) for(ix = 0; ix < LEN; ++ix) {if(Snake[ix][iy] == 1)//蛇头{SetColor(0xf); //oxf代表分配的内存地址setcolor:34行自定义设置颜色的函数gotoxy(ix*2, iy);printf("※");}if(Snake[ix][iy] == 2)//蛇的脖子{color = rand()%15 + 1;//rand()函数是产生随机数的一个随机函数。
C语言里还有srand()函数等。
//头文件:stdlib.hif(color == 14)color -= rand() % 13 + 1; //变色SetColor(color);gotoxy(ix*2, iy);printf("■");}if(Snake[ix][iy] == Snake_Len){gotoxy(ix*2, iy); SetColor(0xe);printf("≈");}}}void Clear_Snake()//擦除贪吃蛇{int iy, ix;for(iy = 0; iy < WID; ++iy) for(ix = 0; ix < LEN; ++ix) {gotoxy(ix*2, iy);if(Snake[ix][iy] ==Snake_Len)printf("");}}void Rand_Apple()//随机产生苹果{int ix, iy;do{ix = rand() % LEN;iy = rand() % WID;}while(Snake[ix][iy]); Snake[ix][iy] = -1; gotoxy(ix*2, iy);printf("⊙");Eat_Apple = 0;}void Game_Over()//蛇死掉了{gotoxy(30, 10);printf("Game Over"); Sleep(3000);system("pause > nul"); exit(0);}void Move_Snake()//让蛇动起来{int ix, iy;for(ix = 0; ix < LEN; ++ix)//先标记蛇头for(iy = 0; iy < WID; ++iy) if(Snake[ix][iy] == 1){switch(Sna_Hea_Dir)//根据新的蛇头方向标志蛇头{case 'w':if(iy == 0)Game_Over();elseSna_Hea_Y = iy - 1;Sna_Hea_X = ix; break;case 's':if(iy == (WID -1))Game_Over();elseSna_Hea_Y = iy + 1; Sna_Hea_X = ix; break;case 'a':if(ix == 0)Game_Over(); elseSna_Hea_X = ix - 1; Sna_Hea_Y = iy; break;case 'd':if(ix == (LEN - 1))Game_Over();elseSna_Hea_X = ix + 1;Sna_Hea_Y = iy; break;default:break;}}if(Snake[Sna_Hea_X][Sna_Hea_Y]!=1&&Snake[Sna_Hea _X][Sna_Hea_Y]!=0&&Snak e[Sna_Hea_X][Sna_Hea_Y]! =-1)Game_Over();if(Snake[Sna_Hea_X][Sna_H ea_Y] < 0)//吃到苹果{++Snake_Len;Eat_Apple = 1;}for(ix = 0; ix < LEN; ++ix)//处理蛇尾for(iy = 0; iy < WID; ++iy) {if(Snake[ix][iy] > 0){if(Snake[ix][iy] !=Snake_Len)Snake[ix][iy] += 1;elseSnake[ix][iy] = 0;}}Snake[Sna_Hea_X][Sna_Hea_Y] = 1;//处理蛇头}void Get_Input()//控制蛇的移动方向{if(kbhit()){switch(getch()){case 87:Sna_Hea_Dir = 'w'; break;case 83:Sna_Hea_Dir = 's'; break;case 65:Sna_Hea_Dir = 'a'; break;case 68:Sna_Hea_Dir = 'd'; break; default:break;}}if(clock() - Now_Time >= Wait_Time)//蛇到时间自动行走{Clear_Snake();Move_Snake();Print_Snake();Now_Time = clock();}}void Init()//初始化{system("title 贪吃毛毛蛇");system("mode con:cols=80 lines=25");Hide_Cursor();gotoxy(61, 4);printf("You Score:"); gotoxy(61, 6);printf("You Level:"); gotoxy(61, 8);printf("The Lenght:");gotoxy(61, 10);printf("The Speed:"); gotoxy(61, 12);printf("Apple Num:"); int i;for(i = 0; i < Snake_Len; ++i)//生成蛇Snake[10+i][15] = i+1; int iy, ix;//打印蛇for(iy = 0; iy < WID; ++iy) for(ix = 0; ix < LEN; ++ix) {if(Snake[ix][iy]){SetColor(Snake[ix][iy]); gotoxy(ix*2, iy);printf("■");}}}void Pri_News()//打印信息{SetColor(0xe);gotoxy(73,4);All_Score += Level;printf("%3d", All_Score); gotoxy(73, 6);printf("%3d", Level); gotoxy(73, 8);printf("%3d",Snake_Len); gotoxy(73, 10);printf("0.%3ds",Wait_Time/10);gotoxy(73, 12);printf("%d", Apple_Num); }void Lev_Sys()//等级系统{if(((Apple_Num-1) / 10) == Level){++Level;if(Wait_Time > 50)Wait_Time -= 50;elseif(Wait_Time > 10)Wait_Time -= 10;elseWait_Time -= 1;}}int main(void){Init();srand((unsigned)time(NULL) );//设置随机数的种子Now_Time = clock();int speed1=1000,speed2,a; printf("\n");printf("请输入你想要的速度\n");scanf("%d",&speed2); Level=1;Wait_Time=speed1-speed2; printf("请输入你想要的苹果数\n");scanf("%d",&a);while(a--)Rand_Apple();while(1){if(Eat_Apple){++Apple_Num;Rand_Apple();Lev_Sys();Pri_News(); }Get_Input(); Sleep(10); }return 0;}。