天涯人论坛
先登录吧!!!
登录可以体验更多权限哦!!!
天涯人论坛

为编程爱好者打造一个学习、交流的平台。
 
首页首页  欢迎页欢迎页  注册注册  登录登录  
论坛刚刚起步,欢迎大家多多支持! 如果有想申请管理员或版主的请给管理员留言!!!
欢迎大家积极发帖!

分享 | 
 

 **晒晒你的源代码**

向下 
作者留言
让一切随风
Admin
avatar

帖子数 : 257
注册日期 : 12-11-03
年龄 : 26
地点 : 湖南

帖子主题: **晒晒你的源代码**   周二 十一月 27, 2012 1:15 pm

Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy
返回页首 向下
http://tyren.forumotion.com
让一切随风
Admin
avatar

帖子数 : 257
注册日期 : 12-11-03
年龄 : 26
地点 : 湖南

帖子主题: c语言写的俄罗斯方块源代码   周二 十一月 27, 2012 12:50 pm

#include <stdlib.h>
#include <graphics.h>
#include <bios.h>
#define mDRAW 5
#define mLINE 6
#define mADOWN 7
#define mGEN 8
#define mLEFT 75
#define mRIGHT 77
#define mSPACE 57
#define mDOWN 80
#define mESC 1
#define TIMEINT 2
#define MAXX 9
#define MAXY 30
#define BACKCOLOR BLACK
#define WINX 50
#define WINY 470
#define GAP 6
#define AREAX (WINX+GAP)
#define AREAY (WINY-GAP)
#define BOXW 15

int oldarea[MAXY+1][MAXX];
int area[MAXY+1][MAXX];
int actW,actH,actX,actY;
int curX,curY,curColor,curW,curH;
int newX,newY,newColor,newW,newH;
int active;
int box[4][4];
int FORCOLOR;
int MESSAGE;
int BOX[7][4][4]={
{
{1,1,1,1},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}
},{
{1,1,1,0},
{1,0,0,0},
{0,0,0,0},
{0,0,0,0}
},{
{1,1,1,0},
{0,0,1,0},
{0,0,0,0},
{0,0,0,0}

},{
{1,1,1,0},
{0,1,0,0},
{0,0,0,0},
{0,0,0,0}
},{
{1,1,0,0},
{0,1,1,0},
{0,0,0,0},
{0,0,0,0}
},{
{0,1,1,0},
{1,1,0,0},
{0,0,0,0},
{0,0,0,0}
},{
{1,1,0,0},
{1,1,0,0},
{0,0,0,0},
{0,0,0,0}
}
};


void init();
void draw();
int genBox();
int getKey();
void lineFull();
int moveLeft();
int moveRight();
int moveDown();
int rotate();
int getW();
int getH();
void clearOldBox();
void putNewBox();
int collisionRotate(int box[][4]);
void getMessage();
void dispatchMessage();
int timeCome();
void fallDown();
int gameOver();

main()
{
int i;
init();
do
{
getMessage();
dispatchMessage();
}
while(!gameOver());

getch();
closegraph();
}

void getMessage()
{
if(MESSAGE) return;
if(timeCome())
{
MESSAGE=mADOWN;
return;
}
if(bioskey(1))
{
MESSAGE=bioskey(0)>>8;
return;
}
}

void dispatchMessage()
{
switch(MESSAGE)
{
case mLEFT: moveLeft();break;
case mRIGHT: moveRight();break;
case mADOWN: moveDown();break;
case mSPACE: rotate();break;
case mDOWN: fallDown(); break;
case mDRAW: draw();break;
case mLINE: lineFull();break;
case mGEN: genBox();break;
case mESC: closegraph(); exit(0);
default: MESSAGE=0;
}
}

void fallDown()
{
while(active)
{
moveDown(); draw();
}

MESSAGE=mLINE;
}

int timeCome()
{

static long tm, old;
tm=biostime(0,tm);
if(tm-old<TIMEINT) return 0;
else
{
old=tm; return 1;
}
}

