c语言游戏代码大全
① 生命游戏c语言代码
黑白棋游戏#include"graphics.h"/*图形系统头文件*/#defineLEFT0x4b00/*光标左键值*/#defineRIGHT0x4d00/*光标右键值*/#defineDOWN0x5000/*光标下键值*/#defineUP0x4800/*光标上键值*/#defineESC0x011b/*ESC键值*/#defineENTER0x1c0d/*回车键值*/inta[8][8]={0},key,score1,score2;/*具体分数以及按键与存放棋子的变量*/charplayone[3],playtwo[3];/*两个人的得分转换成字符串输出*/voidplaytoplay(void);/*人人对战函数*/voidDrawQp(void);/*画棋盘函数*/voidSetPlayColor(intx);/*设置棋子第一次的颜色*/voidMoveColor(intx,inty);/*恢复原来棋盘状态*/intQpChange(intx,inty,intz);/*判断棋盘的变化*/voidDoScore(void);/*处理分数*/voidPrintScore(intn);/*输出成绩*/voidplayWin(void);/*输出胜利者信息*//******主函数*********/voidmain(void){intgd=DETECT,gr;initgraph(&gd,&gr,"c:\\tc");/*初始化图形系统*/DrawQp();/*画棋盘*/playtoplay();/*人人对战*/getch();closegraph();/*关闭图形系统*/}voidDrawQp()/*画棋盘*/{inti,j;score1=score2=0;/*棋手一开始得分都为0*/setbkcolor(BLUE);for(i=100;i=64-score1-score2)/*如果尝试超过空格数则停步*/{MoveColor(x,y);fillellipse(x,y,15,15);break;}elsecontinue;/*如果按键无效*/}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);}elseif(key==RIGHT&&x80)/*右方向键*/{MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);x+=40;fillellipse(x,y,15,15);}elseif(key==UP&&y>120)/*上方向键*/{MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);y-=40;fillellipse(x,y,15,15);}elseif(key==DOWN&&y1)/*判断左边*/{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+i*40,120+kk*40,15,15);}if(kk!=j-1)yes=1;}}if(i1)/*判断上边*/{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+kk*40,120+j*40,15,15);}if(kk!=i-1)yes=1;}}if(i>1&&j=0&&kk=0&&kkk&&k>=0;ii--,jj++){a[ii][jj]=a[i][j];fillellipse(120+ii*40,120+jj*40,15,15);}if(ii!=i-1)yes=1;}}if(i1)/*左下*/{for(k=i+1,kk=j-1;k=0;k++,kk--)if(a[k][kk]==a[i][j]||!a[k][kk])break;if(a[k][kk]!=0&&k=0){for(ii=i+1,jj=j-1;ii1&&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+ii*40,120+jj*40,15,15);}if(ii!=i-1)yes=1;}}if(iscore1)/*开始判断最后的结果*/outtextxy(100,50,"blackwin!");elseif(score2#include#include#include#include#defineLEFT0x4b00#defineRIGHT0x4d00#defineDOWN0x5000#defineUP0x4800#defineESC0x011b#defineSPACE0x3920#defineBILI20#defineJZ4#defineJS3#defineN19intbox[N][N];intstep_x,step_y;intkey;intflag=1;voiddraw_box();voiddraw_cicle(intx,inty,intcolor);voidchange();voidjudgewho(intx,inty);voidjudgekey();intjudgeresult(intx,inty);voidattentoin();voidattention(){charch;window(1,1,80,25);textbackground(LIGHTBLUE);textcolor(YELLOW);clrscr();gotoxy(15,2);printf("游戏操作规则:");gotoxy(15,4);printf("PlayRules:");gotoxy(15,6);printf("1、按左右上下方向键移动棋子");gotoxy(15,8);printf("1.PressLeft,Right,Up,DownKeytomovePiece");gotoxy(15,10);printf("2、按空格确定落棋子");gotoxy(15,12);printf("2.PressSpacetoplacethePiece");gotoxy(15,14);printf("3、禁止在棋盘外按空格");gotoxy(15,16);printf("3.");gotoxy(15,18);printf("你是否接受上述的游戏规则(Y/N)");gotoxy(15,20);printf("?[Y/N]:");while(1){gotoxy(60,20);ch=getche();if(ch=='Y'||ch=='y')break;elseif(ch=='N'||ch=='n'){window(1,1,80,25);textbackground(BLACK);textcolor(LIGHTGRAY);clrscr();exit(0);}gotoxy(51,12);printf("");}}voiddraw_box(){intx1,x2,y1,y2;setbkcolor(LIGHTBLUE);setcolor(YELLOW);gotoxy(7,2);printf("Left,Right,Up,DownKEYtomove,Spacetoput,ESC-quit.");for(x1=1,y1=1,y2=18;x1=1;i--)if(box[i][j]==0){draw_circle(step_x,step_y,LIGHTBLUE);break;}if(i18)break;else{for(i=step_x+1,j=step_y;i18)break;step_x=i;judgewho(step_x,step_y);break;}caseDOWN:if((step_y+1)>18)break;else{for(i=step_x,j=step_y+1;j18)break;step_y=j;judgewho(step_x,step_y);break;}caseUP:if((step_y-1)=1;j--)if(box[i][j]==0){draw_circle(step_x,step_y,LIGHTBLUE);break;}if(j=1&&step_x=1&&step_y=1;j--){if(box[j][k]==flag)n1++;elsebreak;}/*水平向右数*/for(j=x,k=y;j=5){return(1);break;}/*垂直向上数*/n1=0;n2=0;for(j=x,k=y;k>=1;k--){if(box[j][k]==flag)n1++;elsebreak;}/*垂直向下数*/for(j=x,k=y;k=5){return(1);break;}/*向左上方数*/n1=0;n2=0;for(j=x,k=y;j>=1,k>=1;j--,k--){if(box[j][k]==flag)n1++;elsebreak;}/*向右下方数*/for(j=x,k=y;j=5){return(1);break;}/*向右上方数*/n1=0;n2=0;for(j=x,k=y;j=1;j++,k--){if(box[j][k]==flag)n1++;elsebreak;}/*向左下方数*/for(j=x,k=y;j>=1,k=5){return(1);break;}return(0);break;}}voidmain(){intgdriver=VGA,gmode=VGAHI;clrscr();attention();initgraph(&gdriver,&gmode,"c:\\tc");/*setwritemode(XOR_PUT);*/flag=1;draw_box();do{step_x=0;step_y=0;/*draw_circle(step_x,step_y,8);*/judgewho(step_x-1,step_y-1);do{while(bioskey(1)==0);key=bioskey(0);judgekey();}while(key!=SPACE&&key!=ESC);}while(key!=ESC);closegraph();}
② 谁有一些简单小游戏的C语言程序
可以学写“贪吃蛇”代码:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <Windows.h>
#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;i<H;i++)
{ gtxy(0,i); printf("■"); //打印左边框
gtxy(W,i); printf("■"); //打印右边框
}
while(1)
{ srand((unsigned)time(NULL)); //启动随机数发生器srand( )
fod.x=rand()%(W-4)+2; //随机函数rand( )产生一个从0到比”(W-4)”小1的数再加2
fod.y=rand()%(H-2)+1; //随机函数rand( )产生一个从0到比”(H-2)”小1的数再加1
if (fod.x%2==0) break; //fod.x是食物的横坐标,要是2的倍数(为偶数)
}
setColor(12, 0); //设定打印颜色为淡红字黑底
gtxy(fod.x,fod.y); printf("●"); //到食物坐标处打印初试食物
snk.len=3; //蛇身长
snk.speed=350; //刷新蛇的时间,即是移动速度
snk.x[0]=W/2+1; //蛇头横坐标要为偶数(因为W/2=39)
snk.y[0]=H/2; //蛇头纵坐标
setColor(9, 0); //设定打印颜色为淡蓝字黑底
gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头
for(i=1;i<snk.len;i++)
{ snk.x[i]=snk.x[i-1]+2; snk.y[i]=snk.y[i-1];
gtxy(snk.x[i],snk.y[i]); printf("■"); //打印蛇身
}
setColor(7, 0); //恢复默认的白字黑底
return;
}
void keymove( ) //按键操作移动蛇
{ int key;
if( kbhit( ) ) //如有按键输入才执行下面操作
{ key=getch( );
if (key==224) //值为224表示按下了方向键,下面要再次获取键值
{ key=getch( );
if(key==72&&dir!=2)dir=1; //72表示按下了向上方向键
if(key==80&&dir!=1)dir=2; //80为向下
if(key==75&&dir!=4)dir=3; //75为向左
if(key==77&&dir!=3)dir=4; //77为向右
}
if (key==32)
{ while(1) if((key=getch( ))==32) break; } //32为空格键,这儿用来暂停
}
if (Flag==0) //如没吃食物,才执行下面操作擦掉蛇尾
{gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); printf(" "); }
int i;
for (i = snk.len - 1; i > 0; i--) //从蛇尾起每节存储前一节坐标值(蛇头除外)
{ snk.x[i]=snk.x[i-1]; snk.y[i]=snk.y[i-1]; }
switch (dir) //判断蛇头该往哪个方向移动,并获取最新坐标值
{ case 1: snk.y[0]--; break; //dir=1要向上移动
case 2: snk.y[0]++; break; //dir=2要向下移动
case 3: snk.x[0]-=2; break; //dir=3要向左移动
case 4: snk.x[0]+=2; break; //dir=4要向右移动
}
setColor(9, 0);
gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头
if (snk.x[0] == fod.x && snk.y[0] == fod.y) //如吃到食物则执行以下操作
{ printf("