游戏程序魔方.docx
《游戏程序魔方.docx》由会员分享,可在线阅读,更多相关《游戏程序魔方.docx(13页珍藏版)》请在冰豆网上搜索。
游戏程序魔方
计算机技术基础课程设计
C语言
设计报告
题目:
游戏程序----魔方
学院:
机电工程学院
专业:
工业工程
班级:
050109
姓名:
石万广
指导教师:
设计日期:
2007年1月5日
题目:
游戏程序—魔方
一、选题背景:
问题的提出:
大家小时候都玩过魔方,将它设计成一个小程序在C语言里,以便在游戏的过程中丰富我们对这门学科的了解!
二、设计思想:
魔方共有26块,其中6块位于每一面的中心位置,当魔方转动时它们是不动的;有8块位于顶角位置,各有三个不同颜色的小面;剩余12块各有二个不同颜色的小面。
用比较常见的函数来实现魔方程序制作,主要应用画图的方法来制作,并且通过光标来完成魔方的运转,其主要的思想是用光标完成对魔方颜色的改变从而获得胜利!
三、程序流程图
四、程序清单:
#include
#include
#include
#include
inta[3][3]={1,1,1,1,1,1,1,1,1};/*INITIALIZESIXPLANS*/
intb[3][3]={2,2,2,2,2,2,2,2,2};
intc[3][3]={4,4,4,4,4,4,4,4,4};
intd[3][3]={3,3,3,3,3,3,3,3,3};
inte[3][3]={5,5,5,5,5,5,5,5,5};
intf[3][3]={7,7,7,7,7,7,7,7,7};
inttop[3][3],front[3][3],side[3][3];
intposition=0,temp[3]={0,0,0},new[3][3];/*POSITIONASSIGNTHEARROWPOSITION*/
intsign;
main()
{
intl=60,x=50,y=50,key,count,input=0,errorcode;/*LMEANSTHELENTHOFTHESQUARE,XMEANSTHEINITAILPOSITIONOFX,YMEANSTHEINITIALPOSITIONOFY*/
inta1[3][3]={1,1,1,1,1,1,1,1,1};/*INITIALIZESIXPLANSFORBACKUP*/
intb1[3][3]={2,2,2,2,2,2,2,2,2};
intc1[3][3]={4,4,4,4,4,4,4,4,4};
intd1[3][3]={3,3,3,3,3,3,3,3,3};
inte1[3][3]={5,5,5,5,5,5,5,5,5};
intf1[3][3]={7,7,7,7,7,7,7,7,7};
intgraphdriver=VGA,graphmode=VGAHI;
initgraph(&graphdriver,&graphmode,"");
errorcode=graphresult();
if(errorcode!
=grOk)/*MEETERROR*/
{/*SHOWTHEERROE*/
printf("Graphicserror:
%s\n",grapherrormsg(errorcode));
printf("Pressanykeytoexit:
");
getch();
exit
(1);
}
drawing(x,y,l);/*DRAWINGTHEBIGSQUARE*/
drawing(x+400,y,l/2);/*DRAWINGTHESMALLSQUARE*/
do
{
sign=position%3;/*COPYDATATOSHOW*/
copy(&a,&top);
copy(&b,&front);
copy(&c,&side);
color(x,y,l);/*SHOWINGTHEBIGSQUARE*/
copy(&e,&top);
change2(&top,1);
copy(&f,&front);
change2(&front,2);
copy(&d,&side);
color(x+400,y,l/2);/*SHOWINGTHESMALLSQUARE*/
setcolor(14);
arrow(x,y,l);
key=bioskey(0);/*READKEYBORD*/
switch(key)
{
case283:
printf("Esc");/*IFINPUT_KEY="ESC"TOEXITTHEPROGRAM*/
gotoend;
case20480:
setcolor(0);/*IFINPUT_KEY="DOWN"TOCHANGETHEARROW*/
arrow(x,y,l);
if(position==8)
position=0;
else
position=position+1;
break;/*IFINPUT_KEY="UP"TOCHANGETHEARROW*/
case18432:
setcolor(0);
arrow(x,y,l);
if(position==0)
position=8;
else
position=position-1;
break;
case19712:
input=1;/*IFINPUT_KEY="RIGHT"TOMOVEFORWARD*/
break;
case19200:
input=3;/*IFINPUT_KEY="LEFT"TOMOVEREWARD*/
break;
case7181:
setcolor(0);/*IFINPUT_KEY="ENTER"TORELOAD*/
arrow(x,y,l);
copy(&a1,&a);
copy(&b1,&b);
copy(&c1,&c);
copy(&d1,&d);
copy(&e1,&e);
copy(&f1,&f);
position=0;
break;
case14624:
setcolor(0);/*IFINPUT_KEY="SPACE"TORANDOMMOVE*/
arrow(x,y,l);
randomize();
position=random(9);
input=1;
break;
}
if(input!
=0)/*USEPOSITIONTOJUDGEANDCHANGEDATA*/
{/*IFINPUT=0,CHANGECLOCKWISE,IFINPUT=3,CHANGEREVERSE-CLOCKWISE*/
for(count=0;count
{
if(position/3==0)/*IFNEEDTOCHANGETOP*/
{
change2(&b,1);
change2(&c,1);
change2(&d,3);
change2(&f,1);
change1(&b,&c);
change1(&c,&d);
change1(&d,&f);
change1(&f,&b);
change2(&b,3);
change2(&c,3);
change2(&d,1);
change2(&f,3);
temp[0]=0;temp[1]=0;temp[2]=0;
if(position%3==0)
change2(&a,1);
if(position%3==2)
change2(&e,1);
}
if(position/3==1)/*IFNEEDTOCHANGEFRONT*/
{
change1(&a,&b);
change1(&b,&e);
change1(&e,&d);
change1(&d,&a);
temp[0]=0;temp[1]=0;temp[2]=0;
if(position%3==0)
change2(&f,1);
if(position%3==2)
change2(&c,1);
}
if(position/3==2)/*IFNEEDTOCHANGESIDE*/
{
change2(&a,3);
change2(&e,1);
change2(&f,2);
change1(&a,&c);
change1(&c,&e);
change1(&e,&f);
change1(&f,&a);
change2(&a,1);
change2(&e,3);
change2(&f,2);
temp[0]=0;temp[1]=0;temp[2]=0;
if(position%3==0)
change2(&b,1);
if(position%3==2)
change2(&d,1);
}
}
input=0;
}
}
while
(1);
end:
clearviewport();/*CLEARTHESCREEN*/
}
drawing(intinital_x,intinital_y,intlenth)/*DRAWINGTHESTRUCTUREOFTHEPICTURE*/
{
setlinestyle(0,0,3);
line(inital_x+1.5*lenth,inital_y+0,inital_x+4.5*lenth,inital_y+0);
line(inital_x+1.0*lenth,inital_y+0.5*lenth,inital_x+4.0*lenth,inital_y+0.5*lenth);
line(inital_x+0.5*lenth,inital_y+1*lenth,inital_x+3.5*lenth,inital_y+1*lenth);
line(inital_x+0,inital_y+1.5*lenth,inital_x+3*lenth,inital_y+1.5*lenth);
line(inital_x+0,inital_y+2.5*lenth,inital_x+3*lenth,inital_y+2.5*lenth);
line(inital_x+0,inital_y+3.5*lenth,inital_x+3*lenth,inital_y+3.5*lenth);
line(inital_x+0,inital_y+4.5*lenth,inital_x+3*lenth,inital_y+4.5*lenth);
line(inital_x+0,inital_y+1.5*lenth,inital_x+0,inital_y+4.5*lenth);
line(inital_x+1.0*lenth,inital_y+1.5*lenth,inital_x+1.0*lenth,inital_y+4.5*lenth);
line(inital_x+2.0*lenth,inital_y+1.5*lenth,inital_x+2.0*lenth,inital_y+4.5*lenth);
line(inital_x+3.0*lenth,inital_y+1.5*lenth,inital_x+3.0*lenth,inital_y+4.5*lenth);
line(inital_x+3.5*lenth,inital_y+1.0*lenth,inital_x+3.5*lenth,inital_y+4.0*lenth);
line(inital_x+4.0*lenth,inital_y+0.5*lenth,inital_x+4.0*lenth,inital_y+3.5*lenth);
line(inital_x+4.5*lenth,inital_y+0,inital_x+4.5*lenth,inital_y+3.0*lenth);
line(inital_x+1.5*lenth,inital_y+0,inital_x+0,inital_y+1.5*lenth);
line(inital_x+2.5*lenth,inital_y+0,inital_x+1.0*lenth,inital_y+1.5*lenth);
line(inital_x+3.5*lenth,inital_y+0,inital_x+2.0*lenth,inital_y+1.5*lenth);
line(inital_x+4.5*lenth,inital_y+0,inital_x+3.0*lenth,inital_y+1.5*lenth);
line(inital_x+4.5*lenth,inital_y+1.0*lenth,inital_x+3.0*lenth,inital_y+2.5*lenth);
line(inital_x+4.5*lenth,inital_y+2.0*lenth,inital_x+3.0*lenth,inital_y+3.5*lenth);
line(inital_x+4.5*lenth,inital_y+3.0*lenth,inital_x+3.0*lenth,inital_y+4.5*lenth);
}
color(intinital_x,intinital_y,intlenth)/*FILLCOLORBYTHEDATA*/
{
inti,j;
intcolor_num;
intx1=inital_x+1.5*lenth;
inty1=inital_y+0.25*lenth;
intx2,y2;
for(i=0;i<3;i++)
{
x2=x1;
y2=y1;
for(j=0;j<3;j++)
{
color_num=top[i][j];
setfillstyle(1,color_num);
floodfill(x2,y2,WHITE);
x2=x2+lenth;
}
x1=x1-0.5*lenth;
y1=y1+0.5*lenth;
}
x1=inital_x+0.5*lenth;
y1=inital_y+2*lenth;
for(i=0;i<3;i++)
{
x2=x1;
y2=y1;
for(j=0;j<3;j++)
{
color_num=front[i][j];
setfillstyle(1,color_num);
floodfill(x2,y2,WHITE);
x2=x2+lenth;
}
y1=y1+lenth;
}
x1=inital_x+3.25*lenth;
y1=inital_y+2*lenth;
for(i=0;i<3;i++)
{
x2=x1;
y2=y1;
for(j=0;j<3;j++)
{
color_num=side[i][j];
setfillstyle(1,color_num);
floodfill(x2,y2,WHITE);
x2=x2+0.5*lenth;
y2=y2-0.5*lenth;
}
y1=y1+lenth;
}
}
arrow(intinital_x,intinital_y,intlenth)/*DRAWINGTHEARROWBYTHEPOSITION*/
{
if(position<=2)
{
line(inital_x-30,inital_y+2*lenth-3+position*lenth,inital_x-20,inital_y+2*lenth-3+position*lenth);
line(inital_x-30,inital_y+2*lenth+3+position*lenth,inital_x-20,inital_y+2*lenth+3+position*lenth);
line(inital_x-21,inital_y+2*lenth-6+position*lenth,inital_x-15,inital_y+2*lenth+position*lenth);
line(inital_x-21,inital_y+2*lenth+6+position*lenth,inital_x-15,inital_y+2*lenth+position*lenth);
}
if(position<=5&&position>2)
{
line(inital_x+0.5*lenth+(position-3)*lenth-3,inital_y+4.5*lenth+30,inital_x+0.5*lenth+(position-3)*lenth-3,inital_y+4.5*lenth+20);
line(inital_x+0.5*lenth+(position-3)*lenth+3,inital_y+4.5*lenth+30,inital_x+0.5*lenth+(position-3)*lenth+3,inital_y+4.5*lenth+20);
line(inital_x+0.5*lenth+(position-3)*lenth-6,inital_y+4.5*lenth+21,inital_x+0.5*lenth+(position-3)*lenth,inital_y+4.5*lenth+15);
line(inital_x+0.5*lenth+(position-3)*lenth+6,inital_y+4.5*lenth+21,inital_x+0.5*lenth+(position-3)*lenth,inital_y+4.5*lenth+15);
}
if(position>5)
{
line(inital_x+3.25*lenth+(position-6)*lenth*0.5-3+5,inital_y+4.5*lenth+30-(position-5)*0.5*lenth+20,inital_x+3.25*lenth+(position-6)*0.5*lenth-3+5,inital_y+4.5*lenth+20-(position-5)*0.5*lenth+20);
line(inital_x+3.25*lenth+(position-6)*lenth*0.5+3+5,inital_y+4.5*lenth+30-(position-5)*0.5*lenth+20,inital_x+3.25*lenth+(position-6)*0.5*lenth+3+5,inital_y+4.5*lenth+20-(position-5)*0.5*lenth+20);
line(inital_x+3.25*lenth+(position-6)*lenth*0.5-6+5,inital_y+4.5*lenth+21-(position-5)*0.5*lenth+20,inital_x+3.25*lenth+(position-6)*0.5*lenth+5,inital_y+4.5*lenth+15-(position-5)*0.5*lenth+20);
line(inital_x+3.25*lenth+(position-6)*lenth*0.5+6+5,inital_y+4.5*lenth+21-(position-5)*0.5*lenth+20,inital_x+3.25*lenth+(position-6)*0.5*lenth+5,inital_y+4.5*lenth+15-(position-5)*0.5*lenth+20);
}
}
copy(int*p,int*q)
{
inti,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
*q=*p;
p++;
q++;
}
}
}
change1(int*p,int*q)/*CONVERTTWOARRAY*/
{
inti,ling;
p=p+sign;
q=q+sign;
if(temp[0]==0)
{
for(i=0;i<3;i++)
{
temp[i]=*p;
p=p+3;
}
p=p-9;
}
for(i=0;i<3;i++)
{
ling=*q;
*q=temp[i];
temp[i]=ling;
p=p+3;
q=q+3;
}
}
change2(int*p,intx)/*CHANGETHEARRAYBYREVERSE-CLOCKWISE*/
{
inti,j,k;
for(k=0;k{
for(i=0;i<3;i++)
{
for(j=2;j>=0;j--)
{
new[j][i]=*p;
p=p+1;
}
}
p=p-9;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
*p=new[i][j];
p++;
}
}
p=p-9;
}
}
五、设计结果说明:
可以通过键盘对魔方进行旋转,但是由于程序设计还不够完善,不能与鼠标并用进行操作,有待改进。