用C语言设计小游戏的程序急!!!

C++实现的“贪吃蛇”游戏源代码。

// greedsnake.cpp

# include & ltbios.h & gt

# include & ltconio.h & gt

# include & ltdos.h & gt

# include & ltgraphics.h & gt

# include & ltstdlib.h & gt

# include & lttime.h & gt

#包含“conf.h”

typedef结构节点

{

int x,y;

结构节点* next

}节点;

typedef结构

{

节点*头,*尾;

int长度;

}蛇;

typedef结构

{

int left,top,right,bottom

}框架;

typedefinenum//四个方向

{

上,下,左,右

}方向;

typedef枚举

{

假的,真的

} bool//*/

void InitGraphMode();//初始化图形驱动程序

void CloseGraphMode();

void Foot(int,int);

void Head(int,int);

void create frame();//完成整个游戏框架的绘制。

void create snake();//创建一个有两个节点的蛇,蛇的每一段都是队列中的一个节点。

bool play game();//游戏的主要功能,

int Hit(int,int);//判断是过线还是撞到自己。这两个参数是新头部接触的X和Y坐标。

bool game over();//画出游戏结束时弹出的对话框。

void入队(节点);//入队函数

节点出列();//出列函数

void ClearKeyBuf();//清除键盘缓冲区。该功能可以消除不断按无效键的影响。

蛇蛇;

框架框架;

void main()

{

InitGraphMode();

{

create frame();

} while(PlayGame());

CloseGraphMode();

}

void InitGraphMode()