void init()
{
int i,j,x1,y1,x2,y2;
int driver=DETECT, mode=0;
randomize();
registerbgidriver(EG***GA_driver);
initgraph(&driver,&mode,"");
cleardevice();
setfillstyle(SOLID_FILL,BLUE);
bar(0,0,639,479);
x1=AREAX;
y1=AREAY-BOXW*MAXY;
x2=AREAX+MAXX*BOXW;
y2=AREAY;
rectangle(--x1,--y1,++x2,++y2);
setfillstyle(SOLID_FILL,BLACK);
bar(++x1,++y1,--x2,--y2);
y1=AREAY-MAXY*BOXW; y2=AREAY;
setcolor(DARKGRAY);
for(i=0;i<MAXX;i++)
{
x1=AREAX+i*BOXW;
line(x1,y1,x1,y2);
}

x1=AREAX; x2=x1+MAXX*BOXW;
for(j=0;j<MAXY;j++)
{
y1=AREAY-j*BOXW;
line(x1,y1,x2,y1);
}
for(j=0;j<MAXY;j++)
for(i=0;i<MAXX;i++)
area[j][i]=oldarea[j][i]=0;
actX=0; actY=0; actW=MAXX-1; actH=MAXY-1;
draw();
MESSAGE=mGEN;
}

int genBox()
{
int i,j,boxidx;
boxidx=random(7); FORCOLOR=random(7)+1;
for(j=0;j<4;j++)
for(i=0;i<4;i++)
box[j][i]=BOX[boxidx][j][i];
curW=getW(); curH=getH();
curX=(MAXX+curW)/2;
if(curX+curW>=MAXX)curX=MAXX-1-curW;
curY=MAXY-1-curH;
newX=curX; newY=curY; actX=curX;actY=curY;
actW=newW=curW; actH=newH=curH;
active=1;
if(collision(box)) return 0;
putNewBox();
draw(); MESSAGE=0;
return 1;
}

void lineFull()
{
int row,col, rowEnd,full,i,j;
rowEnd=newY+newH;
if(rowEnd>=MAXY-1) rowEnd=MAXY-2;
for(row=newY; row<=rowEnd;)
{
full=1;
for(col=0;col<MAXX;col++)
if(!area[row][col]){full=0; break;}
if(!full){++row; continue;}
for(j=row; j<MAXY-1;j++)
for(i=0;i<MAXX;i++)
area[j][i]=area[j+1][i];
actX=0;actY=row; actW=MAXX-1; actH=MAXY-1-row;
draw(); rowEnd--;
}
MESSAGE=mGEN;
}

void draw()
{
int row,col,x1,y1,x2,y2;
for(row=actY;row<=actY+actH;row++)
for(col=actX;col<=actX+actW;col++)
if(area[row][col]!=oldarea[row][col])
{
if(area[row][col]==0)
setfillstyle(SOLID_FILL,BACKCOLOR);
else
setfillstyle(SOLID_FILL,FORCOLOR);

x1=AREAX+col*BOXW; x2=x1+BOXW;
y1=AREAY-(row+1)*BOXW; y2=y1+BOXW;
bar(++x1,++y1,--x2,--y2);
oldarea[row][col]=area[row][col];
}

MESSAGE=0;
}

int moveLeft()
{
newX=curX-1; clearOldBox();
if(collision(box))
{
newX=curX;
putNewBox();
MESSAGE=0;
return 0;
}
putNewBox();
actW=curW+1; actX=curX=newX;
MESSAGE=mDRAW;
return 1;
}

int moveRight()
{
newX=curX+1; clearOldBox();
if(collision(box))
{
newX=curX;
putNewBox();
MESSAGE=0;
return 0;
}
putNewBox();
actW=curW+1; actX=curX; curX=newX;
MESSAGE=mDRAW;
return 1;
}

