当前位置:首页 » 编程软件 » c游戏编程

c游戏编程

发布时间: 2022-01-09 22:57:37

c语言 游戏编程

必读的书 《计算机图形学》
《数据结构》
《C语言》 中等难度的,别太浅了

建议参考读的书 《C语言编程100例》

另外可以上论坛,网络学习.

不过书是一定要读的。

② 用C语言编写小游戏

用c语言编写一个五子棋吧,不怎么难,给你程序,自己参考一下

/*3.3.4 源程序*/
#include "graphics.h" /*图形系统头文件*/
#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+3*40,120+3*40,15,15);
fillellipse(120+4*40,120+4*40,15,15);
setfillstyle(SOLID_FILL,8);
fillellipse(120+3*40,120+4*40,15,15);
fillellipse(120+4*40,120+3*40,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+i*40,120+kk*40,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+i*40,120+kk*40,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+kk*40,120+j*40,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+kk*40,120+j*40,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+ii*40,120+jj*40,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+ii*40,120+jj*40,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+ii*40,120+jj*40,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+ii*40,120+jj*40,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!");
}

③ c语言游戏编程

C++吧,先研究一下D3D驱动模块吧。如果就你一个人,以你现在提出问题的水平,估计够呛,两年吧,或许更长,看你有没有毅力了

④ 求一个用C语言编写的小游戏代码

/*也不知道你是什么级别的,我是一个新手,刚接触编程语言,以下是我自己变得一个小程序,在所有c语言的编译器(vc++6.0、turbo…………)上都能运行,你还可以进一步改进。这是一个类似贪吃蛇的小游戏。祝你好运*/
/*贪吃蛇*/
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
int head=3 ,tail=0;
int main()
{
int i,j,k=0;
int zuobiao[2][80];
long start;
int direction=77;
int gamespeed;
int timeover;
int change(char qipan[20][80],int zuobiao[2][80],char direction);
zuobiao[0][tail]=1;zuobiao[1][tail]=1;zuobiao[0][1]=1;zuobiao[1][1]=2;zuobiao[0][2]=1;zuobiao[1][2]=3;zuobiao[0][head]=1;zuobiao[1][head]=4;
/*处理棋盘*/
char qipan[20][80];//定义棋盘
for(i=0;i<20;i++)
for(j=0;j<80;j++)
qipan[i][j]=' ';//初始化棋盘
for(i=0;i<80;i++)
qipan[0][i]='_';
for(i=0;i<20;i++)
qipan[i][0]='|';
for(i=0;i<20;i++)
qipan[i][79]='|';
for(i=0;i<80;i++)
qipan[19][i]='_';
qipan[1][1]=qipan[1][2]=qipan[1][3]='*';//初始化蛇的位置
qipan[1][4]='#';
printf("This is a game of a SNAKE.\nGOOD LUCK TO YOU !\n");
printf("Input your game speed,please.(e.g.300)\n");
scanf("%d",&gamespeed);

while(direction!='q')
{
system("cls");
for(i=0;i<20;i++)//打印出棋盘
for(j=0;j<80;j++)
printf("%c",qipan[i][j]);
timeover=1;
start=clock();
while(!kbhit()&&(timeover=clock()-start<=gamespeed));
if(timeover)
{
getch();
direction=getch();
}
else
direction=direction;
if(!(direction==72||direction==80||direction==75||direction==77))
{
return 0;
system("cls");
printf("GAME OVER!\n");
}
if(!change(qipan,zuobiao,direction))
{
direction='q';
system("cls");
printf("GAME OVER!\n");
}
}
return 0;
}
int change(char qipan[20][80],int zuobiao[2][80],char direction)
{
int x,y;
if(direction==72)
x=zuobiao[0][head]-1;y=zuobiao[1][head];
if(direction==80)
x=zuobiao[0][head]+1;y=zuobiao[1][head];
if(direction==75)
x=zuobiao[0][head];y=zuobiao[0][head]-1;
if(direction==77)
x=zuobiao[0][head];y=zuobiao[1][head]+1;
if(x==0||x==18||y==78||y==0)
return 0;
if(qipan[x][y]!=' ')
return 0;
qipan[zuobiao[0][tail]][zuobiao[1][tail]]=' ';
tail=(tail+1)%80;
qipan[zuobiao[0][head]][zuobiao[1][head]]='*';
head=(head+1)%80;
zuobiao[0][head]=x;
zuobiao[1][head]=y;
qipan[zuobiao[0][head]][zuobiao[1][head]]='#';
return 1;
}