{

int gdriver=DETECT,gmode

init graph(& amp;g driver & amp;gmode,"../bgi/";

clear device();

}

void CloseGraphMode()

{

clear device();

closegraph();

}

void CreateFrame()

{

setbkcolor(青色);

//下面四行代码用于计算主框架左上角和右下角的坐标。

frame . left =(get maxx()+1-block width * RowOfFrame)/2;

frame . top =(get maxy()+1-block height * column off frame)/2;

frame . right = frame . left+block width * RowOfFrame;

frame . bottom = frame . top+block height * column off frame;

头(frame.left+100,frame . top-20);

setfillstyle(SOLID_FILL,浅灰色);

bar(frame.left,frame.top,frame.right,frame . bottom);

setlinestyle(SOLID_LINE,1,1);

setcolor(深灰色);

line(frame.left,frame.top,frame.right,frame . top);

line(frame.left,frame.top,frame.left,frame . bottom);

setlinestyle(SOLID_LINE,1,1);

setcolor(白色);

line(frame.left,frame.bottom,frame.right,frame . bottom);

line(frame.right,frame.top,frame.right,frame . bottom);

setlinestyle(DOTTED_LINE,1,1);

setcolor(蓝色);

for(int row = 1;row & ltRowOfFramerow++)

line(frame.left+row*BlockWidth,frame.top,frame.left+row*BlockWidth,frame . bottom);

for(int column = 1;列& ltcolumn off frame;列++)

line(frame.left,frame.top+column*BlockHeight,frame.right,frame . top+column * block height);

脚(frame.left,frame . bottom+20);

}

void CreateSnake()

{

Node * Node 1 =新节点;

Node * node2 =新节点;

node 1->;x = frame . left+block width;

node 1->;y = frame.top

node 1->;next = NULL

snake . tail = node 1;

节点2->x = frame.left

节点2->y = frame.top

节点2->next = snake.tail

snake.head = node2

snake . length = 2;

setfillstyle(SOLID_FILL,蓝色);

bar(snake . head-& gt;x+1,snake . head-& gt;y+1,snake . head-& gt;x+BlockWidth-1,snake . head-& gt;y+block height-1);

bar(snake . tail-& gt;x+1,snake . tail-& gt;y+1,snake . tail-& gt;x+BlockWidth-1,snake . tail-& gt;y+block height-1);

}

布尔游戏()

{

int speed=300,key

方向CurrentDirection = right

Node randomNode

节点newNode,outNode

bool neednode = true

bool overlap = false

int randx,randy

create snake();

while(真)

{

if(neednode==true)

{

randomize();

{

randx = frame . left+rand()% RowOfFrame * block width;

Randy = frame . top+rand()% column off frame * block height;

for(Node * p = snake . head;p!= NULLp = p-& gt;下一步)//击中自身

if(randx = = p-& gt;x & amp& amp兰迪= = p-& gt;y)

{ overlap = true打破;}

}

while(overlap = = true);

randomNode.x = randx

randomNode.y = randy

randomNode.next = NULL

setfillstyle(SOLID_FILL,红色);

bar(randomNode.x+1,randomNode.y+1,random node . x+block width-1,random node . y+block height-1);

neednode = false

}

if((key=bioskey(1))!=0)

{

开关(钥匙)

{

case ESC:返回false

案例向上:

if(当前方向!=向下)

CurrentDirection = up

ClearKeyBuf();

打破;

案例向下:

if(当前方向!=向上)

CurrentDirection =向下;

ClearKeyBuf();

打破;

案例左侧:

if(当前方向!=右)

CurrentDirection =左;

ClearKeyBuf();

打破;

案例权利:

if(当前方向!=左)

CurrentDirection =右;

ClearKeyBuf();

打破;

案例page up:speed = speed-100;

如果(速度& lt100)

速度= 100;

ClearKeyBuf();

打破;

案例PAGEDOWN:速度=速度+100;

如果(速度& gt500)

速度= 500;

ClearKeyBuf();

打破;

默认:break

}

}

int headx = snake . tail-& gt;x;

int heady = snake . tail-& gt;y;

开关(电流方向)

{

case up:heady-= block height;打破;

case down:heady+= block height;打破;

case left:headx-= block width;打破;

case right:headx+= block width;打破;

}

if(Hit(headx,heady))//蛇是撞墙还是自己

返回game over();

其他

{//吃

if(headx = = random node . x & amp;& ampheady==randomNode.y)

{

enqueue(random node);

setfillstyle(SOLID_FILL,蓝色);

bar(randomNode.x+1,randomNode.y+1,random node . x-1+block width,random node . y-1+block height);

neednode = true

}

否则//不吃

{

newNode.x = headx

newNode.y = heady

newNode.next = NULL

入队(new node);

out node = Dequeue();

setfillstyle(SOLID_FILL,浅灰色);

bar(outNode.x+1,outNode.y+1,outNode.x+BlockWidth-1,out node . y+block height-1);

setfillstyle(SOLID_FILL,蓝色);

bar(newNode.x+1,newNode.y+1,newNode.x-1+BlockWidth,new node . y-1+block height);

}

}

延迟(速度);

}

}

void ClearKeyBuf()

{

BIOS key(0);

while(BIOS key(1));

}

空脚(int x,int y)

{

setcolor(蓝色);

outtextxy(x,y," writer:[T]RealXL邮箱:real general @ hotmail . com ");

}

空心头(int x,int y)

{

setcolor(红色);

outtextxy(x,y,“贪吃蛇”);

}

无效入队(节点inNode)

{

Node * p =新节点;

p->;x = inNode.x

p->;y = inNode.y

p->;next = inNode.next

snake . tail-& gt;next = p;

snake . tail = p;

snake . length++;

}

节点出列()

{

Node * p = snake.head

node out node = * p;

snake . head = p-& gt;接下来;

snake . length-;

删除p;

返回outNode

}

int Hit(int x,int y)

{

if(x & lt;frame.left | | x & gt= frame.right | | y & ltframe.top | | y & gt= frame . bottom)//撞墙

返回1;

node * p = snake . head-& gt;接下来;

for(int I = snake . length-1;我& gt3;i -,p = p-& gt;下一步)//击中自身

if(x = = p-& gt;x & amp& ampy = = p-& gt;y)

返回1;

返回0;

}

布尔游戏结束()

{

int x = get maxx()/2-50;

int y = get maxy()/2-20;

setfillstyle(SOLID_FILL,深灰色);

bar(x+3,y+3,x+103,y+43);

setfillstyle(SOLID_FILL,洋红色);

bar(x,y,x+100,y+40);

setlinestyle(0,3,1);

setcolor(红色);

矩形(x,y,x+100,y+40);

outtextxy(x+20,y+10,"仪表完毕!");

char c;

While(true) //按Q或Q退出程序,按R或R重启游戏。

{

c = getch();

if(c=='q'||c=='Q ')

返回false

else if(c=='r'||c=='R ')

返回true

}

}

C++五子棋源程序:

#包括

#包括

#包括

#定义背景色青色

#定义默认颜色黑色

#定义线条颜色洋红色

# define player 1 _颜色红色

#定义播放器2 _颜色白色

#定义错误_红色

#定义赢家_红色

const int left = 40

const int top = 390

const int d = 30

const int line _ num = 9;

const int turn = 0;

const int r = d/3;

const int j = 10;

int x,y,k=1,step =(line _ num+1)*(line _ num+1);

union REGS regs1,regs2

class player 1;

class player2

潘琪级

公共:

潘琪();

~潘琪(){ };

void init _潘琪();

朋友无效下降(player 1 & amp;num1,player2 & ampnum2,潘琪& ampnum);

朋友无效输入(player 1 & amp;num1,player2 & ampnum2,潘琪& ampnum);

私人:

int point[line _ num+1][line _ num+1];

};

