Win-Tc(C语言)小游戏编程实例.doc_第1页
Win-Tc(C语言)小游戏编程实例.doc_第2页
Win-Tc(C语言)小游戏编程实例.doc_第3页
Win-Tc(C语言)小游戏编程实例.doc_第4页
Win-Tc(C语言)小游戏编程实例.doc_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Win-Tc (C语言) 小游戏编程实例扫雷游戏的游戏界面采用3D窗体显示,用鼠标操作实现整个游戏过程。 图、3D窗体扫雷游戏界面 图、游戏中打开非雷方块游戏规则游戏开始时,系统会在布雷区小方块中随机设置游戏级别中规则的雷体个数。布好雷后系统会在非雷方块中填上表示其周围个方块中有多少雷体的数字,(如图所示)。玩家用鼠标左键点开布雷区方块后开始计时,玩家可根据非雷方块中数字正确判断打开所有非雷方块后,游戏胜利结束,系统会自动打开所有雷方块,并显示胜利会话框。在游戏过程中,如果提前打开雷方块则立即失败,游戏结束(如图所示)。游戏计时为999秒,如果时间到了系统会自动结束游戏。图3、提前打开雷方块图4、打开Game按钮子菜单Game按钮单击事件鼠标左键单击Game按钮会打开如图所示的子程序选择会话框,玩家可选择种功能中的某一项来实现某种功能。种功能分别如下所示:BEGIN按钮开局(重新开始)PRIMARY按钮初级(级别)INTERMEDIATE中级(级别)HIGH按钮高级(级别)EXIT返回操作系统(退出应用程序)程序基本流程如下:开始调用鼠标程序(如果调用失败退出)创建并显示窗体布雷(初始化设置)游戏(玩家操作(打开非雷方块、提前打开雷方块游戏结束、Game事件(重新开始)判断胜利(胜利(显示胜利会话框)、Game事件(重新开始)Close按钮事件(退出应用程序返回操作系统)扫雷游戏程序源码清单如下:#include #include #include #include #define PRIMARY 1 /*初级标识符*/#define PRIMARYCOLUMN 9 /*初级布雷区列数*/#define PRIMARYROW 9 /*初级布雷区行数*/#define PRIMARYMINE 10 /*初级布雷区雷的数目*/#define INTERMEDIATE 2 /*中级标识符*/#define INTERMEDIATECOLUMN 16 /*中级布雷区列数*/#define INTERMEDIATEROW 16 /*中级布雷区行数*/#define INTERMEDIATEMINE 40 /*中级布雷区雷的数目*/#define HIGH 3 /*高级标识符*/#define HIGHCOLUMN 30 /*高级布雷区列数*/#define HIGHROW 16 /*高级布雷区行数*/#define HIGHMINE 99 /*高级布雷区雷的数目*/#define GAMEOVER 1 /*游戏结束标识符*/*定义鼠标数据类型*/typedef struct MOUSE int mx; /*鼠标x坐标值*/ int my; /*鼠标y坐标值*/ int mkey;/*鼠标按钮状态*/ char fillcolor1616;/*鼠标覆盖下的颜色*/ Mouseh;/*定义按钮数据类型*/typedef struct BUTTON int left; int top; int right; int down; char *caption;/*按钮标题标识*/ char table100;/*按钮目录标题标识*/ int fillcolor;/*按钮面颜色标识*/ int fontcolor;/*按钮面字体颜色标识*/ BUTTON;/*定义窗口数据类型*/typedef struct WINDOW int left; int top; int width; int height; int backcolor;/*窗体背景颜色*/ char *caption;/*窗体标题*/ BUTTON close;/*窗体关闭按钮*/ BUTTON game;/*游戏按钮*/ BUTTON help;/*帮助按钮*/ BUTTON settime;/*显示时间按钮*/ BUTTON minenumber;/*显示游戏区间雷体数目按钮*/ Hwnd;/*定义布雷区数据类型*/typedef struct MINEFIELD int left; int top; int right; int down; int status; /*是否有雷的标识状态符*/ int openst; /*雷区是否打开的标识符*/ int minenumbers;/*雷区周围有多少雷的标识符*/ MINEFIELD;/*-=-=-=-=-=-=-= 全局变量 -=-=-=-=-=-=-=-=-*/*鼠标屏幕绘码*/char MouseMask=0x00,0x00,0x40,0x00,0x60,0x00,0x70,0x00, 0x78,0x00,0x7c,0x00,0x7e,0x00,0x7f,0x00, 0x7f,0x80,0x7f,0xc0,0x6c,0x00,0x46,0x00, 0x06,0x00,0x03,0x00,0x01,0x80,0x00,0x00 ;/*鼠标屏幕绘码边框码*/char MouseMaskE=0xc0,0x00,0xa0,0x00,0x90,0x00,0x88,0x00, 0x84,0x00,0x82,0x00,0x81,0x00,0x80,0x80, 0x80,0x40,0x80,0x20,0x93,0xf0,0xa9,0x00, 0xc9,0x00,0x04,0x80,0x02,0x40,0x01,0xc0 ;int newx=0,newy=0;/*定义鼠标(x,y)坐标状态变量*/MINEFIELD MineHIGHROWHIGHCOLUMN;/*布雷区域大小数组*/Hwnd hWnd;/*窗口句柄*/Mouseh *hw;/*鼠标句柄*/int row;/*雷区行标识*/int column;/*雷区列标识*/int mine;/*雷区雷数标识*/int end;/*游戏结束标识*/int begin;/*游戏开始标识*/int mtime;/*游戏用时标识*/int active;/*当前活动窗体标识*/*Game按钮子菜单项目录*/static char *MENU5=BEGIN,PRIMARY,INTERMEDIATE,HIGH,EXIT;/*间接递归Select()函数*/void SelectLoop(int r,int c);/*= 鼠标模块源码 =*/*鼠标初始化函数*/int MouseInit(int Xmin,int Xmax,int Ymin,int Ymax) int retcode; union REGS regs; regs.x.ax=0; int86(51,®s,®s); retcode=regs.x.ax; if(retcode=0) return 0; regs.x.ax=7; regs.x.cx=Xmin; regs.x.dx=Xmax; int86(51,®s,®s); regs.x.ax=8; regs.x.cx=Ymin; regs.x.dx=Ymax; int86(51,®s,®s); return retcode;/*显示鼠标*/void ShowMouse(Mouseh *hw,int mousecolor) int i,j,k; for(i=0;i16;i+) for(j=0;jfillcolorij=getpixel(hw-mx+j,hw-my+i); for(j=0;j16;j+) for(i=0;i2;i+) for(k=0;kk) putpixel(hw-mx+i*8+k,hw-my+j,mousecolor); for(j=0;j16;j+) for(i=0;i2;i+) for(k=0;kk) putpixel(hw-mx+i*8+k,hw-my+j,0);/*鼠标屏幕恢复*/void HideMouse(Mouseh *hw) int i,j; for(i=0;i16;i+) for(j=0;jmx+j,hw-my+i,hw-fillcolorij);/*按目录设置按钮*/void SetButton1(BUTTON bt) setfillstyle(1,bt.fillcolor); bar(bt.left,bt.top,bt.right,bt.down); setcolor(bt.fontcolor); outtextxy(bt.left+6,bt.top+7,bt.table);/*读取鼠标位置和按钮状态*/void MouseRead(Mouseh *hd) union REGS r1; int dx,dy,ky; struct time t,t1; gettime(&t1); do gettime(&t); if(t1.ti_sect.ti_sec) t1.ti_sec=t.ti_sec; if(begin) if(t.ti_sec-t1.ti_sec=1) mtime+=1; sprintf(hWnd.settime.table,%d,mtime); SetButton1(hWnd.settime); t1.ti_sec=t.ti_sec; if(mtime=999) begin=0; end=GAMEOVER; break; r1.x.ax=3; int86(51,&r1,&r1); dx=r1.x.cx; dy=r1.x.dx; hd-mkey=r1.x.bx; if(hd-mkey) break; while(dx=newx&dy=newy); HideMouse(hd); hd-mx=r1.x.cx; hd-my=r1.x.dx; newx=hd-mx; newy=hd-my;/*= 绘制框体模块源码 =*/*绘制阴影在左边的框体*/void DrawBox1(int left,int top,int right,int down,int fillcolor) setfillstyle(1,fillcolor); bar(left,top,right,down); setcolor(15); line(right,top,right,down); line(left,down,right,down); setcolor(8); line(left,top,left,down); line(left,top,right,top);/*绘制阴影在右边的框体*/void DrawBox2(int left,int top,int right,int down,int fillcolor) setfillstyle(1,fillcolor); bar(left,top,right,down); setcolor(15); line(left,top,left,down); line(left,top,right,top); setcolor(8); line(right,top,right,down); line(left,down,right,down);/*绘制雷区3D框体*/void DrawBox(int left,int top,int right,int down,int fillcolor) setfillstyle(1,fillcolor); bar(left,top,right,down); setcolor(fillcolor); rectangle(left,top,right,down); setcolor(15); line(left+1,top+1,left+1,down-1); line(left+1,top+1,right-1,top+1); setcolor(8); line(right-1,top+1,right-1,down-1); line(left+1,down-1,right-1,down-1);/*= 按钮模块源码 =*/*按标题设置按钮*/void SetButton(BUTTON bt) setfillstyle(1,bt.fillcolor); bar(bt.left,bt.top,bt.right,bt.down); setcolor(bt.fontcolor); outtextxy(bt.left+3,bt.top+3,bt.caption);/*判断鼠标是否在按钮上*/int OnButton(BUTTON bt) int but=0; if(hw-mxbt.left&hw-mxmybt.top&hw-mymkey) break; SetButton(bt);/*= 绘制脸部源码 =*/*绘制笑脸*/void DrawFace1(int left,int top,int right,int down) int rx,ry; rx=left+(right-left)/2; ry=top+(down-top)/2; DrawBox2(left,top,right,down,7); setcolor(0); circle(rx,ry,8); setfillstyle(1,14); floodfill(rx,ry,0); line(rx-4,ry+3,rx-2,ry+5); line(rx+4,ry+3,rx+2,ry+5); line(rx-2,ry+5,rx+2,ry+5); setfillstyle(1,0); bar(rx-3,ry-2,rx-2,ry-1); bar(rx+3,ry-2,rx+2,ry-1);/*绘制苦瓜脸*/void DrawFace3(int left,int top,int right,int down) int rx,ry; rx=left+(right-left)/2; ry=top+(down-top)/2; DrawBox2(left,top,right,down,7); setcolor(0); circle(rx,ry,8); setfillstyle(1,14); floodfill(rx,ry,0); line(rx-4,ry-4,rx-2,ry-2); line(rx-2,ry-4,rx-4,ry-2); line(rx+4,ry-4,rx+2,ry-2); line(rx+2,ry-4,rx+4,ry-2); line(rx-4,ry+4,rx-2,ry+2); line(rx+4,ry+4,rx+2,ry+2); line(rx-2,ry+2,rx+2,ry+2);/*根据表情状态绘制动画表情脸谱*/void DrawFace2(int left,int top,int right,int down,int status) int rx,ry; rx=left+(right-left)/2; ry=top+(down-top)/2; DrawBox2(left,top,right,down,7); setcolor(0); circle(rx,ry,8); setfillstyle(1,14); floodfill(rx,ry,0); circle(rx,ry+3,2); setfillstyle(1,0); bar(rx-4,ry-4,rx-2,ry-2); bar(rx+4,ry-4,rx+2,ry-2); delay(50000); if(!status) DrawFace1(left,top,right,down); if(status) DrawFace3(left,top,right,down);/*= 窗体源码 =*/*按级别创建窗体*/Hwnd CreateWindow(int rank) int i,j; Hwnd hd; hd.top=20; if(rank=PRIMARY) hd.width=165; hd.left=(getmaxx()-hd.width)/2; hd.height=245; row=PRIMARYROW; column=PRIMARYCOLUMN; mine=PRIMARYMINE; active=PRIMARY; if(rank=INTERMEDIATE) hd.width=284; hd.left=(getmaxx()-hd.width)/2; hd.height=364; row=INTERMEDIATEROW; column=INTERMEDIATECOLUMN; mine=INTERMEDIATEMINE; active=INTERMEDIATE; if(rank=HIGH) hd.width=522; hd.left=(getmaxx()-hd.width)/2; hd.height=364; row=HIGHROW; column=HIGHCOLUMN; mine=HIGHMINE; active=HIGH; hd.backcolor=7; hd.caption=SaoLei; hd.close.left=hd.left+hd.width-46; hd.close.top=hd.top+2; hd.close.right=hd.close.left+43; hd.close.down=hd.close.top+12; hd.close.fillcolor=4; hd.close.fontcolor=15; hd.close.caption=Close; hd.game.left=hd.left+3; hd.game.top=hd.top+25; hd.game.right=hd.game.left+36; hd.game.down=hd.game.top+12; hd.game.fillcolor=7; hd.game.fontcolor=0; hd.game.caption=Game; hd.help.left=hd.left+53; hd.help.top=hd.top+25; hd.help.right=hd.help.left+36; hd.help.down=hd.help.top+12; hd.help.fillcolor=7; hd.help.fontcolor=0; hd.help.caption=Help; hd.settime.left=hd.left+hd.width-40; hd.settime.top=hd.top+55; hd.settime.right=hd.settime.left+30; hd.settime.down=hd.settime.top+20; hd.settime.fillcolor=8; hd.settime.fontcolor=12; sprintf(hd.settime.table,%d,000); hd.minenumber.left=hd.left+10; hd.minenumber.top=hd.top+55; hd.minenumber.right=hd.minenumber.left+30; hd.minenumber.down=hd.minenumber.top+20; hd.minenumber.fillcolor=8; hd.minenumber.fontcolor=12; sprintf(hd.minenumber.table,%d,mine); for(i=0;irow;i+) for(j=0;jcolumn;j+) Mineij.left=hd.left+6+j*17; Mineij.top=hd.top+86+i*17; Mineij.right=Mineij.left+17; Mineij.down=Mineij.top+17; return hd;/*显示窗体*/void ShowWindow(Hwnd hd) int i,j,left,top,right,down; cleardevice(); left=hd.left; top=hd.top; right=hd.left+hd.width; down=hd.top+hd.height; setfillstyle(1,1); bar(left,top,right,top+20); setfillstyle(1,7); bar(left,top+20,right,down); setcolor(7); line(left,top+17,right,top+17); setcolor(9); line(left,top+16,right,top+16); line(left,top+18,right,top+18); rectangle(left-1,top-1,right+1,down+1); setcolor(15); outtextxy(left+5,top+5,hd.caption); SetButton(hd.close); SetButton(hd.game); SetButton(hd.help); DrawBox2(left+2,top+47,right-2,down-2,7); DrawBox1(left+5,top+50,right-5,top+80,7); DrawBox1(left+5,top+85,right-5,down-5,7); SetButton1(hd.minenumber); SetButton1(hd.settime); DrawFace1(hd.left+hd.width/2-10,top+55,hd.left+hd.width/2+10,top+75); for(i=0;irow;i+) for(j=0;jcolumn;j+) DrawBox(Mineij.left,Mineij.top,Mineij.right,Mineij.down,7);/*= 雷区操作源码 =*/*绘制雷体*/void DrawMine(int left,int top,int right,int down) int rx,ry; rx=left+(right-left)/2; ry=top+(down-top)/2; setcolor(0); circle(rx,ry,4); setfillstyle(1,0); floodfill(rx,ry,0); setfillstyle(1,15); bar(rx-2,ry-2,rx-1,ry-1);/*打开雷区域*/void OpenMine(int left,int top,int right,int down,int fillcolor,int status) setfillstyle(1,fillcolor); bar(left,top,right,down); setcolor(7); rectangle(left,top,right,down); if(status) DrawMine(left,top,right,down);/*取得无雷区周围雷体的数目*/void GetMineNumber() int i,j,str,stc,numbers; for(i=0;irow;i+) for(j=0;j0) if(Minestr-1j.status) numbers+; if(stc0) if(Minestr-1stc-1.status) numbers+; if(stccolumn-1) if(Minestr-1stc+1.status) numbers+; if(str0) if(Minestr+1stc-1.status) numbers+; if(stc0) if(Minestrstc-1.status) numbers+; if(stccolumn-1) if(Minestrstc+1.status) numbers+; Mineij.minenumbers=numbers; /*随机安置雷体*/void SetMine() int i,j,k,str,stc; srand(unsigned) time(NULL); for(i=0;irow;i+) for(j=0;jcolumn;j+) Mineij.status=0; Mineij.openst=0; for(i=0;imine;i+) str=rand()%row; stc=rand()%column; if(!Minestrstc.status) Minestrstc.status=1; else for(j=0;jcolumn;j+) if(!Minestrj.status) Minestrj.status=1; break; else for(k=0;krow;k+) if(!Minekstc.status) Minekstc.status=1; break; break; GetMineNumber();/*绘制无雷区*/void DrawNotM(int r,int c) char cn10; OpenMine(Minerc.left,Minerc.top,Minerc.right-1,Minerc.down-1,8,Minerc.status); Minerc.openst=1; if(Minerc.minenumbers) sprintf(cn,%d,Minerc.minenumbers); if(Minerc.minenumbers0) t1-=1; if(ReturnSelect(t1,c) break; else SelectLoop(t1,c); t1=r; while(t10) tc-=1; if(ReturnSelect(r,tc) break; else Select(r,tc); tc=c; while(tccolumn-1) tc+=1; if(ReturnSelect(r,tc) break; else Select(r,tc); /*打开有雷区域*/void DisplayMine() int i,j; for(i=0;irow;i+) for(j=0;jcolumn;j+) if(Mineij.status&!

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论