怎么写c游戏

1. 怎样用C语言编写一个小游戏 “贪吃蛇”C代码:#include #include #include #include #include #define W 78 //游戏框的宽,x轴#define H 26 //游戏框的高,y轴 int dir=3; //方向变量,初值3表示向“左” int Flag=0; //吃了食物的标志(1是0否) int score=0; //玩家得分 struct food{ int x; //食物的x坐标 int y; //食物的y坐标 }fod; //结构体fod有2个成员 struct snake{ int len; //身长 int speed; //速度 int x[100]; int y[100]; }snk; //结构体snk有4个成员 void gtxy( int x,int y) //控制光标移动的函数 { COORD coord; coord.X=x; coord.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } void gtxy( int x,int y); //以下声明要用到的几个自编函数 void csh( ); //初始化界面 void keymove( ); //按键操作移动蛇 void putFod( ); //投放食物 int Over( ); //游戏结束(1是0否) void setColor(unsigned short p, unsigned short q); //设定显示颜色 int main( ) //主函数 { csh( ); while(1) { Sleep(snk.speed); keymove( ); putFod( ); if(Over( )) {system(“cls”); gtxy(W/2+1,H/2); printf(“游戏结束!T__T”); gtxy(W/2+1,H/2+2); printf(“玩家总分:%d分”,score); getch( ); break; } } return 0; } void csh( ) //初始化界面 { int i; gtxy(0,0); CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下两行是隐藏光标的设置 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); for(i=0;i<=W;i=i+2) //横坐标要为偶数,因为这个要打印的字符占2个位置 { setColor(2, 0); //设定打印颜色为绿字黑底 gtxy(i,0); printf("■"); //打印上边框 gtxy(i,H); printf("■"); //打印下边框 } for(i=1;i2. 求C写的最简单的游戏 猜数字游戏,希望对你有帮助# include# include# includeint main(){ int a,b; char c; srand(time(NULL)); a=1+(rand()%1000); printf("I have a number between 1 and 1000.\nCan you guess my number?\nPlease type your first guess.\n"); scanf("%d",&b); while(b!=-1) { if(b==a) { printf("Excellent! You guessed the number!\nWould you like to play again(y or n)?"); scanf("%c",&c); scanf("%c",&c); switch(c){ case 'y': printf("I have a number between 1 and 1000.\nCan you guess my number?\nPlease type your first guess.\n"); scanf("%d",&b); break; case 'n': goto END; break; } } while(ba) { printf("Too high.Try again."); scanf("%d",&b); } }END: return 0;} 。
3. 如何用c语言写一个游戏 请编译前在主目录下建立一个文本文件(xia.txt)内容为:0 0 0 0 0 哈哈 #include #include #include #include union REGS regs; unsigned int zhuan[5]; int Msinit(); void Setmouse(int left,int right,int top,int buttom); int Msread(int *xp,int *yp,int *bup); void Draw(int x,int y,int sizex,int sizey); main() {int mode=VGAHI,driver=VGA; unsigned int l; int i,gi,gj,j,flag=1;/*i,j是循环变量,flag是标记变量,-1:向x负半轴移动,+1:向x正半轴移动*/ double qx,qy,k,b=0.0,speech=0.4,x,y; double r=2.0,bx=60.0,byy=270.0; double pianx=100.0,piany=100.0,tx=20.0,ty=10.0,jx=2.0,jy=2.0; int mx=0,my=0,mb,sum;/*sum纪录砖块的数目*/ FILE * p; if((p = fopen("xia.txt", "r")) == NULL) {printf("The file cannot open!\n"); exit(1);} initgraph(&driver,&mode,"\\tc\\BGI"); Msinit(); Setmouse((int)(pianx+1+bx/2),(int)((tx+jx)*8+pianx-1-bx/2),(int)byy+piany,(int)byy+piany); star:cleardevice();/*程序重载的介入点*/ sum=0; qx=100.0+pianx-10;qy=180.0+pianx-10;k=0.33; setcolor(7); rectangle((int)(pianx-2),(int)(piany-2),(int)((tx+jx)*8+2+pianx),302+piany); setcolor(1); rectangle((int)(pianx-1),(int)(piany-1),(int)((tx+jx)*8+1+pianx),301+piany); /*读取盘面情况*/ for(i=0;i<5;i++) fscanf(p,"%x ",&zhuan[i]); /*画砖块*/ for(i=0;i<5;i++) {l=1; for(j=0;j<16;j++) {if((zhuan[i]&l)==0) {Draw((int)((jx+tx)*((16*i+j)%8)+pianx+jx),(int)((jy+ty)*((int)((16*i+j)/8))+piany+jy), (int)tx,(int)ty); sum+=1; } l=l*2; } } for(;;) { setfillstyle(1, 0); bar(mx-bx/2,my,mx+bx/2,my+5); Msread(&mx, &my, &mb); Draw(mx-bx/2,my,bx,5); setcolor(0); circle(qx,qy,r); /*判断求是否反弹*/ if(qx-r