玩家等级1{

公共:

player 1();

~ player 1(){ };

朋友无效下降(player 1 & amp;num1,player2 & ampnum2,潘琪& ampnum);

朋友无效输入(player 1 & amp;num1,player2 & ampnum 2);

朋友int法官_赢家(player 1 & amp;num1,player2 & ampnum 2);

私人:

int point 1[line _ num+1][line _ num+1];

};

班级玩家2{

公共:

player 2();

~ player 2(){ };

朋友无效下降(player 1 & amp;num1,player2 & ampnum2,潘琪& ampnum);

朋友无效输入(player 1 & amp;num1,player2 & ampnum2,潘琪& ampnum);

朋友int法官_赢家(player 1 & amp;num1,player2 & ampnum 2);

私人:

int point 2[line _ num+1][line _ num+1];

};

无效输入(player 1 & amp;num1,player2 & ampnum 2);

虚空坠落(player 1 & amp;num1,player2 & ampnum2,潘琪& ampnum);

国际法官_赢家(潘琪& amp1 & amp;num1,player2 & ampnum 2);

void input terror();

void display _ winner(int);

void main()

{

int driver=DETECT,mode

init graph(& amp;司机& ampmode," e:\ tc30 \ bgi ");

潘琪num

player 1 num 1;

player2 num2

当(步骤-)

{

输入(num1,num2,num);

fall(num1,num2,num);

if(judge_winner(num1,num2))

{

display _ winner(k);

}

}

//getchar();

}

潘琪::潘琪(无效)

{ int j,I;

char ch[2]= " 0 ";

setbkcolor(back color);

set color(line color);

for(I = 0;我& lt= line _ numi++)

{

line(left,top-i*d,left+line_num*d,top-I * d);

}

for(I = 0;我& lt= line _ numi++)

{

line(left+i*d,top,left+i*d,top-line _ num * d);

}

for(I = 0;我& lt= line _ numi++)

{ if(* ch = = ' 9 '+1)* ch = ' a ';

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

outtextxy(left+i*d-2,top+r+3,ch);

(* ch)=(* ch)+1;

}

* ch =“0”;

for(I = 0;我& lt= line _ numi++)

{ if(* ch = = ' 9 '+1)* ch = ' a ';

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

outtextxy(left-r-10,top-d*i-3,ch);

(* ch)=(* ch)+1;

}

set color(default color);

for(I = 0;我& lt= line _ numi++)

{

for(j = 0;j & lt= line _ numj++)

点[I][j]= 0;

}

}

