導航:首頁 > 編程語言 > c語言編程游戲需要的函數實例

c語言編程游戲需要的函數實例

發布時間:2024-03-20 18:02:23

① 如何使用C語言編寫簡單小游戲

C語言是計算機專業都要學習的一門基礎學科。一般來說,是比較枯燥的.那麼,我們能不能通過編一些小游戲來提高它的趣味性呢?這樣學習程序設計,就不會是一件艱苦 ,枯燥的事,它變得象電腦游戲一樣充滿好奇,富有樂趣。

例如2048這款游戲:

方法/步驟:

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

#include<conio.h>

#include<windows.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]);

② c語言 貪吃蛇 程序

基本思路:

蛇每吃一個食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇頭的運動,而蛇身子跟著蛇頭走,每後一格蛇身子下一步走到上一格蛇身子的位置,以此類推。

#include <stdio.h>

#include <conio.h>

#include <windows.h>

#define BEG_X 2

#define BEG_Y 1

#define WID 20

#define HEI 20

HANDLE hout;

typedef enum {UP, DOWN, LEFT, RIGHT} DIR;

typedef struct Snake_body

{

COORD pos;//蛇身的位置

struct Snake_body *next;//下一個蛇身

struct Snake_body *prev;//前一個蛇身

}SNAKE, *PSNAKE;

PSNAKE head = NULL;//蛇頭

PSNAKE tail = NULL;//蛇尾

//畫游戲邊框的函數

void DrawBorder()

{

int i, j;

COORD pos = {BEG_X, BEG_Y};

for(i = 0; i < HEI; ++i)

{

SetConsoleCursorPosition(hout, pos);

for(j = 0; j < WID; ++j)

{

if(i == 0)//第一行

{

if(j == 0)

printf("┏");

else if(j == WID - 1)

printf("┓");

else

printf("━");

}

else if(i == HEI - 1)//最後一行

{

if(j == 0)

printf("┗");

else if(j == WID - 1)

printf("┛");

else

printf("━");

}

else if(j == 0 || j == WID - 1)//第一列或最後一列

printf("┃");

else

printf(" ");

}

++pos.Y;

}

}

//添加蛇身的函數

void AddBody(COORD pos)

{

PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));

pnew->pos = pos;

if(!head)

{

head = tail = pnew;

}

else

{

pnew->next = head;//新創建蛇身的next指向原先的蛇頭

head->prev = pnew;//原先的蛇頭的prev指向新創建的蛇身

head = pnew;//把新創建的蛇身作為新的蛇頭

}

SetConsoleCursorPosition(hout, head->pos);

printf("◎");

}

//蛇身移動的函數

void MoveBody(DIR dir)

{

PSNAKE ptmp;

COORD pos = head->pos;

switch(dir)

{

case UP:

if(head->pos.Y > BEG_Y + 1)

--pos.Y;

else

return;

break;

case DOWN:

if(head->pos.Y < BEG_Y + HEI - 2)

++pos.Y;

else

return;

break;

case LEFT:

if(head->pos.X > BEG_X + 2)

pos.X -= 2;

else

return;

break;

case RIGHT:

if(head->pos.X < BEG_X + (WID - 2) * 2)

pos.X += 2;

else

return;

break;

}

AddBody(pos);//添加了一個新的蛇頭

ptmp = tail;//保存當前的蛇尾

tail = tail->prev;

if(tail)

tail->next = NULL;

SetConsoleCursorPosition(hout, ptmp->pos);

printf(" ");

free(ptmp);

}

int main()

{

int ctrl;

DIR dir = RIGHT;//初始蛇的方向是向右的

COORD pos = {BEG_X + 2, BEG_Y + HEI / 2};

system("color 0E");

system("mode con cols=90 lines=30");

hout = GetStdHandle(STD_OUTPUT_HANDLE);

printf(" ------------貪吃蛇的移動------------");

DrawBorder();

//自定義幾個蛇的身體

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

//控制蛇的移動

while(ctrl = getch())

{

switch(ctrl)

{

case 'w':

if(dir == DOWN)

continue;

dir = UP;

break;

case 's':

if(dir == UP)

continue;

dir = DOWN;

break;

case 'a':

if(dir == RIGHT)

continue;

dir = LEFT;

break;

case 'd':

if(dir == LEFT)

continue;

dir = RIGHT;

break;

case 'q':

return 0;

}

MoveBody(dir);

}

return 0;

}

(2)c語言編程游戲需要的函數實例擴展閱讀:

實現邏輯

1,可以設置游標,就能實現制定位置列印製定符號。

2,涉及一個結構體,包含兩個元素坐標元素和一個結構體指針。

3,結構體串聯形成鏈表,遍歷獲取成員坐標,列印符號得到蛇身。

4,不斷的加頭,去尾,重新遍歷坐標,再列印形成蛇的移動。

5,食物產生的位置判定,不能越界,也不能與蛇身體重合。

6,蛇的轉向判定,一條規則,不允許倒退。

7,轉向的實現,跟行進方向決定新的關節坐標(當前頭的上下左右)

8,死亡檢測,是否頭節點坐標是否與牆壁重合,是否與身體其他關節重合。

9,加速減速,設置刷新休眠時間實現。

③ C語言編程 猜數游戲

#include
#include
//用到了rand函數,所以要有這個頭文件
#include
//用到了time函數,所以要有這個頭文件
int
main()
{
int
number;
//number用於存儲隨機數
int
guess=0;
//guess用於存儲玩家猜的數
srand((unsigned)
time(null));//用系統時間作為rand函數使用的種子
number=rand()%100;
//隨機除以100,取余數
number++;
//余數加1
printf("猜數字游戲\n");
printf("該數字在1到100之間\n");
while(guess!=number)
{
printf("請輸入您所猜的數:");
scanf("%d",&guess);
//如果玩家猜的數較小,給予提示
if
(guess
number)
{
printf("大了\n");
}
}
//猜中則循環結束,輸出猜中的數字
printf("猜對了,這個數字就是:%d\n",number);
return
0;
}

④ 怎樣用C語言編寫一個小游戲

「貪吃蛇」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("07"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //007是響鈴

else Flag = 0; //沒吃到食物Flag的值為0

if(snk.speed<150) snk.speed= snk.speed+5; //作弊碼,不讓速度無限加快

}

void putFod( ) //投放食物

{ if (Flag == 1) //如吃到食物才執行以下操作,生成另一個食物

{ while (1)

{ int i,n= 1;

srand((unsigned)time(NULL)); //初始化隨機數發生器srand( )

fod.x = rand( ) % (W - 4) + 2; //產生在游戲框范圍內的一個x坐標值

fod.y = rand( ) % (H - 2) + 1; //產生在游戲框范圍內的一個y坐標值

for (i = 0; i < snk.len; i++) //隨機生成的食物不能在蛇的身體上

{ if (fod.x == snk.x[i] &&fod.y == snk.y[i]) { n= 0; break;} }

if (n && fod.x % 2 == 0) break; //n不為0且橫坐標為偶數,則食物坐標取值成功

}

setColor(12, 0);

gtxy(fod.x, fod.y); printf("●"); //游標到取得的坐標處列印食物

}

return;

}

int Over( ) //判斷游戲是否結束的函數

{ int i;

setColor(7, 0);

gtxy(2,H+1); printf(「暫停鍵:space.」); //以下列印一些其它信息

gtxy(2,H+2); printf(「游戲得分:%d」,score);

if (snk.x[0] == 0 || snk.x[0] == W) return 1; //蛇頭觸碰左右邊界

if (snk.y[0] == 0 || snk.y[0] == H) return 1; //蛇頭觸碰上下邊界

for (i = 1; i < snk.len; i++)

{ if (snk.x[0] == snk.x[i] && snk.y[0] == snk.y[i]) return 1; } //蛇頭觸碰自身

return 0; //沒碰到邊界及自身時就返回0

}

void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)

{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute( handle, ForeColor + BackGroundColor * 0x10 );

} //用來設定顏色的函數

⑤ 如何用C語言編寫一如圖模型的推箱子游戲的程序!

/*TC環境下編的*/
#include <dos.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <bios.h>
#include <alloc.h>

typedef struct winer
{
int x,y;
struct winer *p;
}winer;

char status [20][20];
char far *printScreen=(char far* )0xB8000000;

void putoutChar(int y,int x,char ch,char fc,char bc);
void printWall(int x, int y);
void printBox(int x, int y);
void printBoxDes(int x, int y);
void printDestination(int x, int y);
void printDestination1(int x,int y,winer **win,winer **pw);
void printMan(int x, int y);
void init();
winer *initStep1();
winer *initStep2();
winer *initStep3();
winer *initStep4();
void moveBoxSpacetoSpace(int x ,int y, char a);
void moveBoxDestoSpace(int x ,int y, char a) ;
void moveBoxSpacetoDes(int x, int y, char a);
void moveBoxDestoDes(int x, int y, char a);
int judge(int x, int y);
void move(int x, int y, char a);
void reset(int i);

void putoutChar(int y,int x,char ch,char fc,char bc)
{
printScreen[(x*160)+(y<<1)+0]=ch;
printScreen[(x*160)+(y<<1)+1]=(bc*16)+fc;
}
void printWall(int x,int y)
{
putoutChar(y-1,x-1,219,GREEN,BLACK);
status[x][y]='w';
}

void printBox(int x,int y)
{
putoutChar(y-1,x-1,10,WHITE,BLACK);
status[x][y]='b';
}

void printDestination1(int x,int y,winer **win,winer **pw)
{
winer *qw;
putoutChar(y-1,x-1,003,YELLOW,BLACK);
status[x][y]='m';
if(*win==NULL)
{
*win=*pw=qw=(winer* )malloc(sizeof(winer));
(*pw)->x=x;
(*pw)->y=y;
(*pw)->p=NULL;
}
else
{
qw=(winer* )malloc(sizeof(winer));
qw->x=x;
qw->y=y;
(*pw)->p=qw;
(*pw)=qw;qw->p=NULL;
}
}

void printDestination(int x,int y)
{
putoutChar(y-1,x-1,003,YELLOW,BLACK);
status[x][y]='m';
}

void printMan(int x,int y)
{
gotoxy(y,x);
_AL=02;
_CX=01;
_AH=0xa;
geninterrupt(0x10);
}

void printBoxDes(int x,int y)
{
putoutChar(y-1,x-1,10,YELLOW,BLACK);
status[x][y]='i';
}

void init()
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<20;j++)
status[i][j]=0;
_AL=3;
_AH=0;
geninterrupt(0x10);
gotoxy(40,4);
printf("Welcome to the box world!");
gotoxy(40,6);
printf("You can use up, down, left,");
gotoxy(40,8);
printf("right key to control it, or");
gotoxy(40,10);
printf("you can press Esc to quit it.");
gotoxy(40,12);
printf("Press space to reset the game.");
gotoxy(40,14);
printf("Wish you have a good time !");
gotoxy(40,16);
printf("April , 2007");

}
winer *initStep1()
{
int x;
int y;
winer *win=NULL;
winer *pw;
for(x=1,y=5;y<=9;y++)
printWall(x+4,y+10);
for(y=5,x=2;x<=5;x++)
printWall(x+4,y+10);
for(y=9,x=2;x<=5;x++)
printWall(x+4,y+10);
for(y=1,x=3;x<=8;x++)
printWall(x+4,y+10);
for(x=3,y=3;x<=5;x++)
printWall(x+4,y+10);
for(x=5,y=8;x<=9;x++)
printWall(x+4,y+10);
for(x=7,y=4;x<=9;x++)
printWall(x+4,y+10);
for(x=9,y=5;y<=7;y++)
printWall(x+4,y+10);
for(x=8,y=2;y<=3;y++)
printWall(x+4,y+10);
printWall(5+4,4+10);
printWall(5+4,7+10);
printWall(3+4,2+10);
printBox(3+4,6+10);
printBox(3+4,7+10);
printBox(4+4,7+10);
printDestination1(4+4,2+10,&win,&pw);
printDestination1(5+4,2+10,&win,&pw);
printDestination1(6+4,2+10,&win,&pw);
printMan(2+4,8+10);
return win;
}

winer *initStep2()
{
int x;
int y;
winer *win=NULL;
winer *pw;
for(x=1,y=4;y<=7;y++)
printWall(x+4,y+10);
for(x=2,y=2;y<=4;y++)
printWall(x+4,y+10);
for(x=2,y=7;x<=4;x++)
printWall(x+4,y+10);
for(x=4,y=1;x<=8;x++)
printWall(x+4,y+10);
for(x=8,y=2;y<=8;y++)
printWall(x+4,y+10);
for(x=4,y=8;x<=8;x++)
printWall(x+4,y+10);
for(x=4,y=6;x<=5;x++)
printWall(x+4,y+10);
for(x=3,y=2;x<=4;x++)
printWall(x+4,y+10);
for(x=4,y=4;x<=5;x++)
printWall(x+4,y+10);
printWall(6+4,3+10);
printBox(3+4,5+10);
printBox(6+4,6+10);
printBox(7+4,3+10);
printDestination1(5+4,7+10,&win,&pw);
printDestination1(6+4,7+10,&win,&pw);
printDestination1(7+4,7+10,&win,&pw);
printMan(2+4,6+10);
return win;
}
winer *initStep3()
{
int x;
int y;
winer *win=NULL;
winer *pw;
for(x=1,y=2;y<=8;y++)
printWall(x+4,y+10);
for(x=2,y=2;x<=4;x++)
printWall(x+4,y+10);
for(x=4,y=1;y<=3;y++)
printWall(x+4,y+10);
for(x=5,y=1;x<=8;x++)
printWall(x+4,y+10);
for(x=8,y=2;y<=5;y++)
printWall(x+4,y+10);
for(x=5,y=5;x<=7;x++)
printWall(x+4,y+10);
for(x=7,y=6;y<=9;y++)
printWall(x+4,y+10);
for(x=3,y=9;x<=6;x++)
printWall(x+4,y+10);
for(x=3,y=6;y<=8;y++)
printWall(x+4,y+10);
printWall(2+4,8+10);
printWall(5+4,7+10);
printBox(6+4,3+10);
printBox(4+4,4+10);
printBox(5+4,6+10);
printDestination1(2+4,5+10,&win,&pw);
printDestination1(2+4,6+10,&win,&pw);
printDestination1(2+4,7+10,&win,&pw);
printMan(2+4,4+10);
return win;
}

winer *initStep4()
{
int x;
int y;
winer *win=NULL;
winer *pw;
for(x=1,y=1;y<=6;y++)
printWall(x+4,y+10);
for(x=2,y=7;y<=8;y++)
printWall(x+4,y+10);
for(x=2,y=1;x<=7;x++)
printWall(x+4,y+10);
for(x=7,y=2;y<=4;y++)
printWall(x+4,y+10);
for(x=6,y=4;y<=9;y++)
printWall(x+4,y+10);
for(x=3,y=9;x<=5;x++)
printWall(x+4,y+10);
for(x=3,y=3;y<=4;y++)
printWall(x+4,y+10);
printWall(3+4,8+10);
printBox(3+4,5+10);
printBox(4+4,4+10);
printBox(4+4,6+10);
printBox(5+4,5+10);
printBox(5+4,3+10);
printDestination1(3+4,7+10,&win,&pw);
printDestination1(4+4,7+10,&win,&pw);
printDestination1(5+4,7+10,&win,&pw);
printDestination1(4+4,8+10,&win,&pw);
printDestination1(5+4,8+10,&win,&pw);
printMan(2+4,2+10);
return win;
}

void moveBoxSpacetoSpace(int x,int y,char a)
{
switch(a)
{
case 'u':
status[x-1][y]=0;
printf(" ");
printBox(x-2,y);
printMan(x-1,y);
status[x-2][y]='b';
break;
case 'd':
status[x+1][y]=0;
printf(" ");
printBox(x+2,y);
printMan(x+1,y);
status[x+2][y]='b';
break;
case 'l':
status[x][y-1]=0;
printf(" ");
printBox(x,y-2);
printMan(x,y-1);
status[x][y-2]='b';
break;
case 'r':
status[x][y+1]=0;
printf(" ");
printBox(x,y+2);
printMan(x,y+1);
status[x][y+2]='b';
break;
default:
break;
}
}

void moveBoxDestoSpace(int x,int y,char a)
{
switch(a)
{
case 'u':
status[x-1][y]='m';
printf(" ");
printBox(x-2,y);
printMan(x-1,y);
status[x-2][y]='b';
break;
case 'd':
status[x+1][y]='m';
printf(" ");
printBox(x+2,y);
printMan(x+1,y);
status[x+2][y]='b';
break;
case 'l':
status[x][y-1]='m';
printf(" ");
printBox(x,y-2);
printMan(x,y-1);
status[x][y-2]='b';
break;
case 'r':
status[x][y+1]='m';
printf(" ");
printBox(x,y+2);
printMan(x,y+1);
status[x][y+2]='b';
break;
default:
break;
}
}

void moveBoxSpacetoDes(int x,int y,char a)
{
switch(a)
{
case 'u':
status[x-1][y]=0;
printf(" ");
printBoxDes(x-2,y);
printMan(x-1,y);
status[x-2][y]='i';
break;
case 'd':
status[x+1][y]=0;
printf(" ");
printBoxDes(x+2,y);
printMan(x+1,y);
status[x+2][y]='i';
break;
case 'l':
status[x][y-1]=0;
printf(" ");
printBoxDes(x,y-2);
printMan(x,y-1);
status[x][y-2]='i';
break;
case 'r':
status[x][y+1]=0;
printf(" ");
printBoxDes(x,y+2);
printMan(x,y+1);
status[x][y+2]='i';
break;
default:
break;
}
}

void moveBoxDestoDes(int x,int y,char a)
{
switch(a)
{
case 'u':
status[x-1][y]='m';
printf(" ");
printBoxDes(x-2,y);
printMan(x-1,y);
status[x-2][y]='i';
break;
case 'd':
status[x+1][y]='m';
printf(" ");
printBoxDes(x+2,y);
printMan(x+1,y);
status[x+2][y]='i';
break;
case 'l':
status[x][y-1]='m';
printf(" ");
printBoxDes(x,y-2);
printMan(x,y-1);
status[x][y-2]='i';
break;
case 'r':
status[x][y+1]='m';
printf(" ");
printBoxDes(x,y+2);
printMan(x,y+1);
status[x][y+2]='i';
break;
default:
break;
}
}

int judge(int x,int y)
{
int i;
switch(status[x][y])
{
case 0:
i=1;
break;
case 'w':
i=0;
break;
case 'b':
i=2;
break;
case 'i':
i=4;
break;
case 'm':
i=3;
break;
default:
break;
}
return i;
}

void move(int x,int y,char a)
{
switch(a)
{
case 'u':
if(!judge(x-1,y))
{
gotoxy(y,x);
break;
}
else if(judge(x-1,y)==1||judge(x-1,y)==3)
{
if(judge(x,y)==3)
{
printDestination(x,y);
printMan(x-1,y);
break;
}
else
{
printf(" ");
printMan(x-1,y);
break;
}
}
else if(judge(x-1,y)==2)
{
if(judge(x-2,y)==1)
{
moveBoxSpacetoSpace(x,y,'u');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x-1);
}
else if(judge(x-2,y)==3)
{
moveBoxSpacetoDes(x,y,'u');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x-1);
}
else
gotoxy(y,x);
break;
}
else if(judge(x-1,y)==4)
{
if(judge(x-2,y)==1)
{
moveBoxDestoSpace(x,y,'u');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x-1);
}
else if(judge(x-2,y)==3)
{
moveBoxDestoDes(x,y,'u');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x-1);
}
else
gotoxy(y,x);
break;
}
case 'd':
if(!judge(x+1,y))
{
gotoxy(y,x);
break;
}
else if(judge(x+1,y)==1||judge(x+1,y)==3)
{
if(judge(x,y)==3)
{
printDestination(x,y);
printMan(x+1,y);
break;
}
else
{
printf(" ");
printMan(x+1,y);
break;
}
}
else if(judge(x+1,y)==2)
{
if(judge(x+2,y)==1)
{
moveBoxSpacetoSpace(x,y,'d');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x+1);
}
else if(judge(x+2,y)==3)
{
moveBoxSpacetoDes(x,y,'d');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x+1);
}
else
gotoxy(y,x);
break;
}
else if(judge(x+1,y)==4)
{
if(judge(x+2,y)==1)
{
moveBoxDestoSpace(x,y,'d');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x+1);
}
else if(judge(x+2,y)==3)
{
moveBoxDestoDes(x,y,'d');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x+1);
}
else
gotoxy(y,x);
break;
}
case 'l':
if(!judge(x,y-1))
{
gotoxy(y,x);
break;
}
else if(judge(x,y-1)==1||judge(x,y-1)==3)
{
if(judge(x,y)==3)
{
printDestination(x,y);
printMan(x,y-1);
break;
}
else
{
printf(" ");
printMan(x,y-1);
break;
}
}
else if(judge(x,y-1)==2)
{
if(judge(x,y-2)==1)
{
moveBoxSpacetoSpace(x,y,'l');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y-1,x);
}
else if(judge(x,y-2)==3)
{
moveBoxSpacetoDes(x,y,'l');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y-1,x);
}
else
gotoxy(y,x);
break;
}
else if(judge(x,y-1)==4)
{
if(judge(x,y-2)==1)
{
moveBoxDestoSpace(x,y,'l');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y-1,x);
}
else if(judge(x,y-2)==3)
{
moveBoxDestoDes(x,y,'l');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y-1,x);
}
else
gotoxy(y,x);
break;
}
case 'r':
if(!judge(x,y+1))
{
gotoxy(y,x);
break;
}
else if(judge(x,y+1)==1||judge(x,y+1)==3)
{
if(judge(x,y)==3)
{
printDestination(x,y);
printMan(x,y+1);
break;
}
else
{
printf(" ");
printMan(x,y+1);
break;
}
}
else if(judge(x,y+1)==2)
{
if(judge(x,y+2)==1)
{
moveBoxSpacetoSpace(x,y,'r');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y+1,x);
}
else if(judge(x,y+2)==3)
{
moveBoxSpacetoDes(x,y,'r');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y+1,x);
}
else
gotoxy(y,x);
break;
}
else if(judge(x,y+1)==4)
{
if(judge(x,y+2)==1)
{
moveBoxDestoSpace(x,y,'r');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y+1,x);
}
else if(judge(x,y+2)==3)
{
moveBoxDestoDes(x,y,'r');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y+1,x);
}
else
gotoxy(y,x);
break;
}
default:
break;
}
}

void reset(int i)
{
switch(i)
{
case 0:
init();
initStep1();
break;
case 1:
init();
initStep2();
break;
case 2:
init();
initStep3();
break;
case 3:
init();
initStep4();
break;
default:
break;
}
}

void main()
{
int key;
int x;
int y;
int s;
int i=0;
winer *win;
winer *pw;
_AL=3;
_AH=0;
geninterrupt(0x10);
init();
win=initStep1();
do{
_AH=3;
geninterrupt(0x10);
x=_DH+1;
y=_DL+1;
while(bioskey(1)==0);
key=bioskey(0);
switch(key)
{
case 0x4800:
move(x,y,'u');
break;
case 0x5000:
move(x,y,'d');
break;
case 0x4b00:
move(x,y,'l');
break;
case 0x4d00:
move(x,y,'r');
break;
case 0x3920:
reset(i);
break;
default:
break;
}
s=0;
pw=win;
while(pw)
{
if(status[pw->x][pw->y]=='m')
s++;
pw=pw->p;
}
if(s==0)
{
free(win);
gotoxy(25,2);
printf("congratulate! You have done this step!");
getch();
i++;
switch(i)
{
case 1:
init();
win=initStep2();
break;
case 2:
init();
win=initStep3();
break;
case 3:
init();
win=initStep4();
break;
case 4:
gotoxy(15,21);
printf("Congratulation! \n");
gotoxy(15,23);
printf("You have done all the steps, Welcome to play again!");
key=0x011b;
getch();
break;
default:
break;
}
}

}while(key!=0x011b);
_AL=3;
_AH=0;
geninterrupt(0x10);
}

閱讀全文

與c語言編程游戲需要的函數實例相關的資料

熱點內容
威聯通套件編譯 瀏覽:231
清刻pdf 瀏覽:982
可編程延時發生器 瀏覽:93
濱州用伺服器織夢要怎麼上傳文件 瀏覽:866
java7與java8 瀏覽:958
真空壓縮袋什麼材質好 瀏覽:935
excel批量見建文件夾 瀏覽:556
黑馬程序員就業班筆記 瀏覽:370
單片機供電自鎖電路設計 瀏覽:56
pythongui測試工具 瀏覽:834
哈曼l7功放編程 瀏覽:218
體溫單片機 瀏覽:613
快捷鍵命令不能用了 瀏覽:347
邊界層加密網格優點 瀏覽:236
linuxvi保存文件 瀏覽:535
把視頻打包出文件夾是什麼意思 瀏覽:446
如何在藏書館app上注銷賬號 瀏覽:826
51單片機架構 瀏覽:897
安卓下載東西怎麼弄 瀏覽:524
我的世界伺服器地址13 瀏覽:312