⑤ 求c语言游戏编程源代码~~

把你邮箱给我吧~~~~

我有个火影忍者的游戏C语言编的~~~我发给你

⑥ c语言游戏编程,下落的小鸟 求代码

下落的小鸟

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<Windows.h>
int Grade = 1, Score = 0, Max_blank = 9, Distance = 18;
struct Birds{int x; int y;}; //定义一种Birds数据类型(含3个成员)
Birds *Bird = (Birds*)malloc(sizeof(Birds)); //定义Birds类型 指针变量Bird并赋初值
struct Bg{int x, y; int l_blank; Bg *pri; Bg *next;}; //定义一种Bg数据类型(含5个成员)
Bg *Bg1 = (Bg*)malloc(sizeof(Bg)); //定义Bg类型 指针变量Bg1并赋初值

void Position(int x, int y) //光标定位函数(用于指定位置输出)
{COORD pos = { x - 1, y - 1 };
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}

void Csh( ) //初始化界面

{

printf("══════════════════════════════════════ ");

printf(" ■■ ■■ C语言版 Flappy Bird ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ 瞎搞人:yyposs原创 ");

printf(" ■■ ■■ 瞎搞日期:2014.2 ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ 改编:鸣蝉百2021.7 ");

printf(" ■■ ■■ 操作:按向上方向键让小鸟起飞 ");

printf(" ■■ ");

printf(" ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ DEVc++运行通过 ");

printf("══════════════════════════════════════ ");

printf(" 按键继续…");

getch( );

system("cls");

}

void PrFK( ) //输出方框(游戏范围区)

{int i;

Position(1, 1); printf("╔"); Position(79, 1); printf("╗");

Position(1, 24); printf("╚"); Position(79, 24); printf("╝");

for (i = 3; i <= 78; i += 2){Position(i, 1); printf("═"); Position(i, 24); printf("═");}

for(i=2;i<=23;i++)

{ Position(1,i); printf("║");if(i<11)printf("0%d",i-1);else printf("%d",i-1);

Position(79,i); printf("║");

}

Position(4, 25); printf("小鸟即将出现,请准备按键起飞… ");

getch( );

Position(4, 25); printf(" ");

}

void CreatBg( ) //创建障碍物坐标(便于打印输出)
{Bg *Bg2 = (Bg*)malloc(sizeof(Bg));
Bg1->x = 90; Bg1->y = 8; //确定障碍物的一对基本坐标(此时值是在游戏框之外)
Bg2->x = Bg1->x + Distance; Bg2->y = 9; //下一障碍物的基本坐标x、y
Bg1->l_blank = Max_blank - Grade; //障碍物上下两部分之间的空白距离l_blank
Bg2->l_blank = Max_blank - Grade;
Bg1->next = Bg2; Bg1->pri = Bg2;
Bg2->next = Bg1; Bg2->pri = Bg1;
}


void InsertBg(Bg *p) //随机改变障碍物的y坐标,让空白通道有上下变化
{int temp;
Bg *Bgs = (Bg*)malloc(sizeof(Bg));
Bgs->x = p->pri->x + Distance;
Bgs->l_blank = Max_blank - Grade;
srand((int)time(0)); //启动随机数发生器
temp = rand( ); //产生一个随机数并赋值给temp
if (temp % 2 == 0)
{if ((temp % 4 + p->pri->y + Max_blank - Grade)<21)
Bgs->y = p->pri->y + temp % 4;
else Bgs->y = p->pri->y;
}
else
{if ((p->pri->y - temp % 4)>2)Bgs->y = p->pri->y - temp % 4;
else Bgs->y = p->pri->y;
}
Bgs->pri = p->pri; Bgs->next = p;
p->pri->next = Bgs; p->pri = Bgs;
}

void CreatBird( ) //建立小鸟的坐标(初始打印输出小鸟的位置)
{Bird->x = 41; Bird->y = 10;}

int CheckYN(Bg *q) //判断游戏结束与否(值为0是要结束,为1没有要结束)
{Bg *p = q; int i = 0;
while (++i <= 5)
{if (Bird->y>23)return 0;
if (Bird->x == p->x&&Bird->y <= p->y)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y)return 0;
if (Bird->x == p->x&&Bird->y>p->y + p->l_blank)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y + p->l_blank)
return 0;
p = p->next;
}
return 1;
}


void Check_Bg(Bg *q) //核查开头的障碍物坐标是否在游戏区内
{Bg *p = q; int i = 0, temp;
while (++i <= 5)
{if (p->x>-4)p = p->next;
else
{srand((int)time(0)); temp = rand();
if (temp % 2 == 0)
{if ((temp % 4 + p->y + Max_blank - Grade)<21)p->y = p->y + temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
else
{if ((p->y - temp % 4)>2)p->y = p->y - temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
}
}
}

void Prt_Bg(Bg *q) //打印输出障碍物(依据其x、y坐标进行相应输出)
{Bg *p = q; int i = 0, k, j;
while (++i <= 5)
{if (p->x>0 && p->x <= 78)
{for (k = 2; k<p->y; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
Position(p->x, p->y);
printf("■"); printf("■"); printf("■"); printf(" ");
Position(p->x, p->y + p->l_blank);
printf("■"); printf("■"); printf("■"); printf(" ");
k = k + p->l_blank + 1;
for (k; k <= 23; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
}
p = p->next;
if (p->x == 0)
{for (j = 2; j<p->y; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
Position(p->x + 1, p->y);
printf(" "); printf(" "); printf(" ");
Position(p->x + 1, p->y + Max_blank - Grade);
printf(" "); printf(" "); printf(" ");
j = j + Max_blank - Grade + 1;
for (j; j <= 22; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
}
}
}

void PrtBird( ) //打印输出小鸟
{Position(Bird->x, Bird->y - 1); printf(" ");
Position(Bird->x, Bird->y); printf("Ю");
Position(38, 2); printf("Score:%d", Score);
}
void Loop_Bg(Bg *q) //障碍物x坐标左移,并记录成绩
{Bg *p = q; int i = 0;
while (++i <= 5)
{p->x = p->x - 1; p = p->next;
if (Bird->x == p->x)
{Score += 1;
if (Score % 4 == 0 && Grade<4)Grade++;
}
}
}

int main( )
{int i = 0; int t;

while (1)

{
Csh( );PrFK( );CreatBg( );
InsertBg(Bg1);InsertBg(Bg1);InsertBg(Bg1);
CreatBird( );
while (1)
{if (!CheckYN(Bg1))break;
Check_Bg(Bg1);Prt_Bg(Bg1);
PrtBird( );Loop_Bg(Bg1);
Bird->y = Bird->y + 1;
if (GetAsyncKeyState(VK_UP))
//按下了向上方向键
{Position(Bird->x, Bird->y - 1);printf(" ");
Bird->y = Bird->y - 4;
}
Sleep(200);
//程序延时200毫秒(数值大小决定游戏速度快慢)
i = 0;
}
Position(6, 25);
printf("游戏结束! 请输入:0.退出 1.重玩");

scanf("%d",&t);

if (t==0)break;

system("cls"); Score = 0;

}
return 0;
}

⑦ 《C游戏编程从入门到精通》pdf下载在线阅读全文,求百度网盘云资源

《C游戏编程从入门到精通》(浦滨)电子书网盘下载免费在线阅读

链接:

提取码: 1qek

书名:C游戏编程从入门到精通

作者:浦滨

出版社:北希电脑出版社

出版年份:2002-5-1

页数:408

⑧ c语言游戏编程实例

生命游戏
/* ------------------------------------------------------ */
/* PROGRAM game of life : */
/* This is a finite implementation of John H. Conway's */
/* Game of Life. Refere to my book for detail please. */
/* */
/* Copyright Ching-Kuang Shene July/25/1989 */
/* ------------------------------------------------------ */

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 50 /* board size */
#define OCCUPIED 1 /* occupied flag */
#define UNOCCUPIED 0
#define YES 1
#define NO 0

char cell[MAXSIZE][MAXSIZE]; /* the board */
char work[MAXSIZE][MAXSIZE]; /* a working */
int row; /* No. of rows you want */
int column; /* no. of columns you want */
int generations; /* maximum no. of generation*/

/* ------------------------------------------------------ */
/* FUNCTION read_in : */
/* This function reads in the number of generations, */
/* the number of rows, the number of columns and finally */
/* the initial configuration (the generation 0). Then put*/
/* your configuration to the center of the board. */
/* ------------------------------------------------------ */

void read_in(void)
{
int max_row, max_col; /* max # of row and col. */
int col_gap, row_gap; /* incremnet of row and col */
int i, j;
char line[100];

gets(line); /* read in gens, row and col*/
sscanf(line, "%d%d%d", &generations, &row, &column);
for (i = 0; i < row; i++)/* clear the board */
for (j = 0; j < column; j++)
cell[i][j] = UNOCCUPIED;
max_col = 0; /* read in the config. */
for (max_row = 0; gets(line) != NULL; max_row++) {
for (i = 0; line[i] != '\0'; i++)
if (line[i] != ' ')
cell[max_row][i] = OCCUPIED;
max_col = (max_col < i) ? i : max_col;
}
row_gap = (row - max_row)/2; /* the moving gap */
col_gap = (column - max_col)/2;
for (i = max_row + row_gap - 1; i >= row_gap; i--) {
for (j = max_col + col_gap - 1; j >= col_gap; j--)
cell[i][j] = cell[i-row_gap][j-col_gap];
for ( ; j >= 0; j--)
cell[i][j] = UNOCCUPIED;
}
for ( ; i >= 0; i--)
for (j = 0; j < column; j++)
cell[i][j] = UNOCCUPIED;
}

/* ------------------------------------------------------ */
/* FUNCTION display : */
/* Display the board. */
/* ------------------------------------------------------ */

#define DRAW_BOARDER(n) { int i; \
printf("\n+"); \
for (i = 0; i < n; i++) \
printf("-"); \
printf("+"); \
}
void display(int gen_no)
{
int i, j;

if (gen_no == 0)
printf("\n\nInitial Generation :\n");
else
printf("\n\nGeneration %d :\n", gen_no);

DRAW_BOARDER(column);
for (i = 0; i < row; i++) {
printf("\n|");
for (j = 0; j < column; j++)
printf("%c", (cell[i][j] == OCCUPIED) ? '*' : ' ');
printf("|");
}
DRAW_BOARDER(column);
}

/* ------------------------------------------------------ */
/* FUNCTION game_of_life : */
/* This is the main function of Game of Life. */
/* ------------------------------------------------------ */

void game_of_life(void)
{
int stable; /* stable flag */
int iter; /* iteration count */
int top, bottom, left, right; /* neighborhood bound */
int neighbors; /* # of neighbors */
int cell_count; /* # of cells count */
int done;
int i, j, p, q;

display(0); /* display initial config. */
done = NO;
for (iter = 1; iter <= generations && !done; iter++) {
memmove(work, cell, MAXSIZE*MAXSIZE); /**/
stable = YES; /* assume it is in stable */
cell_count = 0; /* # of survived cells = 0 */
for (i = 0; i < row; i++) { /* scan each cell...*/
top = (i == 0) ? 0 : i - 1;
bottom = (i == row - 1) ? row-1 : i + 1;
for (j = 0; j < column; j++) {
left = (j == 0) ? 0 : j - 1;
right = (j == column - 1) ? column-1 : j + 1;

/* compute number of neighbors */

neighbors = 0;
for (p = top; p <= bottom; p++)
for (q = left; q <= right; q++)
neighbors += work[p][q];
neighbors -= work[i][j];

/* determine life or dead */

if (work[i][j] == OCCUPIED)
if (neighbors == 2 || neighbors == 3) {
cell[i][j] = OCCUPIED;
cell_count++;
}
else
cell[i][j] = UNOCCUPIED;
else if (neighbors == 3) {
cell[i][j] = OCCUPIED;
cell_count++;
}
else
cell[i][j] = UNOCCUPIED;
stable = stable && (work[i][j] == cell[i][j]);
}
}
if (cell_count == 0) {
printf("\n\nAll cells die out.");
done = YES;
}
else if (stable) {
printf("\n\nSystem enters a stable state.");
done = YES;
}
else
display(iter);
}
}

/* ------------------------------------------------------ */

void main(void)
{
read_in();
game_of_life();
}


⑨ c语言猜数游戏编程

在VC 平台测试通过。
#include"stdio.h"
#define MAX_NUM 4
#define TRUE 1
#define FALSE 0
void main()
{
int i,j;
int guess_num[MAX_NUM];//随机生成四个数
int input[MAX_NUM];//用户输入四个数
int pos_same = 0;//位置相同个数
int num_same = 0;//数字相同个数
int correct = FALSE;//判断输入是否完全正确

//随机生成四个0到10之间的数
for(i = 0;i<MAX_NUM;i++)
{
guess_num[i] = (int)rand()%10;
}
for(i = 0;i<MAX_NUM;i++)
{
printf(" %d",guess_num[i]);
}
printf("\n");printf("请输入四个0到10之间的数\n");

while(!correct)
{
//输入四个数
for(i = 0;i<MAX_NUM;i++)
{
scanf("%d",&input[i]);
}
//获得位置相同的个数
for(i = 0;i<MAX_NUM;i++)
{
if(input[i] == guess_num[i])
pos_same++;
}
//获得与随机数相同的个数
for(i = 0;i<MAX_NUM;i++)
{
for(j = 0;j<MAX_NUM;j++)
{
if(input[i] == guess_num[j])
{
num_same++;
break;
}

}
if(i>0)//判断与之前的数字是否相同
{
for(j=i;j>0;j--)
{
if(input[i] ==input[j-1])
{
num_same--;
break;
}
}
}
}
printf("数字正确个数: %d \n位置正确个数: %d\n",num_same,pos_same);
num_same = (num_same ==MAX_NUM)?num_same:0;
pos_same = (pos_same ==MAX_NUM)?pos_same:0;
if(pos_same ==MAX_NUM && num_same ==MAX_NUM)
correct = TRUE;
}
printf("你猜对了!\n数字就是");
for(i = 0;i<MAX_NUM;i++)
{
printf(" %d",guess_num[i]);
}
printf("\n");

}

⑩ c语言编程小游戏

原创:
TC2.0以及gcc 编译通过

/*=======================================================
*Author :wacs5
*Date :20100601(YYYYMMDD)
*Function :剪刀石头布
*=======================================================*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
char name[4][15]=;
int x[2];
int i;

srand(time(NULL));
for (i=0;i<10;i++) /*10 times game*/
{
x[0]=1+rand()%3; /*generate a number from 1 to 3*/
x[1]=1+rand()%3; /*generate another number from 1 to 3*/
printf("A=%-12sB=%-12s\t",name[x[0]],name[x[1]]);
if (x[0]==x[1])
printf("draw\n"); /*和*/
else if (x[0]%3+1==x[1]) /*lost*/
printf("lost\n");
else /*win*/
printf("win\n");
}

getch();
return 0;
}

热点内容
域名指定服务器ip 发布:2024-09-20 22:26:43 浏览:673
jsp系统源码下载 发布:2024-09-20 22:22:22 浏览:866
怎么给云服务器套盾 发布:2024-09-20 22:06:21 浏览:8
我的世界建筑大师设计服务器 发布:2024-09-20 22:05:37 浏览:151
sqlserver的check约束 发布:2024-09-20 22:05:25 浏览:405
编程需要p 发布:2024-09-20 22:03:55 浏览:238
安卓小q画笔哪个版本可以用 发布:2024-09-20 21:57:03 浏览:368
mcgs脚本例程 发布:2024-09-20 21:47:01 浏览:697
java配置mysql 发布:2024-09-20 21:32:44 浏览:759
phpapache伪静态 发布:2024-09-20 20:54:45 浏览:588