虚空坠落(player 1 & amp;num1,player2 & ampnum2,潘琪& amp数字)

{

int flag = k % 2;

if(标志)

{ set color(player 2 _ color);

num 2 . point 2[x][y]= 1;

数字点[x][y]= 2;

circle(left+d*x,top-d*y,r);

setfillstyle(1,player 2 _ color);

floodfill(left+d*x,top-d*y,player 2 _ color);

}

其他

{ num 1 . point 1[x][y]= 1;

数字点[x][y]= 1;

set color(player 1 _ color);

circle(left+d*x,top-d*y,r);

setfillstyle(1,player 1 _ color);

floodfill(left+d*x,top-d*y,player 1 _ color);

}

set color(default color);

}

无效输入(player 1 & amp;num1,player2 & ampnum2,潘琪& amp数字)

{ char xx,YY;

k++;

while(1)

{

regs 1 . h . ah = 0;

xx=int86(22,?s1,?s 1)-“0”;

if(xx==('q'-'0')||xx==('Q'-'0 '))

{ step = 0;

返回;

}

regs 1 . h . ah = 0;

yy=int86(22,?s1,?s 1)-“0”;

if(yy==('q'-'0')||yy==('Q'-'0 '))

{

步长= 0;

返回;

}

如果(xx & lt0 | | xx & gt行编号)

{ input terror();

继续;

}

如果(yy & lt0 | | yy & gt行编号)

{ input terror();

继续;

}

if(num.point[xx][yy]==0)

{

打破;

}

其他

{

input terror();

继续;

}

}

x =(int)xx;

y =(int)YY;

set color(back color);

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

outtextxy(left+d*line_num/3,top+d*2,“输入错误”);

set color(default color);

}

player1::player1()

{

int i,j;

for(I = 0;我& lt= line _ numi++)

{

for(j = 0;j & lt= line _ numj++)

point 1[I][j]= 0;

}

}

播放器2::播放器2()

{ int i,j;

for(I = 0;我& lt= line _ numi++)

{

for(j = 0;j & lt= line _ numj++)

point 2[I][j]= 0;

}

}

void输入恐怖(void)

{ set color(error _ color);

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

outtextxy(left+d*line_num/3,top+d*2,“输入错误”);

set color(default color);

}

int judge _ winner(player 1 & amp;num1,player2 & ampnum2)