int moveDown()
{
int i,j;
newY=curY-1;
clearOldBox();
if(collision(box))
{
newY=curY;
putNewBox();
active=0;
MESSAGE=mLINE;
return 0;
}
putNewBox();
actH=curH+1; actY=newY; curY=newY;
MESSAGE=mDRAW;
return 1;
}

int rotate()
{
int newBox[4][4];
int i,j;
clearOldBox();

for(j=0;j<4;j++)
for(i=0;i<4;i++)
newBox[j][i]=0;

for(j=0;j<4;j++)
for(i=0;i<4;i++)
newBox[curW-i][j]=box[j][i];
newW=curH; newH=curW;
if(collisionRotate(newBox))
{
newW=curW; newH=curH; newX=curX; newY=curY;
putNewBox();
MESSAGE=0;
return 0;
}

for(j=0;j<4;j++)
for(i=0;i<4;i++)
box[j][i]=newBox[j][i];
putNewBox();
actH=newH>curH? newH:curH;
actW=curX+actH-newX;
actX=newX; actY=newY; curX=newX;
curY=newY; curW=newW; curH=newH;
MESSAGE=mDRAW;
return 1;
}

int getW()
{
int i,j;
for(i=3;i>0;i--)
for(j=0;j<4;j++)
if(box[j][i]) return i;
return 0;
}

int getH()
{
int i,j;
for(j=3;j>0;j--)
for(i=0;i<4;i++)
if(box[j][i]) return j;
return 0;
}


void clearOldBox()
{
int i,j;
for(j=0;j<=curH; j++)
for(i=0;i<=curW; i++)
if(box[j][i])
area[curY+j][curX+i]=0;
}

void putNewBox()
{
int i,j;
for(j=0;j<=newH;j++)
for(i=0;i<=newW;i++)
if(box[j][i])
area[newY+j][newX+i]=FORCOLOR;
}

int collision(int cbox[][4])
{
int i,j;
if(newX<0) return 1;
if(newX+newW>=MAXX) return 1;
if(newY<0) return 1;
for(j=0;j<=newH;j++)
for(i=0;i<=newW;i++)
if(area[newY+j][newX+i]&&cbox[j][i]) return 1;
return 0;
}

int collisionRotate(int cbox[][4])
{
int i,j;
if(newX+newW>=MAXX) newX=MAXX-1-newW;
if(newY+newH>=MAXY) newY=MAXY-1-newH;
if(collision(cbox)) return 1;
for(i=0;i<=newW;i++)
for(j=0;j<=newH;j++)
if(area[newY+j][newX+i])
{
newX-=newW-i+1; goto L;
}
L: return collision(cbox);
}

int gameOver()
{
if(!active &&(curY+curH>MAXY-3)) return 1;
else return 0;
}
返回页首 向下
http://tyren.forumotion.com
让一切随风
Admin
avatar

帖子数 : 257
注册日期 : 12-11-03
年龄 : 26
地点 : 湖南

帖子主题: --五子棋C语言代码--   周二 十一月 27, 2012 12:47 pm

