c语言小游戏编程100例(C语言程序设计小游戏)

http://www.itjxue.com  2023-01-26 21:18  来源:未知  点击次数: 

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

用c++实现的"贪吃蛇"游戏源码

// greedsnake.cpp

#include bios.h

#include conio.h

#include dos.h

#include graphics.h

#include stdlib.h

#include time.h

#include "conf.h"

typedef struct node

{

int x,y;

struct node *next;

}Node;

typedef struct

{

Node *head,*tail;

int length;

}Snake;

typedef struct

{

int left,top,right,bottom;

}Frame;

typedef enum //四个方向

{

up,down,left,right

}Direction;

typedef enum

{

false,true

}bool;//*/

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

void CloseGraphMode();

void Foot(int,int);

void Head(int,int);

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

void CreateSnake(); //创建一条两个节点的蛇,蛇的每一节是队列中的一个节点

bool PlayGame(); //游戏的主体函数,

int Hit(int,int); //判断是否越界,或者撞到自身,两个参数分别是新的头接点的x,y坐标

bool GameOver(); //绘制游戏结束时弹出的对话框

void Enqueue(Node); //入队函数

Node Dequeue(); //出队函数

void ClearKeyBuf(); //清除键盘缓冲,此函数可以消除不停的按无效键的影响

Snake snake;

Frame frame;

void main()

{

InitGraphMode();

do

{

CreateFrame();

}while(PlayGame());

CloseGraphMode();

}

void InitGraphMode()

{

int gdriver=DETECT,gmode;

initgraph(gdriver,gmode,"../bgi/");

cleardevice();

}

void CloseGraphMode()

{

cleardevice();

closegraph();

}

void CreateFrame()

{

setbkcolor(CYAN);

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

frame.left=(getmaxx()+1-BlockWidth*RowOfFrame)/2;

frame.top=(getmaxy()+1-BlockHeight*ColumnOfFrame)/2;

frame.right=frame.left+BlockWidth*RowOfFrame;

frame.bottom=frame.top+BlockHeight*ColumnOfFrame;

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

setfillstyle(SOLID_FILL,LIGHTGRAY);

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

setlinestyle(SOLID_LINE,1,1);

setcolor(DARKGRAY);

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

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

setlinestyle(SOLID_LINE,1,1);

setcolor(WHITE);

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

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

setlinestyle(DOTTED_LINE,1,1);

setcolor(BLUE);

for(int row=1;rowRowOfFrame;row++)

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

for(int column=1;columnColumnOfFrame;column++)

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

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

}

void CreateSnake()

{

Node *node1=new Node;

Node *node2=new Node;

node1-x=frame.left+BlockWidth;

node1-y=frame.top;

node1-next=NULL;

snake.tail=node1;

node2-x=frame.left;

node2-y=frame.top;

node2-next=snake.tail;

snake.head=node2;

snake.length=2;

setfillstyle(SOLID_FILL,BLUE);

bar(snake.head-x+1,snake.head-y+1,snake.head-x+BlockWidth-1,snake.head-y+BlockHeight-1);

bar(snake.tail-x+1,snake.tail-y+1,snake.tail-x+BlockWidth-1,snake.tail-y+BlockHeight-1);

}

bool PlayGame()

{

int speed=300,key;

Direction CurrentDirection=right;

Node randomNode;

Node newNode,outNode;

bool neednode=true;

bool overlap=false;

int randx,randy;

CreateSnake();

while(true)

{

if(neednode==true)

{

randomize();

do

{

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

randy=frame.top+rand()%ColumnOfFrame*BlockHeight;

for(Node *p=snake.head;p!=NULL;p=p-next)//hit itself

if(randx==p-xrandy==p-y)

{overlap=true;break;}

}

while(overlap==true);

randomNode.x=randx;

randomNode.y=randy;

randomNode.next=NULL;

setfillstyle(SOLID_FILL,RED);

bar(randomNode.x+1,randomNode.y+1,randomNode.x+BlockWidth-1,randomNode.y+BlockHeight-1);

neednode=false;

}

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

{

switch(key)

{

case ESC: return false;

case UP:

if(CurrentDirection!=down)

CurrentDirection=up;

ClearKeyBuf();

break;

case DOWN:

if(CurrentDirection!=up)

CurrentDirection=down;

ClearKeyBuf();

break;

case LEFT:

if(CurrentDirection!=right)

CurrentDirection=left;

ClearKeyBuf();

break;

case RIGHT:

if(CurrentDirection!=left)

CurrentDirection=right;

ClearKeyBuf();

break;

case PAGEUP:speed=speed-100;

if(speed100)

speed=100;

ClearKeyBuf();

break;

case PAGEDOWN:speed=speed+100;

if(speed500)

speed=500;

ClearKeyBuf();

break;

default :break;

}

}

int headx=snake.tail-x;

int heady=snake.tail-y;

switch(CurrentDirection)

{

case up: heady-=BlockHeight;break;

case down: heady+=BlockHeight;break;

case left: headx-=BlockWidth;break;

case right: headx+=BlockWidth;break;

}

if(Hit(headx,heady)) //whether the snake hit the wall or itself

return GameOver();

else

{ //eat

if(headx==randomNode.xheady==randomNode.y)

{

Enqueue(randomNode);

setfillstyle(SOLID_FILL,BLUE);

bar(randomNode.x+1,randomNode.y+1,randomNode.x-1+BlockWidth,randomNode.y-1+BlockHeight);

neednode=true;

}

else //no eat

{

newNode.x=headx;

newNode.y=heady;

newNode.next=NULL;

Enqueue(newNode);

outNode=Dequeue();

setfillstyle(SOLID_FILL,LIGHTGRAY);

bar(outNode.x+1,outNode.y+1,outNode.x+BlockWidth-1,outNode.y+BlockHeight-1);

setfillstyle(SOLID_FILL,BLUE);

bar(newNode.x+1,newNode.y+1,newNode.x-1+BlockWidth,newNode.y-1+BlockHeight);

}

}

delay(speed);

}

}

void ClearKeyBuf()

{

do

bioskey(0);

while(bioskey(1));

}

void Foot(int x,int y)

{

setcolor(BLUE);

outtextxy(x,y,"writer:[T]RealXL E-MAIL:realgeneral@hotmail.com");

}

void Head(int x,int y)

{

setcolor(RED);

outtextxy(x,y,"GREEDY SNAKE");

}

void Enqueue(Node inNode)

{

Node *p=new Node;

p-x=inNode.x;

p-y=inNode.y;

p-next=inNode.next;

snake.tail-next=p;

snake.tail=p;

snake.length++;

}

Node Dequeue()

{

Node *p=snake.head;

Node outNode=*p;

snake.head=p-next;

snake.length--;

delete p;

return outNode;

}

int Hit(int x,int y)

{

if(xframe.left||x=frame.right||yframe.top||y=frame.bottom)//hit the wall

return 1;

Node *p=snake.head-next;

for(int i=snake.length-1;i3;i--,p=p-next)//hit itself

if(x==p-xy==p-y)

return 1;

return 0;

}

bool GameOver()

{

int x=getmaxx()/2-50;

int y=getmaxy()/2-20;

setfillstyle(SOLID_FILL,DARKGRAY);

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

setfillstyle(SOLID_FILL,MAGENTA);

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

setlinestyle(0,3,1);

setcolor(RED);

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

outtextxy(x+20,y+10,"GAGE OVER!");

char c;

while(true) //按q或Q表示退出程序,按r或R表示重新开始游戏

{

c=getch();

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

return false;

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

return true;

}

}

C++五子棋源程序:

#include

#include

#include

#define backcolor CYAN

#define defaultcolor BLACK

#define linecolor MAGENTA

#define player1_color RED

#define player2_color WHITE

#define error_color RED

#define winner_color RED

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 player1;

class player2;

class qipan{

public:

qipan();

~qipan(){};

void init_qipan();

friend void fall(player1 num1,player2 num2,qipan num);

friend void input(player1 num1,player2 num2,qipan num);

private:

int point[line_num+1][line_num+1];

};

class player1{

public:

player1();

~player1(){};

friend void fall(player1 num1,player2 num2,qipan num);

friend void input(player1 num1,player2 num2);

friend int judge_winner(player1 num1,player2 num2);

private:

int point1[line_num+1][line_num+1];

};

class player2{

public:

player2();

~player2(){};

friend void fall(player1 num1,player2 num2,qipan num);

friend void input(player1 num1,player2 num2,qipan num);

friend int judge_winner(player1 num1,player2 num2);

private:

int point2[line_num+1][line_num+1];

};

void input(player1 num1,player2 num2);

void fall(player1 num1,player2 num2,qipan num);

int judge_winner(qipan num,player1 num1,player2 num2);

void inputerror();

void display_winner(int);

void main()

{

int driver=DETECT,mode;

initgraph(driver,mode,"e:\tc30\bgi");

qipan num;

player1 num1;

player2 num2;

while(step--)

{

input(num1,num2,num);

fall(num1,num2,num);

if(judge_winner(num1,num2))

{

display_winner(k);

}

}

// getchar();

}

qipan::qipan(void)

{ int j,i;

char ch[2]="0";

setbkcolor(backcolor);

setcolor(linecolor);

for(i=0;i=line_num;i++)

{

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

}

for(i=0;i=line_num;i++)

{

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

}

for(i=0;i=line_num;i++)

{ 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;i=line_num;i++)

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

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

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

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

}

setcolor(defaultcolor);

for(i=0;i=line_num;i++)

{

for(j=0;j=line_num;j++)

point[i][j]=0;

}

}

void fall(player1 num1,player2 num2,qipan num)

{

int flag=k%2;

if(flag)

{ setcolor(player2_color);

num2.point2[x][y]=1;

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

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

setfillstyle(1,player2_color);

floodfill(left+d*x,top-d*y,player2_color);

}

else

{ num1.point1[x][y]=1;

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

setcolor(player1_color);

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

setfillstyle(1,player1_color);

floodfill(left+d*x,top-d*y,player1_color);

}

setcolor(defaultcolor);

}

void input(player1 num1,player2 num2,qipan num)

{ char xx,yy;

k++;

while(1)

{

regs1.h.ah=0;

xx=int86(22,?s1,?s1)-'0';

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

{ step=0;

return;

}

regs1.h.ah=0;

yy=int86(22,?s1,?s1)-'0';

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

{

step=0;

return ;

}

if(xx0||xxline_num)

{ inputerror();

continue;

}

if(yy0||yyline_num)

{inputerror();

continue;

}

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

{

break;

}

else

{

inputerror();

continue;

}

}

x=(int)xx;

y=(int)yy;

setcolor(backcolor);

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

outtextxy(left+d*line_num/3,top+d*2,"Input error");

setcolor(defaultcolor);

}

player1::player1()

{

int i,j;

for(i=0;i=line_num;i++)

{

for(j=0;j=line_num;j++)

point1[i][j]=0;

}

}

player2::player2()

{ int i,j;

for(i=0;i=line_num;i++)

{

for(j=0;j=line_num;j++)

point2[i][j]=0;

}

}

void inputerror(void)

{ setcolor(error_color);

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

outtextxy(left+d*line_num/3,top+d*2,"Input error");

setcolor(defaultcolor);

}

int judge_winner(player1 num1,player2 num2)

{

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

int flag=k%2;

xx0=x; yy0=y;

if(!flag)

{ //left ------- right

while(xx0=1m4) {xx0--;m++;}

while(n9xx0=line_num)

{

if(num1.point1[xx0][y]==1)

{

count++;

if(count==5) return 1;

}

else

{

count=0;

}

n++;

xx0++;

}

//up ------ down

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

while(yy0=1m4){yy0--;m++;}

while(n9yy0=line_num)

{

if(num1.point1[x][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

{

count=0;

}

n++;

yy0++;

}

//left up ----- right down

xx0=x;

yy0=y;

m=0;

n=0;

count=0;

while(xx0=1m4){ xx0--; a++; m++;} m=0;

while(yy0=line_numm4){ yy0++; b++; m++;}

if(a=b)

{

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

}

else

{

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

}

while(xx0=line_numyy0=0n9)

{

if(num1.point1[xx0][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

{

count=0;

}

xx0++;

yy0--;

n++;

}

//right up ----- left down

count=0;

a=0;

b=0;

n=0;

m=0;

xx0=x;

yy0=y;

while(xx0while(yy0if(a=b)

{

xx0=x+a;

yy0=y+a;

}

else

{

xx0=x+b;

yy0=y+b;

}

while(xx0=0yy0=0n9)

{

if(num1.point1[xx0][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

count=0;

xx0--;

yy0--;

n++;

}

//no winer

return 0;

}

else

{

//left ------- right

while(xx0=1m4) {xx0--;m++;}

while(n9xx0=line_num)

{

if(num1.point1[xx0][y]==1)

{

count++;

if(count==5) return 1;

}

else

{

count=0;

}

n++;

xx0++;

}

//up ------ down

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

while(yy0=1m4){yy0--;m++;}

while(n9yy0=line_num)

{

if(num2.point2[x][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

{

count=0;

}

n++;

yy0++;

}

//left up ----- right down

xx0=x;

yy0=y;

m=0;

n=0;

count=0;

while(xx0=1m4){ xx0--; a++; m++;} m=0;

while(yy0=line_numm4){ yy0++; b++; m++;}

if(a=b)

{

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

}

else

{

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

}

while(xx0=line_numyy0=0n9)

{

if(num2.point2[xx0][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

{

count=0;

}

xx0++;

yy0--;

n++;

}

//right up ----- left down

count=0;

a=0;

b=0;

n=0;

m=0;

xx0=x;

yy0=y;

while(xx0while(yy0if(a=b)

{

xx0=x+a;

yy0=y+a;

}

else

{

xx0=x+b;

yy0=y+b;

}

while(xx0=0yy0=0n9)

{

if(num2.point2[xx0][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

count=0;

xx0--;

yy0--;

n++;

}

//no winer

return 0;

}

}

void display_winner(int k)

{

int flag=k%2;

if(!flag)

{ setcolor(winner_color);

settextstyle(DEFAULT_FONT,HORIZ_DIR,2);

outtextxy(left+d*2,top+40,"Red is winner");

setcolor(defaultcolor);

step=0;

getchar();

}

else

{ setcolor(winner_color);

settextstyle(DEFAULT_FONT,HORIZ_DIR,2);

outtextxy(left+2*d,top+40,"White is winner");

setcolor(defaultcolor);

step=0;

}

}

如何使用C语言编写简单小游戏?

C语言是计算机专业都要学习的一门基础学科。一般来说,是比较枯燥的.那么,我们能不能通过编一些小游戏来提高它的趣味性呢?这样学习程序设计,就不会是一件艰苦 ,枯燥的事,它变得象电脑游戏一样充满好奇,富有乐趣。

例如2048这款游戏:

方法/步骤:

#includestdio.h

#includestdlib.h

#includetime.h

#includeconio.h

#includewindows.h

#define SIZE 4

static int score=0;

void putn(int n[][SIZE]);

void getn(int n[][SIZE]);

int isempty(int n[][SIZE]);

int isfull(int n[][SIZE]);

void math(int n[][SIZE],char c);

void tow(int n[][SIZE]);

void toa(int n[][SIZE]);

void tos(int n[][SIZE]);

void tod(int n[][SIZE]);

//主函数

int main()

{

int i,j;

int n[SIZE][SIZE];

char c=' ';

for(i=0;iSIZE;i++)

{

for(j=0;jSIZE;j++)

{

n[i][j]=0;

}

}

printf( "***********************\n"

" ? ? ?2048(%dX%d) ? ? ?\n"

" ? control:W/A/S/D ? ?\n"

"press any key to begin \n"

"***********************\n",SIZE,SIZE);

getch();

system("cls");

//n[0][1]=2048;

//n[0][3]=2048;

while(1)

{

if(isempty(n))

getn(n);

putn(n);

if(!isempty(n)isfull(n))

break;

sleep(200);

c=getch();

while(c!='w'c!='a'c!='s'c!='d')

c=getch();

math(n,c);

system("cls");

}

printf(" ? ? ?Game Over!\n",score);

return 0;

}

//函数

void putn(int n[][SIZE])

{

int i,j;

for(i=0;iSIZE;i++)

{

for(j=0;jSIZE;j++)

printf("| ? ? ");

printf("|\n");

for(j=0;jSIZE;j++)

{

if(n[i][j]==0)

printf("| ? ? ");

else

printf("|%4d ",n[i][j]);

}

printf("|\n");

for(j=0;jSIZE;j++)

printf("|_____");

printf("|\n");

}

printf("score: %d",score);

}

void getn(int n[][SIZE])

{

int a,b;

a=rand()%SIZE;

b=rand()%SIZE;

while(n[a][b]!=0)

{

a=rand()%SIZE;

b=rand()%SIZE;

}

n[a][b]=2;

}

int isempty(int n[][SIZE])

{

int i,j,count=0;

for(i=0;iSIZE;i++)

for(j=0;jSIZE;j++)

if(n[i][j]==0)

count++;

return count;

}

int isfull(int n[][SIZE])

{

int i,j,count=0;

for(i=0;iSIZE;i++)

{

for(j=1;jSIZE-1;j++)

{

if(n[i][j]==n[i][j+1]||n[i][j]==n[i][j-1])

count++;

}

}

for(j=0;jSIZE;j++)

{

for(i=1;iSIZE-1;i++)

{

if(n[i][j]==n[i+1][j]||n[i][j]==n[i-1][j])

count++;

}

}

return count0?0:1;

}

void math(int n[][SIZE],char c)

{

switch(c)

{

case 'w':tow(n);break;

case 'a':toa(n);break;

case 's':tos(n);break;

case 'd':tod(n);break;

default :;

}

}

void tow(int n[][SIZE])

{

int i,j,a;

int m[SIZE];

for(a=0;aSIZE;a++)

m[a]=0;

for(j=0;jSIZE;j++)

{

for(a=0;aSIZE;a++)

{

for(i=0;iSIZE-1;i++)

{

if(n[i][j]==0)

{

n[i][j]=n[i+1][j];

n[i+1][j]=0;

}

}

}

}

for(j=0;jSIZE;j++)

{

for(a=0,i=0;iSIZE;i++)

{

if(n[i][j]!=n[i+1][j]n[i][j]!=0||n[i][j]==2048)

{

m[a++]=n[i][j];

n[i][j]=0;

}

else if(n[i][j]==n[i+1][j])

{

m[a++]=n[i][j]+n[i+1][j];

score+=m[a-1];

n[i][j]=0,n[i+1][j]=0;

}

}

for(i=0;iSIZE;i++)

{

n[i][j]=m[i];

m[i]=0;

}

}

}

void toa(int n[][SIZE])

{

int i,j,a;

int m[SIZE];

for(a=0;aSIZE;a++)

m[a]=0;

for(i=0;iSIZE;i++)

{

for(a=0;aSIZE;a++)

{

for(j=0;jSIZE-1;j++)

{

if(n[i][j]==0)

{

n[i][j]=n[i][j+1];

n[i][j+1]=0;

}

}

}

}

for(i=0;iSIZE;i++)

{

for(a=0,j=0;jSIZE;j++)

{

if(n[i][j]!=n[i][j+1]n[i][j]!=0||n[i][j]==2048)

{

m[a++]=n[i][j];

n[i][j]=0;

}

else if(n[i][j]==n[i][j+1])

{

m[a++]=n[i][j]+n[i][j+1];

score+=m[a-1];

n[i][j]=0,n[i][j+1]=0;

}

}

for(j=0;jSIZE;j++)

{

n[i][j]=m[j];

m[j]=0;

}

}

}

void tos(int n[][SIZE])

{

int i,j,a;

int m[SIZE];

for(a=0;aSIZE;a++)

m[a]=0;

for(j=SIZE-1;j=0;j--)

{

for(a=SIZE-1;a=0;a--)

{

for(i=SIZE-1;i0;i--)

{

if(n[i][j]==0)

{

n[i][j]=n[i-1][j];

n[i-1][j]=0;

}

}

}

}

for(j=SIZE-1;j=0;j--)

{

for(a=SIZE-1,i=SIZE-1;i=0;i--)

{

if(n[i][j]!=n[i-1][j]n[i][j]!=0||n[i][j]==2048)

{

m[a--]=n[i][j];

n[i][j]=0;

}

else if(n[i][j]==n[i-1][j])

{

m[a--]=n[i][j]+n[i-1][j];

score+=m[a+1];

n[i][j]=0,n[i-1][j]=0;

}

}

for(i=SIZE-1;i=0;i--)

{

n[i][j]=m[i];

m[i]=0;

}

}

}

void tod(int n[][SIZE])

{

int i,j,a;

int m[SIZE];

for(a=0;aSIZE;a++)

m[a]=0;

for(i=SIZE-1;i=0;i--)

{

for(a=SIZE-1;a=0;a--)

{

for(j=SIZE-1;j0;j--)

{

if(n[i][j]==0)

{

n[i][j]=n[i][j-1];

n[i][j-1]=0;

}

}

}

}

for(i=SIZE-1;i=0;i--)

{

for(a=SIZE-1,j=SIZE-1;j=0;j--)

{

if(n[i][j]!=n[i][j-1]n[i][j]!=0||n[i][j]==2048)

{

m[a--]=n[i][j];

n[i][j]=0;

}

else if(n[i][j]==n[i][j-1])

{

m[a--]=n[i][j]+n[i][j-1];

score+=m[a+1];

n[i][j]=0,n[i][j-1]=0;

}

}

for(j=SIZE-1;j=0;j--)

{

n[i][j]=m[j];

m[j]=0;

}

}

}

用C语言编一个小游戏,注明编码,(简单易懂的游戏,不要复杂)

我这有许多C的小游戏。给你一个基础的简单的汉诺塔程序。你看看:

这是个汉诺塔程序,在调试的时候,输入的数字最好不要大于15,因为每大一个数

所得的结果的步骤都会多一倍。如果你有耐心等待结果的话除外。汉诺塔是在欧洲

流行的一种游戏,有a,b,c三个竿。a竿上有若干个由大到小的圆盘,大的在下面,

小的在上面,b,c都是空杆,请你把a杆上的圆盘都倒到别的杆上,或b或c,在倒盘

的过程中不可以大的压小的,实例程序如下:

#include stdio.h

int i=0;

main()

{

unsigned n;

printf("Please enter the number of discs: ");

scanf("%d",n);

printf("\tneedle:\ta\t b\t c\n");

movedisc(n,'a','c','b');

printf("\t Total: %d\n",i);

getch();

}

movedisc(n,fromneedle,toneedle,usingneedle)

unsigned n;

char fromneedle,toneedle,usingneedle;

{

if(n0)

{

movedisc(n-1,fromneedle,usingneedle,toneedle);

i++;

switch(fromneedle)

{

case 'a':switch(toneedle)

{

case 'b':printf("\t[%d]:\t%2d------%2d\n",i,n,n);

break;

case 'c':printf("\t[%d]:\t%2d-------------%2d\n",i,n,n);

break;

}

break;

case 'b':switch(toneedle)

{

case 'a':printf("\t[%d]:\t%2d----------%2d\n",i,n,n);

break;

case 'c':printf("\t[%d]:\t\t%2d------%2d\n",i,n,n);

break;

}

break;

case 'c':switch(toneedle)

{

case 'a':printf("\t[%d]:\t%2d--------------%2d\n",i,n,n);

break;

case 'b':printf("\t[%d]:\t\t%2d--------%2d\n",i,n,n);

break;

}

break;

}

movedisc(n-1,usingneedle,toneedle,fromneedle);

}

}

C语言可以写哪些小游戏?

C语言可以编手机游戏. 你叫他去死 不过我这有 贪吃蛇的代码,你倒可以看看 (用TC 编译一定过)

#include

#include

#include

#include

#include

#define Enter 7181

#define ESC 283

#define UP 18432

#define DOWN 20480

#define LEFT 19200

#define RIGHT 19712

#ifdef __cplusplus

#define __CPPARGS ...

#else

#define __CPPARGS

#endif

void interrupt (*oldhandler)(__CPPARGS);

void interrupt newhandler(__CPPARGS);

void SetTimer(void interrupt (*IntProc)(__CPPARGS));

void KillTimer(void);

void Initgra(void);

void TheFirstBlock(void);

void DrawMap(void);

void Initsnake(void);

void Initfood(void);

void Snake_Headmv(void);

void Flag(int,int,int,int);

void GameOver(void);

void Snake_Bodymv(void);

void Snake_Bodyadd(void);

void PrntScore(void);

void Timer(void);

void Win(void);

void TheSecondBlock(void);

void Food(void);

void Dsnkorfd(int,int,int);

void Delay(int);

struct Snake

{int x;int y;int color;}Snk[12];

struct Food

{int x;int y;int color;}Fd;

int flag1=1,flag2=0,flag3=0,flag4=0,flag5=0,flag6=0,

checkx,checky,num,key=0,Times,Score,Hscore,Snkspeed,TimerCounter,TureorFalse;

char Sco[2],Time[6];

void main()

{ Initgra();

SetTimer(newhandler);

TheFirstBlock();

while(1)

{DrawMap();

Snake_Headmv();

GameOver();

Snake_Bodymv();

Snake_Bodyadd();

PrntScore();

Timer();

Win();

if(key==ESC)

break;

if(key==Enter)

{cleardevice();

TheFirstBlock();

}

TheSecondBlock();

Food();

Delay(Snkspeed);

}

closegraph();

KillTimer();

}

void interrupt newhandler(__CPPARGS)

{

TimerCounter++;

oldhandler();

}

void SetTimer(void interrupt (*IntProc)(__CPPARGS))

{

oldhandler=getvect(0x1c);

disable();

setvect(0x1c,IntProc);

enable();

}

void KillTimer()

{

disable();

setvect(0x1c,oldhandler);

enable();

}

void Initgra()

{int gd=DETECT,gm;

initgraph(gd,gm,"d:\\tc");

}

void TheFirstBlock()

{setcolor(11);

settextstyle(0,0,4);

outtextxy(100,220,"The First Block");

loop:key=bioskey(0);

if(key==Enter)

{cleardevice();

Initsnake();

Initfood();

Score=0;

Hscore=1;

Snkspeed=10;

num=2;

Times=0;

key=0;

TureorFalse=1;

TimerCounter=0;

Time[0]='0';Time[1]='0';Time[2]=':';Time[3]='1';Time[4]='0';Time[5]='\0';

}

else if(key==ESC) cleardevice();

else goto loop;

}

void DrawMap()

{line(10,10,470,10);

line(470,10,470,470);

line(470,470,10,470);

line(10,470,10,10);

line(480,20,620,20);

line(620,20,620,460);

line(620,460,480,460);

line(480,460,480,20);

}

void Initsnake()

{randomize();

num=2;

Snk[0].x=random(440);

Snk[0].x=Snk[0].x-Snk[0].x%20+50;

Snk[0].y=random(440);

Snk[0].y=Snk[0].y-Snk[0].y%20+50;

Snk[0].color=4;

Snk[1].x=Snk[0].x;

Snk[1].y=Snk[0].y+20;

Snk[1].color=4;

}

void Initfood()

{randomize();

Fd.x=random(440);

Fd.x=Fd.x-Fd.x%20+30;

Fd.y=random(440);

Fd.y=Fd.y-Fd.y%20+30;

Fd.color=random(14)+1;

}

void Snake_Headmv()

{if(bioskey(1))

{key=bioskey(0);

switch(key)

{case UP:Flag(1,0,0,0);break;

case DOWN:Flag(0,1,0,0);break;

case LEFT:Flag(0,0,1,0);break;

case RIGHT:Flag(0,0,0,1);break;

default:break;

}

}

if(flag1)

{checkx=Snk[0].x;

checky=Snk[0].y;

Dsnkorfd(Snk[0].x,Snk[0].y,0);

Snk[0].y-=20;

Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);

}

if(flag2)

{checkx=Snk[0].x;

checky=Snk[0].y;

Dsnkorfd(Snk[0].x,Snk[0].y,0);

Snk[0].y+=20;

Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);

}

if(flag3)

{checkx=Snk[0].x;

checky=Snk[0].y;

Dsnkorfd(Snk[0].x,Snk[0].y,0);

Snk[0].x-=20;

Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);

}

if(flag4)

{checkx=Snk[0].x;

checky=Snk[0].y;

Dsnkorfd(Snk[0].x,Snk[0].y,0);

Snk[0].x+=20;

Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);

}

}

void Flag(int a,int b,int c,int d)

{flag1=a;flag2=b;flag3=c;flag4=d;}

void GameOver()

{int i;

if(Snk[0].x460||Snk[0].y460)

{cleardevice();

setcolor(11);

settextstyle(0,0,4);

outtextxy(160,220,"Game Over");

loop1:key=bioskey(0);

if(key==Enter)

{cleardevice();

TheFirstBlock();

}

else

if(key==ESC)

cleardevice();

else

goto loop1;

}

for(i=3;inum;i++)

{if(Snk[0].x==Snk[i].xSnk[0].y==Snk[i].y)

{cleardevice();

setcolor(11);

settextstyle(0,0,4);

outtextxy(160,220,"Game Over");

loop2:key=bioskey(0);

if(key==Enter)

{cleardevice();

TheFirstBlock();

}

else

if(key==ESC)

cleardevice();

else goto loop2;

}

}

}

void Snake_Bodymv()

{int i,s,t;

for(i=1;inum;i++)

{Dsnkorfd(checkx,checky,Snk[i].color);

Dsnkorfd(Snk[i].x,Snk[i].y,0);

s=Snk[i].x;

t=Snk[i].y;

Snk[i].x=checkx;

Snk[i].y=checky;

checkx=s;

checky=t;

}

}

void Food()

{if(flag5)

{randomize();

Fd.x=random(440);

Fd.x=Fd.x-Fd.x%20+30;

Fd.y=random(440);

Fd.y=Fd.y-Fd.y%20+30;

Fd.color=random(14)+1;

flag5=0;

}

Dsnkorfd(Fd.x,Fd.y,Fd.color);

}

void Snake_Bodyadd()

{if(Snk[0].x==Fd.xSnk[0].y==Fd.y)

{if(Snk[num-1].xSnk[num-2].x)

{num++;

Snk[num-1].x=Snk[num-2].x+20;

Snk[num-1].y=Snk[num-2].y;

Snk[num-1].color=Fd.color;

}

else

if(Snk[num-1].xSnk[num-2].x)

{num++;

Snk[num-1].x=Snk[num-2].x-20;

Snk[num-1].y=Snk[num-2].y;

Snk[num-1].color=Fd.color;

}

else

if(Snk[num-1].ySnk[num-2].y)

{num++;

Snk[num-1].x=Snk[num-2].x;

Snk[num-1].y=Snk[num-2].y+20;

Snk[num-1].color=Fd.color;

}

else

if(Snk[num-1].ySnk[num-2].y)

{num++;

Snk[num-1].x=Snk[num-2].x;

Snk[num-1].y=Snk[num-2].y-20;

Snk[num-1].color=Fd.color;

}

flag5=1;

Score++;

}

}

void PrntScore()

{if(Hscore!=Score)

{setcolor(11);

settextstyle(0,0,3);

outtextxy(490,100,"SCORE");

setcolor(2);

setfillstyle(1,0);

rectangle(520,140,580,180);

floodfill(530,145,2);

Sco[0]=(char)(Score+48);

Sco[1]='\0';

Hscore=Score;

setcolor(4);

settextstyle(0,0,3);

outtextxy(540,150,Sco);

}

}

void Timer()

{if(TimerCounter18)

{Time[4]=(char)(Time[4]-1);

if(Time[4]'0')

{Time[4]='9';

Time[3]=(char)(Time[3]-1);

}

if(Time[3]'0')

{Time[3]='5';

Time[1]=(char)(Time[1]-1);

}

if(TureorFalse)

{setcolor(11);

settextstyle(0,0,3);

outtextxy(490,240,"TIMER");

setcolor(2);

setfillstyle(1,0);

rectangle(490,280,610,320);

floodfill(530,300,2);

setcolor(11);

settextstyle(0,0,3);

outtextxy(495,290,Time);

TureorFalse=0;

}

if(Time[1]=='0'Time[3]=='0'Time[4]=='0')

{setcolor(11);

settextstyle(0,0,4);

outtextxy(160,220,"Game Over");

loop:key=bioskey(0);

if(key==Enter)

{cleardevice();

TheFirstBlock();

}

else if(key==ESC) cleardevice();

else goto loop;

}

TimerCounter=0;

TureorFalse=1;

}

}

void Win()

{if(Score==3)

Times++;

if(Times==2)

{cleardevice();

setcolor(11);

settextstyle(0,0,4);

outtextxy(160,220,"You Win");

loop:key=bioskey(0);

if(key==Enter)

{cleardevice();

TheFirstBlock();

key=0;

}

else if(key==ESC) cleardevice();

else goto loop;

}

}

void TheSecondBlock()

{if(Score==3)

{cleardevice();

setcolor(11);

settextstyle(0,0,4);

outtextxy(100,220,"The Second Block");

loop:key=bioskey(0);

if(key==Enter)

{cleardevice();

Initsnake();

Initfood();

Score=0;

Hscore=1;

Snkspeed=8;

num=2;

key=0;

}

else if(key==ESC) cleardevice();

else goto loop;

}

}

void Dsnkorfd(int x,int y,int color)

{setcolor(color);

setfillstyle(1,color);

circle(x,y,10);

floodfill(x,y,color);

}

void Delay(int times)

{int i;

for(i=1;i=times;i++)

delay(15000);

}

仅用c语言能编出哪些小游戏?

可以编写狼追兔子游戏,掷骰子游戏,24点游戏,井字棋游戏,农夫过河游戏,扫雷小游戏,人机猜数游戏,三色球游戏, 推箱子游戏,坦克大战游戏,贪吃蛇游戏等。

(责任编辑:IT教学网)

更多

推荐思科认证文章