{

int count=0,m=0,n=0,a=0,b=0,xx0,yy0

int flag = k % 2;

xx0 = x;yy0 = y;

如果(!旗帜)

{//left & lt;-& gt;正确

while(xx0 & gt;=1。& ampm & lt4){ xx0-;m++;}

while(n & lt;9 & amp& ampxx0 & lt=行编号)

{

if(num 1 . point 1[xx0][y]= = 1)

{

count++;

if(count==5)返回1;

}

其他

{

count = 0;

}

n++;

xx0++;

}

//up & lt;-& gt;向下

count = 0;xx0 = x;m = 0;n = 0;

while(yy0 & gt;=1。& ampm & lt4){ yy0-;m++;}

while(n & lt;9 & amp& ampyy0 & lt=行编号)

{

if(num 1 . point 1[x][yy0]= = 1)

{

count++;

if(count==5)

返回1;

}

其他

{

count = 0;

}

n++;

yy0++;

}

//左上右下

xx0 = x;

yy0 = y;

m = 0;

n = 0;

count = 0;

while(xx0 & gt;=1。& ampm & lt4){ xx0-;a++;m++;} m = 0;

while(yy0 & lt;=行号& amp& ampm & lt4){ yy0++;b++;m++;}

如果(a & lt=b)

{

xx0 = x-a;yy0 = y+a;

}

其他

{

xx0 = x-b;yy0 = y+b;

}

while(xx0 & lt;=行号& amp& ampyy0 & gt= 0 & amp& ampn & lt9)

{

if(num 1 . point 1[xx0][yy0]= = 1)

{

count++;

if(count==5)

返回1;

}

其他

{

count = 0;

}

xx0++;

yy0-;

n++;

}

//右向上& lt-& gt;向左下

count = 0;

a = 0;

b = 0;

n = 0;

m = 0;

xx0 = x;

yy0 = y;

while(xx 0 while(yy0if(a & lt;=b)

{

xx0 = x+a;

yy0 = y+a;

}

其他

{

xx0 = x+b;

yy0 = y+b;

}

while(xx0 & gt;= 0 & amp& ampyy0 & gt= 0 & amp& ampn & lt9)

{

if(num 1 . point 1[xx0][yy0]= = 1)

{

count++;

if(count==5)

返回1;

}

其他

count = 0;

xx0-;

yy0-;

n++;

}

//没有winer

返回0;

}

其他

{

//left & lt;-& gt;正确

while(xx0 & gt;=1。& ampm & lt4){ xx0-;m++;}

while(n & lt;9 & amp& ampxx0 & lt=行编号)

{

if(num 1 . point 1[xx0][y]= = 1)

{

count++;

if(count==5)返回1;

}

其他

{

count = 0;

}

n++;

xx0++;

}

//up & lt;-& gt;向下

count = 0;xx0 = x;m = 0;n = 0;

while(yy0 & gt;=1。& ampm & lt4){ yy0-;m++;}

while(n & lt;9 & amp& ampyy0 & lt=行编号)

{

if(num 2 . point 2[x][yy0]= = 1)

{

count++;

if(count==5)

返回1;

}

其他

{

count = 0;

}

n++;

yy0++;

}

//左上右下

xx0 = x;

yy0 = y;

m = 0;

n = 0;

count = 0;

while(xx0 & gt;=1。& ampm & lt4){ xx0-;a++;m++;} m = 0;

while(yy0 & lt;=行号& amp& ampm & lt4){ yy0++;b++;m++;}

如果(a & lt=b)

{

xx0 = x-a;yy0 = y+a;

}

其他

{

xx0 = x-b;yy0 = y+b;

}

while(xx0 & lt;=行号& amp& ampyy0 & gt= 0 & amp& ampn & lt9)

{

if(num 2 . point 2[xx0][yy0]= = 1)

{

count++;

if(count==5)

返回1;

}

其他

{

count = 0;

}

xx0++;

yy0-;

n++;

}

//右向上& lt-& gt;向左下

count = 0;

a = 0;

b = 0;

n = 0;

m = 0;

xx0 = x;

yy0 = y;

while(xx 0 while(yy0if(a & lt;=b)

{

xx0 = x+a;

yy0 = y+a;

}

其他

{

xx0 = x+b;

yy0 = y+b;

}

while(xx0 & gt;= 0 & amp& ampyy0 & gt= 0 & amp& ampn & lt9)

{

if(num 2 . point 2[xx0][yy0]= = 1)

{

count++;

if(count==5)

返回1;

}

其他

count = 0;

xx0-;

yy0-;

n++;

}

//没有winer

返回0;

}

}

void display_winner(int k)

{

int flag = k % 2;

如果(!旗帜)

{ set color(winner _ color);

settextstyle(DEFAULT_FONT,HORIZ_DIR,2);

outtextxy(左+d*2,上+40,“红为胜”);

set color(default color);

步长= 0;

getchar();

}

其他

{ set color(winner _ color);

settextstyle(DEFAULT_FONT,HORIZ_DIR,2);

outtextxy(左+2*d,上+40,“白为胜”);

set color(default color);

步长= 0;

}

}