#include <graphics.h>
  #include <stdlib.h>
  #include <stdio.h>
  #include <conio.h>
  #define N 15
  #define B 7
  #define STOP -10000
  #define OK 1
  #define NO 0
  #define UP 328
  #define DOWN 336
  #define LEFT 331
  #define RIGHT 333

  int a[N+1][N+1];
  int zx,zy;
  int write=1,biaoji=0;
  struct zn{
  long sum;

  int y;

  int x;

  }w[N+1][N+1],max,max1;


  void cbar(int i,int x,int y,int r);
  void map(int a[][]);
  int getkey();
  int key();
  void zuobiao(int x,int y,int i);
  int tu(int a[][],int write);
  int wtu(int a[][],int write);
  int zhineng(int a[][]);
  int zh5(int y,int x,int a[][]);
  long zzh5(int b[][],int i);
  main()
  {
  int i,j;
  int gdriver=DETECT;
  int gmode;
  initgraph(&gdriver,&gmode,"");
  zx=(N+1)/2;
  zy=(N+1)/2;
  for(i=1;i<=N;i++)
  for(j=1;j<=N;j++)
  a[i][j]=0;
  map(a);
  i=1;
  while(i)
  {
  int k,n;
  k=wtu(a,write);
  if(k==STOP) goto end;
  map(a);
  n=zhineng(a);
  if(n==STOP) goto end;
  map(a);
  }
  end:
  ;
  }


  int zhineng(int a[N+1][N+1])

  {
  int i,j;
  int k;
  max.sum=-1;

  for(i=0;i<=N;i++)
  for(j=0;j<+N;j++)

  {
  w[i][j].sum=0;
  w[i][j].x=i;
  w[i][j].y=j;
  }
  for(i=1;i<=N-4;i++)
  for(j=1;j<=N-4;j++)
  {
  k=zh5(i,j,a);
  if(k==STOP) return (STOP);
  }

  for(i=1;i<=N;i++)
  for(j=1;j<=N;j++)
  {

  if(max.sum<w[i][j].sum)
  {

  max.sum=w[i][j].sum;
  max.y=i;
  max.x=j;
  }

  else if(max.sum==w[i][j].sum)
  {

  if(((max.y-zy)*(max.y-zy)+(max.x-zx)*(max.x-zx))>((i-zy)*(i-zy)+(j-zx)*(j-zx)))
  max.sum=w[i][j].sum;
  max.y=i;
  max.x=j;
  }
  }
  if(a[max.y][max.x]==0)

  {
  a[max.y][max.x]=-1;
  zy=max.y;
  zx=max.x;
  }

  }


  int zh5(int y,int x,int a[N+1][N+1])
  {

  int i,j;
  int b[6][6];
  long c[13];

  long d[6][6];
  long temp;
  for(i=y;i<=y+4;i++)
  for(j=x;j<=x+4;j++)
  b[i+1-y][j+1-x]=a[i][j];
  c[1]=b[1][1]+b[1][2]+b[1][3]+b[1][4]+b[1][5];
  c[2]=b[2][1]+b[2][2]+b[2][3]+b[2][4]+b[2][5];
  c[3]=b[3][1]+b[3][2]+b[3][3]+b[3][4]+b[3][5];
  c[4]=b[4][1]+b[4][2]+b[4][3]+b[4][4]+b[4][5];
  c[5]=b[5][1]+b[5][2]+b[5][3]+b[5][4]+b[5][5];
  c[6]=b[1][1]+b[2][1]+b[3][1]+b[4][1]+b[5][1];
  c[7]=b[1][2]+b[2][2]+b[3][2]+b[4][2]+b[5][2];
  c[8]=b[1][3]+b[2][3]+b[3][3]+b[4][3]+b[5][3];
  c[9]=b[1][4]+b[2][4]+b[3][4]+b[4][4]+b[5][4];
  c[10]=b[1][5]+b[2][5]+b[3][5]+b[4][5]+b[5][5];
  c[11]=b[1][1]+b[2][2]+b[3][3]+b[4][4]+b[5][5];
  c[12]=b[1][5]+b[2][4]+b[3][3]+b[4][2]+b[5][1];


  for(i=1;i<=12;i++)
  {
  switch(c[i])

  {

  case 5:biaoji=1;return(STOP);

  case -5:biaoji=-1;return(STOP);

  case -4:c[i]=100000;break;

  case 4:c[i]=100000;break;

  case -3:c[i]=150;break;

  case 3:c[i]=150;break;

  case -2:c[i]=120;break;

  case 2:c[i]=100;break;

  case -1:c[i]=1;break;

  case 1:c[i]=1;break;

  default: c[i]=0;

  }

  }

  for(i=1;i<=12;i++)

  {

  if(c[i]==150)

  c[i]+=zzh5(b,i);

  }

  for(i=1;i<=5;i++)

  for(j=1;j<=5;j++)

  d[i][j]=0;

  for(i=1;i<=5;i++)

  for(j=1;j<=5;j++)

  {

  if(i==j) d[i][j]+=c[11];

  if((i+j)==6) d[i][j]+=c[12];

  d[i][j]+=c[i]+c[j+5];

  }

  for(i=1;i<=5;i++)

  for(j=1;j<=5;j++)

  {

  if(b[i][j]!=0)

  d[i][j]=-2;

  }

  max1.sum=-1;

  max1.y=0;

  max1.x=0;

  for(i=1;i<=5;i++)

  for(j=1;j<=5;j++)

  {

  if(max1.sum<d[i][j])

  {

  max1.sum=d[i][j];

  max1.y=i;

  max1.x=j;

  w[i+y-1][j+x-1].sum+=max1.sum;

  }

  else if(max1.sum==d[i][j])

  {

  if(((i+y-1-zy)*(i+y-1-zy)+(j+x-1-zx)*(j+x-1-zx))>((max1.y+y-1-zy)*(max1.y+y-1-zy)+(max1.x+x-1-zx)*(max1.x+x-1-zx)))

  {

  max1.sum=d[i][j];

  max1.y=i;

  max1.x=j;

  }

  }

  }

  }

  long zzh5(int b[6][6],int n)

  {

  int i,j,k,l,m;

  switch(n)

  {

  case 1:i=b[1][1];j=b[1][2];k=b[1][3];l=b[1][4];m=b[1][5];break;

  case 2:i=b[2][1];j=b[2][2];k=b[2][3];l=b[2][4];m=b[2][5];break;

  case 3:i=b[3][1];j=b[3][2];k=b[3][3];l=b[3][4];m=b[3][5];break;

  case 4:i=b[4][1];j=b[4][2];k=b[4][3];l=b[4][4];m=b[4][5];break;

  case 5:i=b[5][1];j=b[5][2];k=b[5][3];l=b[5][4];m=b[5][5];break;

  case 6:i=b[1][1];j=b[2][1];k=b[3][1];l=b[4][1];m=b[5][1];break;

  case 7:i=b[1][2];j=b[2][2];k=b[3][2];l=b[4][2];m=b[5][2];break;

  case 8:i=b[1][3];j=b[2][3];k=b[3][3];l=b[4][3];m=b[5][3];break;

  case 9:i=b[1][4];j=b[2][4];k=b[3][4];l=b[4][4];m=b[5][4];break;

  case 10:i=b[1][5];j=b[2][5];k=b[3][5];l=b[4][5];m=b[5][5];break;

  case 11:i=b[1][1];j=b[2][2];k=b[3][3];l=b[4][4];m=b[5][5];break;

  case 12:i=b[1][5];j=b[2][4];k=b[3][3];l=b[4][2];m=b[5][1];break;

  }

  if((i==0&&j==1&&k==1&&l==1&&m==0))

  return (900);

  if((i==0&&j==-1&&k==-1&&l==-1&&m==0))

  return(1000);

  if((i==0&&j==0&&k==1&&l==1&&m==1)||(i==1&&j==1&&k==1&&l==0&&m==0))

  return(20);

  if((i==0&&j==0&&k==-1&&l==-1&&m==-1)||(i==-1&&j==-1&&k==-1&&l==0&&m==0))

  return(20);

  if((i==-1&&j==1&&k==1&&l==1&&m==1)||(i==1&&j==-1&&k==1&&l==1&&m==1)||(i==1&&j==1&&k==-1&&l==1&&m==1)||(i==1&&j==1&&k==1&&l==-1&&m==1)||(i==1&&j==1&&k==1&&l==1&&m==-1))

  return(-60);

  if((i==1&&j==-1&&k==-1&&l==-1&&m==-1)||(i==-1&&j==1&&k==-1&&l==-1&&m==-1)||(i==-1&&j==1&&k==-1&&l==-1&&m==-1)||(i==-1&&j==-1&&k==-1&&l==1&&m==-1)||(i==-1&&j==-1&&k==-1&&l==-1&&m==1))

  return(-60);

  }


  int wtu(int a[N+1][N+1],int write)

  {

  int i=1;

  map(a);

  zuobiao(zx,zy,1);

  while(i)

  {

  int k;

  k=tu(a,write);

  if(k==OK) i=0;

  if(k==STOP) return (STOP);

  }

  }


  int getkey()

  {

  int key,lo,hi;

  key=bioskey(0);

  lo=key&0x00ff;

  hi=(key&0xff00)>>8;

  return((lo==0) ? hi+256:lo);

  }


  int key()

  {

  int k;

  k=getkey();

  switch(k)

  {

  case 27: return (STOP);

  case 13:

  case ' ': return (OK);

  case 328: return (UP);

  case 336: return (DOWN);

  case 331: return (LEFT);

  case 333: return (RIGHT);

  default: return (NO);

  }

  }


  void zuobiao(int x,int y,int i)

  {

  int r;

  if(i!=0)

  {

  setcolor(GREEN);

  for(r=1;r<=5;r++)

  circle(75+25*x,25+25*y,r);


  }

  else

  {

  if(a[zy][zx]==1)

  {

  setcolor(Cool;

  for(r=1;r<=5;r++)

  circle(75+25*x,25+25*y,r);

  }

  else if(a[zy][zx]==-1)

  {

  setcolor(WHITE);

  for(r=1;r<=5;r++)

  circle(75+25*x,25+25*y,r);

  }

  else

  {

  setcolor(B);

  for(r=1;r<=5;r++)

  circle(75+25*x,25+25*y,r);

  setcolor(RED); line(75+25*zx-5,25+25*zy,75+25*x+5,25+25*zy);

  line(75+25*zx,25+25*zy-5,75+25*zx,25+25*zy+5);

  }

  }

  }


  int tu(int a[N+1][N+1],int write)

  {

  int k;

  re:

  k=key();

  if(k==OK)

  {

  if(a[zy][zx]==0)

  {

  a[zy][zx]=write;

  }

  else

  goto re;

  }

  if(k==STOP) return(STOP);

  if(k==NO) goto re;

  if(k==UP)

  {

  int i,j;

  if(zy==1) j=zy;

  else j=zy-1;

  zuobiao(zx,zy,0);

  zuobiao(zx,j,1);

  zy=j;

  goto re;

  }

  if(k==DOWN)

  {

  int i,j;

  if(zy==N) j=zy;

  else j=zy+1;

  zuobiao(zx,zy,0);

  zuobiao(zx,j,1);

  zy=j;

  goto re;

  }

  if(k==LEFT)

  {

  int i,j;

  if(zx==1) i=zx;

  else i=zx-1;

  zuobiao(zx,zy,0);

  zuobiao(i,zy,1);

  zx=i;

  goto re;

  }

  if(k==RIGHT)

  {

  int i,j;

  if(zx==N) i=zx;

  else i=zx+1;

  zuobiao(zx,zy,0);

  zuobiao(i,zy,1);

  zx=i;

  goto re;

  }

  }


  void cbar(int i,int x,int y,int r)

  {

  if(i!=0)

  {

  if(i==1)

  setcolor(Cool;

  else if(i==-1)

  setcolor(WHITE);

  for(i=1;i<=r;i++)

  {

  circle(x,y,i);

  }

  }

  }


  void map(int a[N+1][N+1])

  {

  int i,j;

  cleardevice();

  setbkcolor(B);

  setcolor(RED);

  for(i=0;i<N;i++)

  {

  line(100,50+25*i,75+N*25,50+25*i);

  line(100+25*i,50,100+25*i,25+N*25);

  }

  for(i=1;i<=N;i++)

  for(j=1;j<=N;j++)

  cbar(a[i][j],75+25*j,25+25*i,10);

  }
返回页首 向下
http://tyren.forumotion.com
让一切随风
Admin
avatar

帖子数 : 257
注册日期 : 12-11-03
年龄 : 26
地点 : 湖南

帖子主题: C语言的贪吃蛇源代码   周二 十一月 27, 2012 12:44 pm

#define N 200
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;/*得分*/
int gamespeed=50000;/*游戏速度自己调整*/
struct Food
{
int x;/*食物的横坐标*/
int y;/*食物的纵坐标*/
int yes;/*判断是否要出现食物的变量*/
}food;/*食物的结构体*/
struct Snake
{
int x[N];
int y[N];
int node;/*蛇的节数*/
int direction;/*蛇移动方向*/
int life;/* 蛇的生命,0活着,1死亡*/
}snake;
void Init(void);/*图形驱动*/
void Close(void);/*图形结束*/
void DrawK(void);/*开始画面*/
void GameOver(void);/*结束游戏*/
void GamePlay(void);/*玩游戏具体过程*/
void PrScore(void);/*输出成绩*/
/*主函数*/
void main(void)
{
Init();/*图形驱动*/
DrawK();/*开始画面*/
GamePlay();/*玩游戏具体过程*/
Close();/*图形结束*/
}
/*图形驱动*/
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/
void DrawK(void)
{
/*setbkcolor(LIGHTGREEN);*/
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/
for(i=50;i<=600;i+=10)/*画围墙*/
{
rectangle(i,40,i+10,49); /*上边*/
rectangle(i,451,i+10,460);/*下边*/
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10); /*左边*/
rectangle(601,i,610,i+10);/*右边*/
}
}
/*玩游戏具体过程*/
void GamePlay(void)
{
randomize();/*随机数发生器*/
food.yes=1;/*1表示需要出现新食物,0表示已经存在食物*/
snake.life=0;/*活着*/
snake.direction=1;/*方向往右*/
snake.x[0]=100;snake.y[0]=100;/*蛇头*/
snake.x[1]=110;snake.y[1]=100;
snake.node=2;/*节数*/
PrScore();/*输出得分*/
while(1)/*可以重复玩游戏,压ESC键结束*/
{
while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/
{
if(food.yes==1)/*需要出现新食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60;
while(food.x%10!=0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;/*画面上有食物了*/
}
if(food.yes==0)/*画面上有食物了就要显示*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/
switch(snake.direction)
{
case 1:snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
for(i=3;i<snake.node;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();/*显示失败*/
snake.life=1;
break;
}
}
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||
snake.y[0]>455)/*蛇是否撞到墙壁*/
{
GameOver();/*本次游戏结束*/
snake.life=1; /*蛇死*/
}
if(snake.life==1)/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y)/*吃到食物以后*/
{
setcolor(0);/*把画面上的食物东西去掉*/
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/
snake.node++;/*蛇的身体长一节*/
food.yes=1;/*画面上需要出现新的食物*/
score+=10;
PrScore();/*输出新得分*/
}
setcolor(4);/*画出蛇*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
snake.y[i]-10);
delay(gamespeed);
setcolor(0);/*用黑色去除蛇的的最后一节*/
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
} /*endwhile(!kbhit)*/
if(snake.life==1)/*如果蛇死就跳出循环*/
break;
key=bioskey(0);/*接收按键*/
if(key==ESC)/*按ESC键退出*/
break;
else
if(key==UP&&snake.direction!=4)
/*判断是否往相反的方向移动*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
/*游戏结束*/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
/*输出成绩*/
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,20,str);
}
/*图形结束*/
void Close(void)
{
getch();
closegraph();
}
返回页首 向下
http://tyren.forumotion.com
 
**晒晒你的源代码**
返回页首 
1页/共1

您在这个论坛的权限:不能在这个论坛回复主题
天涯人论坛 :: 学习交流-
转跳到: