?? c編小游戲.txt
字號:
{
case Up: if(y>3) { y--; gotoxy(x,y); }
break;
case Down: if(y<n) { y++; gotoxy(x,y); }
break;
case Left: if(x>4) { x-=2; gotoxy(x,y); }
break;
case Right: if(x<2*m-2) { x+=2; gotoxy(x,y); }
break;
case Del: if(y-2==StartPlace.y&&x/2-1==StartPlace.x) b_start=FALSE;
if(y-2==EndPlace.y&&x/2-1==EndPlace.x) b_end=FALSE;
putch('''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''); Maze[y-2][x/2-1]=1; gotoxy(x,y);
break;
case Enter: if(y-2==StartPlace.y&&x/2-1==StartPlace.x) break;
if(y-2==EndPlace.y&&x/2-1==EndPlace.x) break;
putch(''''''''''''''''''''''''''''''''#''''''''''''''''''''''''''''''''); Maze[y-2][x/2-1]=0; gotoxy(x,y);
break;
case Home: if(Maze[y-2][x/2-1]&&!b_start)
{
StartPlace.x=x/2-1;
StartPlace.y=y-2;
putch(''''''''''''''''''''''''''''''''S'''''''''''''''''''''''''''''''');
gotoxy(x,y);
b_start=TRUE;
}
break;
case End: if(Maze[y-2][x/2-1]&&!b_end)
{
EndPlace.x=x/2-1;
EndPlace.y=y-2;
putch(''''''''''''''''''''''''''''''''E'''''''''''''''''''''''''''''''');
gotoxy(x,y);
b_end=TRUE;
}
break;
case Esc: gotoxy(2,22); printf("exit"); sleep(1); exit(1);
case F9: if(b_start&&b_end) flag=TRUE; break;
case F2: gotoxy(2,22);
printf("Savename:");
scanf("%s",savename);
gotoxy(2,22);
if(SaveMaze(savename)) printf("Save OK! ");
else printf("Save fail! ");
sleep(1);
gotoxy(2,22);
printf(" ");
gotoxy(x,y);
break;
default: break;
}
}
while(!flag);
for(i=0;i<30;i++)
for(j=0;j<20;j++)
{
maze[j][i].td=Maze[j][i];
maze[j][i].mark=0;
maze[j][i].foot=0;
}
}
Status LoadMaze(char *file)
/* The maze has been loaded. */
{
FILE *fp;
char *buffer;
char ch;
int i=0,j,k;
Boolean len=FALSE,wid=FALSE;
if((fp=fopen(file,"r"))==NULL)
return ERROR;
buffer=(char *)malloc(600*sizeof(char));
ch=fgetc(fp);
while(ch!=EOF)
{
buffer[i]=ch;
i++;
ch=fgetc(fp);
}
m=30;n=20;
for(i=0;i<600;i++)
{
j=i/30; k=i%30;
if(buffer[i]==''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''&&!len){ m=i; len=TRUE; }
if(k==0&&buffer[i]==''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''&&!wid){ n=j; wid=TRUE; }
switch(buffer[i])
{
case ''''''''''''''''''''''''''''''''0'''''''''''''''''''''''''''''''': Maze[j][k]=0; break;
case ''''''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''': Maze[j][k]=1; break;
case ''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''': Maze[j][k]=2; break;
case ''''''''''''''''''''''''''''''''3'''''''''''''''''''''''''''''''': Maze[j][k]=1;
StartPlace.x=k;
StartPlace.y=j;
b_start=TRUE;
break;
case ''''''''''''''''''''''''''''''''4'''''''''''''''''''''''''''''''': Maze[j][k]=1;
EndPlace.x=k;
EndPlace.y=j;
b_end=TRUE;
break;
default : break;
}
}
fclose(fp);
clrscr();
for(i=0;i<30;i++)
for(j=0;j<20;j++)
{
maze[j][i].td=Maze[j][i];
maze[j][i].foot=0;
maze[j][i].mark=0;
if(Maze[j][i]==0)
{
gotoxy(2*i+2,j+2);
putch(''''''''''''''''''''''''''''''''#'''''''''''''''''''''''''''''''');
}
}
gotoxy(2*StartPlace.x+2,StartPlace.y+2);
putch(''''''''''''''''''''''''''''''''S'''''''''''''''''''''''''''''''');
gotoxy(2*EndPlace.x+2,EndPlace.y+2);
putch(''''''''''''''''''''''''''''''''E'''''''''''''''''''''''''''''''');
return OK;
}
Status SaveMaze(char *filename)
/* The maze has been saved. */
{
FILE *fp;
char *buffer;
int i,j,k;
fp=fopen(filename,"wb");
buffer=(char *)malloc(600*sizeof(char));
for(i=0;i<600;i++)
{
j=i/30; k=i%30;
switch(Maze[j][k])
{
case 0: buffer[i]=''''''''''''''''''''''''''''''''0''''''''''''''''''''''''''''''''; break;
case 1: buffer[i]=''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''; break;
case 2: buffer[i]=''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''; break;
default : Error("Write"); break;
}
if(k==StartPlace.x&&j==StartPlace.y) buffer[i]=''''''''''''''''''''''''''''''''3'''''''''''''''''''''''''''''''';
if(k==EndPlace.x&&j==EndPlace.y) buffer[i]=''''''''''''''''''''''''''''''''4'''''''''''''''''''''''''''''''';
}
fwrite(buffer,600,1,fp);
free(buffer);
fclose(fp);
return OK;
}
void Error(char *message)
{
clrscr();
fprintf(stderr,"Error:%s\n",message);
exit(1);
} /* Error */
Status InitStack(Stack *s)
/* The stack s has been created and is initialized to be empty. */
{
s->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!s->base) Error("Overflow");
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
} /* InitStack */
Status DestroyStack(Stack *s)
/* The stack s has been destroyed. */
{
s->top=NULL;
s->stacksize=0;
free(s->base);
s->base=NULL;
return OK;
} /* DestroyStack */
Status ClearStack(Stack *s)
/* The stack has been clear to be maximum. */
{
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
} /* ClearStack */
Boolean StackEmpty(Stack *s)
/* Check if the stack s is empty. */
{
if(s->top==s->base) return TRUE;
else return FALSE;
} /* StackEmpty */
int StackLength(Stack *s)
/* Gain the length of the stack s. */
{
if(s->top>s->base) return (int)(s->top-s->base);
else return 0;
} /* StackLength */
Status Push(Stack *s,SElemType e)
/* The element e has been pushed into the stack s. */
{
if(s->top-s->base>=s->stacksize)
{
s->base=(SElemType *)realloc(s->base,
(s->stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!s->base) Error("Overflow");
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=e;
return OK;
} /* Push */
SElemType Pop(Stack *s,SElemType e)
/* The element e has been removed from the stack s. */
{
if(s->top==s->base) Error("Pop");
e=*--s->top;
return e;
} /* Pop */
Status GetTop(Stack *s,SElemType *e)
/* The element e has got to the top of the stack s.*/
{
if(s->top==s->base) Error("GetTop");
*e=*(s->top-1);
return OK;
} /* GetTop */
/* Traverse the stack s using ''''''''''''''''''''''''''''''''visiting'''''''''''''''''''''''''''''''' function. */
/* Status StackTraverse(Stack *s,Status (* visit)(SElemType *se))
{
SElemType p;
int result;
if(s->top==s->base) return ERROR;
p=s->base;
while(!(p==s->top))
{
result=(*visit)(p);
p++;
}
return OK;
} */
Boolean Pass(PosType curpos)
/* Check if the current position can be passed. */
{
if(maze[curpos.x][curpos.y].td==1&&
maze[curpos.x][curpos.y].foot==0&&maze[curpos.x][curpos.y].mark==0)
return TRUE;
else return FALSE;
} /* Pass */
void MarkPrint(PosType seat)
/* Mark the position seat. */
{
maze[seat.x][seat.y].mark=-1;
/* Marking ''''''''''''''''''''''''''''''''-1'''''''''''''''''''''''''''''''' symbolize the current position cannot be put. */
} /* MarkPrint */
void FootPrint(PosType curpos)
/* The foot of the curpos of the maze has been set ''''''''''''''''''''''''''''''''true''''''''''''''''''''''''''''''''. */
{
maze[curpos.x][curpos.y].foot=1;
} /* FootPrint */
PosType NextPos(PosType seat,int di)
{
switch(di)
{
case 1: seat.y++; return seat; /* Eastward */
case 2: seat.x++; return seat; /* Southward */
case 3: seat.y--; return seat; /* Westward */
case 4: seat.x--; return seat; /* Northward */
default: seat.x=0; seat.y=0; return seat;
}
} /* NextPos */
/* The key to the program. */
/* Pre: The maze array & the startplace & the endplace.
Post: Find the one traverse of the maze and perform the mazepath.
Uses: The ADT stack class.
*/
Status MazePath(PosType start,PosType end)
{
PosType curpos;
int curstep;
SElemType e;
Stack *s,stack;
stack.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!stack.base) Error("Overflow");
stack.top=stack.base;
stack.stacksize=STACK_INIT_SIZE;
s=&stack;
curpos=start;
curstep=1;
do
{
if(Pass(curpos))
{
FootPrint(curpos);
e.ord=curstep; e.seat=curpos; e.di=1;
gotoxy((curpos.y+1)*2,curpos.x+2);
putch(''''''''''''''''''''''''''''''''@'''''''''''''''''''''''''''''''');
delay(8000); /* pospone time. */
Push(s,e);
if(curpos.x==end.x&&curpos.y==end.y) /* Proceed recursively. */
{
DestroyStack(s);
return TRUE;
}
curpos=NextPos(curpos,1); /* Try next position. */
curstep++;
}
else
{
if(!StackEmpty(s))
{
e=Pop(s,e); /* Removed e from s. */
while(e.di==4&&!StackEmpty(s)) /* Four directions have been checked
and s is not empty. */
{
MarkPrint(e.seat);
gotoxy((e.seat.y+1)*2,e.seat.x+2);
putch(''''''''''''''''''''''''''''''''@'''''''''''''''''''''''''''''''');
delay(8000); /* Pospone time. */
gotoxy((e.seat.y+1)*2,e.seat.x+2);
putch('''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''');
e=Pop(s,e); /* Remove e from s. */
curstep--;
}
if(e.di<4) /* The current position hasnot been checked. */
{
e.di++;
Push(s,e); /* Insert e into s. */
curpos=NextPos(e.seat,e.di); /* Try next position. */
}
}
}
}
while(!StackEmpty(s));
DestroyStack(s);
return FALSE;
} /* MazePath */
void main()
{
PosType start,end;
CreatMaze();
start.x=StartPlace.y;
start.y=StartPlace.x;
end.x=EndPlace.y;
end.y=EndPlace.x;
if(MazePath(start,end))
{
gotoxy(2,22);
printf("Path found\n");
}
else
{
gotoxy(2,22);
printf("Path not found\n");
}
getch();
clrscr();
}
十全十美游戲原程序
加入時間 2005-12-15 22:59:27 本站域名 www.code365.com
學了好長時間C了,一直想做點什么,以前一直編一些很菜的游戲用來練手,暑假又做了一個16位圖形處理系統,代碼又長又爛,除了我沒人能看懂,這些日子有按耐不住,編了這個簡單的不能再簡單的游戲,對初學者很有幫助!
#include<dos.h>
#include<graphics.h>
#include<stdio.h>
int x,y;
void doexit(),saveimage(),doimage(),imagedrive();
void *buff;
main()
{
int i,j,key;
int dokey();
char ch[]="BEAUIDEAL",c[]="help: right,down,left,up,enter,esc; very easy,OK!~!";
imagedrive();
saveimage();
cleardevice();
setbkcolor(7);
setcolor(1);
settextstyle(0,0,2);
outtextxy(250,50,ch);
setlinestyle(1,0,3);
rectangle(30,30,600,400);
setlinestyle(0,0,0);
settextstyle(0,0,1);
setcolor(8);
outtextxy(100,380,c);
setcolor(15);
for(i=0;i<5;i++)
for(j=0;j<5;j++)
rectangle(200+j*35,100+i*35,230+j*35,130+i*35);
setcolor(1);
rectangle(200,100,230,130);
x=200;
y=100;
while(1)
{
key=bioskey(0);
dokey(key);
}
free(buff);
closegraph();
}
void saveimage() /*存儲方格*/
{
bar(0,0,29,29);
buff=sizeof(imagesize(0,0,29,29));
getimage(0,0,28,28,buff);
}
void imagedrive() /*圖形模式初始化*/
{
int gd,gm;
gd=VGA;
gm=VGAHI;
initgraph(&gd,&gm,"");
cleardevice();
}
void doimage(x,y) /*畫方格*/
{
void tell();
putimage(x+1,y+1,buff,1);
if(x!=200)
putimage(x+1-35,y+1,buff,1);
if(x!=340)
putimage(x+1+35,y+1,buff,1);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -