我这儿有c语言的自写俄罗斯方块,请指教:#include
#include
#include
#include
#include
#include
#include
#define ESC 0x011b
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define SPACE 0x3920
#define Y 0x1579
#define N 0x316e
#define clearkbd(); while(bioskey(1)) bioskey(0); /清空键盘缓冲队列/
void update();
void messagebox();
void process();
void initremove();
void initinfo();
void initbox();
void initposition();
void next_shape();
typedef struct shape /形状单一状态的记录/
{ int attr;
int co[8];
}shape;
typedef struct RE_AB /相对,绝对坐标记录/
{ int Rx,Ry;
int x1,x2,y1,y2;
}RE_AB;
RE_AB RA;
shape p[19]={ { RED,0,1,1,0,1,1,2,1 }, /数组中保证y最大的在最后,以便initposition使用/
{ RED,0,1,1,0,1,1,1,2 },
{ RED,0,0,1,0,2,0,1,1 },
{ RED,0,0,0,1,1,1,0,2 },
{ GREEN,0,0,1,0,2,0,3,0 },
{ GREEN,0,0,0,1,0,2,0,3 },
{ CYAN,0,0,0,1,1,0,1,1 },
{ BROWN,0,0,1,0,1,1,2,1 },
{ BROWN,1,0,0,1,1,1,0,2 },
{ BLUE,1,0,2,0,1,1,0,1 },
{ BLUE,0,0,0,1,1,1,1,2 },
{ MAGENTA,0,0,0,1,0,2,1,2 },
{ MAGENTA,2,0,0,1,1,1,2,1},
{ MAGENTA,0,0,1,0,1,1,1,2 },
{ MAGENTA,0,0,0,1,1,0,2,0 },
{ YELLOW,0,2,1,0,1,1,1,2 },
{ YELLOW,0,0,1,0,2,0,2,1 },
{ YELLOW,1,0,0,0,0,1,0,2},
{ YELLOW,0,0,0,1,1,1,2,1 },
};
int nback,nleft,nright,r_f[12][22],rs1,rs2,xcors,xcorb,ycors,ycorb;
/检查方快有没有左,右,下接触,游戏区内所有格子有无颜色记录数组,rs1形状记录,rs2为提示框用,记录小格子在游戏区中的位置,按键存储/
void interrupt (oldint)(); /系统定时中断/
int count_down=0,count_other=0; /中断记时/
void interrupt newint() /设置新的中断程序/
{ count_down++;
count_other++;
oldint();
}
void intenable() /设置中断向量表,启动新的中断程序/
{ oldint=getvect(0x1c);
disable();
setvect(0x1c,newint);
enable();
}
void intrestore() /恢复中断向量/
{ disable();
setvect(0x1c,oldint);
enable();
}
void HZ12(int x0,int y0,int w,int color,char s) /根据字模,在dos下显示汉字/
/横坐标,纵坐标,字间隔,汉字颜色,汉字字符串/
{ FILE fp;
register char buffer[24];
register char str[2];
unsigned long fpos;/fpos为最终偏移动量/
register int i,j,k;
fp=fopen(hzk12,r);/打开1212汉字苦/
while(s)/一直到字符串结束为止/
{
if(s<0)/汉字输出/
{ str[0]=(s)-0xa0;
str[1]=(s+1)-0xa0;
fpos=((str[0]-1)94+(str[1]-1))24L;/计算汉字在hzk12的偏移量/
fseek(fp,fpos,SEEK_SET);/指针移动到当前位置/
fread(buffer,24,1,fp);/读取一个汉字到数组中/
for(i=0;i<12;i++)/12行/
for(j=0;j<2;j++)/两个字节/
for(k=0;k<8;k++)/8位/
if (((buffer[i2+j]>>(7-k))&0x1)!=NULL)/是一就画点/
putpixel(x0+8j+k,y0+i,color);
s+=2;/一个汉字占两个字节,现在将指针移动两个字节/
x0+=w;/显示坐标也按照间隔移动/
}
else/显示非汉字字符/
{ settextstyle(0,0,1);
setcolor(color);
str[0]=s;str[1]=0;
outtextxy(x0,y0+3,str);/显示单个字符/
x0+=w-7;/显示单个字符后的x坐标变化/
s++;/指针移动到下一个字节/
}
}
fclose(fp);
}
void translation() /把相对坐标解释为绝对坐标/
{ if(RARx==1)
{ RAx1=1; RAx2=16; }
else
{ RAx1=16(RARx-1); RAx2=16RARx; }
if(RARy==1)
{ RAy1=1; RAy2=16; }
else
{ RAy1=16(RARy-1); RAy2=16RARy; }
}
int check_b() /检查是否到达低部/
{ int x,y,i,zf=0; /zf为是否有颜色填充记录/
for(i=0;i<7;i++,i++)
{ x=RARx+p[rs1]co[i];
y=RARy+p[rs1]co[i+1];
if(y>=6)
zf+=r_f[x-15][y-6+1];
}
if(zf==0)
return 1;
else
return 0;
}
int finish()
{ int tfull=0,i; /判断顶层空间是否有填充/
for(i=1;i<11;i++)
tfull+=r_f[i][1];
if(tfull!=0)
return 1; /告诉judge()可以结束了/
}
int check_l() /检查形状是否与左接触/
{ int x,y,i,zf=0;
for(i=0;i<7;i++,i++)
{ x=RARx+p[rs1]co[i];
y=RARy+p[rs1]co[i+1];
if(y>6)
zf+=r_f[x-15-1][y-6];
if(y<=6&&x==16)
zf+=1;
}
if(zf==0)
return 1;
else
return 0;
}
int check_r() /检查形状是否与右接触/
{ /zf为是否有颜色填充记录/
int x,y,i,zf=0; /zf为是否有颜色填充记录/
for(i=0;i<7;i++,i++)
{
x=RARx+p[rs1]co[i];
y=RARy+p[rs1]co[i+1];
if(y>6)
zf+=r_f[x-15+1][y-6];
if(y<=6&&x==25)
zf+=1;
}
if(zf==0)
return 1;
else
return 0;
}
void check_touch()
{ nback=check_b();
nleft=check_l();
nright=check_r();
}
void draw(int cb) /画形状,cb=1以填充色画形状,cb=2以背景色画形状,cb=3以白色画形状/
{ int i,recordx=RARx,recordy=RARy;
for(i=0;i<7;i++,i++)
{ RARx+=p[rs1]co[i];
RARy+=p[rs1]co[i+1];
if(RARy<=6)
{ RARx=recordx;
RARy=recordy;
continue;
}
translation();
if(cb==1)
setfillstyle(1,p[rs1]attr);
else
if(cb==2)
setfillstyle(1,BLACK);
else
if(cb==3)
{ setfillstyle(1,WHITE);
r_f[RARx-15][RARy-6]=1; /置对应数组标记元素/
}
bar(RAx1+1,RAy1+1,RAx2-1,RAy2-1);
RARx=recordx;
RARy=recordy;
}
}
void mov(int key) /向下,左,右移动方块/
{ draw(2);
if(key==LEFT&&nleft)
RARx--;
else
if(key==RIGHT&&nright)
RARx++;
else
RARy++;
nback=check_b();
if(nback) /判断形状有没有到达底部,有就将其颜色变为白色/
draw(1);
else
draw(3);
}
void change() /变换形状/
{ int status=rs1,buffer,i,x,y,zf=0;
if(p[rs1]attr==p[rs1+1]attr)
rs1++;
else
while(p[rs1]attr==p[rs1-1]attr)
rs1--;
for(i=0;i<7;i++,i++) /检查变化形状后是否与已存形状发生冲突/
{ x=RARx+p[rs1]co[i];
y=RARy+p[rs1]co[i+1];
if(y>6)
zf+=r_f[x-15][y-6];
}
if(zf!=0)
rs1=status;
buffer=rs1;
rs1=status;
status=buffer;
draw(2);
buffer=rs1;
rs1=status;
status=buffer;
nback=check_b(); /判断变化后的形状是不是到达了低部,这个检查是十分必要的/
if(nback)
draw(1);
else
draw(3);
}
void accelerate()
{ if(count_down>=1)
{ check_touch(); /消除上一步动作对方块状态的影响/
count_down=0;
if(nback) /0表示到达底部,1表示没有到达/
mov(DOWN);
}
}
void drawbox() /画方块所在方框/
{ int xcor,ycor;
for(xcor=xcors;xcor<=xcorb;xcor++)
for(ycor=ycors;ycor<=ycorb;ycor++)
{ if(xcor==xcors||xcor==xcorb||ycor==ycors||ycor==ycorb)
{ RARx=xcor;
RARy=ycor;
translation();
setfillstyle(1,DARKGRAY);
bar(RAx1+1,RAy1+1,RAx2-1,RAy2-1);
}
}
}
void erasure(int k)
{ int i,j,recordx=RARx,recordy=RARy;
{ j=k-1;
for(;j>0;j--)
{ for(i=1;i<11;i++)
{ r_f[i][j+1]=r_f[i][j];
RARx=i+15;
RARy=j+1+6;
translation();
if(r_f[i][j+1]==1)
setfillstyle(1,WHITE);
else
setfillstyle(1,BLACK);
bar(RAx1+1,RAy1+1,RAx2-1,RAy2-1);
RARx=recordx;
RARy=recordy;
}
}
}
}
void pause()
{ HZ12(450,400,15,BLACK,正常);
HZ12(450,400,15,GREEN,暂停);
for(;;)
if(bioskey(1)&&bioskey(0)==SPACE)
{ clearkbd();
HZ12(450,400,15,BLACK,暂停);
HZ12(450,400,15,RED,正常);
return;
}
}
void judge()
{ int i,j,full=0; /full等于10说明某一行满,该消除了/
if(finish()) /判断游戏是否该结束了/
messagebox(); /win编程里有这个函数/
for(j=1;j<21;j++) /判断某一行是否满了/
{ for(i=1;i<11;i++)
full+=r_f[i][j];
if(full==10)
erasure(j); /消除这行/
full=0;
}
}
void update() /使程序可以重新运行/
{ cleardevice();
setbkcolor(BLACK);
initinfo(); /提示信息初始化/
initbox(); /游戏框架初始化/
srand((unsigned)time(NULL)); /随机器函数的初始化/
rs1=random(19);
rs2=random(19);
next_shape();
initposition(); /方块最开始的出现位置/
initremove(); /记录每个方格有无颜色填充数组初始化/
HZ12(450,400,15,RED,正常);
process();
}
void EXIT()
{ closegraph();
intrestore(); /恢复中断向量/
exit(0);
}
void initremove()
{ int i,j;
for(i=0;i<12;i++)
for(j=0;j<22;j++)
if(i==0||i==11||j==0||j==21)
r_f[i][j]=1;
else
r_f[i][j]=0;
}
void initinfo()
{ char aStr[2];
setcolor(RED);
outtextxy(450,100,This game's writer is:);
HZ12(450,140,15,RED,该程序作者:NULL);
outtextxy(525,110,NULL);
outtextxy(450,180,FUNCTION FOR KEYS:);
outtextxy(450,200,UP:change the shape);
outtextxy(450,210,DOWN:accelerate);
outtextxy(450,220,LEFT:move left);
outtextxy(450,230,RIGHT:move right);
outtextxy(450,240,ESC:exit this game);
outtextxy(450,250,SPACE:pause);
HZ12(450,260,20,RED,上:);
HZ12(450,280,20,RED,下:);
HZ12(450,300,20,RED,左:);
HZ12(450,320,20,RED,右:);
HZ12(450,340,20,RED,ESC:退出);
HZ12(450,360,15,RED,空格: 暂停/开始);
HZ12(450,380,15,RED,目前状态:);
HZ12(20,200,15,RED,下一个形状);
aStr[0]=24;
aStr[1]=0;
aStr[6]=0;
HZ12(480,260,12,GREEN,aStr);
HZ12(500,260,12,GREEN,( 变形 ));
aStr[0]=25;
aStr[1]=0;
HZ12(480,280,12,GREEN,aStr);
HZ12(500,280,12,GREEN,( 加速 ));
aStr[0]=27;
aStr[1]=0;
HZ12(480,300,12,GREEN,aStr);
HZ12(500,300,12,GREEN,向左);
aStr[0]=26;
aStr[1]=0;
HZ12(480,320,12,GREEN,aStr);
HZ12(500,320,12,GREEN,向右);
}
void messagebox()
{ int key;
setcolor(GREEN);
setfillstyle(1,DARKGRAY);
rectangle(220,200,420,300);
bar(221,201,419,299);
HZ12(280,210,15,GREEN,GAME OVER);
HZ12(275,230,15,GREEN,重新游戏: Y);
HZ12(275,270,15,GREEN,退出游戏: N);
HZ12(450,400,15,BLACK,正常);
HZ12(450,400,15,GREEN,GAME OVER);
for(;;)
if(bioskey(1))
{ key=bioskey(0);
if(key==Y)
{ clearkbd();
update();
}
else
if(key==N)
{ clearkbd();
EXIT();
}
else
clearkbd();
}
}
void initbox()
{ xcors=15; /画游戏框/
xcorb=26;
ycors=6;
ycorb=27;
drawbox();
xcors=2; /画提示框/
xcorb=7;
ycors=6;
ycorb=11;
drawbox();
}
void initposition()
{ RARx=18;
RARy=6-p[rs1]co[7];;
RAx1=0;
RAx2=0;
RAy1=0;
RAy2=0;
}
void next_shape() /画下一形状提示框/
{ int recordx=RARx,recordy=RARy,buffer;
RARx=3;
RARy=7;
draw(2);
buffer=rs1;
rs1=rs2;
rs2=buffer;
draw(1);
RARx=recordx;
RARy=recordy;
buffer=rs1;
rs1=rs2;
rs2=buffer;
}
void process() /游戏过程/
{ for(;;)
{ check_touch();
if(!nback)
{ rs1=rs2;
rs2=random(19); /产生另一种方块的码数/
initposition();
judge(); /判断某一行是否满了和这个游戏是否可以结束了/
draw(1);
next_shape();
}
if(count_other>=1)
{ count_other=0;
if(bioskey(1)) /对按键的处理/
{ int key=bioskey(0);
clearkbd(); /清除键盘缓冲队列/
if(key==ESC)
EXIT();
if(key==LEFT&&nleft&&nback)
mov(LEFT);
if(key==RIGHT&&nright&&nback)
mov(RIGHT);
if(key==UP&&nback)
change();
if(key==SPACE)
pause();
if(key==DOWN)
accelerate();
}
}
if(count_down>=4)
{ check_touch(); /消除上一步动作对方块状态的影响/
count_down=0;
if(nback) /0表示到达底部,1表示没有到达/
mov(DOWN);
}
}/for/
}
main()
{ int gdriver=DETECT,gmode=0;
initgraph(&gdriver,&gmode,d:turboc); /启动图形与中断部分/
intenable();
update();
}
用c语言编写一个五子棋吧,不怎么难,给你程序,自己参考一下
/334 源程序/
#include "graphicsh" /图形系统头文件/
#define LEFT 0x4b00 /光标左键值/
#define RIGHT 0x4d00 /光标右键值/
#define DOWN 0x5000 /光标下键值/
#define UP 0x4800 /光标上键值/
#define ESC 0x011b / ESC键值/
#define ENTER 0x1c0d / 回车键值/
int a[8][8]={0},key,score1,score2;/具体分数以及按键与存放棋子的变量/
char playone[3],playtwo[3];/两个人的得分转换成字符串输出/
void playtoplay(void);/人人对战函数/
void DrawQp(void);/画棋盘函数/
void SetPlayColor(int x);/设置棋子第一次的颜色/
void MoveColor(int x,int y);/恢复原来棋盘状态/
int QpChange(int x,int y,int z);/判断棋盘的变化/
void DoScore(void);/处理分数/
void PrintScore(int n);/输出成绩/
void playWin(void);/输出胜利者信息/
/主函数/
void main(void)
{
int gd=DETECT,gr;
initgraph(&gd,&gr,"c:\\tc"); /初始化图形系统/
DrawQp();/画棋盘/
playtoplay();/人人对战/
getch();
closegraph();/关闭图形系统/
}
void DrawQp()/画棋盘/
{
int i,j;
score1=score2=0;/棋手一开始得分都为0/
setbkcolor(BLUE);
for(i=100;i<=420;i+=40)
{
line(100,i,420,i);/画水平线/
line(i,100,i,420); /画垂直线/
}
setcolor(0);/取消圆周围的一圈东西/
setfillstyle(SOLID_FILL,15);/白色实体填充模式/
fillellipse(500,200,15,15); /在显示得分的位置画棋/
setfillstyle(SOLID_FILL,8); /黑色实体填充模式/
fillellipse(500,300,15,15);
a[3][3]=a[4][4]=1;/初始两个黑棋/
a[3][4]=a[4][3]=2;/初始两个白棋/
setfillstyle(SOLID_FILL,WHITE);
fillellipse(120+340,120+340,15,15);
fillellipse(120+440,120+440,15,15);
setfillstyle(SOLID_FILL,8);
fillellipse(120+340,120+440,15,15);
fillellipse(120+440,120+340,15,15);
score1=score2=2; /有棋后改变分数/
DoScore();/输出开始分数/
}
void playtoplay()/人人对战/
{
int x,y,t=1,i,j,cc=0;
while(1)/换棋手走棋/
{
x=120,y=80;/每次棋子一开始出来的坐标,x为行坐标,y为列坐标/
while(1) /具体一个棋手走棋的过程/
{
PrintScore(1);/输出棋手1的成绩/
PrintScore(2);/输出棋手2的成绩/
SetPlayColor(t);/t变量是用来判断棋手所执棋子的颜色/
fillellipse(x,y,15,15);
key=bioskey(0);/接收按键/
if(key==ESC)/跳出游戏/
break;
else
if(key==ENTER)/如果按键确定就可以跳出循环/
{
if(y!=80&&a[(x-120)/40][(y-120)/40]!=1
&&a[(x-120)/40][(y-120)/40]!=2)/如果落子位置没有棋子/
{
if(t%2==1)/如果是棋手1移动/
a[(x-120)/40][(y-120)/40]=1;
else/否则棋手2移动/
a[(x-120)/40][(y-120)/40]=2;
if(!QpChange(x,y,t))/落子后判断棋盘的变化/
{
a[(x-120)/40][(y-120)/40]=0;/恢复空格状态/
cc++;/开始统计尝试次数/
if(cc>=64-score1-score2) /如果尝试超过空格数则停步/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
break;
}
else
continue;/如果按键无效/
}
DoScore();/分数的改变/
break;/棋盘变化了,则轮对方走棋/
}
else/已经有棋子就继续按键/
continue;
}
else /四个方向按键的判断/
if(key==LEFT&&x>120)/左方向键/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x-=40;
fillellipse(x,y,15,15);
}
else
if(key==RIGHT&&x<400&&y>80)/右方向键/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x+=40;
fillellipse(x,y,15,15);
}
else
if(key==UP&&y>120)/上方向键/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y-=40;
fillellipse(x,y,15,15);
}
else
if(key==DOWN&&y<400)/下方向键/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y+=40;
fillellipse(x,y,15,15);
}
}
if(key==ESC)/结束游戏/
break;
if((score1+score2)==64||score1==0||score2==0)/格子已经占满或一方棋子为0判断胜负/
{
playWin();/输出最后结果/
break;
}
t=t%2+1; /一方走后,改变棋子颜色即轮对方走/
cc=0; /计数值恢复为0/
} /endwhile/
}
void SetPlayColor(int t)/设置棋子颜色/
{
if(t%2==1)
setfillstyle(SOLID_FILL,15);/白色/
else
setfillstyle(SOLID_FILL,8);/灰色/
}
void MoveColor(int x,int y)/走了一步后恢复原来格子的状态/
{
if(y<100)/如果是从起点出发就恢复蓝色/
setfillstyle(SOLID_FILL,BLUE);
else/其他情况如果是1就恢复白色棋子,2恢复黑色棋子,或恢复蓝色棋盘/
switch(a[(x-120)/40][(y-120)/40])
{
case 1:
setfillstyle(SOLID_FILL,15);break; /白色/
case 2:
setfillstyle(SOLID_FILL,8);break; /黑色/
default:
setfillstyle(SOLID_FILL,BLUE); /蓝色/
}
}
int QpChange(int x,int y,int t)/判断棋盘的变化/
{
int i,j,k,kk,ii,jj,yes;
yes=0;
i=(x-120)/40; /计算数组元素的行下标/
j=(y-120)/40; /计算数组元素的列下标/
SetPlayColor(t);/设置棋子变化的颜色/
/开始往8个方向判断变化/
if(j<6)/往右边/
{
for(k=j+1;k<8;k++)
if(a[i][k]==a[i][j]||a[i][k]==0)/遇到自己的棋子或空格结束/
break;
if(a[i][k]!=0&&k<8)
{
for(kk=j+1;kk<k&&k<8;kk++)/判断右边/
{
a[i][kk]=a[i][j]; /改变棋子颜色/
fillellipse(120+i40,120+kk40,15,15);
}
if(kk!=j+1) /条件成立则有棋子改变过颜色/
yes=1;
}
}
if(j>1)/判断左边/
{
for(k=j-1;k>=0;k--)
if(a[i][k]==a[i][j]||!a[i][k])
break;
if(a[i][k]!=0&&k>=0)
{
for(kk=j-1;kk>k&&k>=0;kk--)
{
a[i][kk]=a[i][j];
fillellipse(120+i40,120+kk40,15,15);
}
if(kk!=j-1)
yes=1;
}
}
if(i<6)/判断下边/
{
for(k=i+1;k<8;k++)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0&&k<8)
{
for(kk=i+1;kk<k&&k<8;kk++)
{
a[kk][j]=a[i][j];
fillellipse(120+kk40,120+j40,15,15);
}
if(kk!=i+1)
yes=1;
}
}
if(i>1)/判断上边/
{
for(k=i-1;k>=0;k--)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0&&k>=0)
{
for(kk=i-1;kk>k&&k>=0;kk--)
{
a[kk][j]=a[i][j];
fillellipse(120+kk40,120+j40,15,15);
}
if(kk!=i-1)
yes=1;
}
}
if(i>1&&j<6)/右上/
{
for(k=i-1,kk=j+1;k>=0&&kk<8;k--,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]&&k>=0&&kk<8)
{
for(ii=i-1,jj=j+1;ii>k&&k>=0;ii--,jj++)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii40,120+jj40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i<6&&j>1)/左下/
{
for(k=i+1,kk=j-1;k<8&&kk>=0;k++,kk--)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k<8&&kk>=0)
{
for(ii=i+1,jj=j-1;ii<k&&k<8;ii++,jj--)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii40,120+jj40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
if(i>1&&j>1)/左上/
{
for(k=i-1,kk=j-1;k>=0&&kk>=0;k--,kk--)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k>=0&&kk>=0)
{
for(ii=i-1,jj=j-1;ii>k&&k>=0;ii--,jj--)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii40,120+jj40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i<6&&j<6)/ 右下/
{
for(k=i+1,kk=j+1;kk<8&&kk<8;k++,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&kk<8&&k<8)
{
for(ii=i+1,jj=j+1;ii<k&&k<8;ii++,jj++)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii40,120+jj40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
return yes;/返回是否改变过棋子颜色的标记/
}
void DoScore()/处理分数/
{
int i,j;
score1=score2=0;/重新开始计分数/
for(i=0;i<8;i++)
for(j=0;j<8;j++)
if(a[i][j]==1)/分别统计两个人的分数/
score1++;
else
if(a[i][j]==2)
score2++;
}
void PrintScore(int playnum)/输出成绩/
{
if(playnum==1)/清除以前的成绩/
{
setfillstyle(SOLID_FILL,BLUE);
bar(550,100,640,400);
}
setcolor(RED);
settextstyle(0,0,4);/设置文本输出样式/
if(playnum==1)/判断输出哪个棋手的分,在不同的位置输出/
{
sprintf(playone,"%d",score1);
outtextxy(550,200,playone);
}
else
{
sprintf(playtwo,"%d",score2);
outtextxy(550,300,playtwo);
}
setcolor(0);
}
void playWin()/输出最后的胜利者结果/
{
settextstyle(0,0,4);
setcolor(12);
if(score2>score1)/开始判断最后的结果/
outtextxy(100,50,"black win!");
else
if(score2<score1)
outtextxy(100,50,"white win!");
else
outtextxy(60,50,"you all win!");
}
一、数学知识:
长方形的面积S=ab
长方形周长L=2(a+b)
其中a b分别为长方形的宽和高。
二、算法分析:
长方形面积及周长均依赖于宽和高,所以先要输入宽高值,然后根据公式计算,输出结果即可。
三、参考代码:
#include <stdioh>void main()
{
double a,b;
double L,S;
scanf("%lf%lf",&a,&b);//输入宽和高。
L=2(a+b);//计算周长。
S=ab;//计算面积。
printf("面积=%lf, 周长=%lf\n", S, L);//输出结果。
}
四、注意事项:
因为没有限制输入为整型,所以使用浮点型用来存储各项值。输入输出要用%lf。
C语言是计算机专业都要学习的一门基础学科。一般来说,是比较枯燥的那么,我们能不能通过编一些小游戏来提高它的趣味性呢这样学习程序设计,就不会是一件艰苦 ,枯燥的事,它变得象电脑游戏一样充满好奇,富有乐趣。
例如2048这款游戏:
方法/步骤:
#include<stdioh>
#include<stdlibh>
#include<timeh>
#include<conioh>
#include<windowsh>
#define SIZE 4
static int score=0;
void putn(int n[][SIZE]);
void getn(int n[][SIZE]);
int isempty(int n[][SIZE]);
int isfull(int n[][SIZE]);
void math(int n[][SIZE],char c);
void tow(int n[][SIZE]);
void toa(int n[][SIZE]);
void tos(int n[][SIZE]);
void tod(int n[][SIZE]);
//主函数
int main()
{
int i,j;
int n[SIZE][SIZE];
char c=' ';
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
n[i][j]=0;
}
}
printf( "\n"
" 2048(%dX%d) \n"
" control:W/A/S/D \n"
"press any key to begin \n"
"\n",SIZE,SIZE);
getch();
system("cls");
//n[0][1]=2048;
//n[0][3]=2048;
while(1)
{
if(isempty(n))
getn(n);
putn(n);
if(!isempty(n)&&isfull(n))
break;
sleep(200);
c=getch();
while(c!='w'&&c!='a'&&c!='s'&&c!='d')
c=getch();
math(n,c);
system("cls");
}
printf(" Game Over!\n",score);
return 0;
}
//函数
void putn(int n[][SIZE])
{
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
printf("| ");
printf("|\n");
for(j=0;j<SIZE;j++)
{
if(n[i][j]==0)
printf("| ");
else
printf("|%4d ",n[i][j]);
}
printf("|\n");
for(j=0;j<SIZE;j++)
printf("|_____");
printf("|\n");
}
printf("score: %d",score);
}
void getn(int n[][SIZE])
{
int a,b;
a=rand()%SIZE;
b=rand()%SIZE;
while(n[a][b]!=0)
{
a=rand()%SIZE;
b=rand()%SIZE;
}
n[a][b]=2;
}
int isempty(int n[][SIZE])
{
int i,j,count=0;
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
if(n[i][j]==0)
count++;
return count;
}
int isfull(int n[][SIZE])
{
int i,j,count=0;
for(i=0;i<SIZE;i++)
{
for(j=1;j<SIZE-1;j++)
{
if(n[i][j]==n[i][j+1]||n[i][j]==n[i][j-1])
count++;
}
}
for(j=0;j<SIZE;j++)
{
for(i=1;i<SIZE-1;i++)
{
if(n[i][j]==n[i+1][j]||n[i][j]==n[i-1][j])
count++;
}
}
return count>00:1;
}
void math(int n[][SIZE],char c)
{
switch(c)
{
case 'w':tow(n);break;
case 'a':toa(n);break;
case 's':tos(n);break;
case 'd':tod(n);break;
default :;
}
}
void tow(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(j=0;j<SIZE;j++)
{
for(a=0;a<SIZE;a++)
{
for(i=0;i<SIZE-1;i++)
{
if(n[i][j]==0)
{
n[i][j]=n[i+1][j];
n[i+1][j]=0;
}
}
}
}
for(j=0;j<SIZE;j++)
{
for(a=0,i=0;i<SIZE;i++)
{
if(n[i][j]!=n[i+1][j]&&n[i][j]!=0||n[i][j]==2048)
{
m[a++]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i+1][j])
{
m[a++]=n[i][j]+n[i+1][j];
score+=m[a-1];
n[i][j]=0,n[i+1][j]=0;
}
}
for(i=0;i<SIZE;i++)
{
n[i][j]=m[i];
m[i]=0;
}
}
}
void toa(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(i=0;i<SIZE;i++)
{
for(a=0;a<SIZE;a++)
{
for(j=0;j<SIZE-1;j++)
{
if(n[i][j]==0)
{
n[i][j]=n[i][j+1];
n[i][j+1]=0;
}
}
}
}
for(i=0;i<SIZE;i++)
{
for(a=0,j=0;j<SIZE;j++)
{
if(n[i][j]!=n[i][j+1]&&n[i][j]!=0||n[i][j]==2048)
{
m[a++]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i][j+1])
{
m[a++]=n[i][j]+n[i][j+1];
score+=m[a-1];
n[i][j]=0,n[i][j+1]=0;
}
}
for(j=0;j<SIZE;j++)
{
n[i][j]=m[j];
m[j]=0;
}
}
}
void tos(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(j=SIZE-1;j>=0;j--)
{
for(a=SIZE-1;a>=0;a--)
{
for(i=SIZE-1;i>0;i--)
{
if(n[i][j]==0)
{
n[i][j]=n[i-1][j];
n[i-1][j]=0;
}
}
}
}
for(j=SIZE-1;j>=0;j--)
{
for(a=SIZE-1,i=SIZE-1;i>=0;i--)
{
if(n[i][j]!=n[i-1][j]&&n[i][j]!=0||n[i][j]==2048)
{
m[a--]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i-1][j])
{
m[a--]=n[i][j]+n[i-1][j];
score+=m[a+1];
n[i][j]=0,n[i-1][j]=0;
}
}
for(i=SIZE-1;i>=0;i--)
{
n[i][j]=m[i];
m[i]=0;
}
}
}
void tod(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(i=SIZE-1;i>=0;i--)
{
for(a=SIZE-1;a>=0;a--)
{
for(j=SIZE-1;j>0;j--)
{
if(n[i][j]==0)
{
n[i][j]=n[i][j-1];
n[i][j-1]=0;
}
}
}
}
for(i=SIZE-1;i>=0;i--)
{
for(a=SIZE-1,j=SIZE-1;j>=0;j--)
{
if(n[i][j]!=n[i][j-1]&&n[i][j]!=0||n[i][j]==2048)
{
m[a--]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i][j-1])
{
m[a--]=n[i][j]+n[i][j-1];
score+=m[a+1];
n[i][j]=0,n[i][j-1]=0;
}
}
for(j=SIZE-1;j>=0;j--)
{
n[i][j]=m[j];
m[j]=0;
}
}
}
C语言做游戏
点灯游戏(把灯都打开变黄):
#include <conioh>
#include <stdioh>
#include <stdlibh>
#include <timeh>
int j[8][6][6]={
0,0,0,0,0,0,0,1,1,0,1,1,0,1,0,1,0,1,0,0,1,1,1,0,0,1,0,1,0,1,0,1,1,0,1,1,
0,0,0,0,0,0,0,0,1,0,1,0,0,1,1,0,1,1,0,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,
0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,1,0,1,1,0,0,0,
0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,1,1,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,1,1,0,
0,0,0,0,0,0,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,
0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,0,0,0,0,0,0,0
},x,y,s,i,h=0,r=0,n;
pr(int u)
{ int g,f;
for (g=1;g<=5;g++)
for (f=1;f<=5;f++)
if (j[u][g][f]==1)
{gotoxy(f,g); textcolor(1); putch('o');}
else {gotoxy(f,g); textcolor(14); putch('o');}
}
hkg(int t,int y)
{ if (j[h][t][y]==1) {j[h][t][y]=0;}
else {j[h][t][y]=1;}
}
kg()
{ int e,r;
hkg(y,x); hkg(y-1,x); hkg(y,x+1); hkg(y+1,x); hkg(y,x-1); pr(h);
}
inkey()
{ char o;
if (kbhit()) {
o=getch();
if (o==27) {exit(0);}
if (o==13) {
kg(); r++;
gotoxy(7,5); printf("Bu Shu: %d",r);
if (r>=299) {over(); getch(); exit(0);}
}
if (o==0) {
o=getch();
if (o==72&&y>1) {y--;}
if (o==77&&x<5) {x++;}
if (o==80&&y<5) {y++;}
if (o==75&&x>1) {x--;}
}
}
}
test()
{ int o,p,k=0;
for (o=1;o<=5;o++)
for (p=1;p<=5;p++)
if (j[h][o][p]==1) {k=1;}
if (k==0)
{gotoxy(7,3); printf("Ni Ying Le! An Jian Ji Xu"); getch(); h++; pr(h);
gotoxy(7,3); printf(" ");
textcolor(10); gotoxy(3,7); printf("Di %d Guan",h+1);
}
}
over()
{ int v,b;
for (v=1;v<=24;v++)
for (b=1;b<=40;b++)
{gotoxy(b,v); textcolor(v); putch(219); delay(1000);}
for (v=1;v<=40;v++)
for (b=1;b<=24;b++)
{gotoxy(v,b); textcolor(v); putch(219); delay(1000);}
gotoxy(20,12); printf("Ni Shu Le!");
}
zai()
{ int g,h,y; gotoxy(15,10); printf("Zai Ru");
gotoxy(15,15); printf("Zai Ru : Dong Hua ");
for (g=10;g<=15;g++)
{gotoxy(g,12); textcolor(2); putch(219);
for (h=1;h<=5;h++) delay(g10000000);
gotoxy(19,17); printf("%d%",(g-10)5);}
gotoxy(15,15); printf("Zai Ru : Di Tu ");
for (g=16;g<=25;g++)
{gotoxy(g,12); textcolor(2); putch(219);
for (h=1;h<=5;h++) delay(g10000000);
gotoxy(19,17); printf("%d%",(g-10)5);}
gotoxy(15,15); printf("Zai Ru : Tu Xing He Zi Ti");
for (g=26;g<=30;g++)
{gotoxy(g,12); textcolor(2); putch(219);
for (h=1;h<=5;h++) delay(g10000000);
gotoxy(19,17); printf("%d%",(g-10)5);}
for (y=1;y<=5;y++) delay(10000);
}
main()
{ textmode(C40); randomize(); clrscr(); zai(); clrscr(); textcolor(10);
gotoxy(7,5); printf("Bu Shu: %d",0);
gotoxy(3,7); printf("Di %d Guan",h+1);
for (i=1;i<=n;i++) {
do {x=rand()%5+1; y=rand()%5+1;} while (j[h][y][x]==1);
j[h][y][x]=1;
} pr(h); x=1; y=1;
while (1)
{ inkey(); gotoxy(x,y); test();
}
getch();
}
作者: 54qiuwenda
2008-7-26 12:17 回复此发言
--------------------------------------------------------------------------------
3 回复:C语言做游戏
走迷宫(用工具走到):
#include <conioh>
#include <timeh>
#include <stdlibh>
#include <stdioh>
main()
{ int map[20][20]={1,1,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,7,
1,0,1,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,0,7,
1,0,1,1,1,1,3,3,1,1,0,1,0,0,1,0,0,1,0,7,
1,0,0,0,1,2,4,2,0,1,0,1,1,0,1,0,0,1,1,7,
1,0,0,0,1,2,1,2,1,1,0,0,1,0,1,2,1,1,2,7,
1,0,1,1,1,0,1,2,2,2,2,1,1,0,2,2,1,2,2,7,
1,0,1,0,0,0,1,2,1,2,2,1,1,0,1,2,2,2,2,7,
1,0,1,1,1,0,1,2,1,2,2,1,0,0,1,1,2,1,2,7,
1,0,0,0,1,0,1,2,1,2,2,1,0,1,1,0,2,1,2,7,
1,0,1,0,0,0,1,2,1,1,0,1,0,0,0,0,2,1,2,7,
1,0,1,1,1,1,1,2,2,3,0,1,1,1,1,2,2,1,2,7,
1,0,0,0,2,2,2,2,8,1,0,0,1,2,2,2,1,1,2,7,
1,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,2,7,
1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,2,7,
1,2,1,1,1,1,0,0,1,1,1,1,2,2,1,2,2,2,2,7,
1,2,1,6,0,1,0,0,0,0,0,2,2,2,1,2,1,1,1,7,
1,2,1,1,5,1,1,1,1,1,1,2,2,2,1,2,1,2,0,7,
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0,9,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
,x,y,q,w,
z[5][2]={{-1,0},{0,1},{1,0},{0,-1},{0,0}},l=4,s=0,d=0,j,i,u=0;
char o;
textmode(C40); randomize(); q=2; w=2; clrscr();
while (1) {
gotoxy(22,3); printf("D: ChuanQiang");
gotoxy(22,4); printf("(CrossWall)");
gotoxy(22,6); putch(219); printf(": QiangBi(Wall)");
gotoxy(22,8); printf("=: Shui(Water)");
gotoxy(22,10); printf("F: ShuiShang");
gotoxy(22,11); printf("FeiXing(FlyOn");
gotoxy(22,12); printf("Water)");
gotoxy(22,14); printf("/: Men(Door)");
gotoxy(22,16); printf(": BaoZang");
gotoxy(22,17); printf("(Treasure)");
gotoxy(22,19); printf("Esc:TuiChu(Exit)");
for (y=0;y<20;y++) {
for (x=0;x<20;x++) {
if (map[y][x]==1||map[y][x]==7) {gotoxy(x+1,y+1); putch(219);}
if (map[y][x]==9) {gotoxy(x+1,y+1); putch('');}
if (map[y][x]==3) {gotoxy(x+1,y+1); putch(219);}
if (map[y][x]==2) {gotoxy(x+1,y+1); putch('=');}
if (map[y][x]==5) {gotoxy(x+1,y+1); putch('/');}
if (map[y][x]==8) {gotoxy(x+1,y+1); putch('8');}
if (map[y][x]==4) {gotoxy(x+1,y+1); putch('F');}
if (map[y][x]==6) {gotoxy(x+1,y+1); putch('D');}
}
}
if (kbhit()) {
o=getch();
if (o==27) {exit(0);}
if (o==72) {l=0;}
if (o==77) {l=1;}
if (o==80) {l=2;}
if (o==75) {l=3;}
j=map[q-1+z[l][0]][w-1+z[l][1]];
if (j!=0&&j!=4&&j!=8&&j!=3&&j!=s2&&j!=9&&j!=6&&j!=u1&&j!=7)
{l=4;}
gotoxy(w,q); putch(' ');
gotoxy(22,22); for (i=1;i<=18;i++) putch(' ');
q=q+z[l][0]; w=w+z[l][1]; l=4;
if (j==4) {map[q-1][w-1]=0; s=1; gotoxy(35,10); printf(" /");
gotoxy(35,11); printf(" \\/");}
if (j==8) {map[q-1][w-1]=0; map[17][4]=0; gotoxy(5,18); putch(' ');
gotoxy(35,14); printf(" /"); gotoxy(35,15); printf(" \\/");}
if (j==3) {gotoxy(22,22); printf("AnMen(SecretDoor)");}
if (j==6) {map[q-1][w-1]=0; u=1; gotoxy(4,16); putch(' ');
gotoxy(35,3); printf(" /"); gotoxy(35,4); printf(" \\/");}
} gotoxy(w,q); putch(178);
if (map[q-1][w-1]==9)
{gotoxy(10,23); printf("You won! Very good!"); gotoxy(35,16);
printf(" /"); gotoxy(35,17); printf(" \\/"); break;}
delay(8000);
}
getch();
}
贪吃蛇(上下左右移动吃食物):
#include <stdioh>
#include <conioh>
#include <stdlibh>
#include <timeh>
int x=5,y=2,r=1,mx[4]={0,1,0,-1},my[4]={-1,0,1,0},s[40][3],u=5,g=1,
map[40][24]={0},score=0;
boxx(int x1,int y1,int x2,char c)
{ int j;
for (j=x1;j<=x2;j++) {gotoxy(j,y1); putch©;}
}
boxy(int x1,int y1,int y2,char c)
{ int j;
for (j=y1;j<=y2;j++) {gotoxy(x1,j); putch©;}
}
inkey()
{ char o;
randomize();
if (kbhit()) {
o=getch();
if (o==27) {exit(0);}
if (o==0) {
o=getch();
if (o==72) r=0;
if (o==77) r=1;
if (o==80) r=2;
if (o==75) r=3;
}
}
x=x+mx[r]; y=y+my[r];
if (x<2||x>39) {x=x-mx[r];}
if (y<2||y>23) {y=y-my[r];}
gotoxy(x,y); putch(178); p(u); if (map[x][y]==1) {score++;
gotoxy(20,1); printf("Score : %d",score); map[x][y]=0;
u++; s[u][1]=s[u-1][1]; s[u][2]=s[u-1][2];
}
gotoxy(s[g][1],s[g][2]); putch(' ');
s[g][1]=x; s[g][2]=y; g=g++;
if (g>u) g=1;
}
p(int h)
{int z; for (z=1;z<=h;z++) {gotoxy(s[z][1],s[z][2]); putch(178);}
}
egg()
{int ex,ey,h;
for (h=1;h<=30;h++){ex=rand()%38+2; ey=rand()%22+2;
map[ex][ey]=1; gotoxy(ex,ey); putch('O');}
}
main()
{ randomize();
textmode(C40); textcolor(rand()%16);
s[1][1]=2; s[1][2]=2; s[2][1]=3; s[2][2]=2; s[3][1]=4;
s[3][2]=2; s[4][1]=5; s[4][2]=2; s[5][1]=6; s[5][2]=2;
boxx(1,1,40,219); boxx(1,24,40,219);
boxy(1,1,24,219); boxy(40,1,24,219); egg();
gotoxy(20,1); printf("Score : %d",score);
while (1) {
gotoxy(x,y); putch(' ');
inkey();
delay(30000); delay(30000);
}
getch();
}
猫捉老鼠(躲开猫吃食物):
#include<stdioh>
#include<conioh>
#include<stdlibh>
#include<timeh>
main()
{ int cx,cy,x,y,xx,yy,fx,fy,n,kb,r=0,p,o,g=0,sc=0; int set[25][41]={0};
int mx[2][5]={{0,1,0,-1,0},{0,2,0,-2,0}},
my[2][5]={{0,0,-1,0,1},{0,0,-2,0,2}};
clrscr(); textmode(C40); randomize(); n=0;
while (n<50) { do{ fx=rand()%38+2; fy=rand()%22+2;} while(set[fy][fx]!=0);
set[fy][fx]=1; gotoxy(fx,fy); putch(''); n++;} n=3;
while (n) { do{ fx=rand()%38+2;fy=rand()%22+2;} while (set[fy][fx]!=0);
set[fy][fx]=2; gotoxy(fx,fy); putch('o'); n--;}
gotoxy(2,2); cx=2; cy=2; putch('&');
for (x=1;x<=40;x++) {gotoxy(x,1); putch(219); gotoxy(x,24); putch(219);
if (x<=24) {gotoxy(1,x); putch(219); gotoxy(40,x); putch(219);}}
gotoxy(15,1); printf("Score: %d",sc);
gotoxy(39,23); putch('@'); x=39; y=23;
gotoxy(40,8); putch(' '); gotoxy(1,16); putch(' ');
while (1)
{ if (kbhit()) { kb=getch(); if (kb==27) exit(0); kb=getch();
if (x==40&&y==8) {
if (kb==75)
{gotoxy(x,y); putch(' '); x=x-1; gotoxy(x,y); putch('@');}
if (kb==77)
{gotoxy(x,y); putch(' '); x=2; gotoxy(x,y); putch('@');}
}
if (x==1&&y==16) {
if (kb==75)
{gotoxy(x,y); putch(' '); x=39; gotoxy(x,y); putch('@');}
if (kb==77)
{gotoxy(x,y); putch(' '); x=x+1; gotoxy(x,y); putch('@');}
}
if (kb==77&&y==8&&x==39) {gotoxy(x,y); putch(' ');
x=x+1; gotoxy(x,y); putch('@');} else
if (kb==75&&y==16&&x==2) {gotoxy(x,y); putch(' ');
x=x-1; gotoxy(x,y); putch('@');} else
{if (kb==77&&x<39) r=1; else if (kb==72&&y>2) r=2; else
if (kb==75&&x>2) r=3; else if (kb==80&&y<23) r=4; else r=0;
gotoxy(x,y); putch(' '); xx=x+mx[0][r]; yy=y+my[0][r];
if (g==0) {x=x+mx[0][r]; y=y+my[0][r];} else
{x=x+mx[1][r]; y=y+my[1][r]; g=g-1;
gotoxy(3,1); printf("Gao neng:%d",g); putch(219);}
if ((x==39&&kb==77)||(x==38&&kb==77&&g>0)) x=2;
if ((y==2&&kb==72)||(y==3&&kb==72&&g>0)) y=23;
if ((x==2&&kb==75)||(x==3&&kb==75&&g>0)) x=39;
if ((y==23&&kb==80)||(y==22&&kb==80&&g>0)) y=2;
gotoxy(x,y); putch('@'); if (set[y][x]==1||set[yy][xx]==1)
{gotoxy(15,1); printf("Score: %d",sc+1); sc=sc+1; set[y][x]=0;
gotoxy(xx,yy); putch(' '); gotoxy(x,y); putch('@');} else
if (set[y][x]==2||set[yy][xx]==2)
{gotoxy(3,1); printf("Gao neng:%d",g+10); putch(219); g=g+10;
set[y][x]=0; set[yy][xx]=0;}
if (g==0) { gotoxy(3,1); for (n=1;n<=11;n++) putch(219);}}
if (sc==20) break; if (x==cx&&y==cy) break;}
if ((x==40&&y==8)||(x==1&&y==16)) {} else {
gotoxy(cx,cy); if (set[cy][cx]==1) putch(''); else putch(' ');
if (cx<x) cx=cx+1; if (cx>x) cx=cx-1;
if (cy<y) cy=cy+1; if (cy>y) cy=cy-1;
gotoxy(cx,cy); putch('&'); delay(10000); if (x==cx&&y==cy) break;}
}
if (cx==x&&cy==y) {gotoxy(15,3); printf("The cat win!");} else
if (sc>=20) {gotoxy(15,3); printf("The mouse(you) win!");}
getch();
}
俄罗斯方块(移动方块消行):
#include <stdioh>
#include <stdlibh>
#include <timeh>
#include <conioh>
int x[8][4]={0,0,0,0,
0,1,2,0,
0,1,-1,0,
0,0,0,0,
0,0,1,2,
0,1,0,1,
0,-1,0,1,
0,1,0,-1};
int y[8][4]={0,0,0,0,
0,0,0,-1,
0,0,0,-1,
0,1,2,3,
0,1,0,0,
0,0,1,1,
0,0,1,1,
0,0,1,1};
int fxj[4]={0,1,0,-1},fyj[4]={-1,0,1,0},k[8]={0,0,0,3,1,3,2,2};
int fx,fy,j=2,map[25][41]={0},mapc[25][41]={0},h,w=0,z,nx[4],ny[4],score;
p(int t,char o,int b)
{ int i;
for (i=0;i<=3;i++)
{textcolor(t); gotoxy(fx+nx[i],fy+ny[i]); putch(o);
map[fy+ny[i]][fx+nx[i]]=b;
}
}
inkey()
{ int l,e,k,nnx[4],nny[4]; char o;
if (kbhit()) {
o=getch();
if (o==27) exit(0);
if (o==0) {
o=getch();
if (o==77)
{e=0; for (l=0;l<=3;l++) {
if (fx+nx[l]+1==41) {e=1; break;}
if (map[fy+ny[l]][fx+nx[l]+1]==1) {e=1; break;}
}
if (e==0) {j=1;}
}
if (o==75)
{e=0; for (l=0;l<=3;l++) {
if (fx+nx[l]-1==10) {e=1; break;}
if (map[fy+ny[l]][fx+nx[l]-1]==1) {e=1; break;}
}
if (e==0) {j=3;}
}
if (o==80)
{e=0; for (l=0;l<=3;l++) {
if (map[fy+ny[l]+1][fx+nx[l]]==1) {e=1; break;}
}
if (e==0) {w=10; z=19;}
}
if (o==72)
{ k=0;
for (l=0;l<=3;l++) {
e=nx[l]; nx[l]=ny[l](-1); ny[l]=e;
if (map[fy+ny[l]][fx+nx[l]]==1) {k=1;}
if (fx+nx[l]<11||fx+nx[l]>40) {k=1;}
if (fy+ny[l]>24||fy+ny[l]<2) {k=1;}
}
if (k==1)
for (l=0;l<=3;l++) {e=nx[l]; nx[l]=ny[l]; ny[l]=e/(-1);}
}
}
}
}
win()
{gotoxy(20,12); textcolor(3); printf("You won!");
}
over()
{gotoxy(19,12); textcolor(14); printf("Game over!");
}
main()
{ int g,c,u,b,v; textmode(C40); clrscr(); randomize(); h=rand()%7+1;
for (g=1;g<=24;g++) {gotoxy(10,g); putch('|');}
gotoxy(3,3); printf("Next :"); gotoxy(3,15); printf("Score:");
gotoxy(3,16); printf("%d",0); gotoxy(3,11); printf("Esc=");
gotoxy(3,12); printf("TuiChu");
while (1) {
c=0; for (g=0;g<=3;g++) {nx[g]=x[h][g]; ny[g]=y[h][g];}
b=rand()%7+1; fx=25; fy=2;
for (g=0;g<=3;g++)
{textcolor(b); gotoxy(3+x[b][g],5+y[b][g]); putch(219);
}
while (c==0) {
inkey();
p(h,219,0); for (g=1;g<=30-z;g++) delay(1000);
w=w-1; if (w==0) z=0;
p(h,32,0); if (j!=2) {fx=fx+fxj[j]; fy=fy+fyj[j];}
j=2; fx=fx+fxj[2]; fy=fy+fyj[2];
for (g=0;g<=3;g++) {
if (fy+ny[g]>24) {c=1; break;}
if (map[fy+ny[g]][fx+nx[g]]==1) {c=1; break;}
}
} fx=fx-fxj[j]; fy=fy-fyj[j];
p(h,219,1); u=0; w=0;
for (g=0;g<=3;g++) {mapc[fy+ny[g]][fx+nx[g]]=h;}
for (g=0;g<=3;g++)
{textcolor(b); gotoxy(3+x[b][g],5+y[b][g]); putch(32);
}
for (g=1;g<=40;g++) if (map[2][g]==1||map[1][g]) {u=1; break;}
if (u==1)
{ over(); b=rand()%7+1; fx=30; fy=2;
for (g=0;g<=3;g++)
{textcolor(b); gotoxy(3+x[b][g],5+y[b][g]); putch(219);}
getch(); exit(0);
}
h=b; z=1;
do {
u=0; for (g=11;g<=40;g++) {if (map[z][g]==0) {u=1; break;}}
if (u==0) {
for (g=11;g<=40;g++) {gotoxy(g,z); putch(32);}
for (g=z-1;g>=1;g--) for (v=11;v<=40;v++) {map[g+1][v]=map[g][v];}
for (g=z-1;g>=1;g--) for (v=11;v<=40;v++) {mapc[g+1][v]=mapc[g][v];}
for (g=1;g<=24;g++) for (v=11;v<=40;v++)
{gotoxy(v,g); textcolor(mapc[g][v]); putch(219);}
score=score+10; gotoxy(3,16); printf("%d",score);
if (score>=30) {win(); getch(); exit;}
}
z++;
} while (z<25); z=0;
}
}
圈圈叉叉游戏(光标控制):
#include <conioh>
#include <stdioh>
int w[3][3]={2,1,2,
1,3,1,
2,1,2},i,j,k,l,kk,ll,x,y,m,prx=3,pry=3,s=0,f,g,e,r,h,m,q;
inkey()
{ char o,c[1];
if (kbhit())
{ o=getch();
if (o==27) {exit(0);}
if (o==13) {
gettext(x,y,x,y,&c);
if (c[0]==32) {gotoxy(x,y); putch('o'); s=1;
w[(y-pry)/2][(x-prx)/2]=-1;
}
}
if (o==0) {
o=getch();
if (o==72&&y>pry) {y--;}
if (o==77&&x<prx+4) {x++;}
if (o==80&&y<pry+4) {y++;}
if (o==75&&x>prx) {x--;}
}
}
}
pr()
{ gotoxy(prx,pry+0); printf(" | | ");
gotoxy(prx,pry+1); printf("-+-+-");
gotoxy(prx,pry+2); printf(" | | ");
gotoxy(prx,pry+3); printf("-+-+-");
gotoxy(prx,pry+4); printf(" | | ");
}
p()
{ for (h=0;h<=2;h++) {
q=0; for (m=0;m<=2;m++) if (w[h][m]==-1) q++;
if (q==3) {gotoxy(10,5); printf("Ni Ying Le"); getch(); exit(0);}
q=0; for (m=0;m<=2;m++) if (w[m][h]==-1) q++;
if (q==3) {gotoxy(10,5); printf("Ni Ying Le"); getch(); exit(0);}
q=0; for (m=0;m<=2;m++) if (w[h][m]==-2) q++;
if (q==3) {gotoxy(10,5); printf("Wo Ying Le"); getch(); exit(0);}
q=0; for (m=0;m<=2;m++) if (w[m][h]==-2) q++;
if (q==3) {gotoxy(10,5); printf("Wo Ying Le"); getch(); exit(0);}
}
q=w[0][0]+w[1][1]+w[2][2];
if (q==-6) {gotoxy(10,5); printf("Wo Ying Le"); getch(); exit(0);}
q=w[0][2]+w[1][1]+w[2][0];
if (q==-6) {gotoxy(10,5); printf("Wo Ying Le"); getch(); exit(0);}
q=0; for (h=0;h<=2;h++) for (m=0;m<=2;m++) if (w[h][m]>0) {q=1;}
if (q==0) {gotoxy(10,5); printf("Ping Ju"); getch(); exit(0);}
}
main()
{ textmode(C40); pr(); gotoxy(prx+2,pry+2); putch('x');
w[1][1]=-2; x=prx; y=pry; gotoxy(10,7); printf("Esc Tui Chu");
while (1) {
inkey(); gotoxy(x,y);
if (s==1) {s=0; e=-1; r=-1;
for (i=0;i<=2;i++)
{ l=0; f=-1; g=-1; ll=0; for (j=0;j<=2;j++) {
if (w[i][j]==-1) {l++;}
if (w[i][j]==-2) {ll++;}
if (w[i][j]>0) {f=i; g=j;}
}
if ((l==2)&&(e<0||r<0)&&f>=0&&g>=0) {e=f; r=g;}
if (ll==2&&f>=0&&g>=0) {e=f; r=g;}
k=0; f=-1; g=-1; kk=0; for (j=0;j<=2;j++) {
if (w[j][i]==-1) {k++;}
if (w[j][i]==-2) {kk++;}
if (w[j][i]>0) {f=j; g=i;}
}
if ((k==2)&&(e<0||r<0)&&f>=0&&g>=0) {e=f; r=g;}
if (kk==2&&f>=0&&g>=0) {e=f; r=g;}
}
k=0; f=-1; g=-1;
if (w[0][0]==-1) {k++;} if (w[0][0]>0) {f=0; g=0;}
if (w[1][1]==-1) {k++;} if (w[1][1]>0) {f=1; g=1;}
if (w[2][2]==-1) {k++;} if (w[2][2]>0) {f=2; g=2;}
if (k==2) {e=f; r=g;}
l=0; f=-1; g=-1;
if (w[0][2]==-1) {l++;} if (w[0][2]>0) {f=0; g=0;}
if (w[1][1]==-1) {l++;} if (w[1][1]>0) {f=1; g=1;}
if (w[2][0]==-1) {l++;} if (w[2][0]>0) {f=2; g=2;}
if (l==2&&f>=0&&g>=0) {e=f; r=g;}
if (e<0||r<0) {
k=0; l=0; m=0;
for (i=0;i<=2;i++)
for (j=0;j<=2;j++)
if (w[i][j]>m) {m=w[i][j]; k=i; l=j;}
gotoxy(l+prx+l,k+pry+k); w[k][l]=-2; putch('x');
} else {gotoxy(r+prx+r,e+pry+e); putch('x'); w[e][r]=-2;}
p();
}
}
}
d球游戏(WASD和8546控制):
#include <stdioh>
#include <conioh>
main()
{ int x,y,xf=1,yf=1,bf=6,bg=22,pf=30,pg=22,k=800,o,j,i,score=0;
char p;
clrscr(); textmode(C40);
for (x=1;x<=40;x++) { gotoxy(x,1); putch(219); gotoxy(x,24); putch(219);}
for (y=1;y<=24;y++) { gotoxy(1,y); putch(219); gotoxy(40,y); putch(219);}
gotoxy(3,3); putch('O'); x=3; y=3; gotoxy(bf,bg); printf("----");
gotoxy(pf,pg); printf("----");
gotoxy(3,1); printf("<=-20--Lost");
gotoxy(16,1); printf("Score: %d ",score);
gotoxy(30,1); printf(">=50--Win");
while (k&&score<50&&score>-20)
{ gotoxy(bf,bg); printf("----");
gotoxy(pf,pg); printf("----");
gotoxy(x,y); putch('O'); delay(1000000000);
gotoxy(x,y); putch(' ');
if (x==39) {xf=-1;} if (x==2) {xf=1;}
if (y==23) {yf=-1;} if (y==2) {yf=1;}
x+=xf; y+=yf;
if (y==bg-1&&x>=(bf-1)&&x<=(bf+6)&&yf==1)
if ((x==bf-1&&xf==1)||(x>=bf&&x<=(bf+5))||(x==bf+6&&xf==-1)) {
if (x==bf-1&&xf==1) xf=-1; if (x==bf+6&&xf==-1) xf=1; yf=-1;
if (yf==1) {score+=5; gotoxy(15,1);
printf("Score: %d +5",score); putch(219);}
}
if (y==pg-1&&x>=(pf-1)&&x<=(pf+6))
if ((x==pf-1&&xf==1)||(x>=pf&&x<=(pf+5))||(x==pf+6&&xf==-1)) {
yf=-1; score+=5; gotoxy(15,1);
printf("Score: %d +5",score); putch(219);}
if (y==23) {score-=5; gotoxy(15,1);
printf("Score: %d -5",score); putch(219);}
if (kbhit())
{ p=getch(); o=p;
if (o==27) exit();
if (p=='a'&&bf>3) {gotoxy(bf,bg); printf(" "); bf=bf-2;
gotoxy(bf,bg); printf("----");}
if (p=='d'&&bf+5<pf-1) {gotoxy(bf,bg); printf(" "); bf=bf+2;
gotoxy(bf,bg); printf("----");}
if (p=='w'&&bg-1>1) {gotoxy(bf,bg); printf(" "); bg=bg-1;
gotoxy(bf,bg); printf("----");}
if (p=='s'&&bg+1<24) {gotoxy(bf,bg); printf(" "); bg=bg+1;
gotoxy(bf,bg); printf("----");}
if (p=='4'&&pf>bf+6) {gotoxy(pf,pg); printf(" "); pf=pf-2;
gotoxy(pf,pg); printf("----");}
if (p=='6'&&pf+5<38) {gotoxy(pf,pg); printf(" "); pf=pf+2;
gotoxy(pf,pg); printf("----");}
if (p=='8'&&pg-1>1) {gotoxy(pf,pg); printf(" "); pg=pg-1;
gotoxy(pf,pg); printf("----");}
if (p=='5'&&pg+1<24) {gotoxy(pf,pg); printf(" "); pg=pg+1;
gotoxy(pf,pg); printf("----");}
}
k--;
} gotoxy(x,y); putch('O');
if (score>=50) {gotoxy(10,3); printf("Score>=50! You win!");}
if (score<=-20) {gotoxy(10,3); printf("Score<=-20! You lost!");}
getch();
}
基本思路:
蛇每吃一个食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇头的运动,而蛇身子跟着蛇头走,每后一格蛇身子下一步走到上一格蛇身子的位置,以此类推。
#include <stdioh>
#include <conioh>
#include <windowsh>
#define BEG_X 2
#define BEG_Y 1
#define WID 20
#define HEI 20
HANDLE hout;
typedef enum {UP, DOWN, LEFT, RIGHT} DIR;
typedef struct Snake_body
{
COORD pos;//蛇身的位置
struct Snake_body next;//下一个蛇身
struct Snake_body prev;//前一个蛇身
}SNAKE, PSNAKE;
PSNAKE head = NULL;//蛇头
PSNAKE tail = NULL;//蛇尾
//画游戏边框的函数
void DrawBorder()
{
int i, j;
COORD pos = {BEG_X, BEG_Y};
for(i = 0; i < HEI; ++i)
{
SetConsoleCursorPosition(hout, pos);
for(j = 0; j < WID; ++j)
{
if(i == 0)//第一行
{
if(j == 0)
printf("┏");
else if(j == WID - 1)
printf("┓");
else
printf("━");
}
else if(i == HEI - 1)//最后一行
{
if(j == 0)
printf("┗");
else if(j == WID - 1)
printf("┛");
else
printf("━");
}
else if(j == 0 || j == WID - 1)//第一列或最后一列
printf("┃");
else
printf(" ");
}
++posY;
}
}
//添加蛇身的函数
void AddBody(COORD pos)
{
PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));
pnew->pos = pos;
if(!head)
{
head = tail = pnew;
}
else
{
pnew->next = head;//新创建蛇身的next指向原先的蛇头
head->prev = pnew;//原先的蛇头的prev指向新创建的蛇身
head = pnew;//把新创建的蛇身作为新的蛇头
}
SetConsoleCursorPosition(hout, head->pos);
printf("◎");
}
//蛇身移动的函数
void MoveBody(DIR dir)
{
PSNAKE ptmp;
COORD pos = head->pos;
switch(dir)
{
case UP:
if(head->posY > BEG_Y + 1)
--posY;
else
return;
break;
case DOWN:
if(head->posY < BEG_Y + HEI - 2)
++posY;
else
return;
break;
case LEFT:
if(head->posX > BEG_X + 2)
posX -= 2;
else
return;
break;
case RIGHT:
if(head->posX < BEG_X + (WID - 2) 2)
posX += 2;
else
return;
break;
}
AddBody(pos);//添加了一个新的蛇头
ptmp = tail;//保存当前的蛇尾
tail = tail->prev;
if(tail)
tail->next = NULL;
SetConsoleCursorPosition(hout, ptmp->pos);
printf(" ");
free(ptmp);
}
int main()
{
int ctrl;
DIR dir = RIGHT;//初始蛇的方向是向右的
COORD pos = {BEG_X + 2, BEG_Y + HEI / 2};
system("color 0E");
system("mode con cols=90 lines=30");
hout = GetStdHandle(STD_OUTPUT_HANDLE);
printf(" ------------贪吃蛇的移动------------");
DrawBorder();
//自定义几个蛇的身体
AddBody(pos);
posX += 2;
AddBody(pos);
posX += 2;
AddBody(pos);
posX += 2;
AddBody(pos);
posX += 2;
AddBody(pos);
posX += 2;
AddBody(pos);
posX += 2;
AddBody(pos);
//控制蛇的移动
while(ctrl = getch())
{
switch(ctrl)
{
case 'w':
if(dir == DOWN)
continue;
dir = UP;
break;
case 's':
if(dir == UP)
continue;
dir = DOWN;
break;
case 'a':
if(dir == RIGHT)
continue;
dir = LEFT;
break;
case 'd':
if(dir == LEFT)
continue;
dir = RIGHT;
break;
case 'q':
return 0;
}
MoveBody(dir);
}
return 0;
}
扩展资料:
实现逻辑
1,可以设置光标,就能实现制定位置打印制定符号。
2,涉及一个结构体,包含两个元素坐标元素和一个结构体指针。
3,结构体串联形成链表,遍历获取成员坐标,打印符号得到蛇身。
4,不断的加头,去尾,重新遍历坐标,再打印形成蛇的移动。
5,食物产生的位置判定,不能越界,也不能与蛇身体重合。
6,蛇的转向判定,一条规则,不允许倒退。
7,转向的实现,跟行进方向决定新的关节坐标(当前头的上下左右)
8,死亡检测,是否头节点坐标是否与墙壁重合,是否与身体其他关节重合。
9,加速减速,设置刷新休眠时间实现。
参考资料来源:百度百科-C语言
以上就是关于关于用C语言编写的小游戏的游戏代码,如黑白棋贪吃蛇等全部的内容,包括:关于用C语言编写的小游戏的游戏代码,如黑白棋贪吃蛇等、用C语言编写小游戏、如何用c语言编写一个小游戏等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)