C程序 移格子Word文档下载推荐.docx
- 文档编号:16348232
- 上传时间:2022-11-23
- 格式:DOCX
- 页数:13
- 大小:49.24KB
C程序 移格子Word文档下载推荐.docx
《C程序 移格子Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C程序 移格子Word文档下载推荐.docx(13页珍藏版)》请在冰豆网上搜索。
游戏的目的是将一个矩阵中的所有格子都由白色变成红色。
游戏中利用“上”,“下”,“左”,“右“键来控制游戏中的“目标格子”,用“回车键”来控制颜色的变化。
游戏中的目标格子是用一个带有标记的方框表示的,游戏中,可以使用回车键将以目标格子为中心和围绕它的所有格子的颜色都改变。
在游戏中,回车键每敲击一次,目标格子以及围绕它的格子的颜色都将改变。
游戏程序中用到了结构体定义以及使用,通过多重调用函数和结构体,完成了矩阵和移动光标的目的,运用循环和判断语句了实现游戏的可运行性。
、
三、程序流程图
若想退出请按Esc键。
流程图中按“上,下,左,右”键是一种并列关系,并无顺序。
四、程序清单:
#defineUp0x4800
#defineDown0x5000
#defineP0x1970
#defineR0x1372
#defineEnter0x1c0d
#defineEsc0x11b
#defineRight0x4d00
#defineLeft0x4b00
#include<
bios.h>
stdio.h>
dos.h>
graphics.h>
conio.h>
stdlib.h>
intLargeStartx;
intLargeStarty;
intSmallStartx;
intSmallStarty;
intarray=5;
intposition;
structrect2
{
intnowx;
intnowy;
intagox;
intagoy;
intsize;
charcolor;
}Large;
structrect2LargeRect;
charAttr[10*10];
typedefstructadd
intx;
inty;
intp;
}Add;
typedefstructrect1
}Small;
SmallSmallRect;
voidChargeColor(int);
voidInitData1();
voidInitData2();
voidDrawLargeRect(int,int,int,char);
voidDrawSmallRect(int,int,int,char);
voidInitPic();
intWin();
intJudge(int,int);
voidmain()
intkey,i,j;
intgdriver=DETECT,gmode;
/*registerbgidriver(VGA);
*/
initgraph(&
gdriver,&
gmode,"
"
);
setbkcolor(BLUE);
cleardevice();
InitData1();
InitData2();
InitPic();
do
{
key=bioskey(0);
LargeRect.agox=LargeRect.nowx;
LargeRect.agoy=LargeRect.nowy;
switch(key)
caseUp:
LargeRect.nowy-=LargeRect.size;
if(LargeRect.nowy<
LargeStarty){LargeRect.nowy+=LargeRect.size;
continue;
}
setwritemode
(1);
DrawLargeRect(LargeRect.agox,LargeRect.agoy,LargeRect.size,LargeRect.color);
DrawLargeRect(LargeRect.nowx,LargeRect.nowy,LargeRect.size,LargeRect.color);
setwritemode(0);
position-=array;
break;
}
caseDown:
LargeRect.nowy+=LargeRect.size;
if(LargeRect.nowy>
LargeStarty+LargeRect.size*(array-1)){LargeRect.nowy-=LargeRect.size;
position+=array;
caseLeft:
LargeRect.nowx-=LargeRect.size;
if(LargeRect.nowx<
LargeStartx){LargeRect.nowx+=LargeRect.size;
position-=1;
caseRight:
LargeRect.nowx+=LargeRect.size;
if(LargeRect.nowx>
LargeStartx+LargeRect.size*(array-1)){LargeRect.nowx-=LargeRect.size;
position+=1;
caseP:
setbkcolor(BLUE);
cleardevice();
if(array<
10)array++;
elsearray=5;
InitData2();
InitPic();
caseR:
caseEnter:
SmallRect.nowx=LargeRect.nowx+10;
SmallRect.nowy=LargeRect.nowy+10;
for(i=0;
i<
5;
i++)
ChargeColor(i);
caseEsc:
closegraph();
exit
(1);
if(Win())
outtextxy(100,50,"
CONGRATULATIONS!
sleep(5);
}while
(1);
voidInitData1()
array=5;
LargeStartx=50;
LargeStarty=50;
SmallStartx=LargeStartx+10;
SmallStarty=LargeStarty+10;
LargeRect.size=40;
LargeRect.color=RED;
SmallRect.size=20;
voidInitData2()
inti;
position=0;
LargeRect.nowx=LargeStartx;
LargeRect.nowy=LargeStarty;
for(i=0;
100;
Attr[i]='
W'
;
voidDrawSmallRect(intx,inty,intsize,charcolor)
setfillstyle(1,color);
bar(x,y,x+size,y+size);
voidDrawLargeRect(intx,inty,intsize,charcolor)
setcolor(color);
rectangle(x,y,x+size,y+size);
voidInitPic()
intj,i;
for(j=0;
j<
array;
j++)
DrawSmallRect(SmallStartx+i*40,SmallStarty+j*40,SmallRect.size,WHITE);
DrawLargeRect(LargeStartx,LargeStarty,LargeRect.size,LargeRect.color);
voidChargeColor(intDirection)
AddAdd;
switch(Direction)
{
case0:
Add.x=0;
Add.y=0;
Add.p=0;
case1:
Add.x=-40;
Add.p=-1;
case2:
Add.x=40;
Add.p=1;
case3:
Add.y=-40;
Add.p=-array;
case4:
Add.y=40;
Add.p=array;
if(Judge(Add.x+SmallRect.nowx,Add.y+SmallRect.nowy))
if(Attr[Add.p+position]=='
R'
)
Attr[Add.p+position]='
SmallRect.color=WHITE;
else
SmallRect.color=RED;
DrawSmallRect(SmallRect.nowx+Add.x,SmallRect.nowy+Add.y,SmallRect.size,SmallRect.color);
intWin()
array*array;
if(Attr[i]!
='
)return0;
return1;
intJudge(intx,inty)
if(x>
=SmallStartx&
&
x<
=(SmallStartx+(array-1)*40)&
y>
=SmallStarty&
(y<
=SmallStarty+(array-1)*40))
return1;
elsereturn0;
五、主要解决问题的方法及技术关键
1、主要问题及解决方法
目标格子的移动问题
目标格子的移动是通过利Switch()循环语句来实现的。
格子变色的问题
同样,运用Switch()语句来实现目标格子和其周围的格子的变色问题。
建立格子矩阵的问题
建立一个格子矩阵是通过调用库函数来实现的。
2、技术关键
格子矩阵的建立(库函数)
目标格子的移动以及格子变色(Switch语句);
六、设计结果说明
设计优点:
本程序优点在于大量使用了Switch()语句。
使得程序简单易读,便于操作和修改,另外,使得程序运行容易和可靠。
其次,该程序运用了大量库函数,保证了程序在调用时顺利进行。
设计不足:
设计中并没有考虑到时间的限制,游戏没有一个结束时间。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C程序 移格子 程序 格子